乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 121|回复: 9

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

[复制链接]

3

主题

11

帖子

1

银币

初来乍到

Rank: 1

铜币
23
发表于 2010-9-15 13:51:00 | 显示全部楼层 |阅读模式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk..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 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 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 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自带的可用
             }
         }


         ///
         ///  //实现自定义窗口的调用
         ///
         /// 选中的ObjectId
         /// 点取的点坐标
         /// 是否是上次选中的
         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]  (ptMin[1] - dist) && ptPic[1]  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
             { }
         }

     }
}

回复

使用道具 举报

3

主题

11

帖子

1

银币

初来乍到

Rank: 1

铜币
23
发表于 2010-9-15 13:52:00 | 显示全部楼层
窗口请定义。
自己坐沙发
回复

使用道具 举报

26

主题

589

帖子

10

银币

中流砥柱

Rank: 25

铜币
693
发表于 2010-9-17 17:21:00 | 显示全部楼层
直接修改CUI文件的节里的内容,应该更简便些。
回复

使用道具 举报

72

主题

2726

帖子

9

银币

社区元老

Rank: 75Rank: 75Rank: 75

铜币
3014
发表于 2010-9-17 21:54:00 | 显示全部楼层
efan回来了,嘿嘿
同意,netapi也有相应的方面的方法的
回复

使用道具 举报

3

主题

11

帖子

1

银币

初来乍到

Rank: 1

铜币
23
发表于 2010-9-19 10:13:00 | 显示全部楼层
麻烦讲具体一点。谢谢!
回复

使用道具 举报

0

主题

3

帖子

2

银币

初来乍到

Rank: 1

铜币
3
发表于 2011-2-22 16:28:00 | 显示全部楼层
回复
我在06下使用出现了一个 参数计数不正确的异常
回复

使用道具 举报

0

主题

3

帖子

2

银币

初来乍到

Rank: 1

铜币
3
发表于 2011-2-22 18:15:00 | 显示全部楼层
我在CAD2006下使用,无法屏蔽CAD本身的特性对话框
回复

使用道具 举报

19

主题

154

帖子

5

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
230
发表于 2011-2-23 22:22:00 | 显示全部楼层
同求06下的实现方法……
回复

使用道具 举报

26

主题

589

帖子

10

银币

中流砥柱

Rank: 25

铜币
693
发表于 2011-3-3 16:41:00 | 显示全部楼层
cui文件的层次结构类似如下:
  
   
   
       '菜单
       '工具栏
       '双击动作
        
         
          块
         
            
         
        
        
         
          属性块
         
            
         
        
      
   
  
可以通过CUI命令在界面中进行编辑,或者通过Autodesk.AutoCAD.Customization提供的操作进行编程。
回复

使用道具 举报

6

主题

15

帖子

1

银币

初来乍到

Rank: 1

铜币
39
发表于 2011-3-5 19:20:00 | 显示全部楼层
好东西,学习了
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-15 12:42 , Processed in 1.967657 second(s), 83 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表