Add layer description

Hi,
I have wrote a function to create new layer. It create layer with name, color and off status set. But fail to add description to new layer.
bool newLayer( const ACHAR * name, const ACHAR * description, Adesk::UInt32 color, bool isOff )
{
Acad::ErrorStatus es = Acad::eOk;
AcDbLayerTableRecord * pLayerTableRecord = new AcDbLayerTableRecord;
es = pLayerTableRecord->setName( name );
if ( es != Acad::eOk )
{
acutPrintf( _T( "\nERROR: Could not add layer name! Error code: %s" ), acadErrorStatusText( es ) );
delete pLayerTableRecord;
return false;
}

if ( description != nullptr )
{
es = pLayerTableRecord->setDescription( description );
if ( es != Acad::eOk )
{
acutPrintf( _T( "\nERROR: Could not add layer description! Error code: %s" ), acadErrorStatusText( es ) );
acutPrintf( _T( "\nLayer description: %s" ), description );
}
}

AcCmColor colorObject;
if ( colorObject.setColorIndex( color ) == Acad::eOk )
pLayerTableRecord->setColor( colorObject );

pLayerTableRecord->setIsOff( isOff );

AcDbLayerTable * pLayerTable = nullptr;
es = acdbHostApplicationServices( )->workingDatabase( )->getSymbolTable( pLayerTable, AcDb::kForWrite );
if ( es != Acad::eOk )
{
acutPrintf( _T( "\nERROR: Could not open SymbolTable! Error code: %s" ), acadErrorStatusText( es ) );
delete pLayerTableRecord;
return false;
}

es = pLayerTable->add( pLayerTableRecord );
pLayerTable->close( );
if ( es != Acad::eOk )
{
acutPrintf( _T( "\nERROR: Could not add new layer to SymbolTable! Error code: %s" ), acadErrorStatusText( es ) );
delete pLayerTableRecord;
return false;
}

pLayerTableRecord->close( );

acutPrintf( _T( "\nLayer %s %s" ), name, _T( " have been created." ) );

return true;
}
After running this code in BricsCAD I get this output:
ERROR: Could not add layer description! Error code: eNullObjectPointer
Layer description: Place holder description.
Layer MyLayerName have been created.
Why pLayerTableRecord->setDescription( description ); returns eNullObjectPointer? As second line in output shows variable description is set to Place holder description.

Comments

  • Hi Donatas,
    interestingly, the behaviour is the same in AutoCAD/ARX ...
    reason is, that the "layer description" property is stored as XData, and XData on a database object require that the object is database-resident.

    > Layer description: Place holder description.
    which version of BricsCAD do you use ? In my testing on V22, the (input) string is not changed at all.

    So easiest solution :
    first add the new layer to the LayerTable, then assign all the properties :smile:
    
    bool newLayer(const ACHAR* name, const ACHAR* description, Adesk::UInt16 color, bool isOff)
    {
        AcDbLayerTable* pLayerTable = nullptr;
        Acad::ErrorStatus es = acdbCurDwg()->getSymbolTable(pLayerTable, AcDb::kForWrite);
        if (es != Acad::eOk)
        {
            acutPrintf(_T("\nERROR: Could not open SymbolTable! Error code: %ls"), acadErrorStatusText(es));
            return false;
        }
    
        AcDbLayerTableRecord* pLayerTableRecord = new AcDbLayerTableRecord();
    
        es = pLayerTable->add(pLayerTableRecord);
        pLayerTable->close();
        if (es != Acad::eOk)
        {
            acutPrintf(_T("\nERROR: Could not add new layer to SymbolTable! Error code: %ls" ), acadErrorStatusText(es));
            delete pLayerTableRecord;
            return false;
        }
    
        es = pLayerTableRecord->setName(name);
        if (es != Acad::eOk)
        {
            acutPrintf(_T("\nERROR: Could not add layer name! Error code: %ls"), acadErrorStatusText(es));
        }
    
        if (description != nullptr)
        {
            es = pLayerTableRecord->setDescription(description);
            if (es != Acad::eOk)
            {
                acutPrintf(_T("\nERROR: Could not add layer description! Error code: %ls"), acadErrorStatusText(es));
                acutPrintf(_T("\nLayer description: %ls"), description);
            }
        }
    
        AcCmColor colorObject;
        if (colorObject.setColorIndex(color) == Acad::eOk)
            pLayerTableRecord->setColor(colorObject);
    
        pLayerTableRecord->setIsOff(isOff);
        pLayerTableRecord->close();
    
        acutPrintf(_T("\nLayer %ls %ls"), name, _T(" have been created."));
        return true;
    }
    
    many greetings !
  • 😳
    Thanks. This is new for me. So, I need to first add it to database.
    I use BricsCAD Platinum 20.
    I had BricsCAD 20, upgraded it to Platinum and after 1-2 months found out Bricsys merged Platinum into BricsCAD. It would been better if I extended my license :neutral: