通过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:\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下都可以正常运行,具体介绍请看这里

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值