AutoCAD Map 3D Developer
Welcome to Autodesk’s AutoCAD Map 3D Developer Forums. Share your knowledge, ask questions, and explore popular AutoCAD Map 3D Developer topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Updating an entity's object data table from form

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
tdunn3
3884 Views, 5 Replies

Updating an entity's object data table from form

I have a form that all the object data gets imported into when I select a parcel. I want to be able to edit the data and overwrite the object data in my drawing.

 

Using the code below when I hit the button and select the parcel I want to overwrite the data to, it attaches a duplicate table with the same name to the entity and all the data is empty except for the owners name.  What do I need to change to make it just overwrite the existing table without creating a duplicate one. 

 

Private Sub updateOD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles updateOD.Click

Dim id As ObjectId '= myid

Dim doc As Autodesk.AutoCAD.ApplicationServices.Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument

Dim db As Database = doc.Database
Dim trans As Transaction = db.TransactionManager.StartTransaction()
Dim odTable As Autodesk.Gis.Map.ObjectData.Table
Dim odTables As Autodesk.Gis.Map.ObjectData.Tables
odTables = HostMapApplicationServices.Application.ActiveProject.ODTables

Try
Dim ed As Autodesk.AutoCAD.EditorInput.Editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor

Using docloc As DocumentLock = doc.LockDocument
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("All_Parcels")
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(3)
mapVal.Assign(OwnerName.Text)

' dbObj should be opened for Write, otherwise

odTable.AddRecord(odRecord, dbObj)
odRecord.Dispose()
odrecords.Dispose()
trans.Commit()
End Using

Catch exc As Autodesk.Gis.Map.MapException
MsgBox("Error : " + exc.Message.ToString())

 

End Try

End Sub
End Class

5 REPLIES 5
Message 2 of 6
norman.yuan
in reply to: tdunn3

Your code actually does is no matter whether there is already a record of the ODTable attached the entity or not, it always create a new record and attach it to the entity, thus duplicated. In fact, if you run your code multiple time, there would be more records of the same ODTable to be attached to the entity.

 

Yo need to do is, first, check if the entity already has the record attached. If yes, update the record, if not, create a new record, attach it to the entity.

 

Something like (not tested):

 

using (Records records=theTable.GetObjectTableRecords(0, objId,OpenMode.OpenForWrite,true))

{

    Record rd=null;

    bool attach=false;

    If (records.Count==0)

    {

        rd=Record.Create();

        theTable.InitRecord(rd);

        attach=true;

    }

    else

    {

        rd=records[0]

    }

 

    //Set record's field value here

    for (int i=0; i<rd.Count; i++)

    {

         MapValue val=rd[i];

 

        //Assign value to each field as needed

        val.Assign(.....);

    }

 

    if (attach) theTable.AddRecord(rd, objId);

}

 

By the way, you do not need to start a transaction and open an DBObject/Entity. ObjectId obtained from Editor.getEntity() is enough. The ObjectData API handles Transaction internally as needed.

 

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 6
tdunn3
in reply to: norman.yuan

Thanks for your help! Made some changes but it still duplicates the table instead of updating the existing one. I really do not need to check if the record exists because I am populating the form text fields with OD from the entity I want to update.

If I take the transaction out and replace odTable.AddRecord(odRecord, dbObj) with odTable.AddRecord(odRecord, id) it throws an error. I just starting to learn vb.net and doing a lot of cutting and pasting from snippets of other code I find on the net and learning as I go. 

 

 

 

Private Sub updateOD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles updateOD.Click

Dim id As ObjectId

Dim doc As Autodesk.AutoCAD.ApplicationServices.Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument

Dim db As Database = doc.Database
Dim trans As Transaction = db.TransactionManager.StartTransaction()


Dim odTable As Autodesk.Gis.Map.ObjectData.Table
Dim odTables As Autodesk.Gis.Map.ObjectData.Tables
odTables = HostMapApplicationServices.Application.ActiveProject.ODTables


Dim ed As Autodesk.AutoCAD.EditorInput.Editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor

Using docloc As DocumentLock = doc.LockDocument
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)
' Dim tableName As String

odTable = odTables.Item("All_Parcels")
Dim odrecords As ObjectData.Records = odTable.GetObjectTableRecords(Convert.ToUInt32(0), id, Constants.OpenMode.OpenForWrite, True)
Dim odRecord As ObjectData.Record = odrecords(0)
Dim mapVal As Autodesk.Gis.Map.Utilities.MapValue

mapVal = odRecord(2)
mapVal.Assign(CntyN.Text)

mapVal = odRecord(3)
mapVal.Assign(OwnerName.Text)

mapVal = odRecord(6)
mapVal.Assign(Sec.Text)

mapVal = odRecord(7)
mapVal.Assign(Town.Text)

mapVal = odRecord(8)
mapVal.Assign(Range.Text)

mapVal = odRecord(11)
mapVal.Assign(Book.Text)

mapVal = odRecord(12)
mapVal.Assign(Page.Text)

mapVal = odRecord(13)
mapVal.Assign(Desc.Text)

odTable.AddRecord(odRecord, dbObj)

End Using

 

End Sub

Message 4 of 6
tdunn3
in reply to: tdunn3

figured it out..replaced 

 

odTable.AddRecord(odRecord, dbObj)

 

with 

 

odrecords.UpdateRecord(odRecord)

 

and now I can take out the transaction without an error

Message 5 of 6
Daniel.Du
in reply to: tdunn3

Would you please mark the thread as solution by clicking  "Accept as Solution" button? This helps everyone find answers more quickly!

 

 



Daniel Du
Developer Technical Services
Autodesk Developer Network

Message 6 of 6
tdunn3
in reply to: Daniel.Du

Here is what I used to finally get it to work

 

Using docloc As DocumentLock = doc.LockDocument
odTable = odTables.Item("All_Parcels")
Dim odrecords As ObjectData.Records = odTable.GetObjectTableRecords(Convert.ToUInt32(0), idEntity, Constants.OpenMode.OpenForWrite, True)
Dim odRecord As ObjectData.Record = odrecords(0)
Dim mapVal As Autodesk.Gis.Map.Utilities.MapValue

mapVal = odRecord(1)
mapVal.Assign(TractID.Text)

mapVal = odRecord(2)
mapVal.Assign(CntyN.Text)

mapVal = odRecord(3)
mapVal.Assign(StrConv(OwnerName.Text, VbStrConv.Uppercase))

mapVal = odRecord(6)
mapVal.Assign(Sec.Text)

mapVal = odRecord(7)
mapVal.Assign(Town.Text)

mapVal = odRecord(8)
mapVal.Assign(Range.Text)

mapVal = odRecord(11)
mapVal.Assign(Book.Text)

mapVal = odRecord(12)
mapVal.Assign(Page.Text)

mapVal = odRecord(13)
mapVal.Assign(Desc.Text)

odrecords.UpdateRecord(odRecord)


End Using

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost