Adding Object Data Records to Entity using Map 3D API

By Partha Sarkar

Many a times you want to add Object Data to entities in DWG file using Map 3D API. Using Map 3D C++ API or .NET API we can add object data to entities. Here is a VB.NET code snippet which shows how to add Object Data to a selected entity in AutoCAD Map 3D. One thing to note here – when you use AddRecord(record As Autodesk.Gis.Map.ObjectData.Record, dbObj As Autodesk.AutoCAD.DatabaseServices.DBObject) make sure that DBObject is open for write, otherwise you are likely to see an exception message.

Try
            Dim ed As Editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor
            Dim prompt_result As PromptEntityResult = ed.GetEntity(vbCrLf + "Select the object...")
            id = prompt_result.ObjectId
            Dim dbObj As DBObject = trans.GetObject(id, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite)
 
            '' Presuming OD Table "MyTestODTable" exist in the DWG file  
            odTable = odTables.Item("MyTestODTable")
            Dim odrecords As ObjectData.Records = odTable.GetObjectTableRecords(Convert.ToUInt32(0), id, Constants.OpenMode.OpenForRead, False)
            Dim odRecord As ObjectData.Record = odrecords.Item(0)
            odRecord = Autodesk.Gis.Map.ObjectData.Record.Create()
            odTable.InitRecord(odRecord)
            Dim mapVal As Autodesk.Gis.Map.Utilities.MapValue
            '' Add a Record and assign a value to it
            mapVal = odRecord(0)
            mapVal.Assign("Test")
 
            mapVal = odRecord(1)
            mapVal.Assign(22)
 
            ' dbObj should be opened for Write, otherwise 
            odTable.AddRecord(odRecord, dbObj)            
            odRecord.Dispose()
            odrecords.Dispose()
            trans.Commit()
 
        Catch exc As Autodesk.Gis.Map.MapException           
            MsgBox("Error : " + exc.Message.ToString())   
        End Try

And here is the result :

AddOD


Comments

4 responses to “Adding Object Data Records to Entity using Map 3D API”

  1. Where is the step where you get the odTables object? I don’t see it Dimmed or set anywhere in your sample.

  2. Here you go :
    Dim db As Database = Application.DocumentManager.MdiActiveDocument.Database
    Dim mapApp As MapApplication = Autodesk.Gis.Map.HostMapApplicationServices.Application
    Dim projModel As ProjectModel = mapApp.ActiveProject
    Dim odTable As ObjectData.Table
    Dim odTables As ObjectData.Tables = projModel.ODTables
    Dim id As ObjectId
    Thanks for asking this,
    Cheers,
    Partha

  3. Anton Huizinga Avatar
    Anton Huizinga

    Is it possible to expand the FieldDefinitions in an existing Table? If I use the function myTable.FieldDefinitions.AddColumn(FieldDefinition.Create(…)) and debug it, the code does not generate an exception, it seems to run well, but the field does not exist in the table.

  4. Hi Anton,
    Did you try calling Tables:: UpdateTable -> Updates the object data table in the project and all its active attached drawings.
    Does it help ?
    Thanks,
    Partha

Leave a Reply to Anton HuizingaCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading