Many of you might be writing applications, not only for Civil 3D, but also for plain vanilla AutoCAD. Maybe your users, once in a while, use the “AutoCAD Civil 3D as AutoCAD” configuration to run the application. This was the case of one of our users, who reported a problem when his/her extension was running Civil 3D as plain AutoCAD.
Without going into too much technical detail, let’s explain that when you are running Civil 3D as AutoCAD and you create a new drawing or open a plain AutoCAD drawing, the Civil 3D specific nodes and objects are not initialized or created. This can cause some problems if you are using the Civil 3D API in your application; in particular, with the ‘CivilDocument’ object.
Our user was asking for a way to detect if the drawing contained Civil specific objects and disable some features in case it did not. I talked to Chris Putnam, who is the Civil 3D Architect, and he told me that it is possible to know if the drawing has been initialized for Civil 3D using just AutoCAD API’s. The way to do it is to check the “named object dictionary” in the database and look to see if contains an object named “Root”. Civil 3D creates that object when initializes a drawing, and at a very low-level, it is the entry point to all Civil objects (not that you want to use that stuff directly). Here is a snippet of code that does exactly that:
C#
public bool isCivilDatabase(Database db)
{DBDictionary namedObjectDict = db.NamedObjectsDictionaryId.GetObject(OpenMode.ForRead) as DBDictionary;
return namedObjectDict.Contains("Root");}
VB.NET
Public Function isCivilDatabase(db As Database) As BooleanDim namedObjectDict As DBDictionary = TryCast( _db.NamedObjectsDictionaryId.GetObject(OpenMode.ForRead), _
DBDictionary)
Return namedObjectDict.Contains("Root")End Function
If the “named object dictionary” contains an object named “Root”, we know the drawing has been initialized by Civil 3D, and we can use Civil 3D specific functionality. Otherwise, we can disable the parts of our application that deal with Civil objects. Needless to say, that to open the “named object dictionary”, you want to be inside the context of a transaction. And that’s it; quick and simple.
Good use of 3d.
Posted by: Victor | 08/08/2011 at 04:21 AM