Sunday, March 20, 2005

Lab 1 - Solved

Hello,

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;
acdbHostApplicationServices()->workingDatabase()
->getBlockTable(pBlockTable,AcDb::kForRead);
// Get Block Table Record (Model Space)
if (pBlockTable) {
AcDbBlockTableRecord* pBTR = NULL;
// Open ModelSpace (for write)
pBlockTable->getAt(ACDB_MODEL_SPACE,
pBTR,AcDb::kForWrite);
if (pBTR) {
// Add entity and get its ObjectId
pBTR->appendAcDbEntity(entId,pEnt);
// Close BTR
pBTR->close();
}
// Close Block Table
pBlockTable->close();
}
return entId;
}

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

// 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
addToModelSpace(pLine);
pLine->close();
}

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) {
pEnt->setColorIndex(1);
// Close the entity!
pEnt->close();
}
}
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: ARXLAB1.zip!

7 comments :

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 pedrohenrique@engetower.com.br ficarei grato.

Abraços, Pedro Henrique Liberato

Anonymous said...

Fernando,

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.

Regards.

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.

Dawie

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.

Regards.

Anonymous said...

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

Fernando Malard said...

Hello,

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.
Regards,