Book Image

Instant Autodesk AutoCAD 2014 Customization with .NET

By : Tom Nelson
Book Image

Instant Autodesk AutoCAD 2014 Customization with .NET

By: Tom Nelson

Overview of this book

<p>AutoCAD's .NET API can be used to create small but powerful applications that help CAD users achieve productivity gains and improve quality. CAD users can accelerate drafting and design processes, improve drawing accuracy, minimize time spent on repetitive or demanding tasks, and reduce errors. In short, users can deliver better drawings faster with customized CAD tools.</p> <p>Learn how to use AutoCAD's .NET API to create your own high-powered, custom applications for AutoCAD. This book is a toolbox of small projects for handling common AutoCAD tasks. You can add to these recipes to develop your own specialized AutoCAD program library. Clear, step-by-step instructions and complete code examples illustrate the process, making it easy to develop your own custom AutoCAD tools.</p> <p><br />Giving you the building blocks of AutoCAD application development, you&rsquo;ll learn to create compact user interfaces for your AutoCAD plugins and add command buttons to the ribbon interface. Next, you&rsquo;ll create programs to insert and modify AutoCAD block and attribute references, as well as working with custom data stored on AutoCAD objects. Learn how to publish layouts from external drawings in multi-sheet PDF files, export AutoCAD data to MS Excel for processing, and respond to AutoCAD event notifications (such as when an object is selected). With the tools presented in this book, you can develop and implement new functionality to address your specialized business needs.</p>
Table of Contents (7 chapters)

Working with AutoCAD events (Become an expert)


AutoCAD fires event notifications as a result of specific actions and states. These notifications provide crucial information to our custom applications, allowing them to react in real time to changes in your AutoCAD drawing.

Getting ready

This is a simple example that responds to AutoCAD commands being cancelled. The name of the cancelled command is displayed at the command prompt.

How to do it...

  1. First, we add two commands to turn the event handler monitoring on or off for the AutoCAD document CommandCancelled event:

    using System;
    using Autodesk.AutoCAD.Runtime;
    using Autodesk.AutoCAD.ApplicationServices;
    using Autodesk.AutoCAD.EditorInput;
    // This line is not mandatory, but improves loading performances
    [assembly: CommandClass(typeof(Ch11AcadPlugin.MyCommands))]
    
    namespace Ch11AcadPlugin {
       public class MyCommands {        
          [CommandMethod("EventON")]
          public void EventON() {
             Document doc = 
                Application.DocumentManager.MdiActiveDocument;
             //Add the event handler
             doc.CommandCancelled += new 
                CommandEventHandler(doc_CommandCancelled);    
          } 
    
          [CommandMethod("EventOFF")]
          public void EventOFF() {
             Document doc = 
                Application.DocumentManager.MdiActiveDocument;
             //Remove the event handler
             doc.CommandCancelled -= new 
                CommandEventHandler(doc_CommandCancelled);
          } 
  2. Now we will add the actual event handler functionality. We will use the WriteMessage() method to display the cancelled command name:

          public static void doc_CommandCancelled(object sender, 
             CommandEventArgs e) {
             Document doc = 
                Application.DocumentManager.MdiActiveDocument;
             Editor ed = doc.Editor;
             ed.WriteMessage(string.Format(
                "\n{0} command was cancelled by the user.\n", 
                e.GlobalCommandName));
          }       
       }
    }

How it works...

The CommandCancelled event handler receives the GlobalCommandName of the cancelled command, which is passed to the command prompt in a formatted string.