Creating object data tables: step by step

AutoCAD Map 3D ActiveX

Creating object data tables

The following example creates an object data table and attaches records to drawing objects.

1 Declare variables.

Dim amap As AcadMap

Dim acadObj As Object

Dim ODfdfs As ODFieldDefs

Dim ODfdf As ODFieldDef

Dim ODtb As ODTable

Dim ODrc As ODRecord

2 Set application and project objects.

Set amap = ThisDrawing.Application. _

GetInterfaceObject("AutoCADMap.Application") 

3 Create the schema for the object data table.

Set ODfdfs = amap.Projects(ThisDrawing).MapUtil.NewODFieldDefs

4 Create fields for object data. For example, create fields for the name, color, and layer of a drawing object. Specify default values, for example, empty string, AcRed,.and layer 0, respectively. Add the fields to the table using consecutive index numbers.

Set ODfdf = ODfdfs.Add("Entity", "Entity name", "", 0)

Set ODfdf = ODfdfs.Add("Color", "Object color", acRed, 1)

Set ODfdf = ODfdfs.Add("Layer", "Object layer", "0", 2)

5 Name the table and test the uniqueness of the name.

If amap.Projects(ThisDrawing).ODTables. _

Item("SampleOD3") Is Nothing Then 

6 Register the OD Table in the drawing and specify the type of object data as Xdata.

Set ODtb = amap.Projects(ThisDrawing) _

.ODTables.Add("SampleOD3", "Sample Xdata", ODfdfs, True) 

7 Create a record of data with defaults specified in step 4.

Set ODrc = ODtb.CreateRecord

8 Loop though each drawing object, get its values for the name, color, and layer of the object, and attach the data to the drawing object.

For Each acadObj In ThisDrawing.ModelSpace

ODrc.Item(0).Value = acadObj.EntityName 

ODrc.Item(1).Value = acadObj.Color 

ODrc.Item(2).Value = acadObj.Layer 

ODrc.AttachTo (acadObj.ObjectID) 

Next

Testing the example

Run the example, and enter ADEEDITDATA at the command prompt to see the output. To run the code again, change the name of the table from SampleOD to another name.