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

Convert polyline2d to lightweightpolyline

9 REPLIES 9
SOLVED
Reply
Message 1 of 10
Anonymous
2592 Views, 9 Replies

Convert polyline2d to lightweightpolyline

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

9 REPLIES 9
Message 2 of 10
Jeff_M
in reply to: Anonymous

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

Jeff_M, also a frequent Swamper
EESignature
Message 3 of 10
Anonymous
in reply to: Jeff_M

I know is for programing but you also set PLINETYPE varible to 2 which automaiclly converts them when a drawing is open

 

 

Message 4 of 10
_gile
in reply to: Anonymous

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();
        }
    }
}

 

 

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 5 of 10
Jeff_M
in reply to: _gile

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.  

Jeff_M, also a frequent Swamper
EESignature
Message 6 of 10
_gile
in reply to: Jeff_M

Thanks Jeff,

but I can't get it work, I always have aa eInvalidContext exception



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 7 of 10
Anonymous
in reply to: Anonymous

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

Message 8 of 10
Jeff_M
in reply to: _gile

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, also a frequent Swamper
EESignature
Message 9 of 10
Anonymous
in reply to: Anonymous

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.

Message 10 of 10
Ta7a
in reply to: Jeff_M

i am still getting eInvalidContext exception
can you help me, please?

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

Post to forums  

Forma Design Contest


Autodesk Design & Make Report