This minimun data is represented basically by objects like layers, linetypes, text styles, etc. Due that you have layer 0, stadard text style, continuous linetype and others.
How data is stored
This Database mantains all sort of objects a drawing needs to exist. These objects are stored into appropriate containers which are special objects made to manage objects of the same type. This way we have appropriate methods and procedures to store entities, layers, text styles, etc.
Each object stored into Database receives an identifier that is called ObjectId. This identifier is unique inside the same AutoCAD session and it is valid during the lifecycle of each object. The ObjectId is generated by its Database and you don't need to worry about how to create it.
Inside ObjectARX we have basically 3 kind of objects:
- Entities: Objects with graphical representation (lines, arcs, texts, ...);
- Containers: Special objects to store and manage collections (layer table, linetype table, ...);
- Objects: Objects without any graphical representation (groups, layouts, ...).
AutoCAD's Database structure
To create an object through ObjectARX we have some kind of recipe depending on what type of object it is and where we would like to store it (most of time we need to store an objects inside its specific container). Basically, you will follow this sequence:
- Declare a pointer to the object type you would like to create and call its new operator;
- With this pointer, call appropriate methods of this object to change its features;
- Get a pointer to the Database where you would like to create the object (most of time the current Database);
- Open the appropriate container where it should be stored;
- Call the specific container method to store your object passing its pointer;
- Receive its ObjectId automatically generated by its container;
- Finish the operation closing all opened objects including containers and the object you have just created.
Obviously you will create some handy classes to allow the automation of this processes because they are very similar and can be easily reused. The main idea is to create a sort of database utility funcions like: AddLayer, AddLine, AddCircle, AddTextStyle, etc.
* It is very important to not forget to close opened objects because this will cause AutoCAD to terminate.
Sample code to create a line (AcDbLine)
This code demonstrates how to create a simple line between two points. The process is simple and no error check is made. This code needs to be embedded inside an ObjectARX application structure to work. The main idea is to show you the concepts. Further we will create a working code. Pay attention to the order of opening and closing operations.
// We first need to declare a couple of points
AcGePoint3d startPt(1.0, 1.0, 0.0);
AcGePoint3d endPt(10.0, 10.0, 0.0);
// Now we need to instantiate an AcDbLine pointer
// In this case, its constructor allows me to pass the 2 points
AcDbLine *pLine = new AcDbLine(startPt, endPt);
// Now we need to open the appropriate container which is inside BlockTable
AcDbBlockTable *pBlockTable = NULL;
// First, get the current database and then get the BlockTable
AcDbDatabase* pDB = acdbHostApplicationServices()->workingDatabase();
// Inside BlockTable, open the ModelSpace
AcDbBlockTableRecord* pBlockTableRecord = NULL;
pBlockTable->getAt(ACDB_MODEL_SPACE, pBlockTableRecord, AcDb::kForWrite);
// After get ModelSpace we can close the BlockTable
// Using ModelSpace pointer we can add our brand new line
AcDbObjectId lineId = AcDbObjectId::kNull;
// To finish the process we need to close ModelSpace and the entity
On the next class we will present the ObjectARX application structure and will build and compile a simple application using the above code. See you there!