通过API执行AutoCAD命令来…
大家知道AutoCAD功能豐富,而更可貴的是,這么多豐富的功能背后都有一個(gè)命令,有些東西,直接用API調(diào)用寫起來可能很費(fèi)勁或者無法實(shí)現(xiàn),可如果能用命令的話卻很簡單,這時(shí)候我們就可以通過API來調(diào)用AutoCAD命令來實(shí)現(xiàn)通用的效果,簡單而強(qiáng)大。
比如今天有人問,如何在AutoCAD里加入wmf文件,應(yīng)用的場景是在圖紙中加入設(shè)計(jì)人審核人的電子簽名。其實(shí)通過搜索能很容易的找到這篇文章:
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);
這個(gè)其實(shí)也不高深,就是用API調(diào)用了AutoCAD的 wmfin命令。
在.net環(huán)境下同樣也可以做類似的事。比如下面的c#代碼,在圖紙中插入一個(gè)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:\n").Value;
??????????????? string wmfPath = @"C:\TEMP\flower.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的方式調(diào)用ARX里的acedCommand方法,這里有個(gè)很好的例子:
最后,通過API調(diào)用AutoCAD命令時(shí),最佳實(shí)踐是使用前綴 _.英文命令 的方式,這樣不管在任何語言的AutoCAD下都可以正常運(yùn)行,具體介紹請看這里。
總結(jié)
以上是生活随笔為你收集整理的通过API执行AutoCAD命令来…的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: QQ for Linux,还有百度网盘,
- 下一篇: hdu2520 我是菜鸟,我怕谁