Creating object data tables: sample code

AutoCAD Map 3D ActiveX

Creating object data tables

The following example creates a table of object data: the AutoCAD database entity name, the color, and the layer of the object. It builds object data records from the table and attaches them to objects in the drawing.

 

Sub tableproc()

 

Dim amap As AcadMap

Dim ODfdfs As ODFieldDefs

Dim ODfdf As ODFieldDef

Dim ODtb As ODTable

Dim ODrc As ODRecord

 

Set amap = ThisDrawing.Application. _

GetInterfaceObject("AutoCADMap.Application") 

 

'Create OD Table Definition

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

 

' Add Column Headings and Defaults

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)

 

'Ensure Table Does Not Exist

If amap.Projects(ThisDrawing) _

.ODTables.Item("SampleOD") Is Nothing Then 

   

'Register OD Table in the drawing 

Set ODtb = amap.Projects(ThisDrawing) _ 

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

 

'Create OD Record with Defaults 

Set ODrc = ODtb.CreateRecord

 

'Loop Through Entities in Model Space 

For Each acadObj In ThisDrawing.ModelSpace 

 

'Fill Records with Entity Data 

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

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

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

   

'Attach Record to Entity

ODrc.AttachTo(acadObj.ObjectID)

 

Next 

 

Else

 

'Table Already Exists 

MsgBox "Unable to create " & "SampleOD", , _ 

"Object Data Table Error" 

End If

 

End Sub