.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

VB.NET Upgrade ODtable error

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
Sgear
795 Views, 4 Replies

VB.NET Upgrade ODtable error

 

Hi

 

I am try to Upgrade ODtable I get this error 

 

Autodesk.Gis.Map.MapException: Exception of type 'Autodesk.Gis.Map.MapException' was thrown.

 

 

 

 Public Sub odTablesinsert(ByVal id, ByVal Category, ByVal Materials)

        Dim doc As Autodesk.AutoCAD.ApplicationServices.Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
        Dim db As Database = doc.Database
        Dim Ed As Editor = doc.Editor

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

        Dim odTable As Autodesk.Gis.Map.ObjectData.Table
        Dim odTables As Autodesk.Gis.Map.ObjectData.Tables
        Dim mapVal As Autodesk.Gis.Map.Utilities.MapValue

        odTables = HostMapApplicationServices.Application.ActiveProject.ODTables
        odTable = odTables.Item("TABLE_NAME")
        Try
            Using docloc As DocumentLock = doc.LockDocument

                Dim dbObj As DBObject = trans.GetObject(id, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite)
                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)


                If odrecords.Count = 0 Then
                    Ed.WriteMessage("New" & vbCr)
                    mapVal = odRecord(0)
                    mapVal.Assign(Category)
                    mapVal = odRecord(1)
                    mapVal.Assign(Materials)
                    odTable.AddRecord(odRecord, dbObj)

                Else
                    Ed.WriteMessage("Update" & vbCr)
                    mapVal = odRecord(0)
                    mapVal.Assign(Category)
                    mapVal = odRecord(1)
                    mapVal.Assign(Materials)
                    odrecords.UpdateRecord(odRecord)  <<< ERROR
                End If

                odRecord.Dispose()
                odrecords.Dispose()
                trans.Commit()
            End Using

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

    End Sub
4 REPLIES 4
Message 2 of 5
norman.yuan
in reply to: Sgear

Is it the case that the entity the OD record to be attached to could have a record from the table already prior to your code being exists? From your code, it looks like this is what you intend to do: if the entity does not have a record from the OD table, you attach a new record to it, otherwise, update the existing one. If this is what you are to do, your code has logical error: you do not just go ahead to create a new OD record before testing if a record from the OD table already exists.

 

So, the code should be like this:

 

Dim odrecords As ObjectData.Records = odTable.GetObjectTableRecords( _
Convert.ToUInt32(0), id, Constants.OpenMode.OpenForRead, False)
Dim odRecord As ObjectData.Record=Nothing
If odrecords.Count>0 Then
For Each rd As Record in odrecords
If rd.TableName.ToUpper()=odTable.Name.ToUpper() Then
odRecord=rd
Exit For
End If
Next

End If

If odRecord Is Nothing Then
odRecord=ObjectData.Record.Create()
odTable.InitRecord(odRecord)
''Asign values to the record here
odTable.AddRecord(...)
Else
''Assign value to existing record
odRecords.UpdateRecord(odRecord)
End If


 

In your code, the "Else" branch, you simply tried to update the odRecords with a newly created record just because of odRecords.Count>0 (which only means the entity has at least one ODrecord attached, but from which table?). Your code will never work in the "Else" brach, because, if the entity has already had a record from the same table, it will be wrong to try to update it with a new record; if the entity does not have a record from the table, it will also be wrong to try to update a record by calling "UpdateRecord()" method, you should call AddRecord() instead.

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 5
Sgear
in reply to: norman.yuan

 

Thanks norman.yuan

 

this is just sample vb.net 2013 I still getting this error or I am not follow your instructions 🙂

 

 

 

Message 4 of 5
norman.yuan
in reply to: Sgear

OK, there is one thing: since the code opens a Records object in order to test if adding or updating Record is needed, the Records object must be disposed before TableAddRecord() method is called.

 

It is not uncommon in AutoCAD .NET API implementation the requirements of disposing object are quite inconsistent, especially in AutoCAD Map (and maybe in most verticals that have its own .NET API built on top of AutoCAD .NET API, in which transactions are wrapped inside and we as outsider programmer have no idea something need to be disposed or not. In AutoCAD Map, I tend to wrap most Map objects in Using... block, even my code do not create them, which I rarely do in vanilla AutoCAD .NET API.

 

I have attached the project here, in which I updated the code slightly and added some comments. It works OK.

 

Since you use AutoCAD 2015 based product (Map or Civl3D?) If it is Civil3D, there is a nasty bug for adding OD Record, if you need to add OD records to multiple entities in one call. This bug does not exists in Map 2015, nor in Civil 2017 (I do not have Civil2016).

Norman Yuan

Drive CAD With Code

EESignature

Message 5 of 5
Sgear
in reply to: norman.yuan

 

work well can change with no problem, I use Autocad Map

 

Thank you norman.yuan for your Help with this !

 

Sgear

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

Post to forums  

Technology Administrators


Autodesk Design & Make Report