Sei sulla pagina 1di 11

Las Vegas, Nevada

November 27-30, 2001

Speaker Name: Bill Kramer

Course Title: Writing your first ObjectARX Program

Course ID: PR33-2L

Course Outline:

The first program in ObjectARX is the hardest. There are many pieces to get in place and several things that must be set up
properly in order to do the development work. The purpose of this lab is to provide a simple ObjectARX example program for you
to create and compile on the computers. The template file developed will enable you to use other templates supplied by Autodesk
and begin a path towards advanced programming of AutoCAD.

READ THIS RIGHT AWAY!


Welcome, please find a seat near a computer. If you are not able to type, then locate a partner who can.
We will do a little bit of typing in this class. Most of the code has been prepared for you in advance and
placed on the disk.

We will use two programs in this course. Please start the following as soon as you have selected your
workstation for the class.

AutoCAD
Microsoft Visual C++ 6

If either of these programs are not present on the computer you have selected please let the instructor or
lab assistant know before class starts.

STOP: DO NOT GO ANY FURTHER WITH THESE PROGRAMS UNTIL INSTRUCTED!


Writing your first ObjectARX Program

These handouts contain screen component captures to enable you to keep up with the class and
to serve as a reminder of the steps involved. We are going to compile an existing C++ program
and perform each step on the computer. We will then read through the source code to understand
how it works and if time permits, do another example. The handouts make this look worse than it
really is.

You should already have Microsoft C++ 6.0 up and running on your computer. Each computer is
equipped with the ObjectARX tool kit (a set of C++ libraries for the creation of ARX programs) as
well as Microsoft C++ and AutoCAD.

Set Microsoft Visual C++ as the current window and start a new project. Click on
the File pull down menu and pick New... to present the New dialog box.

Select Win32 Dynamic-Link Library, and a custom project name "AU2001" in the
C:\CSOURCE\ location. C++ will create a new directory. If C:\CSOURCE\AU2001\ already
exists, then select a new name in the C:\CSOURCE\ folder.
Create an empty DLL project

Now we will add the files already on your hard disk to the project

Add two files to the project - MCOPY1.CPP and AU2001.DEF

You will have to repeat the process of adding files to the project to add both files.
To view the source code select the "File View" tab (lower left) and open the Source files
folder in the panel above the tabs. Then select the CPP file to see the source code of our
project (note code may appear different than presented in the following screen).

The first time a project is created, you must modify the default DLL creation settings for the
project.

In the Projects pull down menu select the Settings... option.


Pick the Win32 Release in the "Settings For:" pop list.

4
We need to adjust a couple of things in the Project Settings. They are located under the
C/C++ tab on the right. Select the C/C++ tab and then select "Code Generation" from the
Category: pop up list.

In the code generation panel change the Use run-time library setting to "Multithreaded DLL".

Jump to the Preprocessor category in the C/C++ panel.

Add the preprocessor directory ",ACRXAPP" to the line.

5
The next stop is the Link panel entries where we need to change the name of the output file
name so that it has an ARX extension. By default it will be DLL.

We next adjust the Output setting of the Link panel.

Where we change the base address to be 0x1c000000 - this is a hex number, only the x and
c are letters. Everything else is a numeral.

In the Input settings of the Link panel we need to add a list of libraries. These are the
libraries that enable your program to communicate with AutoCAD.

6
The names to type in are:

rxapi.lib acrx15.lib acutil15.lib acedapi.lib acad.lib

The project is now ready, the next step is to prepare C++ to look for the ObjectARX files in
the ObjectARX folders. You will only do this once at your work station at home since these
values are retained from one project to the next.

Pick the Tools pull down menu and then Options...

Add the ObjectARX 2000 folder path to both the Include Files and Library files entries.

7
That's it! We are ready to build the program!

Test your program!

Go into AutoCAD and type the following at the command line:


(ARXLOAD "/csource/au2001/release/au2001.arx")

To unload the ARX module, use (ARXUNLOAD "AU2001")

The command "MCOPY" should now be available at the command line.

If we have time, let's do another one! This time use MCOPY2.CPP and call the project
"MCOPY".

MCOPY1.CPP Source Code Listing


#include <aced.h>
#include <rxregsvc.h>
#include <adscodes.h>
//
// The multiple copy command in ARX C++
//
void mcopy_run()
{
acutPrintf("\nMCOPY - multiple copy");
8
//
int iRet;
ads_name SS1;
ads_point ptBase, ptTo;
//
iRet = acedSSGet(NULL,NULL,NULL,NULL,SS1);
//
if (iRet == RTNORM)
{
iRet = acedGetPoint(NULL,"\nBase point:",ptBase);
while (iRet == RTNORM)
{
iRet = acedGetPoint(ptBase," copy point: ",ptTo);
if (iRet == RTNORM)
{
acedCommand(RTSTR,"_COPY",
RTPICKS, SS1,
RTSTR,"",
RTPOINT, ptBase,
RTPOINT, ptTo,
0
);
}
}
}
}
//
// ARX Entry Point, This is where the program actually starts.
//
extern "C" AcRx::AppRetCode
acrxEntryPoint(AcRx::AppMsgCode msg, void* pkt)
{
switch (msg) {
case AcRx::kInitAppMsg:
acutPrintf("\nCommand Group addition");
//
acrxDynamicLinker->unlockApplication(pkt);
acrxDynamicLinker->registerAppMDIAware(pkt);
//
// Add the new commands
//
acedRegCmds->addCommand("MY_COMMANDS",
"MCOPY",
"MCOPY",
ACRX_CMD_MODAL,
mcopy_run //direct link to function.
);
break;
case AcRx::kUnloadAppMsg:
acutPrintf("\nCommand group removal");
acedRegCmds->removeGroup("MY_COMMANDS");
break;
default:
break;
} //end switch
return AcRx::kRetOK;
}

AU2001.DEF Source File Listing


LIBRARY "AU2001"

9
EXPORTS
acrxEntryPoint PRIVATE
acrxGetApiVersion PRIVATE

MCOPY2.CPP Partial Source Code Listing


//
ads_point ptBase;
//
// Transformation function
// Will be used in the drag operation.
//
int dragSet(ads_point usrPt,ads_matrix Matrix)
{
int i, j;
//
// Create identity matrix
//
for (i=0; i<=3; i++)
for (j=0; j<=3; j++)
Matrix[i][j] = 0.0;
for (i=0; i<=3; i++)
Matrix[i][i] = 1.0;
//
// Add transformation point, relative to base point.
//
Matrix[0][3] = usrPt[0] - ptBase[0];
Matrix[1][3] = usrPt[1] - ptBase[1];
Matrix[2][3] = usrPt[2] - ptBase[2];
return RTNORM;
}
//
// The multiple copy command
//
void mcopy_run()
{
acutPrintf("\nMCOPY - multiple copy");
//
// AutoCAD System Variable set
//
struct resbuf rb; //Turn off the command echo
rb.restype = RTSHORT;
rb.resval.rint = 0;
acedSetVar("CMDECHO",&rb);
//
int iRet;
ads_name SS1;
ads_point ptTo;
//
iRet = acedSSGet(NULL,NULL,NULL,NULL,SS1);
//
if (iRet == RTNORM)
{
iRet = acedGetPoint(NULL,"\nBase point:",ptBase);
while (iRet == RTNORM)
{
//
// Create copy of set
acedCommand(RTSTR,"_COPY",
RTPICKS, SS1,
RTSTR, "",

10
RTPOINT, ptBase,
RTPOINT, ptBase,
0);
//
// Drag set on screen
iRet = acedDragGen(SS1,
"Copy location: ",
0, //flag for normal cursor
dragSet, //transform function
ptTo //result point
);
//
if (iRet == RTNORM)
{
acedCommand(RTSTR,"_MOVE",
RTPICKS, SS1,
RTSTR,"",
RTPOINT, ptBase,
RTPOINT, ptTo,
0
);
ptBase[0] = ptTo[0];
ptBase[1] = ptTo[1];
ptBase[2] = ptTo[2];
}else{
acedCommand(RTSTR,"_ERASE",
RTPICKS, SS1,
RTSTR,"",
RTSTR,"_REDRAW",
0
);
}
}
}
}

At the completion of this class, please delete the C:\CSOURCE folder from your computer.

Please remember to fill out the class evaluation form. I hope this helps get you started
along the road of using ObjectARX.

Thank you for attending Autodesk University 2001!

11

Potrebbero piacerti anche