如何使用 AutoCad API 和 C# 将 DWG 文件导出为图像? [英] How to export DWG files to images using AutoCad API with C#?

查看:101
本文介绍了如何使用 AutoCad API 和 C# 将 DWG 文件导出为图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

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

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

这是我用来通过外部应用程序驱动AutoCAD(控制台应用程序项目);由于 Autodesk 还使用 COM 接口开发其产品,因此我们的开发人员可以使用可由外部应用程序执行的 Autodesk 软件的固有功能.在下面的代码中,通过您的程序 ID 打开 AutoCAD 应用程序并使用名为 JPGOUT.

class 程序{public static void Main(string[] args){AcadApplication acAppComObj = null;//查询您的Regedit Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Autodesk\AutoCAD 以获取指定版本的正确后缀const string strProgId = "AutoCAD.Application.20";//获取正在运行的 AutoCAD 实例尝试{acAppComObj = (AcadApplication)Marshal.GetActiveObject(strProgId);}catch//如果没有实例在运行,则发生错误{尝试{//创建一个新的 AutoCAD 实例acAppComObj = (AcadApplication)Activator.CreateInstance(Type.GetTypeFromProgID(strProgId), true);}抓住{//如果没有创建 AutoCAD 的实例,则消息并退出System.Windows.Forms.MessageBox.Show("AutoCAD.Application 的实例" +"无法创建.");返回;}}//显示应用如果(空!= acAppComObj){尝试{int i = 0;AcadState appState = app.GetAcadState();而 (!appState.IsQuiescent){如果(我 == 120){环境.退出(-1);}//等待 0.25 秒线程睡眠(250);我++;}app.Visible = true;var docs = app.Documents;docs.Add("acadiso.dwt");}捕获(COMException 错误){if (err.ErrorCode.ToString() == "-2147417846"){线程睡眠(5000);}}捕获(异常前){throw new Exception("Falha durante a obtenção do documento ativo.", ex);}}别的{throw new Exception("打开第一个文档时出错.");}//打开 AutoCAD 项目文件,如果所有 DWG 都与带有服务器数据库的 AutoCAD 项目相关联,则使用此代码#region '开放项目'acDocComObj.SendCommand("FILEDIA","0");acDocComObj.SendCommand("-OPENPROJECT", "C:\\\\Users\\\\Documents\\ProjectFolder\\Project.xml");acDocComObj.SendCommand("FILEDIA","1");#endregionstring[] dwgFiles =//要做的事:在这里添加列出所有完整路径DWG文件的规则AcadDocuments 文档 = app.Documents;foreach(dwgFiles 中的字符串 dwgPath){docs.Open(dwgPath, true);线程.睡眠(3000);AcadDocument acadDoc = acAppComObj.ActiveDocument;acDocComObj.SendCommand("FILEDIA","0");acadDoc.SendCommand("JPGOUT", "C:\\\\Users\\\\Images\\" + Path.GetFileName(dwgPath) + ".jpg");acDocComObj.SendCommand("FILEDIA","1");}}}

使用此源代码示例的前提(已测试并正在使用):

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

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

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

仅此而已.

I have AutoDesk 2014 and VS2012 installed. I already have the dlls mentioned here and also tried this but not worked. I really need to know how to export those files to images, jpg, png,.. using C# code. Thanks!

解决方案

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; 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.

The only aspects that are separators in your choice is to know if the DWGs are linked from Database Servers or not. 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.

Here is an example code I use to drive AutoCAD through an external application (Console Application Project); 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. At the code bellow, open the AutoCAD application by your Program ID and iterate throw all DWG files using a native command named JPGOUT.

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);

b) Create a Visual Studio project of type Console Application with build in x64 processing architecture;

c) Add the references "C: \ ProgramFiles \ Autodesk \ AutoCAD 20XX \ Autodesk.AutoCAD.Interop.dll" and "C: \ ProgramFiles \ Autodesk \ AutoCAD 20XX \ Autodesk.AutoCAD.Interop.Common.dll";

Thats all.

这篇关于如何使用 AutoCad API 和 C# 将 DWG 文件导出为图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆