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();
Solved! Go to Solution.
Solved by Virupaksha_aithal. Go to Solution.
Use “wcscmp” as shown in below code.
//if (pLName = name)
if(wcscmp(pLName, name) == 0)
Regards
Virupaksha Aithal
Autodesk Developer Network
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();
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());
}
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