.NET

Reply
Active Contributor
Posts: 33
Registered: ‎02-13-2013
Message 1 of 8 (491 Views)
Accepted Solution

converting 2dpolyline to regular polyline

491 Views, 7 Replies
02-13-2013 12:56 PM

Although I have found code that should allow me to convert 2dpolylines to regular polylines, I am having troubles implementing it.  At first I got the eInvalid Input but have since fixed that.  However, the error now is that there is no error.  Autocad just seems to lock up but I can still switch to visual studio and look at my code.  It as though Autocad thinks it is doing something but my code is not doing anything.  I can scroll around.

 

My code is this:

        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

 Anyone have any ideas?  It appears to hang on the acpolyline.convertofrm(acpolyline2d, true) line.

 

Thanks a bunch,

Moderator
Alexander.Rivilis
Posts: 1,346
Registered: ‎04-09-2008
Message 2 of 8 (473 Views)

Re: converting 2dpolyline to regular polyline

02-13-2013 01:41 PM in reply to: greg

As I know one of the problems - using transactions.

Try samples and explanation:

Converting a Polyline2d to Polyline

Converting a Polyline to Polyline2d

Also Polyline2d have to be simple (SimplePoly) or fit (FitCurvePoly) and has not any Xdata attached to vertexes.


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

*Expert Elite*
Posts: 1,981
Registered: ‎04-29-2006
Message 3 of 8 (465 Views)

Re : converting 2dpolyline to regular polyline

02-13-2013 02:03 PM in reply to: greg

Hi,

 

You have to add the newly created polyline to the Database (and erase the polyline 2d).

 

Here's a quick and dirty C# snippet:

            Document doc = AcAp.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            using (doc.LockDocument())
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTableRecord mSpace =
                    (BlockTableRecord)SymbolUtilityServices
                    .GetBlockModelSpaceId(db)
                    .GetObject(OpenMode.ForWrite);
                RXClass rxc = RXClass.GetClass(typeof(Polyline2d));
                foreach (ObjectId id in mSpace)
                {
                    if (id.ObjectClass == rxc)
                    {
                        Polyline2d pl2d = (Polyline2d)tr.GetObject(id, OpenMode.ForWrite);
                        if (pl2d.PolyType != Poly2dType.CubicSplinePoly &&
                            pl2d.PolyType != Poly2dType.QuadSplinePoly)
                            using (Polyline pline = new Polyline())
                            {
                                pline.ConvertFrom(pl2d, false);
                                mSpace.AppendEntity(pline);
                                tr.AddNewlyCreatedDBObject(pline, true);
                                pl2d.Erase();
                            }
                    }
                }
                tr.Commit();
            }

 

 

 

Gilles Chanteau
Moderator
Alexander.Rivilis
Posts: 1,346
Registered: ‎04-09-2008
Message 4 of 8 (461 Views)

Re : converting 2dpolyline to regular polyline

02-13-2013 02:16 PM in reply to: _gile

Hi, Gilles!

Are you sure that Transaction can be using in that context?


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Active Contributor
Posts: 33
Registered: ‎02-13-2013
Message 5 of 8 (455 Views)

Re: converting 2dpolyline to regular polyline

02-13-2013 02:24 PM in reply to: greg

I tried the code from that link, but I was having troubles getting it to work.  It could have something to do with me translating to VB.net.  Perhaps my translation was messed up.  Do you have a quick translation?

 

 

Active Contributor
Posts: 33
Registered: ‎02-13-2013
Message 6 of 8 (453 Views)

Re : converting 2dpolyline to regular polyline

02-13-2013 02:26 PM in reply to: _gile

Thanks Gile,

 

I had come across that code at one point in my travels, but didnt think it did what I wanted it to do.  I will try that next.

 

Thanks,

Moderator
Alexander.Rivilis
Posts: 1,346
Registered: ‎04-09-2008
Message 7 of 8 (444 Views)

Re : converting 2dpolyline to regular polyline

02-13-2013 02:39 PM in reply to: greg
' Translated C#->VB.NET code:
' http://adndevblog.typepad.com/autocad/2012/05/converting-a-polyline2d-to-polyline.html
<CommandMethod("TestConvertPoly")> _
Public Sub testConvertPoly()
    Dim doc As Document = Application.DocumentManager.MdiActiveDocument
    Dim db As Database = doc.Database
    Dim ed As Editor = doc.Editor
    ' select the entity
    Dim res As PromptEntityResult = ed.GetEntity("Select PolyLine: ")
    ' if ok
    If res.Status = PromptStatus.OK Then

        ' use the using keyword so that the objects auto-dispose

        '(close)  at the end of the brace

        ' open for write otherwise ConvertFrom will fail


        Using ent As Entity = DirectCast(res.ObjectId.Open(OpenMode.ForRead), Entity)

            ' if it's a polyline2d

            If TypeOf ent Is Polyline2d Then


                Dim poly2d As Polyline2d = DirectCast(ent, Polyline2d)

                poly2d.UpgradeOpen()


                ' again use the using keyword to ensure auto-closing


                Using poly As New Autodesk.AutoCAD.DatabaseServices.Polyline()

                    poly.ConvertFrom(poly2d, True)

                End Using

            End If

        End Using
    End If

End Sub

 


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Active Contributor
Posts: 33
Registered: ‎02-13-2013
Message 8 of 8 (432 Views)

Re : converting 2dpolyline to regular polyline

02-13-2013 05:49 PM in reply to: Alexander.Rivilis

Thanks Alexander and everyone who has help so far.

 

The vb.net code actually worked.  I think what happened to me preivously when attempting to use that code is that I saw a message about the Open function being obsolete to use GetObject instead.  So I did, but that didnt work.  When I actually used the Open the way you have it in your example code and ignored that little message, everything worked beautifully.

 

Thanks for your help.  I greatly appreciate it!

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Welcome to the new Autodesk Community!
If this is your first visit, click here to get started and make the most of the Community. Let us know what you think of the new experience in the Community Feedback Forum.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community