Monday, January 31, 2005

Class 1 - Overview

Introduction

ObjectARX is an AutoCAD Runtime Extension.
With ObjectARX SDK you can build applications that will alow you to extend AutoCAD features like commands, dialog boxes, entities, objects and much more.

The ObjectARX application is actually a DLL that is loaded into AutoCAD environment and allows you to access new features as mentioned above. To be able to build these DLLs you need to follow some basic rules to setup Microsoft Visual Studio.NET environment and assert your application will respect AutoCAD requirements.

The performance of this application will be the same of native features. By the way, Autodesk uses ObjectARX to build vertical products you may already know like Autodesk MAP and Architectural Desktop, among many others.

User Requirements

Because ObjectARX is not a simple customization tool, some requirements must be observed to allow you to be able to proceed. If you don't match these requirements I would recommend you to first increase your skills and then go back to try this course.

The minimum requirements to learn at least basic ObjectARX are:
  • Basic Visual Studio.NET concepts;
  • Average C++ knowledge;
  • Advanced AutoCAD knowledge;
  • MFC (Microsoft Foundation Classes) concepts;
  • Object Oriented Techniques;

As mentioned before, this course is not intended to be a complex guide or even cover advanced features. I believe you can go further by yourself after learn the basic features and mainly the basic concepts which are the secret of ObjectARX.

I will not cover .NET Framework capabilities to make the course as much simple as I can. Once you learn and figure out how AutoCAD works from inside you will open your mind to ObjectARX capabilities and will be able to build great application!

How to use ObjectARX SDK

Once you have downloaded your copy of ObjectARX SDK and extracted it to your hard drive, you will find the following folders inside it:

\arxlabs : This directory consists of a set of labs that shows some aspects of the ObjectARX.
\classmap : This directory contains an AutoCAD drawing with the ObjectARX class hierarchy tree.
\docs : This directory contains the ObjectARX online help files.
\inc : The inc directory contains the ObjectARX header files.
\lib : The lib directory contains the ObjectARX library files.
\redistrib : This directory contains DLLs that may be required for an ObjectARX application to run.
\samples : This directory contains examples of ObjectARX applications.
\utils : This directory contains other libraries like brep for boundary representation and ObjARXWiz for the ObjectARX wizards.

ObjectARX Classes Naming

ObjectARX classes names follow the following prefix standards:

AcRx : Classes for binding an application and for runtime class registration and identification.
AcEd : Classes for registering native AutoCAD commands and for AutoCAD event notification.
AcDb : AutoCAD database classes.
AcGi : Graphics classes for rendering AutoCAD entities.
AcGe : Utility classes for common linear algebra and geometric objects.

Depending on which set of features you use in your applications you will need to use the corresponding library as follows:

AcRx : acad.lib, rxapi.lib, acdb16.lib
AcEd : acad.lib, rxapi.lib, acedapi.lib, acdb16.lib
AcDb : acad.lib, rxapi.lib, acdb16.lib
AcGi : acad.lib, rxapi.lib, acdb16.lib
AcGe : acad.lib, rxapi.lib, acge16.lib, acdb16.lib

ObjectARX Wizard

I will bypass the Visual Studio environment configuration to build ObjectARX applications. You could refer to this information inside SDK documentation. We will use on this course the Wizard provided by ADN (Autodesk Developer Network) team. It is located inside ObjectARX directory called \utils\ObjARXWiz. Inside it you will find the installation package named ArxWizards.msi.

To install this Wizard, close your Visual Studio.NET and double click the above mentioned file. Follow the steps. When finished, open Visual Studio.NET again and you will see a new toolbar.

See you on Class 2!

27 comments :

Anonymous said...

Is possible to use ObjectARX applications made to AutoCAD 2000 inside 2004?

Fernando Malard said...

No. ObjectARX application are binary compatible only inside the same family (built with same compiler). You will need to recompile your project on the appropriate compiler. Be aware that you may run into problems depending on which features you are using. Please refer to the ObjectARX documentation about Migration.

Anonymous said...

can i use objarx in autocad2007

Fernando Malard said...

Hi,

ObjectARX 2004: Will run on AutoCAD 2004, 2005 and 2006. Requires Visual Studio 2002

ObjectARX 2007: Will run on AutoCAD 2007 and 2008. Requires Visual Studio 2005

Regards,
Fernando.

Wassim Mansouri said...

Hi,

Very interesting!

I'm a CSharp (C#) developer and I wish to use my CSharp skills to develop Autocad based applications, is it possible to develop ObjectARX applications using CSharp as a programming language? If not; what is the right choice between VB.Net, VBA and C++?

Thanks Fernando.

Fernando Malard said...

Hello Wassim,

Yes, it is possible to program AutoCAD with C# and VB.NET.

If you need, you can mix the C++ world with .NET using mixed-mode modules.

I would recommend you a very nice Blog of a friend:

http://through-the-interface.typepad.com/

Regards,

Anonymous said...

How can I make a custom object derived from AcDbLine or AcDBPolyline, having constraints in XY, YZ or XZ planes when moving or dragging it

Fernando Malard said...

Hello,

All the transformations like MOVE, ROTATE, etc. are made through the transformBy() method.

Once you override it you can tweak the behavior of your custom entity when receiving these commands.

Take a look at the transformBy() method documentation for a better understanding of its capabilities.

Cheers.

kirti said...

Hi,
Please can you send me a sample code for reading existing dwg file and for reading the dimension of an entity
Thnking you in advance.

Fernando Malard said...

kirti,

Unfortunately I don't have a sample which demonstrate this.

The best reference is the sample "\ObjectARX 2010\samples\database\testdb_dg".

The term "dimension" is different depending on the type of entity you are dealing with. For a LINE entity it will be the distance between its start and end points. For a CIRCLE or ARC it will be the curve length, etc.

I would recommend you to take a look at the AcDbCurve derived classes like these:

AcDb2dPolyline
AcDb3dPolyline
AcDbArc
AcDbCircle
AcDbEllipse
AcDbLeader
AcDbLine
AcDbPolyline
AcDbRay
AcDbSpline
AcDbXline

All classes are base on parametric curves and you will have methods such as getDistAtPoint() and getDistAtParam().

Hope this help.
Regards,

smita said...

Hi,
I have tried u'r following code from class 5,


ads_name na;
AcDbObjectId id;
acedSSGet(_T("L"),NULL,NULL,NULL,na);
acdbGetObjectId(id,na);
AcDbEntity* pEnt = NULL;
if (acdbOpenObject(pEnt, id, AcDb::kForRead) == Acad::eOk) {
if (pEnt->colorIndex() != 3) {
pEnt->upgradeOpen();
pEnt->setColorIndex(3);
}
else {
acutPrintf(_T("\nEntity already has color=3"));
}
pEnt->close();
}








but,i got error at acdbOpenObject.
error is:-
fatal error:Unhandled access violation reading 0x0005 Exception at 620663e4h.
Can u help me.
Thanking you.
Smita.

Fernando Malard said...

smita,

I have answered your question at the other thread. Please do not post the same issue at different threads. The publish process needs my approval due the SPAM protecting measures so depending on my availability it may take some time to appear after you submit it.

Regards.

Anonymous said...

Dear, thank you for this blog
I'm a Civil Engineer working in France.
Can I used another compiler program than Microsoft Visual Studio to program with Object ARX?

My best IDE for C++ is Code Block. Can I use it to program Autocad with ObjectARX ? Please could wou explain me how to setup the ARX for CodeBlocks ??
Thank you for your helps

Guy Kengne said...

Dear, thank you for this blog
I'm a Civil Engineer working in France.
Can I used another compiler program than Microsoft Visual Studio to program with Object ARX?

My best IDE for C++ is Code Block. Can I use it to program Autocad with ObjectARX ? Please could wou explain me how to setup the ARX for CodeBlocks ??
Thank you for your helps

Fernando Malard said...

Hello,

Unfortunately only Visual Studio is supported for ObjectARX development.

You can use (with some limitations) the Express version which is FREE.

Please take a look at this post from my friend Kean: http://through-the-interface.typepad.com/through_the_interface/2006/07/getting_the_obj.html

Hope this help you.
Regards,

Fernando Malard said...

Guy, just answered you at the previous post.
Thank you,

Anonymous said...

Hello Fernando,
Can you please tell me how to form bitmap of definite size say 32 X 32 pixel from Selection Set in ObjectARX 2010. I can do it from Export() function on IAcadDocument, but it gives me bitmap of complete view. I want the bitmap of only selected entity cropped in.

Thanks in Advance.

Fernando Malard said...

Hello,
I'm afraid it can't be easily done.
The Export() method defaults the window are to be exported so I don't think there is a way to change this behaviour.

I think you will need to do something after the bitmap is generated by calculating the entity Bounding Box (in screen coordinates) and cropping the bitmap with that information.

Sorry about the bad news.
Regards,

Anonymous said...

Hi fernando,

I need to draw a solid by extruding an existing solid. Can you give some reference code as to how to make it.

Fernando Malard said...

Hi,

You have several options.
I believe you actually meant you want to extrude a region to generate a solid.

If so, take a look at the following method:

virtual Acad::ErrorStatus extrudeAlongPath(
const AcDbRegion* region,
const AcDbCurve* path,
double taperAngle = 0.0
);

"Creates a solid by extruding region along the path curve. path must be an AcDbLine, AcDbArc, AcDbCircle, AcDbEllipse, AcDbSpline, AcDb2dPolyline, or a non-spline fit AcDb3dPolyline. path should not have self-intersections and, preferably, should not have high curvature areas. If one of the endpoints of the path is not on the plane of the region, then the path will be moved to make this true."

Regards.

Rahul said...

Hi Fernando,

thanks for your prompt reply. I have generated solid by using the method

virtual Acad::ErrorStatus extrude(
const AcDbRegion* region,
double height,
double taperAngle = 0.0
);

but instead of taperAngle I need to extrude my rectangle through its length, breadth and height. Can you help me out

Fernando Malard said...

Rahul,

The AcDbRegion is a planar entity so it has a normal vector which is used to drive the extrusion direction.
If your region represents the XY base of your solid it is going to be extruded towards it height direction Z.

So the extrusion depends on the region normal.

If you need an irregular extrusion, i.e., following a curve path, take a look at this other method:


virtual Acad::ErrorStatus extrudeAlongPath(
const AcDbRegion* region,
const AcDbCurve* path,
double taperAngle = 0.0
);


Regards,

Rahul said...

Fernando,

I have drawn a rectangle and then I extruded it. But I dont know how can I specify the length and width of my rectangle dynamically?

Is there any API to specify length and width?

Fernando Malard said...

Hi Rahul,

A rectangle is not an entity itself, it is indeed a Polyline.
As any Polyline it is created by passing in its vertexes so you need to calculate them yourself.
Make sure you also set Polyline as closed so it behaves appropriately.

Regards,

Rahul said...

you are just awesome... thanks alot!!
I hope I will learn ObjectARX in quick succession with your guidance.

Regards,
rahul

Rahul said...

Hi Fernando,

I have set the region of my rectangle in the following way.

AcGePoint3dArray pointArray;
ads_real length,width;

AcGePoint3d VertexPoint((0.0), (0.0), (0.0));
pointArray.append(VertexPoint);

if (acedGetReal(L"\nenter length of rectangle ",&length)!=RTNORM) {
return;
}

VertexPoint.set(((length1)), (0.0), (0.0));
pointArray.append(VertexPoint);

if (acedGetReal(L"\nenter width of rectangle ",&width)!=RTNORM) {
return;
}

VertexPoint.set(((length)), (width), (0.0));
pointArray.append(VertexPoint);

VertexPoint1.set((0.0), (width), (0.0));
pointArray.append(VertexPoint);

VertexPoint.set((0.0), (0.0), (0.0));
pointArray.append(VertexPoint);

How can I set the region for a circle? Can I set it in a similar way?

Regards

Fernando Malard said...

Hello Rahul,

The method AcDbRegion::createFromCurves() accepts any closed curve derived from AcDbCurve class.
AcDbCircle, which represents a circle entity in AutoCAD, is also derived from AcDbCurve:

======================================
AcDbRegion::createFromCurves()

This static member function creates a set of AcDbRegion objects from the closed loops represented by the curves contained in the curveSegments array. The newly created region objects are returned in the regions array.

The curveSegments array must contain only pointers to AcDbLine, AcDbArc, AcDbEllipse, AcDbCircle, AcDbSpline, AcDb3dPolyline, or AcDbPolyline objects.

Note
The objects in curveSegments must be opened for read and not for write. If the objects are opened, calling this function will crash AutoCAD.

It is the calling application's responsibility to either add the AcDbRegion objects returned in the regions array to an AcDbDatabase or to delete them when they are no longer needed.

Returns Acad::eOk if the function is completely successful. If there is any problem during the creation of an AcDbRegion from any of the curves in curveSegments, then this function returns Acad::eInvalidInput and the regions array contains pointers to any AcDbRegion objects that were created before the error occurred. So, do not assume that a non-Acad::eOk return status indicates a total failure with no dynamically-allocated AcDbRegion objects returned.

======================================

Regards,