Insert Row (VB.NET)

Insert Row (VB.NET)

Anonymous
Not applicable
639 Views
2 Replies
Message 1 of 3

Insert Row (VB.NET)

Anonymous
Not applicable
I am pretty new to .net and am having some issues with transactions (I
think)...

I want to add a row to an existing table. AutoCAD crashes when I try to
open the object for write. Am I using the transaction improperly?

Here is the code I have so far:

Dim editor As Editor =
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor
Dim pOptions As PromptEntityOptions = New PromptEntityOptions("Select a
table: ")
Dim pResults As PromptEntityResult = editor.GetEntity(pOptions)
Dim tbl As Table
Dim trans As Transaction
trans =
HostApplicationServices.WorkingDatabase().TransactionManager.StartTransaction()
tbl = trans.GetObject(pResults.ObjectId, OpenMode.ForWrite) <----
****crashes here****
tbl.InsertRows(1, 1.0, 1)
trans.Commit()
trans.Dispose()


Thanks for the help,

--
Joel Roderick
Water Technology, Inc.
www.watertechnologyinc.com
0 Likes
640 Views
2 Replies
Replies (2)
Message 2 of 3

Mikko
Advocate
Advocate
This might help.

<CommandMethod("AddTableRow")> _
Public Shared Sub AddTableRow()
Dim ed As Editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor
Dim db As Database = HostApplicationServices.WorkingDatabase
Dim tm As Transaction = db.TransactionManager.StartTransaction()
Try
Dim res As PromptSelectionResult = ed.GetSelection
If res.Status PromptStatus.OK Then
Exit Sub
End If
Dim SS As Autodesk.AutoCAD.EditorInput.SelectionSet = res.Value
Dim tempIdArray() As ObjectId
tempIdArray = SS.GetObjectIds()
Dim tempId As ObjectId
Dim tbl As Table
For Each tempId In tempIdArray
Dim Ent As Entity = CType(tm.GetObject(tempId, OpenMode.ForWrite), Entity)
If UCase(Ent.GetType.Name.ToString) = "TABLE" Then
tbl = Ent
tbl.InsertRows(1, 1.0, 1)
End If
Next
tm.Commit()
Catch
Finally
tm.Dispose()
End Try
End Sub
0 Likes
Message 3 of 3

Anonymous
Not applicable
Look like I needed to lock the document before opening the object...
So this line solved the problem:

editor.Document.LockDocument()
0 Likes