This interesting question came in by email from Igor, over the weekend:
Let say I want to delete a layer by it's name. I can get ObjectId or LayerTabelRecord from the name, like
LayerTable tLayers = (LayerTable)
Transaction.GetObject(Database.LayerTableId,OpenMode.ForRead,false)
LayerTableRecord ltRecord = (LayerTableRecord)
Transaction.GetObject(tLayers.Item[Name],OpenMode.ForWrite,false);
Now having LayerTableRecord how can I found out that this DBObject is not the built-in one? Like names '0' or 'DEFPOINTS'.
Same goes for TextStyle (STANDARD) or layout (MODEL)….?
I can't found any property regarding this, like IsBuiltIn. The IsPersistent property is no help.
It’s true that there isn’t an IsBuiltIn property on AutoCAD objects… for block definitions you can check IsLayout, although that doesn’t tell you whether the modelspace or a deletable paperspace, mind.
The first suggestion would be to use the very-handy SymbolUtilityServices namespace. I usually use this to check the validity of a symbol name, or to get the ObjectId of a built-in BlockTableRecord – such as modelspace – from a specified Database, but it can also be used to check whether symbols are “built-in”.
Here are the methods you can use – each takes a string – to see whether you have the name of a built-in object:
SymbolUtilityServices.IsBlockLayoutName()
SymbolUtilityServices.IsBlockModelSpaceName()
SymbolUtilityServices.IsBlockPaperSpaceName()
SymbolUtilityServices.IsLayerDefpointsName()
SymbolUtilityServices.IsLayerZeroName()
SymbolUtilityServices.IsLinetypeByBlockName()
SymbolUtilityServices.IsLinetypeContinuousName()
SymbolUtilityServices.IsRegAppAcadName()
SymbolUtilityServices.IsTextStyleStandardName()
SymbolUtilityServices.IsViewportActiveName()
So it’s not quite a simple as checking a property, but you can, at least, validate whether a name is “built-in” before you choose to do something destructive with the associated object.
One other thought occurred to me: if you wanted to do this more dynamically – i.e. protect a set of objects beyond those baked into the SymbolUtilityServices API – then you could drive it from the standard drawing template (e.g. acad.dwt). You would have some kind of “database” (which could simple be a JSON file) that is created by code that traverses the drawing structure in the file. It might also be hand-crafted, but the effort to automate it should be modest, and it would allow the file to be recreated when additional “standard” objects get added.
This “database” would then be read into memory and used at runtime to validate whether the user (or the application) should be allowed to modify or erase certain objects. It could be done at the name- or the ObjectId-level: we could create a simple list of “protected” ObjectIds the first time we need to check an modify operation on a particular drawing.
It strikes me that this must be a fairly common issue. Has anyone else address this in their application, in some (other) way?