Execute the AutoCAD command on the Ribbon interface created by the program in AutoCAD

Source: Internet
Author: User

During secondary development based on AutoCAD, common tutorials are about defining some custom commands in the DLL, and then loading the DLL using the netload command, execute custom commands to execute custom functions. This method is very simple in learning, but it is too amateur to use it in formal products without professionalism. Of course, more professional is of course the same as AutoCAD, providing some Ribbon-based user interfaces to call our custom functions. This method also applies to other products based on AutoCAD, such as Map 3D and Civil 3D. The following describes how to create a Ribbon user interface.

 

First, let's take a look at the concept of Ribbon. It is a Ribbon interface of AutoCAD, which consists of tabs, such as Home and Insert. There are different panels in one Tab, that is, the split part of the vertical line; the panel contains buttons, which are used to execute specific functions. Not very accurate. I understand this is probably the case. :)

 

Next, we will create a tab through the program, add two panels to the tab, and put two buttons in the panel to execute custom AutoCAD commands.

To create a new class library project, you must add the following references:

Acmgd

Acdbmgd

AcCoreMgd (for autoscaling 2013)

AcCui

AcWindows

AdWindows

The following is a code snippet:

 

        private const string MY_TAB_ID = "MY_TAB_ID";    [CommandMethod("addMyRibbon")]    public void createRibbon()    {      Autodesk.Windows.RibbonControl ribCntrl =                Autodesk.AutoCAD.Ribbon.RibbonServices.RibbonPaletteSet.RibbonControl;      //can also be Autodesk.Windows.ComponentManager.Ribbon;           //add the tab      RibbonTab ribTab = new RibbonTab();      ribTab.Title = "My custom tab";      ribTab.Id = MY_TAB_ID;      ribCntrl.Tabs.Add(ribTab);      //create and add both panels      addPanel1(ribTab);      addPanel2(ribTab);      //set as active tab      ribTab.IsActive = true;    }    private void addPanel2(RibbonTab ribTab)    {      //throw new NotImplementedException();    }    private void addPanel1(RibbonTab ribTab)    {      //throw new NotImplementedException();    }

 

With this code, you can create a blank Tab and set it to the current active Tab,

 

Now add a Panel to this Tab and add a button to execute my custom commands.

 

Private const string MY_TAB_ID = "MY_TAB_ID"; [CommandMethod ("addMyRibbon")] public void createRibbon () {Autodesk. windows. ribbonControl ribCntrl = Autodesk. autoCAD. ribbon. ribbonServices. ribbonPaletteSet. ribbonControl; // can also be Autodesk. windows. componentManager. ribbon; // add the tab RibbonTab ribTab = new RibbonTab (); ribTab. title = "My custom tab"; ribTab. id = MY_TAB_ID; ribCntrl. tabs. add (rib Tab); // create and add both panels addPanel1 (ribTab); addPanel2 (ribTab); // set as active tab ribTab. isActive = true;} private void addPanel2 (RibbonTab ribTab) {// create the panel source RibbonPanelSource ribPanelSource = new RibbonPanelSource (); ribPanelSource. title = "Edit Registry"; // create the panel RibbonPanel ribPanel = new RibbonPanel (); ribPanel. source = ribPanelSource; ribTab. panels. add (RibPanel); // create button1 RibbonButton ribButtonDrawCircle = new RibbonButton (); ribButtonDrawCircle. text = "My Draw Circle"; ribButtonDrawCircle. showText = true; // pay attention to the SPACE after the command name ribButtonDrawCircle. commandParameter = "DrawCircle"; ribButtonDrawCircle. commandHandler = new AdskCommandHandler (); ribPanelSource. items. add (ribButtonDrawCircle);} private void add Panel1 (RibbonTab ribTab) {// throw new NotImplementedException ();} [CommandMethod ("DrawCircle")] public void DrawCircle () {// draw a circle, which is omitted here, this is not the focus of this blog.}

Note that the above Code defines a ribButtonDrawCircle, specifying its CommandParameter as my custom command name "DrawCircle", and specifying its CommandHandler as AdskCommandHandler. Here, AdskCommandHandler is a custom class and must implement the System. Windows. Input. ICommand interface. The implementation method is to send commandParameter to the AutoCAD command line window for execution during Execute. The Code is as follows:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using Autodesk.Windows;namespace AutoCAD_Debugger{  class AdskCommandHandler : System.Windows.Input.ICommand  {    public bool CanExecute(object parameter)    {      return true;    }    public event EventHandler CanExecuteChanged;    public void Execute(object parameter)    {      //is from Ribbon Button      RibbonButton ribBtn = parameter as RibbonButton;      if (ribBtn != null)      {        //execute the command         Autodesk.AutoCAD.ApplicationServices.Application          .DocumentManager.MdiActiveDocument          .SendStringToExecute(             (string)ribBtn.CommandParameter, true, false, true);      }    }  }}

 

The execution result is as follows. Note: If you find that your button only sends the command string to the AutoCAD command line but does not execute it, you must press enter to execute it. Most of the time you ignore the space behind CommandParameter!

 

Well, the basic process should be like this. Of course we can also add icons for the button to make it look better. The following section describes how to enable AutoCAD to automatically load the Ribbon menu. This is more convenient.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.