当AutoCAD双击实体对象时调用自定义窗口的C#实现方法
<p><font face="Verdana">using System;<br/>using System.Collections.Generic;<br/>using System.Linq;<br/>using System.Text;</font></p><p><font face="Verdana">using Autodesk.AutoCAD.Runtime;<br/>using Autodesk.AutoCAD.ApplicationServices;<br/>using Autodesk.AutoCAD.EditorInput;<br/>using Autodesk.AutoCAD.Windows;<br/>using Autodesk.AutoCAD.Windows.ToolPalette;<br/>using Autodesk.AutoCAD.Windows.Data;<br/>using Autodesk.AutoCAD.Internal;<br/>using System.Windows.Forms;<br/>using Autodesk.AutoCAD.DatabaseServices;<br/>using Autodesk.AutoCAD.Interop;//引用com中的autoCAD type library(就不是纯arx了)<br/>using Autodesk.AutoCAD.Interop.Common;//引用com中的AutoCAD/ObjectDBX Common ;(c:\programfiles\commonfile\autoshared\)</font></p>
<p><font face="Verdana">namespace AutoCadLibTest<br/>{<br/> public class Class1<br/> {<br/> #region events<br/> public void OnCADCmd(object o, ObjectEventArgs e){</font></p>
<p><font face="Verdana"> MessageBox.Show(@"xam:" + e.ToString());<br/> </font></p><font face="Verdana">
<p><br/> }<br/> public void OnCADCmdB(object sender, EventArgs e)<br/> {</p>
<p> MessageBox.Show(@"xam:" + e.ToString());</p>
<p> </p>
<p> }<br/> public void cmdWillStart(object o, CommandEventArgs e)<br/> {<br/> Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;<br/> try<br/> {<br/> // if (e.GlobalCommandName == "MOVE") {<br/> ed.WriteMessage("xam begin command:" + e.GlobalCommandName + "\n");<br/> // }<br/> switch(e.GlobalCommandName){<br/> case "PROPERTIES":<br/> ed.WriteMessage("xam 禁止" + e.GlobalCommandName + o.ToString() + "\n");<br/> Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;<br/> <br/> break;<br/> }</p>
<p><br/> }<br/> catch<br/> {<br/> ed.WriteMessage("CommandWillStart事件的处理函数错误");<br/> }<br/> }<br/> public void objOpenedForMod(object o, ObjectEventArgs e)<br/> {</p>
<p> ObjectId objId = e.DBObject.ObjectId;<br/> Database db = HostApplicationServices.WorkingDatabase;<br/> Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;<br/> try<br/> {<br/> ed.WriteMessage(@"xam " + e.DBObject.ObjectId.ToString() + "\n");<br/> }<br/> catch<br/> {<br/> ed.WriteMessage("ObjectOpenedForModify事件的处理函数错误");<br/> }<br/> }<br/> public void cmdEnded(object o, CommandEventArgs e)<br/> {<br/> //判断监控命令是否是激活的<br/> Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;<br/> try<br/> {<br/> ed.WriteMessage("xam end command: " + e.GlobalCommandName + "\n");</p>
<p> }<br/> catch<br/> {<br/> ed.WriteMessage("CommandWillStart事件的处理函数错误");<br/> }<br/> }<br/> #endregion events<br/> <br/> <br/> public void HelloNet(){<br/> Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;<br/> ed.WriteMessage("使用NET开发AutoCAD 程序00");</p>
<p> Autodesk.AutoCAD.Windows.StatusBar stb = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.StatusBar;<br/> stb.Panes.Clear();<br/> <br/> <br/> }</p>
<p> <br/> public void AppendEvent()<br/> {<br/> //MessageBox.Show(@"xam:");<br/> Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;<br/> try{<br/> Database db;<br/> Document doc;<br/> //<br/> doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;<br/> db = HostApplicationServices.WorkingDatabase;<br/> db.ObjectAppended += new ObjectEventHandler(OnCADCmd);<br/> db.ObjectOpenedForModify += new ObjectEventHandler(objOpenedForMod);<br/> doc.CommandWillStart += new CommandEventHandler(cmdWillStart);<br/> doc.CommandEnded += new CommandEventHandler(cmdEnded);<br/> //bCmdActive = false;<br/> //bReposition = false;<br/> ed.WriteMessage("xam:绑定事件成功");<br/> }catch{<br/> ed.WriteMessage("添加事件错误\n");<br/> }<br/> }</p>
<p> static List<string> lstAppName;//传入的扩展数据名列表</p>
<p> static bool m_DbClick = false;<br/> static Document doc;<br/> static Editor ed;<br/> static AcadDocument acaddoc;<br/> static ObjectId objSelId;<br/> static double dist = 1;//选择点与选择实体的距离偏差</p>
<p> static frmTrace glbFrmTrace;<br/> <br/> <br/> public static void AppendEventB(List<string> lstAppNameIn){<br/> try{<br/> glbFrmTrace = new frmTrace();<br/> glbFrmTrace.Show();</p>
<p> lstAppName = lstAppNameIn;<br/> doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;<br/> ed = doc.Editor;<br/> acaddoc = (AcadDocument)doc.AcadDocument;<br/> acaddoc.BeginDoubleClick += new _DAcadDocumentEvents_BeginDoubleClickEventHandler(beginDoubleClick);<br/> Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.DocumentLockModeChanged += new DocumentLockModeChangedEventHandler(vetoCommand);<br/> Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.DocumentCreated += new DocumentCollectionEventHandler(documentCreated);<br/> ed.SelectionAdded += new SelectionAddedEventHandler(ed_SelectionAdded);<br/> }catch{<br/> <br/> }<br/> }<br/> //鼠标悬浮提示<br/> public static void AppendEventC(List<string> lstAppNameIn) {<br/> try {<br/> <br/> doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;<br/> ed = doc.Editor;<br/> ed.PointMonitor += new PointMonitorEventHandler(ed_PointMonitor);</p>
<p> } catch {</p>
<p> }<br/> }<br/> //---------------------------------------------<br/> static void ed_PointMonitor(object sender, PointMonitorEventArgs e) {<br/> //拾取当前实体对象提交<br/> FullSubentityPath[] fsp = e.Context.GetPickedEntities();<br/> if (fsp.Length > 0) {<br/> // 显示实体ID<br/> // doc.Editor.WriteMessage(fsp.GetObjectIds().ToString());<br/> doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;<br/> ed = doc.Editor;<br/> ed.WriteMessage("ed_PointMonitor");<br/> e.AppendToolTipText("敢创:id=" + fsp.GetObjectIds().ToString());<br/> </p>
<p> DBObject obj = null;<br/> using (Transaction transaction = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction())<br/> {<br/> obj = transaction.GetObject(fsp.GetObjectIds(), Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead, true);<br/> transaction.Commit();<br/> }<br/> glbFrmTrace.txtHint.Text += "type : = "+obj.GetType().ToString() + "\n";<br/> <br/> //if(obj.GetType().ToString()==){<br/> // Line objLine = (Line)obj;</p>
<p> // glbFrmTrace.txtHint.Text += "isline" + "\n";</p>
<p> //}else{<br/> // glbFrmTrace.txtHint.Text += "not line" + "\n";<br/> //}<br/> </p>
<p> <br/> }<br/> }</p>
<p> </p>
<p> static void ed_SelectionAdded(object sender, SelectionAddedEventArgs e) {<br/> glbFrmTrace.txtHint.Text += "ed_SelectionAdded \r\n";<br/> //glbFrmTrace.txtHint.Text.Insert(glbFrmTrace.txtHint.Text.Length, "ed_SelectionAdded \n");<br/> try{<br/> if (e.AddedObjects.Count == 1){<br/> SelectedObject objSel = e.AddedObjects;<br/> objSelId = objSel.ObjectId;<br/> }<br/> else<br/> {<br/> // objSelId = ObjectId.Null; //不能加上,否则就和不用此方法效果一样<br/> }<br/> }catch{<br/> <br/> }<br/>}<br/> <br/> <br/> static void documentCreated(object sender, DocumentCollectionEventArgs e) {<br/> glbFrmTrace.txtHint.Text += "documentCreated \r\n";<br/> //glbFrmTrace.txtHint.Text.Insert(glbFrmTrace.txtHint.Text.Length, "documentCreated \n");<br/> try<br/> {<br/> doc = e.Document;<br/> acaddoc = (AcadDocument)doc.AcadDocument;<br/> acaddoc.BeginDoubleClick += new _DAcadDocumentEvents_BeginDoubleClickEventHandler(beginDoubleClick);<br/> }<br/> catch<br/> { }<br/> }<br/> <br/> static void beginDoubleClick(object PickPoint)<br/> {<br/> glbFrmTrace.txtHint.Text += "beginDoubleClick \r\n";<br/> //glbFrmTrace.txtHint.Text.Insert(glbFrmTrace.txtHint.Text.Length, "beginDoubleClick \n");<br/> try<br/> {<br/> PromptSelectionResult res = ed.SelectImplied();<br/> if (res.Status == PromptStatus.Error)//属性块双击不能选中的处理<br/> {<br/> if (objSelId != ObjectId.Null)<br/> {<br/> ShowDialog(objSelId, PickPoint, true);//true表示是上次选中的(在方法中判断是属性块时用)<br/> objSelId = ObjectId.Null;<br/> return;<br/> }<br/> }<br/> else<br/> {<br/> SelectionSet SS = res.Value;<br/> if (SS.GetObjectIds().Length == 1)<br/> {<br/> ObjectId oId = SS.GetObjectIds();<br/> ShowDialog(oId, PickPoint, false);<br/> return;<br/> }<br/> }<br/> m_DbClick = false;<br/> }<br/> catch<br/> {<br/> m_DbClick = false; //异常时保证面板等原来CAD自带的可用<br/> }<br/> }<br/> <br/> <br/> /// <summary><br/> /// //实现自定义窗口的调用<br/> /// </summary><br/> /// <param name="oId">选中的ObjectId</param><br/> /// <param name="ptPick">点取的点坐标</param><br/> /// <param name="isSeled">是否是上次选中的</param><br/> static void ShowDialog(ObjectId oId, object ptPick, bool isSeled) {<br/> frmHint dlg = new frmHint();<br/> dlg.ShowDialog();<br/> m_DbClick = true;<br/> try<br/> {<br/> using (Transaction tr = doc.TransactionManager.StartTransaction())<br/> {<br/> string appNameUse="";<br/> DBObject dbObj = tr.GetObject(oId, OpenMode.ForRead);<br/> string strXData="";<br/> bool isGet = false;<br/> foreach (string appName in lstAppName)<br/> {<br/> //isGet = CADXData.TryGetXData(dbObj, appName, out strXData);<br/> appNameUse = appName;<br/> }<br/> if (isGet)<br/> {<br/> AcadEntity ent = dbObj.AcadObject as AcadEntity;<br/> bool isAttBlock = IsAttBlock(ent);//是否是属性块 ,需要另类处理<br/> if (isSeled && !isAttBlock)//是非属性块时直接退出<br/> {<br/> m_DbClick = true;<br/> return;<br/> }<br/> <br/> object objMin; object objMax; double[] ptMin; double[] ptMax;<br/> ent.GetBoundingBox(out objMin, out objMax);<br/> ptMin = (double[])objMin; ptMax = (double[])objMax;<br/> <br/> double[] ptPic = ptPick as double[];<br/> if (ptPic > (ptMin - dist) && ptPic < (ptMax + dist) &<br/> ptPic > (ptMin - dist) && ptPic < (ptMax + dist))<br/> {<br/> <br/> //这个函数用委托 有可能是将全部扩展数据全显示出来<br/> Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("扩展数据名为: " + appNameUse + ",扩展数据名为: " + strXData);<br/> <br/> <br/> m_DbClick = true;<br/> }<br/> }<br/> // tr.Commit();<br/> }<br/> }<br/> catch<br/> { }<br/> }<br/> <br/> //得到是否是属性块<br/> private static bool IsAttBlock(AcadEntity ent)<br/> {<br/> if (ent is AcadBlockReference)<br/> {<br/> AcadBlockReference refEnt = ent as AcadBlockReference;<br/> object[] atts = (object[])refEnt.GetAttributes();<br/> if (atts.Length > 0) return true;<br/> }<br/> return false;<br/> }<br/> <br/> //屏蔽CAD命令<br/> static void vetoCommand(object sender, DocumentLockModeChangedEventArgs e) {<br/> glbFrmTrace.txtHint.Text += "vetoCommand \r\n";<br/> //glbFrmTrace.txtHint.Text.Insert(glbFrmTrace.txtHint.Text.Length, "vetoCommand \n");<br/> try<br/> {<br/> string com2 = e.GlobalCommandName.ToLower();<br/> if (m_DbClick)<br/> {<br/> string com = e.GlobalCommandName.ToLower();<br/> switch (com)<br/> {<br/> case "properties":<br/> m_DbClick = false;<br/> e.Veto();<br/> break;<br/> case "bedit":<br/> m_DbClick = false;<br/> e.Veto();<br/> break;<br/> case "eattedit":<br/> m_DbClick = false;<br/> e.Veto();<br/> break;<br/> case "ddedit":<br/> m_DbClick = false;<br/> e.Veto();<br/> break;<br/> }<br/> }<br/> }<br/> catch<br/> { }<br/> }<br/> <br/> }<br/>}</p>
<p></font> </p> <p>窗口请定义。</p>
<p>自己坐沙发</p> 直接修改CUI文件的<font face="Verdana"><DoubleClickRoot></font>节里的内容,应该更简便些。 <p>efan回来了,嘿嘿</p>
<p>同意,netapi也有相应的方面的方法的</p> <p>麻烦讲具体一点。谢谢!</p> 回复 chase_wang 的帖子
我在06下使用出现了一个 参数计数不正确的异常 我在CAD2006下使用,无法屏蔽CAD本身的特性对话框 同求06下的实现方法…… cui文件的层次结构类似如下:
<CustSection>
<MenuGroup Name="自定义菜单组名称">
<MacroGroup Name="自定义宏组名称">
<MenuRoot>
<PopMenuRoot /> '菜单
<ToolbarRoot /> '工具栏
<DoubleClickRoot> '双击动作
<DoubleClickAction DxfName="BLOCKREF" UID="DCU_BLOCKREF">
<ModifiedRev MajorVersion="17" MinorVersion="0" UserVersion="1" />
<Name xlate="true" UID="XLS_0701">块</Name>
<MenuItem>
<MacroRef MenuMacroID="自定义菜单项的ID" />
</MenuItem>
</DoubleClickAction>
<DoubleClickAction DxfName="ATTBLOCKREF" UID="DCU_ATTBLOCKREF">
<ModifiedRev MajorVersion="17" MinorVersion="0" UserVersion="1" />
<Name xlate="true" UID="XLS_0702">属性块</Name>
<MenuItem>
<MacroRef MenuMacroID="自定义菜单项的ID" />
</MenuItem>
</DoubleClickAction>
</DoubleClickRoot>
</MenuRoot>
</MenuGroup>
</CustSection>
可以通过CUI命令在界面中进行编辑,或者通过Autodesk.AutoCAD.Customization提供的操作进行编程。 好东西,学习了
页:
[1]
2