Life is Good.

Enhance Tech and English
随笔 - 65, 文章 - 20, 评论 - 21, 引用 - 0
数据加载中……

AutoCAD: 添加鼠标右键

using System;
using System.Collections.Generic;
using System.Text;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Windows;

namespace ContextMenuApp
{
    
public class ContextMenuApp : IExtensionApplication
    {

        
private ContextMenuExtension m_ContextMenu;

        
public void Initialize()
        {
            AddContextMenu();
        }

        
public void Terminate()
        {
            RemoveContextMenu();
        }


        
// AddContextMenu
        public void AddContextMenu()
        {
            
try
            {
                m_ContextMenu 
= new ContextMenuExtension();
                m_ContextMenu.Title 
= (("ContextMenu Title")); // Change it to your Context Menu Title
                m_ContextMenu.Popup += new EventHandler(BlkRefContextMenu_Popup);

                MenuItem mi 
= new MenuItem(("HelloWorld")); // Change it to your Context Menu Item Name
                mi.Click += new EventHandler(CallbackOnClick);
                m_ContextMenu.MenuItems.Add(mi);
                RXClass rxc 
= BlockReference.GetClass(typeof(BlockReference));
                Application.AddObjectContextMenuExtension(rxc, m_ContextMenu); 
// Only Add it to BlockReference
            }
            
catch (System.Exception exc)
            {
                Editor ed 
= Application.DocumentManager.MdiActiveDocument.Editor;
                ed.WriteMessage(
string.Format("\n Add ContextMenu error: {0}", exc.Message));
            }
        }

        
// RemoveContextMenu
        public void RemoveContextMenu()
        {
            
try
            {
                
if (m_ContextMenu != null)
                {
                    RXClass rxc 
= BlockReference.GetClass(typeof(BlockReference));
                    Application.RemoveObjectContextMenuExtension(rxc, m_ContextMenu);
                    m_ContextMenu 
= null;
                }
            }
            
catch (System.Exception exc)
            {
                Editor ed 
= Application.DocumentManager.MdiActiveDocument.Editor;
                ed.WriteMessage(
string.Format("\n Add ContextMenu error: {0}", exc.Message));
            }
        }

        
private void BlkRefContextMenu_Popup(Object o, EventArgs e)
        {
            DocumentLock docLock 
= Application.DocumentManager.MdiActiveDocument.LockDocument();

            Editor ed 
= Application.DocumentManager.MdiActiveDocument.Editor;
            
bool visible = true;

            
// If this is multiple selection, disabled the menu item.
            PromptSelectionResult selectionRes = ed.SelectImplied();
            
if (selectionRes.Status == PromptStatus.OK)
            {
                ObjectId[] objIds 
= selectionRes.Value.GetObjectIds();
                
if (objIds != null && objIds.Length > 1)
                {
                    visible 
= false;
                }
            }

            ContextMenuExtension objContextMenu 
= o as ContextMenuExtension;
            
if (objContextMenu != null)
            {
                
foreach (MenuItem item in objContextMenu.MenuItems)
                {
                    item.Enabled 
= visible;
                }
            } 
            
            docLock.Dispose();
        }

        
// CallbackOnClick
        private void CallbackOnClick(Object o, EventArgs e)
        {
            DocumentLock docLock 
= Application.DocumentManager.MdiActiveDocument.LockDocument();

            HelloWorld(); 
// Invoke this function

            docLock.Dispose();
        }

        
// You can change this function name 
        [CommandMethod("HelloWorld")]
        
public void HelloWorld()
        {
            
// Get current file name
            string strFileName = Application.DocumentManager.MdiActiveDocument.Name;

            
// Get the selection block reference and block record  handle
            string strBlkRefHandle = ("");
            
string strBlkRecHandle = ("");
            Editor ed 
= Application.DocumentManager.MdiActiveDocument.Editor;
            PromptSelectionResult sSelResult 
= ed.SelectImplied(); // ed.GetSelection();

            
if (sSelResult.Status == PromptStatus.OK)
            {
                ObjectId[] objectIDs 
= sSelResult.Value.GetObjectIds();
                
if (objectIDs.Length == 1)
                {
                    ObjectId blkRefID 
= objectIDs[0];
                    
if (blkRefID.IsValid)
                    {
                        
// Get handle
                        Handle handle = blkRefID.Handle;
                        strBlkRefHandle 
= handle.ToString();

                        Database db 
= HostApplicationServices.WorkingDatabase;
                        Transaction tr 
= db.TransactionManager.StartTransaction();

                        BlockReference blf 
= (BlockReference)tr.GetObject(blkRefID, OpenMode.ForRead);
                        
if (blf is BlockReference)
                        {
                            ObjectId blkRecID 
= blf.BlockTableRecord;
                            
if (blkRecID.IsValid)
                                strBlkRecHandle 
= blkRecID.Handle.ToString();
                        }

                        tr.Commit();
                    }
                }
            }

            
if (strFileName.Length > 0 && strBlkRefHandle.Length > 0 && strBlkRecHandle.Length > 0)
            {
                
// Invoke external EXE 
                System.Diagnostics.ProcessStartInfo info =
                    
new System.Diagnostics.ProcessStartInfo(@"E:\work\ContextMenuApp\SimpleProject\bin\Release\SimpleProject.exe"); // Your exe path

                
// Pass three arguments
                string sArg = strFileName + " " + strBlkRefHandle + " " + strBlkRecHandle; 
                info.Arguments 
= sArg;
                System.Diagnostics.Process p 
= System.Diagnostics.Process.Start(info);
            }
        }
    }
}


posted on 2010-05-29 23:17 Mike Song 阅读(412) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理