明经CAD社区

 找回密码
 注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 6642|回复: 13

[事件] 当AutoCAD双击实体对象时调用自定义窗口的C#实现方法

    [复制链接]
发表于 2010-9-15 13:51 | 显示全部楼层 |阅读模式

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Windows;
using Autodesk.AutoCAD.Windows.ToolPalette;
using Autodesk.AutoCAD.Windows.Data;
using Autodesk.AutoCAD.Internal;
using System.Windows.Forms;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Interop;//引用com中的autoCAD type library(就不是纯arx了)
using Autodesk.AutoCAD.Interop.Common;//引用com中的AutoCAD/ObjectDBX Common  ;(c:\programfiles\commonfile\autoshared\)

namespace AutoCadLibTest
{
    public class Class1
    {
        #region events
        public void OnCADCmd(object o, ObjectEventArgs e){

            MessageBox.Show(@"xam:" + e.ToString());
           


        }
        public void OnCADCmdB(object sender, EventArgs e)
        {

            MessageBox.Show(@"xam:" + e.ToString());

 

        }
        public void cmdWillStart(object o, CommandEventArgs e)
        {
            Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
            try
            {
                // if (e.GlobalCommandName == "MOVE") {
                ed.WriteMessage("xam begin command:[name]" + e.GlobalCommandName + "\n");
                // }
                switch(e.GlobalCommandName){
                    case "PROPERTIES":
                        ed.WriteMessage("xam 禁止" + e.GlobalCommandName + o.ToString() + "\n");
                        Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
                       
                        break;
                }


            }
            catch
            {
                ed.WriteMessage("CommandWillStart事件的处理函数错误");
            }
        }
        public void objOpenedForMod(object o, ObjectEventArgs e)
        {

            ObjectId objId = e.DBObject.ObjectId;
            Database db = HostApplicationServices.WorkingDatabase;
            Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
            try
            {
                ed.WriteMessage(@"xam " + e.DBObject.ObjectId.ToString() + "\n");
            }
            catch
            {
                ed.WriteMessage("ObjectOpenedForModify事件的处理函数错误");
            }
        }
        public void cmdEnded(object o, CommandEventArgs e)
        {
            //判断监控命令是否是激活的
            Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
            try
            {
                ed.WriteMessage("xam end command: [name] " + e.GlobalCommandName + "\n");

            }
            catch
            {
                ed.WriteMessage("CommandWillStart事件的处理函数错误");
            }
        }
        #endregion events
       
        [CommandMethod("HelloNet")]
        public void HelloNet(){
            Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
            ed.WriteMessage("使用NET开发AutoCAD 程序00");

            Autodesk.AutoCAD.Windows.StatusBar stb = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.StatusBar;
           stb.Panes.Clear();
         
           
        }

        [CommandMethod("AppendEvent")]
        public void AppendEvent()
        {
            //MessageBox.Show(@"xam:");
            Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
                try{
                    Database db;
                    Document doc;
                    //
                    doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
                    db = HostApplicationServices.WorkingDatabase;
                    db.ObjectAppended += new ObjectEventHandler(OnCADCmd);
                    db.ObjectOpenedForModify += new ObjectEventHandler(objOpenedForMod);
                    doc.CommandWillStart += new CommandEventHandler(cmdWillStart);
                    doc.CommandEnded += new CommandEventHandler(cmdEnded);
                    //bCmdActive = false;
                    //bReposition = false;
                    ed.WriteMessage("xam:绑定事件成功");
                }catch{
                    ed.WriteMessage("添加事件错误\n");
                }
        }

        static List<string> lstAppName;//传入的扩展数据名列表

        static bool m_DbClick = false;
        static Document doc;
        static Editor ed;
        static AcadDocument acaddoc;
        static ObjectId objSelId;
        static double dist = 1;//选择点与选择实体的距离偏差

        static frmTrace glbFrmTrace;
        
       [CommandMethod("AppendEventB")]
        public static void AppendEventB(List<string> lstAppNameIn){
         try{
                glbFrmTrace = new frmTrace();
                glbFrmTrace.Show();

                lstAppName = lstAppNameIn;
                doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
          ed = doc.Editor;
          acaddoc = (AcadDocument)doc.AcadDocument;
          acaddoc.BeginDoubleClick += new _DAcadDocumentEvents_BeginDoubleClickEventHandler(beginDoubleClick);
                Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.DocumentLockModeChanged += new DocumentLockModeChangedEventHandler(vetoCommand);
                Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.DocumentCreated += new DocumentCollectionEventHandler(documentCreated);
          ed.SelectionAdded += new SelectionAddedEventHandler(ed_SelectionAdded);
         }catch{
         
         }
        }
       [CommandMethod("AppendEventC")]//鼠标悬浮提示
       public static void AppendEventC(List<string> lstAppNameIn) {
           try {
              
               doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
               ed = doc.Editor;
               ed.PointMonitor += new PointMonitorEventHandler(ed_PointMonitor);

           } catch {

           }
       }
        //---------------------------------------------
       static void ed_PointMonitor(object sender, PointMonitorEventArgs e) {
           //拾取当前实体对象提交
           FullSubentityPath[] fsp = e.Context.GetPickedEntities();
           if (fsp.Length > 0) {
               // 显示实体ID
               // doc.Editor.WriteMessage(fsp[0].GetObjectIds()[0].ToString());
               doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
               ed = doc.Editor;
               ed.WriteMessage("ed_PointMonitor");
               e.AppendToolTipText("敢创:id=" + fsp[0].GetObjectIds()[0].ToString());
              

               DBObject obj = null;
               using (Transaction transaction = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction())
                {
                   obj = transaction.GetObject(fsp[0].GetObjectIds()[0], Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead, true);
                    transaction.Commit();
                }
               glbFrmTrace.txtHint.Text += "type : = "+obj.GetType().ToString() + "\n";
              
               //if(obj.GetType().ToString()==){
               //     Line objLine = (Line)obj;

               //     glbFrmTrace.txtHint.Text += "isline" + "\n";

               //}else{
               //    glbFrmTrace.txtHint.Text += "not line" + "\n";
               //}
              

              
           }
       }

 

        static void ed_SelectionAdded(object sender, SelectionAddedEventArgs e) {
             glbFrmTrace.txtHint.Text += "ed_SelectionAdded \r\n";
            //glbFrmTrace.txtHint.Text.Insert(glbFrmTrace.txtHint.Text.Length, "ed_SelectionAdded \n");
 try{
  if (e.AddedObjects.Count == 1){
              SelectedObject objSel = e.AddedObjects[0];
              objSelId = objSel.ObjectId;
          }
          else
          {
              // objSelId = ObjectId.Null; //不能加上,否则就和不用此方法效果一样
          }
         }catch{
        
          }
}
 
 
         static void documentCreated(object sender, DocumentCollectionEventArgs e) {
            glbFrmTrace.txtHint.Text += "documentCreated \r\n";
             //glbFrmTrace.txtHint.Text.Insert(glbFrmTrace.txtHint.Text.Length, "documentCreated \n");
             try
             {
                 doc = e.Document;
                 acaddoc = (AcadDocument)doc.AcadDocument;
                 acaddoc.BeginDoubleClick += new _DAcadDocumentEvents_BeginDoubleClickEventHandler(beginDoubleClick);
             }
             catch
             { }
         }
 
         static void beginDoubleClick(object PickPoint)
         {
             glbFrmTrace.txtHint.Text += "beginDoubleClick \r\n";
             //glbFrmTrace.txtHint.Text.Insert(glbFrmTrace.txtHint.Text.Length, "beginDoubleClick \n");
             try
             {
                 PromptSelectionResult res = ed.SelectImplied();
                 if (res.Status == PromptStatus.Error)//属性块双击不能选中的处理
                 {
                     if (objSelId != ObjectId.Null)
                     {
                         ShowDialog(objSelId, PickPoint, true);//true表示是上次选中的(在方法中判断是属性块时用)
                         objSelId = ObjectId.Null;
                         return;
                     }
                 }
                 else
                 {
                     SelectionSet SS = res.Value;
                     if (SS.GetObjectIds().Length == 1)
                     {
                         ObjectId oId = SS.GetObjectIds()[0];
                         ShowDialog(oId, PickPoint, false);
                         return;
                     }
                 }
                 m_DbClick = false;
             }
             catch
             {
                 m_DbClick = false; //异常时保证面板等原来CAD自带的可用
             }
         }
 
 
         /// <summary>
         ///  //实现自定义窗口的调用
         /// </summary>
         /// <param name="oId">选中的ObjectId</param>
         /// <param name="ptPick">点取的点坐标</param>
         /// <param name="isSeled">是否是上次选中的</param>
         static void ShowDialog(ObjectId oId, object ptPick, bool isSeled) {
             frmHint dlg = new frmHint();
             dlg.ShowDialog();
             m_DbClick = true;
             try
             {
                 using (Transaction tr = doc.TransactionManager.StartTransaction())
                 {
                     string appNameUse="";
                     DBObject dbObj = tr.GetObject(oId, OpenMode.ForRead);
                     string strXData="";
                     bool isGet = false;
                     foreach (string appName in lstAppName)
                     {
                         //isGet = CADXData.TryGetXData(dbObj, appName, out strXData);
                         appNameUse = appName;
                     }
                     if (isGet)
                     {
                         AcadEntity ent = dbObj.AcadObject as AcadEntity;
                         bool isAttBlock = IsAttBlock(ent);//是否是属性块 ,需要另类处理
                         if (isSeled && !isAttBlock)//是非属性块时直接退出
                         {
                             m_DbClick = true;
                             return;
                         }
 
                         object objMin; object objMax; double[] ptMin; double[] ptMax;
                         ent.GetBoundingBox(out objMin, out objMax);
                         ptMin = (double[])objMin; ptMax = (double[])objMax;
 
                         double[] ptPic = ptPick as double[];
                         if (ptPic[0] > (ptMin[0] - dist) && ptPic[0] < (ptMax[0] + dist) &
                             ptPic[1] > (ptMin[1] - dist) && ptPic[1] < (ptMax[1] + dist))
                         {
 
                             //这个函数用委托  有可能是将全部扩展数据全显示出来
                             Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("扩展数据名为: " + appNameUse + ",扩展数据名为: " + strXData);
 
 
                             m_DbClick = true;
                         }
                     }
                    // tr.Commit();
                 }
             }
             catch
             { }
         }
 
         //得到是否是属性块
         private static bool IsAttBlock(AcadEntity ent)
         {
             if (ent is AcadBlockReference)
             {
                 AcadBlockReference refEnt = ent as AcadBlockReference;
                 object[] atts = (object[])refEnt.GetAttributes();
                 if (atts.Length > 0) return true;
             }
             return false;
         }
 
         //屏蔽CAD命令
         static void vetoCommand(object sender, DocumentLockModeChangedEventArgs e) {
             glbFrmTrace.txtHint.Text += "vetoCommand \r\n";
             //glbFrmTrace.txtHint.Text.Insert(glbFrmTrace.txtHint.Text.Length, "vetoCommand \n");
             try
             {
                 string com2 = e.GlobalCommandName.ToLower();
                 if (m_DbClick)
                 {
                     string com = e.GlobalCommandName.ToLower();
                     switch (com)
                     {
                         case "properties":
                             m_DbClick = false;
                             e.Veto();
                             break;
                         case "bedit":
                             m_DbClick = false;
                             e.Veto();
                             break;
                         case "eattedit":
                             m_DbClick = false;
                             e.Veto();
                             break;
                         case "ddedit":
                             m_DbClick = false;
                             e.Veto();
                             break;
                     }
                 }
             }
             catch
             { }
         }
 
     }
}

 

评分

参与人数 2威望 +1 明经币 +1 金钱 +20 贡献 +10 激情 +10 收起 理由
yxr_MJTD + 10
雪山飞狐_lzh + 1 + 1 + 10 + 10 + 10 【好评】好程序 加上注释说明下更好哈

查看全部评分

 楼主| 发表于 2010-9-15 13:52 | 显示全部楼层

窗口请定义。

自己坐沙发

发表于 2010-9-17 17:21 | 显示全部楼层
直接修改CUI文件的<DoubleClickRoot>节里的内容,应该更简便些。
发表于 2010-9-17 21:54 | 显示全部楼层

efan回来了,嘿嘿

同意,netapi也有相应的方面的方法的

 楼主| 发表于 2010-9-19 10:13 | 显示全部楼层

麻烦讲具体一点。谢谢!

发表于 2011-2-22 16:28 | 显示全部楼层
回复 chase_wang 的帖子

我在06下使用出现了一个 参数计数不正确的异常
发表于 2011-2-22 18:15 | 显示全部楼层
我在CAD2006下使用,无法屏蔽CAD本身的特性对话框
发表于 2011-2-23 22:22 | 显示全部楼层
同求06下的实现方法……
发表于 2011-3-3 16:41 | 显示全部楼层
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提供的操作进行编程。
发表于 2011-3-5 19:20 | 显示全部楼层
好东西,学习了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号  
©2000-2017 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2019-10-16 02:25 , Processed in 0.233522 second(s), 28 queries , Gzip On.

Powered by Discuz! X3.3

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表