简体   繁体   English

如何使用带有C#的AutoCad API将DWG文件导出到图像?

[英]How to export DWG files to images using AutoCad API with C#?

I have AutoDesk 2014 and VS2012 installed. 我安装了AutoDesk 2014和VS2012。 I already have the dlls mentioned here and also tried this but not worked. 我已经提到的dll 这里也试过这个 ,但没有奏效。 I really need to know how to export those files to images, jpg, png,.. using C# code. 我真的需要知道如何使用C#代码将这些文件导出到图像,jpg,png,..。 Thanks! 谢谢!

The DLLs and code you mentioned are used to create plugins for AutoCAD. 您提到的DLL和代码用于创建AutoCAD插件。

You can create images (PNG or other) using a code like this: http://through-the-interface.typepad.com/through_the_interface/2007/04/taking_a_snapsh.html 您可以使用以下代码创建图像(PNG或其他图像): http : //through-the-interface.typepad.com/through_the_interface/2007/04/taking_a_snapsh.html

But you may need to get started with the API, see a basic tutorial at http://www.autodesk.com/myfirstautocadplugin 但是您可能需要开始使用API​​,请参阅http://www.autodesk.com/myfirstautocadplugin上的基本教程

To meet the requirements of your post, you can opt for third party plugins (which allow you to export DWGs to PNG, JPG etc) and associate the chosen plugin with your Visual Studio Solution to allow you to export DWGs to PNG, JPG etc ... However, under Autodesk's point of view, the recommendation will always be to consume the API for you to develop plugins and / or achieve your requirements through the API; 为了满足您的帖子要求,您可以选择第三方插件(允许您将DWG导出到PNG,JPG等),并将所选插件与Visual Studio解决方案相关联,以允许将DWG导出到PNG,JPG等。 ..但是,从Autodesk的角度来看,建议始终是使用API​​以供您开发插件和/或通过API满足您的要求; I particularly prefer to go and spoil the manufacturer's native solutions and then think about using a third-party solution. 我特别喜欢破坏制造商的本机解决方案,然后考虑使用第三方解决方案。 It is worth mentioning that I am a Developer and I develop plugins for AutoCAD software but I am not from Autodesk and I do not win today to defend this point of view. 值得一提的是,我是一名开发人员,并且为AutoCAD软件开发插件,但我并非来自Autodesk,因此今天我没有赢得捍卫这一观点的机会。

The only aspects that are separators in your choice is to know if the DWGs are linked from Database Servers or not. 选择分隔符的唯一方面是知道DWG是否从数据库服务器链接。 And, in case DWGs are independent of Database Server, if the third-party plugin is free or you have to pay to take advantage of the features you need. 而且,如果DWG独立于数据库服务器,则如果第三方插件是免费的,或者您必须付费以利用所需的功能。

Here is an example code I use to drive AutoCAD through an external application (Console Application Project); 这是我用来通过外部应用程序 (控制台应用程序项目) 驱动AutoCAD的示例代码; as Autodesk develops its products using also COM interfaces, allows us developers to consume the features intrinsic to Autodesk Softwares that can be executed by the external application. 随着Autodesk还使用COM接口开发其产品,使我们的开发人员可以使用可由外部应用程序执行的Autodesk软件固有的功能。 At the code bellow, open the AutoCAD application by your Program ID and iterate throw all DWG files using a native command named JPGOUT . 在下面的代码中,通过您的程序ID打开AutoCAD应用程序,并使用名为JPGOUT的本机命令迭代抛出所有DWG文件。

class Program
{
    public static void Main(string[] args)
    {
        AcadApplication acAppComObj = null;

        //Query your Regedit Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Autodesk\AutoCAD to get the correctly suffix that specifies the version
        const string strProgId = "AutoCAD.Application.20";

        // Get a running instance of AutoCAD
        try
        {
            acAppComObj = (AcadApplication)Marshal.GetActiveObject(strProgId);
        }
        catch // An error occurs if no instance is running
        {
            try
            {
                // Create a new instance of AutoCAD
                acAppComObj = (AcadApplication)Activator.CreateInstance(Type.GetTypeFromProgID(strProgId), true);
            }
            catch
            {
                // If an instance of AutoCAD is not created then message and exit
                System.Windows.Forms.MessageBox.Show("Instance of 'AutoCAD.Application'" +
                                                     " could not be created.");

                return;
            }
        }

        // Display the application
        if (null != acAppComObj)
        {
            try
            {
                int i = 0;
                AcadState appState = app.GetAcadState();
                while (!appState.IsQuiescent)
                {
                    if (i == 120)
                    {
                        Environment.Exit(-1);
                    }
                    // Wait .25s
                    Thread.Sleep(250);
                    i++;
                }
                app.Visible = true;
                var docs = app.Documents;
                docs.Add("acadiso.dwt");
            }
            catch (COMException err)
            {
                if (err.ErrorCode.ToString() == "-2147417846")
                {
                    Thread.Sleep(5000);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Falha durante a obtenção do documento ativo.", ex);
            }
        }
        else
        {
            throw new Exception("Erro to open first document.");
        }


        // Open AutoCAD project file, use this code if all DWGs is associated with a AutoCAD Project with Server Database
        #region ' Open Project '
        acDocComObj.SendCommand("FILEDIA","0");
        acDocComObj.SendCommand("-OPENPROJECT", "C:\\\\Users\\<username>\\Documents\\ProjectFolder\\Project.xml");
        acDocComObj.SendCommand("FILEDIA","1");
        #endregion

        string[] dwgFiles = //To do: add here the rule that list all full path DWG files
        AcadDocuments docs = app.Documents;
        foreach(string dwgPath in dwgFiles)
        {
            docs.Open(dwgPath, true);
            Thread.Sleep(3000);
            AcadDocument acadDoc = acAppComObj.ActiveDocument;

            acDocComObj.SendCommand("FILEDIA","0");
            acadDoc.SendCommand("JPGOUT ", "C:\\\\Users\\<username>\\Images\\" + Path.GetFileName(dwgPath) + ".jpg");
            acDocComObj.SendCommand("FILEDIA","1");
        }
    }
}

Premise to use this source code example (this was tested and is in using): 使用此源代码示例的前提(已通过测试并正在使用):

a) Have an AutoCAD product installed (if you do not have a license and will use a student version to download the 2018 version because the 2019 has license-based encryption to open the DWG will always throw an exception); a)安装了AutoCAD产品(如果您没有许可证,并且将使用学生版下载2018版本,因为2019具有基于许可证的加密来打开DWG将始终引发异常);

b) Create a Visual Studio project of type Console Application with build in x64 processing architecture; b)创建具有x64处理体系结构的Console Application类型的Visual Studio项目;

c) Add the references "C: \\ ProgramFiles \\ Autodesk \\ AutoCAD 20XX \\ Autodesk.AutoCAD.Interop.dll" and "C: \\ ProgramFiles \\ Autodesk \\ AutoCAD 20XX \\ Autodesk.AutoCAD.Interop.Common.dll"; c)添加引用“ C:\\ ProgramFiles \\ Autodesk \\ AutoCAD \\ 20XX \\ Autodesk.AutoCAD.Interop.dll”和“ C:\\ ProgramFiles \\ Autodesk \\ AutoCAD 20XX \\ Autodesk.AutoCAD.Interop.Common.dll”;

Thats all. 就这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM