Creating an AutoCAD command

Exclusive offer: get 50% off this eBook here
Instant Autodesk AutoCAD 2014 Customization with .NET [Instant]

Instant Autodesk AutoCAD 2014 Customization with .NET [Instant] — Save 50%

Use AutoCAD's .NET API to create your own powerful, custom applications for AutoCAD with this book and ebook

$19.99    $10.00
by Tom Nelson | October 2013 | .NET Architecture & Analysis

In this article by Tom Nelson, author of AutoCAD 2014 Customization with .NET, create a custom command for AutoCAD.

Some custom AutoCAD applications are designed to run unattended, such as when a drawing loads or in reaction to some other event that occurs in your AutoCAD drawing session. But, the majority of your AutoCAD programming work will likely involve custom AutoCAD commands, whether automating a sequence of built-in AutoCAD commands, or implementing new functionality to address a business need. Commands can be simple (printing to the command window or a dialog box), or more difficult (generating a new design on-the-fly, based on data stored in an existing design). Our first custom command will be somewhat simple.

We will define a command which will count the number of AutoCAD entities found in ModelSpace (the space in AutoCAD where you model your designs). Then, we will display that data in the command window. Frequently, custom commands acquire information about an object in AutoCAD (or summarize a collection of user input), and then present that information to the user, either for the purpose of reporting data or so the user can make an informed choice or selection based upon the data being presented.

Using Netload to load our command class

You may be wondering at this point, "How do we load and run our plugin?" I'm glad you asked! To load the plugin, enter the native AutoCAD command NETLOAD. When the dialog box appears, navigate to the DLL file, MyAcadCSharpPlugin1.dll, select it and click on OK. Our custom command will now be available in the AutoCAD session. At the command prompt, enter COUNTENTS to execute the command.

Getting ready

In our initial project, we have a class MyCommands, which was generated by the AutoCAD 2014 .NET Wizard. This class contains stubs for four types of AutoCAD command structures: basic command; command with pickfirst selection; a session command; and a lisp function. For this plugin, we will create a basic command, CountEnts, using the stub for the Modal command.

How to do it...

Let's take a look at the code we will need in order to read the AutoCAD database, count the entities in ModelSpace, identify (and count) block references, and display our findings to users:

  1. First, let's get the active AutoCAD document and the drawing database.
  2. Next, begin a new transaction. Use the using keyword, which will also take care of disposing of the transaction.
  3. Open the block table in AutoCAD. In this case, open it for read operation using the ForRead keyword.
  4. Similarly, open the block table record for ModelSpace, also for read (ForRead) (we aren't writing new entities to the drawing database at this time).
  5. We'll initialize two counters: one to count all AutoCAD entities; one to specifically count block references (also known as Inserts). Then, as we iterate through all of the entities in AutoCAD's ModelSpace, we'll tally AutoCAD entities in general, as well as block references.
  6. Having counted the total number of entities overall, as well as the total number of block references, we'll display that information to the user in a dialog box.

How it works...

AutoCAD is a multi-document application. We must identify the active document (the drawing that is activated) in order to read the correct database. Before reading the database we must start a transaction. In fact, we use transactions whenever we read from or write to the database. In the drawing database, we open AutoCAD's block table to read it. The block table contains the block table records ModelSpace, PaperSpace, and PaperSpace0. We are going to read the entities in ModelSpace so we will open that block table record for reading.

We create two variables to store the tallies as we iterate through ModelSpace, keeping track of both block references and AutoCAD entities in general. A block reference is just a reference to a block. A block is a group of entities that is selectable as if it was a single entity. Blocks can be saved as drawing files (.dwg) and then inserted into other drawings. Once we have examined every entity in ModelSpace, we display the tallies (which are stored in the two count variables we created) to the user in a dialog box. Because we used the using keyword when creating the transaction, it is automatically disposed of when our command function ends.

Summary

The Session command, one of the four types of command stubs added to our project by the AutoCAD 2014 .NET Wizard, has application (rather than document) context. This means it is executed in the context of the entire AutoCAD session, not just within the context of the current document. This allows for some operations that are not permitted in document context, such as creating a new drawing.

The other command stub, described as having pickfirst selection is executed with pre-selected AutoCAD entities. In other words, users can select (or pick) AutoCAD entities just prior to executing the command and those entities will be known to the command upon execution.

Resources for Article:


Further resources on this subject:


Instant Autodesk AutoCAD 2014 Customization with .NET [Instant] Use AutoCAD's .NET API to create your own powerful, custom applications for AutoCAD with this book and ebook
Published: September 2013
eBook Price: $19.99
See more
Select your format and quantity:

About the Author :


Tom Nelson

Tom Nelson is an independent AutoCAD programming consultant and freelance technical writer and editor. He began freelance consulting after nearly nine years with Autodesk, first with the Developer Technical services, and later as a technical writer for various Inventor feature teams and the Autodesk Intent and Fusion product teams. He has worked as an in-house CAD applications programmer for an HVAC manufacturing company and as a mechanical designer for the aviation and semiconductor industries. Tom has delivered numerous CAD programming projects, both in-house and as a consultant, and has nearly 20 years' experience supporting or developing with Autodesk APIs.

Tom has developed and presented a training class, AutoCAD ActiveX/VBA API for Autodesk Developer Network members, and contributed code and content to the Autodesk AutoCAD .NET API training class, which he has also presented onsite at Autodesk in San Rafael, CA.

Tom is an Autodesk authorized developer. He lives in Portland, Oregon with his wife and daughter, and two cats.

Books From Packt


	 Autodesk AutoCAD 2013 Practical 3D Drafting and Design
Autodesk AutoCAD 2013 Practical 3D Drafting and Design

Instant Autodesk Revit 2013 Customization with .NET How-to [Instant]
Instant Autodesk Revit 2013 Customization with .NET How-to [Instant]

	 FreeCAD [Instant]
FreeCAD [Instant]

Blender 3D Architecture, Buildings, and Scenery
Blender 3D Architecture, Buildings, and Scenery

3ds Max Speed Modeling for 3D Artists
3ds Max Speed Modeling for 3D Artists

Unity for Architectural Visualization
Unity for Architectural Visualization

Getting Started with DraftSight
Getting Started with DraftSight

SketchUp 7.1 for Architectural Visualization: Beginner's Guide
SketchUp 7.1 for Architectural Visualization: Beginner's Guide


No votes yet

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
j
q
H
r
n
5
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software