ObjectARX
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Need help with ACHAR pointer please !

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
Chaos0205
468 Views, 4 Replies

Need help with ACHAR pointer please !

This command is supposed to get the input from user and make the layer with that name become on and active. However, when i try to run it make all the layer in the drawing on. How can i fix this please ?

 

        // declare variable

        AcDbLayerTable* pLayerTbl = NULL;
        acdbCurDwg()->getSymbolTable(pLayerTbl,AcDb::kForRead);
        AcDbLayerTableRecord* pLTR;
        AcDbLayerTableIterator* pLTI;
        pLayerTbl->newIterator(pLTI);
        AcDbObjectId pId;
        ACHAR* pLName;
        ACHAR name[134];

        // show all layers
        for (; !pLTI->done(); pLTI->step())
        {
            pLTI->getRecord(pLTR,AcDb::kForWrite);
            pLTR->getName(pLName);

            acutPrintf(_T("\n Layer name : %s\n"),pLName);
            pLTR->close();
        }
        
        // input layer name
        acedGetString(0,_T("\n Input layer name: "), name);

        // set layer on and active
        for (; !pLTI->done(); pLTI->step())
        {
            pLTI->getRecord(pLTR,AcDb::kForWrite);
            pLTR->getName(pLName);
            if (pLName = name)
            {
                pLTR->setIsOff(false);
                pLayerTbl->getAt(name,pId);
                acdbHostApplicationServices()->workingDatabase()->setClayer(pId);
            }
            pLTR->close();
        }
        pLayerTbl->close();
                
       

4 REPLIES 4
Message 2 of 5

Use “wcscmp” as shown in below code.


//if (pLName = name)
if(wcscmp(pLName, name) == 0)

 

Regards

Virupaksha Aithal

Autodesk Developer Network



Virupaksha Aithal KM
Developer Technical Services
Autodesk Developer Network

Message 3 of 5

Thank you very much for your help !

Here is the completed code for anyone that may need it

 

AcDbLayerTable* pLayerTbl = NULL;
        acdbCurDwg()->getSymbolTable(pLayerTbl,AcDb::kForRead);
        AcDbLayerTableRecord* pLTR;
        AcDbLayerTableIterator* pLTI;
        pLayerTbl->newIterator(pLTI);
        AcDbObjectId pId;
        ACHAR* pLName;
        ACHAR name[134];
        
        // Input layer name
        acedGetString(0,_T("\n Input layer name: "), name);

        // show all layer inside the drawing 
        for (; !pLTI->done(); pLTI->step())
        {
            pLTI->getRecord(pLTR,AcDb::kForWrite);
            pLTR->getName(pLName);
            acutPrintf(_T("\n Layer name : %s\n"),pLName);

            // set the layer with right name to ON

            if(wcscmp(pLName, name) == 0)
            {
                pLTR->setIsOff(false);
            }

            // set the layer with right name to active
            if (pLName = name)
            {
                pLayerTbl->getAt(name,pId);
                acdbHostApplicationServices()->workingDatabase()->setClayer(pId);
            }
            pLTR->close();
        }
        pLayerTbl->close();   

Message 4 of 5
artc2
in reply to: Chaos0205

You've got a memory leak in that code.  Since your pLName variable is not const, you are calling the AcDbSymbolTableRecord::getName() version that returns a dynamically allocated string that you must free.  If you change your pLName variable to be const, then you'll be calling the getName() version that returns a pointer to the string within the AcDbSymbolTableRecord and you don't have to free it.

 

Also, you need to change this:

 

            // set the layer with right name to active
            if (pLName = name)

 

to this:

 

            // set the layer with right name to active
            if(wcscmp(pLName, name) == 0)

 

But, it would be more efficient to do all the setting inside one if() and you can get the record's objectId from the record rather than asking the table like this:

 

            // set the layer with right name to ON

            // set the layer with right name to active

            if(wcscmp(pLName, name) == 0)
            {
                pLTR->setIsOff(false);
                acdbHostApplicationServices()->workingDatabase()-​>setClayer(pLTR->objectId());
            }

Message 5 of 5
Chaos0205
in reply to: artc2

Actually, this is my original code as well. However, when i try running that, the code only able to set the layer to ON, but can not make it into ACTIVE, so i have to make it look like that now

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost