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

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

问题描述

我安装了AutoDesk 2014和VS2012.我已经提到了 ,但不起作用.我真的需要知道如何使用C#代码将这些文件导出为图像,jpg,png,...谢谢!

解决方案

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

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

这是我用来

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

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

b)创建具有x64处理体系结构的Console Application类型的Visual Studio项目;

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.

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

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