How to reference Object Data for a selected object with VBA

Posted on 2005-04-15
Last Modified: 2009-07-29
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?
Question by:BillPowell
    LVL 15

    Expert Comment

    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. _


    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)


    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.

    LVL 11

    Author Comment

    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
    LVL 15

    Expert Comment

    I gave full and correct answer, don't see reasons why it should be refunded.
    LVL 11

    Author Comment

    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.
    LVL 15

    Expert Comment

    OK, PAQ and refund

    Accepted Solution

    Question answered by asker or dialog deemed valuable.
    Closed, 500 points refunded.
    ee_ai_construct (replacement part #xm34)
    Community Support Admin

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Our company has proprietary drawing files that we do not give to our customers for final documentation. Our customers now require that we give them these drawings. They understand that they are proprietary and do not require them to be in AutoCAD bu…
    The following article will describe how to add/edit a dimension style through AutoCAD VBA. AutoCAD VBA has its quirks and when it comes to dimensions and controlling how they look through VBA.  This is where AutoCAD can be vividly confusing. The…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now