About the Speaker:
Fernando is a senior developer for OFCDesk L.L.C. He has been an ADN member since 1996, and
specializes in AutoCAD, C++, ObjectARX, MFC, .NET, SQL, and Oracle. He is a teacher with experience in
software development, ERP solutions, CAD solutions, and system integration. Fernando is an AUGI member
(and serves on the AUGI board, Brazil), an Autodesk beta tester, and holds a Master’s degree in Structural
Engineering from the Federal University of Minas Gerais, Brazil.
Email: fernando.malard@ofcdesk.com (business) / fpmalard@yahoo.com.br (personal)
The Power of ObjectARX
®
Fernando P. Malard – OFCDesk, LLC.
CP401-2 Most users are unaware of the great solutions they can make using ObjectARX
®
programming.
This class will show you how to create and use AutoCAD
®
custom entities and how powerful
they can be. With simple and direct examples, you will have a wonderful experience creating a
custom entity step by step.
Key Topics:
ObjectARX introduction
Application concepts
Database and Containers
Creating custom entities
Improving custom entities
Target Audience:
VBA
®
and Visual LISP
®
programmers, C++ programmers, AutoCAD users
The Power of ObjectARX
®
Introduction
During the last years AutoCAD has evolved to become a generic and powerful CAD platform
providing a huge development in CAD and Design industries. This evolution, in its majority, was due the
reduction of the existing lack between real situations and conceptual projects. This was only possible with
the AutoCAD open architecture which was introduced at AutoCAD R13. At that moment Autodesk
®
has
decided to open part of AutoCAD core to developers in form of a C++ SDK (Software Development Kit)
providing a powerful tool and allowing developers to create applications and solutions that can take full
advantage of AutoCAD core.
ObjectARX, named at that time ARX, was introduced officially at release 13. After this release,
AutoCAD evolved to a powerful interface with release 14 and next, to a MDI (Multiple Document
Interface) environment with release 2000. Nowadays, AutoCAD in its release 17.1 (also known as 2008)
is going beyond all optimistic forecasts and it is providing great tools for developers like .NET interface
through C# and VB.NET languages.
ObjectARX, through C++, stays at the top of performance and power list when compared with
other AutoCAD development languages. The reason is really simple, until today the major part of
AutoCAD is still coded in C++. It’s true that there are parts that provide interfaces with .NET but this tier
only provide an indirect access to the real C++ core.
ObjectARX takes one great advantage when compared to the other AutoCAD customization
languages. Currently it is the only one that fully implements Custom Entities (Objects). This great
capability of ObjectARX was intensively used by Autodesk itself to create its vertical products since
AutoCAD R13. Today we can see several AutoCAD based solutions made by Autodesk using the power
provided by ObjectARX. We can quote a couple of verticals like Autodesk Architectural Desktop and
Autodesk MAP. These two products, for instance, use a lot of Custom Entities to bring the CAD solution
near to the real industry scenario. Instead of using lines and circles to represent architectural symbols,
Autodesk Architectural Desktop users are able to create specific entities like windows, doors, stairs, etc.
This makes the application much more suitable to its specific industry and reduces the time spent to
create specific projects.
In this class session you will be able to taste part of this power by implementing a simple Custom
Entity inside AutoCAD. You will start by learning about ObjectARX application basics. Next you will learn
some very important concepts about AutoCAD database, how its objects are organized, stored and how
to manipulate them. Finally, you will learn how to create a simple Custom Entity with basic features and
how to expand it to a higher level of complexity.
Note
: We will use AutoCAD 2008, Visual Studio 2005 and ObjectARX 2007 on this class. ObjectARX
modules created with ObjectARX 2007 are binary compatible with AutoCAD 2008.
AutoCAD Database
AutoCAD database is well organized to make easier to manipulate its objects. To better
understand how things happen you may imagine it as some kind of “object oriented database” which
receives commands, react to events, fire its own events and act as a storage place for objects. This
database needs to operate under certain conditions and requirements. It is sometimes a little complex to
manipulate but this enforces the database integrity and the application stability.
When you are creating a drawing inside of AutoCAD you are creating, deleting or changing
objects inside its database. AutoCAD database is a set of objects hierarchically organized which
represents a drawing (a DWG file). If we take as an example, a simple entity, say a LINE, it contains all
the necessary data to graphically represent itself. As a vector based application, AutoCAD doesn’t need
to store the pixels between the start and end points for each line. It stores only both the start and end
three-dimensional points (X,Y,Z). The line graphics are then generated by a math line equation and
mapped to the screen coordinates by a translation to the X,Y pixels.
The Power of ObjectARX
®
The process of creating a LINE is not finished by specifying its properties but it also requires you
to specify where and how this LINE will be stored. Inside AutoCAD database there are several existing
objects called containers which are intended to store and keep other objects. Each container has its
own procedures and rules to store and retrieve its child objects. In most cases, the container is
responsible for keeping and saving its children. In our example, a LINE can be stored into containers
made to store entities. These entity’s containers are called Block Table Records (BTR). Each BTR can
store several entities including lines, circles, texts, blocks, etc. The diagram presented in Figure 1, shows
part of the AutoCAD database structure.
Figure 1 – AutoCAD Database structure showing entity’s containers
The Figure 1 shows that entities can be stored into Model Space, Paper Space and other Block
Table Records. These “other” BTR can be a Layout page or even another Block. Each block inserted
inside AutoCAD drawing is a Block Reference entity which refers to the original Block which is a Block
Table Record. Due that, when you edit a Block with REFEDIT command it reflects on all its pointed Block
References because you are really editing the original Block entity objects. Next you will find a code
fragment which demonstrates the outline of LINE creation process through a sequence of C++ code
instructions which will create a LINE from point (1.0, 1.0, 0.0) to point (10.0, 10.0, 0.0):
01 // create two points and the line
02 AcGePoint3d startPt (1.0, 1.0, 0.0);
03 AcGePoint3d endPt (10.0, 10.0, 0.0);
04 AcDbLine* pLine = new AcDbLine (startPt, endPt);
05 // open the proper entity container
06 AcDbBlockTable* pBlock = NULL;
07 AcDbDatabase* pDB = acdbHostApplicationServices()->workingDatabase();
08 pDB->getSymbolTable(pBlock,AcDb::kForRead);
09 AcDbBlockTableRecord* pBTR = NULL;
10 pBlock->getAt(ACDB_MODEL_SPACE, pBTR, AcDb::kForWrite);
11 pBlock->close();
12 // now, add the entity to container
13 AcDbObjectId Id;
14 pBTR->appendAcDbEntity(Id, pLine);
15 pBTR->close();
16 pLine->close();
The Power of ObjectARX
®
The LINE creation process is similar to all other entities creation and it basically consists of the
following steps:
• We first Instantiate a pointer to the desired entity class (some constructors will allow or require
you to pass some basic parameters);
• Next, we need to open the Block Table where we will find the desired entity container. In this
example we will use the Model Space container;
• After that, we open the Model Space (a Block Table Record) and append to it the recently
instantiated entity.
It is very important to note that this process requires you to open and close each specific
object to have access to its properties and methods. Once you add the new entity to database it will
receive a unique identifier, provided automatically by AutoCAD database, called AcDbObjectId. This is
the key to access the entity whenever you want and it is valid and kept unchanged only during the
AutoCAD drawing session (database lifecycle).
This is just the outline process to make clear to you the basic steps required to create a simple
entity and to show the basic protocol to access some database objects and procedures. Next you will
learn how to create an ObjectARX application project using Visual Studio.
ObjectARX Application Overview
The ObjectARX application is in fact a DLL (Dynamic Link Library). This allows it to be loaded
at runtime inside AutoCAD. This concept has evolved to the Object Enabler which was introduced some
versions ago. This splits the application out to two modules. The Object Enabler is an ObjectARX
application with only a set of custom objects/entities classes. It is called ObjectDBX and it is compatible
to all Autodesk applications which are built at the top of DWG technology like all DWG viewers and all
other AutoCAD verticals. The last 3 characters of both types of application are meant to define its use:
ARX (AutoCAD Runtime Extension) and DBX (Database Extension). Basically your application will be
constructed by several modules of these two types:
Interfaces
: They will contain all dialogs, commands, events and all further AutoCAD interface
specific code. It will consist of an ObjectARX application DLL (.arx file extension) and will be compatible
only with AutoCAD and its based verticals;
Custom classes
: They will contain all custom classes (objects and/or entities) which do not rely
on AutoCAD application presence. It will consist of an ObjectDBX application DLL (.dbx file extension)
and it cannot contain any AutoCAD specific interaction or information. It can be loaded inside AutoCAD,
its verticals and all Autodesk applications with ObjectDBX support;
You can split your application into several ARX and DBX modules depending on its complexity
and this will make you application much more organized and flexible to expand. Imagine that you will
work with a set of clients which belong to the same industry segment with some small differences among
their products. You may organize your application classes to be implemented using two levels of
inheritance. The first level will implement the common behavior among client’s products. The next level
will then implement specific client requirements and will allow you to fit your application perfectly to each
client demands. The outline of your solution modules would be something like this:
• MyClientBasics.dbx;
• MyClient1.dbx, MyClient2.dbx, MyClient3.dbx, etc.
This way you can reuse the same core from client to client, and better, when you fix a problem or
add a new feature to your core modules all of your clients will get improved on the next update. This is a
pretty good strategy to keep your application organized, up to date and suitable to each client’s industry
business rules.