大家知道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:\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的方式調用ARX里的acedCommand方法,這里有個很好的例子:
最后,通過API調用AutoCAD命令時,最佳實踐是使用前綴 _.英文命令 的方式,這樣不管在任何語言的AutoCAD下都可以正常運行,具體介紹請看這里。