Hello there, for a few weeks now i'm getting into objectarx .net and rather succesfully. Though I have one problem where I cannot find a solution for. I would like to convert a Polyline2d (as a simplepoly of course) to a lightweightPolyline.
Below is the testcode. Altough it is a very simple piece of code, I really cannot see what the problem is. I already checked out the objectarx documentation and the entire internet to get an understanding about the correct way the "convertfrom" method should be used, but not much documention or sample code about this topic found. Most of the time I get the errors: "System.AccessViolationException" and "eInvalidContext". Is there anybody able to point out the problem?
Thanks in advance.
Try
Dim acDrawing As Document = Application.DocumentManager.MdiActiveDocument
Using acDrawingLock As DocumentLock = acDrawing.LockDocument
Using acTransaction As Transaction = acDrawing.Database.TransactionManager.StartTransaction
Dim acBlockTable As BlockTable = acTransaction.GetObject(acDrawing.Database.BlockTableId, OpenMode.ForWrite)
Dim acBlockTableRecord As BlockTableRecord = acTransaction.GetObject(acBlockTable("*MODEL_SPACE"), OpenMode.ForWrite)
For Each Id As ObjectId In acBlockTableRecord
If Id.ObjectClass.Name = "AcDb2dPolyline" Then
Dim acPolyline2d As Polyline2d = Id.GetObject(OpenMode.ForWrite, True)
Dim acPolyLine As New Polyline()
acPolyLine.ConvertFrom(acPolyline2d, True)
End If
Next
acTransaction.Commit()
End Using
End Using
Catch ex As System.Exception
MsgBox(ex.ToString)
End Try
Solved! Go to Solution.
Solved by Jeff_M. Go to Solution.
I've seen this asked a few times on here and never seen an answer that worked. Today I needed this so started searching again, I finally found the answer on Kean's blog from July 14, 2010. In order to avoid this particular error, one must use the StartOpenCloseTransaction instead of the normal StartTransaction.
Once I made that 1 little change, it works as expected.
HTH
Hi,
You don't add the new Polyline to the Modelspace and the transaction (you don't erase the Polyline 2d too)
private void ConvertToLwPolyline(ObjectId id, Database db)
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
Polyline2d pl2d = tr.GetObject(id, OpenMode.ForWrite) as Polyline2d;
if (pl2d != null &&
(pl2d.PolyType == Poly2dType.FitCurvePoly ||
pl2d.PolyType == Poly2dType.SimplePoly))
{
BlockTableRecord btr =
(BlockTableRecord)tr.GetObject(pl2d.OwnerId, OpenMode.ForWrite);
Polyline pl = new Polyline();
pl.ConvertFrom(pl2d, false);
pl2d.Erase();
btr.AppendEntity(pl);
tr.AddNewlyCreatedDBObject(pl, true);
tr.Commit();
}
}
}
With the True argument of the ConvertFrom method you do not need to do any of the adding/deleting, gile.
From the help:
If transferId is true, the calling Polyline will become database-resident. The calling Polyline will assume the ObjectId and handle, plus any extended entity data (except data on vertices), the extension dictionary, and reactors of the Polyline2d entity entity. entity will then be deleted and set to NULL.
Hi guys, I used the below code and it works fine for me. Convertfrom is called with transferId set to false, which I didn't like to do, because I wanted to keep the objectId of the source entity. Until I discovered the swapidwith function, which swaps objectid's of both entities. After this I only have to erase the source entity. This gives me a similar result as if I would have set transferId to true, because, if I'm right, transferId hides a handoverto operation (which implies that objectId and handle are handed over to the new entity, source entity is erased, and new entity is added to database automatically).
I didn't have the opportunity to test the solution from Kean's blog yet, but I assume it works. I will try it as soon as possible. Thanks to everyone!!!
Using acTransaction As Transaction = acDrawing.Database.TransactionManager.StartTransaction
Dim acBlockTable As BlockTable = acTransaction.GetObject(acDrawing.Database.BlockTableId, OpenMode.ForWrite)
Dim acBlockTableRecord As BlockTableRecord = acTransaction.GetObject(acBlockTable("*MODEL_SPACE"), OpenMode.ForWrite)
For Each Id As ObjectId In acBlockTableRecord
If Id.ObjectClass.Name = "AcDb2dPolyline" Then
Dim acPolyline2d As Polyline2d = Id.GetObject(OpenMode.ForWrite, True)
Dim acPolyLine As New Polyline()
acPolyline2d.Straighten()
acPolyLine.ConvertFrom(acPolyline2d, False)
acBlockTableRecord.AppendEntity(acPolyLine)
acTransaction.AddNewlyCreatedDBObject(acPolyLine, True)
acPolyLine.SwapIdWith(acPolyline2d.ObjectId, True, True)
acPolyline2d.Erase(True)
End If
Next
acTransaction.Commit()
End Using
See my first reply in this thread for how to cure that error, gile. PeGo001, read my relpy, too. If you take the code posted in your original post and change
Using acTransaction As Transaction = acDrawing.Database.TransactionManager.StartTransaction
to
Using acTransaction As Transaction = acDrawing.Database.TransactionManager.StartOpenCloseTransaction
it will work fine. Also, I think your assumption about the new polyline having the handle/data transferred from the original is wrong. That only occurs when the argument in the method is true.
Jeff_M, you're absolutely right. It works beautifully, so makes my code totally irrelevant. Gives more compact code also. But concerning the swapidwith function, it really seemed to work as far as I tested it, but as you said, I could be wrong. Thanks anyway.
Can't find what you're looking for? Ask the community or share your knowledge.