Sunday, March 20, 2005

Lab 1 - Solved


Hope you did solve this lab. I will present my solution now but this doesn't mean that this is the best or the only way to go.

After have created the project and added the two commands as the Lab instructions said, open the acrxEntryPoint.cpp file, move your cursor into CARXLAB1App class and add the following code:

static AcDbObjectId addToModelSpace(AcDbEntity* pEnt) {

AcDbObjectId entId = AcDbObjectId::kNull;
// Get Block Table
AcDbBlockTable* pBlockTable = NULL;
// Get Block Table Record (Model Space)
if (pBlockTable) {
AcDbBlockTableRecord* pBTR = NULL;
// Open ModelSpace (for write)
if (pBTR) {
// Add entity and get its ObjectId
// Close BTR
// Close Block Table
return entId;

static void ARXLAB1_CENTS(void) {
// First, let's create the Circle
AcGePoint3d cenPt(0,0,0);
AcDbCircle* pCirc = new AcDbCircle(cenPt,
// Add the circle to database and close it

// Now, let's create the Line
AcGePoint3d startPt(0,0,0);
AcGePoint3d endPt(10,10,0);
AcDbLine* pLine = new AcDbLine(startPt,endPt);
// Add the line to database and close it

static void ARXLAB1_CHENTS(void) {
ads_name en;
ads_point pt;
// Prompt for selection
if (acedEntSel(_T("\nSelect an entity: "),
en, pt) == RTNORM) {
AcDbObjectId eId = AcDbObjectId::kNull;
// Get the ObjectId from ads_name
acdbGetObjectId(eId, en);

AcDbEntity* pEnt = NULL;
// Open entity (for Write) to chance its color
if (acdbOpenObject((AcDbObject*&)pEnt,
eId,AcDb::kForWrite) == Acad::eOk) {
// Close the entity!
else acutPrintf(_T("\nCommand aborted."));

The first function, called addtoModelSpace() is a generic utility function which adds an arbitrary entity to ModelSpace and return its ObjectId. Following this function you will find two functions that were linked with CENTS and CHENTS commands.

All 3 funtions are placed into your application class. Pay attention to the code flow and be careful to not forget any braces or close() methods.

You may download this sample from here:!


Anonymous said...

Tudo bem Fernando?

Como faço para converter várias entidades em uma Polyline(como o pedit)? Tenho várias linhas e vários arcos.

Se puder escrever para ficarei grato.

Abraços, Pedro Henrique Liberato

Anonymous said...


Is there some way to get the nearest entity of a specified type in a similar manner as calling acedEntSel()? For example; I want to get the closest line to the clicked position,

Fernando Malard said...

You can use a Crossing polygon selection with a buffer to get the entities that touch this polygon.

Depending on your drawing scale you can determine the size of this polygon.

Use the "WP" option of acedSSGet() and pass in an entity filter for the desired entities you want to search for.

Take a look at acedSSGet() documentation for further information.


Anonymous said...

Thanks Fernando. Can the same be done with Pickfirst in stead of crossing polygon? I would like the user to click and I would like to get the nearest entity of a certain type to the click position.


Fernando Malard said...

I think it should work the same way WP and CP works.

I have never tried this but I htink it worth a try to see what happen.


Anonymous said...

hi, Fernando!
how can i modify points coordinates???

Fernando Malard said...


The point entity is represented by the AcDbPoint class. Once the user creates the point and you obtain its pointer with acdbOpenObject() in AcDb::kForWrite mode you can change the point position by calling the method:

Acad::ErrorStatus setPosition(const AcGePoint3d&);

The argument is a Geometry helper point object which contains the X,Y,Z coordinates of the point to where you want to modify your entity.

Hope this helps.

Ahmed Ahmed said...

You are not allowed to download the solved exercise.
You may download this sample from here:!

Clicking on will flash an error going to the following URL that doesn't yield the zip file to download.

This is true also for the other 2 solved exercises.

Fernando Malard said...

Hello Ahmed,

All samples were moved to Google Drive a while ago and I posted a message about this in 2011:

Please use the following URL to get access to all samples: