Link to home
Start Free TrialLog in
Avatar of BillPowell
BillPowell

asked on

How to reference Object Data for a selected object with VBA

I have not found any good examples of how to do this anywhere.  I do find plenty of references to Block Attributes, which does not interest me, particularily because Im using Map.  All I need to do really is to be able to populate a variable with object data from a single selected item.  Ive figured already that I need a reference to the Autocad Map library.  The program Im writing will connect to a MS SQL database using ADO, which is the easy part, the hard part is finding good examples of referencing Autocad Map object data.
Any takers?
Avatar of OMC2000
OMC2000
Flag of Russian Federation image

1. AutoCad and AutoCad Map already has built-in database access facilities, their usage would give you higher compatibility with its AutoCad. But in general it's not too important.

2. you can find examples at AcMapAtm.chm "Object Data"

3. Objects to be used:
ODFieldDef object
ODFieldDefs collection
ODFieldValue object
ODRecord collection
ODRecords collection
ODTable object
ODTables collection

4. example
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.

Avatar of BillPowell
BillPowell

ASKER

Actually, I figured it out.  Heres a piece of sample code:

Dim ODrcs As ODRecords
Dim boolVal As Boolean
Dim returnObj As AcadObject
Dim basePnt As Variant
Dim strValue As String
Dim MTextObj As AcadMText


Set amap = ThisDrawing.Application.GetInterfaceObject("AutoCADMap.Application")
Set ODrcs = amap.Projects.Item(ThisDrawing).ODTables.Item("TitledParcel").GetODRecords
'Prompt user to select an object
ThisDrawing.Utility.GetEntity returnObj, basePnt, "Select an object"
boolVal = ODrcs.Init(returnObj, True, False)
strValue = ODrcs.Record.Item(0).Value
I gave full and correct answer, don't see reasons why it should be refunded.
OMC2000
No offense to you is intended.  If my question had been: "How can I create an Object Data Table with specified schema, then loop through and get the object properties and populate the table with values?", then you would have answered the question perfectly and received an A grade.  My question was specifically how to : <<populate a variable with object data from a single selected item>>.  If you can tell me which part of your answer addresses that, then the points are yours.  Believe me, its way more effort to ask a moderator to close a question than it is to just give the points away (I get unlimited points from being an expert the same as you), but I like to maintain the integrity of the PAQ archive and not be accused of points dumping.
OK, PAQ and refund
ASKER CERTIFIED SOLUTION
Avatar of ee_ai_construct
ee_ai_construct
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial