【问题标题】:How to export DWG files to images using AutoCad API with C#?如何使用带有 C# 的 AutoCad API 将 DWG 文件导出为图像?
【发布时间】:2015-05-23 10:27:38
【问题描述】:

我安装了 AutoDesk 2014 和 VS2012。我已经提到了here 的dll,也尝试了this 但没有奏效。我真的需要知道如何使用 C# 代码将这些文件导出为图像、jpg、png 等。谢谢!

【问题讨论】:

    标签: c# api export autocad dwg


    【解决方案1】:

    为满足您的帖子要求,您可以选择第三方插件(允许您将 DWG 导出为 PNG、JPG 等)并将所选插件与您的 Visual Studio 解决方案相关联,以便您将 DWG 导出为 PNG, JPG 等... 但是,在 Autodesk 的观点下,建议始终为您使用 API 来开发插件和/或通过 API 实现您的要求;我特别喜欢去破坏制造商的原生解决方案,然后考虑使用第三方解决方案。值得一提的是,我是一名开发人员,我为 AutoCAD 软件开发插件,但我不是来自 Autodesk,我今天不会为这个观点辩护。

    您选择分隔符的唯一方面是了解 DWG 是否从数据库服务器链接。而且,如果 DWG 独立于数据库服务器,如果第三方插件是免费的,或者您必须付费才能利用所需的功能。

    这是我用于drive AutoCAD through an external application(控制台应用程序项目)的示例代码;由于 Autodesk 也使用 COM 接口开发其产品,因此我们的开发人员可以使用 Autodesk 软件固有的功能,这些功能可以由外部应用程序执行。在下面的代码中,通过您的程序 ID 打开 AutoCAD 应用程序,并使用名为 JPGOUT 的本机命令迭代 throw 所有 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");
            }
        }
    }
    

    使用本源代码示例的前提(已测试,正在使用中):

    a) 已安装 AutoCAD 产品(如果您没有许可证并且将使用学生版下载 2018 版,因为 2019 具有基于许可证的加密打开 DWG 将始终抛出异常);

    b) 创建一个带有内置 x64 处理架构的控制台应用程序类型的 Visual Studio 项目;

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

    就是这样。

    【讨论】:

      【解决方案2】:

      您提到的 DLL 和代码用于为 AutoCAD 创建插件。

      您可以使用如下代码创建图像(PNG 或其他):http://through-the-interface.typepad.com/through_the_interface/2007/04/taking_a_snapsh.html

      但您可能需要开始使用 API,请参阅 http://www.autodesk.com/myfirstautocadplugin 的基本教程

      【讨论】:

      • 您还可以从代码或脚本调用 AutoCAD JPGOUT 和 PNGOUT 命令。
      猜你喜欢
      • 1970-01-01
      • 2020-02-15
      • 1970-01-01
      • 2017-07-02
      • 2016-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多