AutoCAD Map 3D Developer

AutoCAD Map 3D Developer

Reply
Contributor
tdunn3
Posts: 18
Registered: ‎09-13-2007
Message 1 of 6 (1,524 Views)
Accepted Solution

Updating an entity's object data table from form

1524 Views, 5 Replies
06-25-2012 02:28 PM

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

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

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

*Expert Elite*
norman.yuan
Posts: 1,045
Registered: ‎04-27-2009
Message 2 of 6 (1,516 Views)

Re: Updating an entity's object data table from form

06-26-2012 07:07 AM 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.

 

Contributor
tdunn3
Posts: 18
Registered: ‎09-13-2007
Message 3 of 6 (1,501 Views)

Re: Updating an entity's object data table from form

06-28-2012 05:52 AM 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

Contributor
tdunn3
Posts: 18
Registered: ‎09-13-2007
Message 4 of 6 (1,494 Views)

Re: Updating an entity's object data table from form

06-28-2012 02:18 PM 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

Board Manager
Daniel.Du
Posts: 225
Registered: ‎12-23-2008
Message 5 of 6 (1,411 Views)

Re: Updating an entity's object data table from form

08-06-2012 08:53 PM 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

Contributor
tdunn3
Posts: 18
Registered: ‎09-13-2007
Message 6 of 6 (1,397 Views)

Re: Updating an entity's object data table from form

08-07-2012 04:51 AM 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

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!