There are two primary methods for adding application data to DWG files. Both of these techniques have analogues within the DGN format. Extended Data (xdata) can be added to any entity in a manner very similar to element linkages. Extended Records (Xrecords) are non-graphic objects that can store arbitrary application data in a manner that is somewhat analogous to MicroStation's (Type 66, Level 20) application elements.
When designing applications that are to store data in DWG files there are essentially two approaches to take. The MicroStation oriented approach is appropriate for most applications and simply involves using the existing MicroStation API to create element linkages and application elements just as you would with a DGN file and leaving the conversion of this data to and from DWG to MicroStation. A second, more AutoCAD oriented approach is appropriate for applications that must deal with existing AutoCAD application data or applications where accessing application data within AutoCAD is required.
If the configuration variable MS_DWGSAVEAPPLICATIONDATA is set then application data created by a MicroStation application using conventional methods is preserved in a manner that is essentially transparent to the applications that create or access the data. This is accomplished by automatically converting element linkages and application elements to their equivalent AutoCAD analogues when a DGN file is saved to DWG or new data is added while editing a DWG file. When the DWG file is reopened, the application data is then extracted and restored to element linkages and application elements that exactly match the original data.
An important limitation to the DWG file format is that the concept of normal (unshared) cells does not exist. Typically this means that when a cell is saved to DWG it is converted to a shared cell instance and a definition. As cells are important building blocks for many MicroStation applications, MicroStation will automatically preserve and restore any cell with an application linkage when they are saved with the MS_DWGSAVEAPPLICATIONDATA variable set. The application will continue to see the normal cell and is not required to deal with shared cell instances.
For many simple applications, simply setting MS_DWGSAVEAPPLICATION data will allow the application to run without change on DWG files. For example, many of the MDL examples such as the "Circuit" application work equally well in DWG or DGN files.
When DGN application data is saved to DWG with MS_DWGSAVEAPPLICATION set, the element linkages are converted to binary extended data and stored under the application name "Mstation". In general, this is completely transparent to the application. The extended data can be viewed in AutoCAD, although since it is binary it not particularly useful. In DXF format, one of these linkages would appear as follows:
In a similar manner, application elements (Type 66, Level 20) are converted to xrecords These records could be located within AutoCAD by first finding the "MicroStation" Extended Dictionary entity and then traversing the "Application Element" records of that dictionary. Again, the information is simply a binary representation of the type 66 contents.
In many instances, it may be necessary to access application data that is either generated by AutoCAD applications or to generate or manipulate data that must be accessible from AutoCAD applications. In this case the MicroStation-Centric approach described above is not suitable as the data is encapsulated within binary blobs that are not readily visible outside of the MicroStation application.
In order to efficiently access DWG application data it is necessary to at least briefly mention a few important DWG concepts. A complete discussion of the AutoCAD application extensions is beyond the scope of this document and the reader is referred to the AutoCAD Developer documentation for more detailed information.
In order for an application to properly add extended data to an entity it must first identify itself by placing an entry in the files registered application table. The application registration process will generate an ElementID that uniquely identifies the application. This Element ID of this table entry is then inserted as the first entry in a series of extended data records and all of the records that follow are implicitly owned by that application. Unlike MicroStation's linkage Ids where an application requests a single ID value that will uniquely identify their linkage in any design file, the registered application ID will be different for each file. The mdlRegApp_idFromName function can be used to either find an ID from a previously registered application or to register a new application and obtain a new ID. The mdlRegApp_nameFromId function is used to extract the name for an existing application ID.