CAD实体双击弹出自定义窗体,可根据扩展数据(通用)

 

  1  //双击测试     QQ 420021327 hlb
  2 [CommandMethod("ddd")]
  3 public void DoubleChick()
  4 {
  5   List<string> lstAppName = new List<string>();
  6   lstAppName.Add("WireData");
  7   ArxHlb.CADDoubleChick.CADDoubleChick.DoubleChickStart(lstAppName);
  8 }
  9 
 10 /////以下为双击代码
 11 using System;
 12 using System.Collections.Generic;
 13 using Autodesk.AutoCAD.Runtime;
 14 using Autodesk.AutoCAD.DatabaseServices;
 15 using Autodesk.AutoCAD.ApplicationServices;
 16 using Autodesk.AutoCAD.EditorInput;
 17 using Autodesk.AutoCAD.Interop;
 18 using Autodesk.AutoCAD.Interop.Common;
 19 
 20 namespace ArxHlb.CADDoubleChick
 21 {
 22      QQ 420021327 hlb
 23 
 24     /// <summary>
 25     /// 当前文件双击事件
 26     /// </summary>
 27     public class CADDoubleChick
 28     {
 29         static List<string> lstAppName;//传入的扩展数据名列表
 30 
 31         static bool m_DbClick = false;
 32         static Document doc;
 33         static Editor ed;
 34         static AcadDocument acaddoc;
 35         static ObjectId objSelId;
 36         static double dist = 1;//选择点与选择实体的距离偏差
 37         public static void DoubleChickStart(List<string> lstAppNameIn)
 38         {
 39             try
 40             {
 41                 lstAppName = lstAppNameIn;
 42                 doc = Application.DocumentManager.MdiActiveDocument;
 43                 ed = doc.Editor;
 44                 acaddoc = (AcadDocument)doc.AcadDocument;
 45                 acaddoc.BeginDoubleClick += new _DAcadDocumentEvents_BeginDoubleClickEventHandler(beginDoubleClick);
 46                 Application.DocumentManager.DocumentLockModeChanged += new DocumentLockModeChangedEventHandler(vetoCommand);
 47                 Application.DocumentManager.DocumentCreated += new DocumentCollectionEventHandler(documentCreated);
 48                 ed.SelectionAdded += new SelectionAddedEventHandler(ed_SelectionAdded);
 49 
 50 
 51 
 52             }
 53             catch
 54             { }
 55         }
 56 
 57    
 58 
 59 
 60         //属性块的双击而增加此事件
 61         static void ed_SelectionAdded(object sender, SelectionAddedEventArgs e)
 62         {
 63             try
 64             {
 65                 if (e.AddedObjects.Count == 1)
 66                 {
 67                     SelectedObject objSel = e.AddedObjects[0];
 68                     objSelId = objSel.ObjectId;
 69                 }
 70                 else
 71                 {
 72                     // objSelId = ObjectId.Null; //不能加上,否则就和不用此方法效果一样
 73                 }
 74             }
 75             catch
 76             { }
 77         }
 78 
 79 
 80         static void documentCreated(object sender, DocumentCollectionEventArgs e)
 81         {
 82             try
 83             {
 84                 doc = e.Document;
 85                 acaddoc = (AcadDocument)doc.AcadDocument;
 86                 acaddoc.BeginDoubleClick += new _DAcadDocumentEvents_BeginDoubleClickEventHandler(beginDoubleClick);
 87             }
 88             catch
 89             { }
 90         }
 91 
 92         static void beginDoubleClick(object PickPoint)
 93         {
 94             try
 95             {
 96                 PromptSelectionResult res = ed.SelectImplied();
 97                 if (res.Status == PromptStatus.Error)//属性块双击不能选中的处理
 98                 {
 99                     if (objSelId != ObjectId.Null)
100                     {
101                         ShowDialog(objSelId, PickPoint, true);//true表示是上次选中的(在方法中判断是属性块时用)
102                         objSelId = ObjectId.Null;
103                         return;
104                     }
105                 }
106                 else
107                 {
108                     SelectionSet SS = res.Value;
109                     if (SS.GetObjectIds().Length == 1)
110                     {
111                         ObjectId oId = SS.GetObjectIds()[0];
112                         ShowDialog(oId, PickPoint, false);
113                         return;
114                     }
115                 }
116                 m_DbClick = false;
117             }
118             catch
119             {
120                 m_DbClick = false//异常时保证面板等原来CAD自带的可用
121             }
122         }
123 
124 
125         /// <summary>
126         ///  //实现自定义窗口的调用
127         /// </summary>
128         /// <param name="oId">选中的ObjectId</param>
129         /// <param name="ptPick">点取的点坐标</param>
130         /// <param name="isSeled">是否是上次选中的</param>
131         static void ShowDialog(ObjectId oId, object ptPick, bool isSeled)
132         {
133             try
134             {
135                 using (Transaction tr = doc.TransactionManager.StartTransaction())
136                 {
137                     string appNameUse="";
138                     DBObject dbObj = tr.GetObject(oId, OpenMode.ForRead);
139                     string strXData="";
140                     bool isGet = false;
141                     foreach (string appName in lstAppName)
142                     {
143                         isGet = ArxHlb.CADXData.TryGetXData(dbObj, appName, out strXData);
144                         appNameUse = appName;
145                     }
146                     if (isGet)
147                     {
148                         AcadEntity ent = dbObj.AcadObject as AcadEntity;
149                         bool isAttBlock = IsAttBlock(ent);//是否是属性块 ,需要另类处理
150                         if (isSeled && !isAttBlock)//是非属性块时直接退出
151                         {
152                             m_DbClick = true;
153                             return;
154                         }
155 
156                         object objMin; object objMax; double[] ptMin; double[] ptMax;
157                         ent.GetBoundingBox(out objMin, out objMax);
158                         ptMin = (double[])objMin; ptMax = (double[])objMax;
159 
160                         double[] ptPic = ptPick as double[];
161                         if (ptPic[0> (ptMin[0- dist) && ptPic[0< (ptMax[0+ dist) &
162                             ptPic[1> (ptMin[1- dist) && ptPic[1< (ptMax[1+ dist))
163                         {
164 
165                             //这个函数用委托  有可能是将全部扩展数据全显示出来
166                             Application.ShowAlertDialog("扩展数据名为: " + appNameUse + ",扩展数据名为: " + strXData);
167 
168 
171                             m_DbClick = true;
172                         }
173                     }
174                    // tr.Commit();
175                 }
176             }
177             catch
178             { }
179         }
180 
181         //得到是否是属性块
182         private static bool IsAttBlock(AcadEntity ent)
183         {
184             if (ent is AcadBlockReference)
185             {
186                 AcadBlockReference refEnt = ent as AcadBlockReference;
187                 object[] atts = (object[])refEnt.GetAttributes();
188                 if (atts.Length > 0return true;
189             }
190             return false;
191         }
192 
193         //屏蔽CAD命令
194         static void vetoCommand(object sender, DocumentLockModeChangedEventArgs e)
195         {
196             try
197             {
198                 string com2 = e.GlobalCommandName.ToLower();
199                 if (m_DbClick)
200                 {
201                     string com = e.GlobalCommandName.ToLower();
202                     switch (com)
203                     {
204                         case "properties":
205                             m_DbClick = false;
206                             e.Veto();
207                             break;
208                         case "bedit":
209                             m_DbClick = false;
210                             e.Veto();
211                             break;
212                         case "eattedit":
213                             m_DbClick = false;
214                             e.Veto();
215                             break;
216                         case "ddedit":
217                             m_DbClick = false;
218                             e.Veto();
219                             break;
220                     }
221                 }
222             }
223             catch
224             { }
225         }
226 
227     }
228 }
229 

 

 

posted @ 2010-05-22 16:33  houlinbo  阅读(5017)  评论(0编辑  收藏  举报