ObjectARX & Dummies

Course Index

*Course support: Click here!
*Course and User samples: Download here!

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: ARXLAB1.zip!


  • 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

    By Anonymous Anonymous, at 7:54 PM  

  • 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,

    By Anonymous Anonymous, at 5:22 AM  

  • 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.


    By Blogger Fernando Malard, at 8:12 AM  

  • 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.


    By Anonymous Anonymous, at 4:00 AM  

  • 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.


    By Blogger Fernando Malard, at 8:34 AM  

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

    By Anonymous Anonymous, at 9:43 PM  

  • 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.

    By Blogger Fernando Malard, at 1:48 PM  

Post a Comment

<< Home