通过API执行AutoCAD命令来…

大家知道AutoCAD功能丰富,而更可贵的是,这么多丰富的功能背后都有一个命令,有些东西,直接用API调用写起来可能很费劲或者无法实现,可如果能用命令的话却很简单,这时候我们就可以通过API来调用AutoCAD命令来实现通用的效果,简单而强大。

比如今天有人问,如何在AutoCAD里加入wmf文件,应用的场景是在图纸中加入设计人审核人的电子签名。其实通过搜索能很容易的找到这篇文章:

http://adndevblog.typepad.com/autocad/2013/01/insert-a-wmf-file-multiple-times.html

但他提到不懂里面的ARX语句是干什么的:

acedCommand(RTSTR,_T("_wmfin"),RTSTR,A_WmfFile, RT3DPOINT,point1,RTSTR,"",RTSTR,"", RTREAL, 0,0,RTNONE);

这个其实也不高深,就是用API调用了AutoCAD的 wmfin命令。

在.net环境下同样也可以做类似的事。比如下面的c#代码,在图纸中插入一个wmf文件:

        [CommandMethod("MyGroup", "InsertWmf", "InsertWmf", CommandFlags.Modal)]
        public void MyCommand() // This method can have any name
        {
            // Put your command code here
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed;
            if (doc != null)
            {
                ed = doc.Editor;

                //save the filedia sysvar
                var filedia_old = Application.GetSystemVariable("filedia");
                //set filedia to 0, not open the file dialogue
                Application.SetSystemVariable("filedia", 0);

                Point3d pnt = ed.GetPoint("select a point to insert: ").Value;

                string wmfPath = @"C:TEMPflower.WMF";

                ed.Command("_.wmfin"//command name
                    wmfPath,            //wmf file path
                    pnt,                //insert point
                    1,                  //scale X
                    1,                  //scale Y
                    0.0);               //rotation


                //restore file dialogue sys var
                Application.SetSystemVariable("filedia", filedia_old);
            }
        }

另外,还可以使用p/invoke的方式调用ARX里的acedCommand方法,这里有个很好的例子

最后,通过API调用AutoCAD命令时,最佳实践是使用前缀 _.英文命令 的方式,这样不管在任何语言的AutoCAD下都可以正常运行,具体介绍请看这里

原文地址:https://www.cnblogs.com/junqilian/p/4187322.html