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

How to copy lines and arcs and transform them into a closed polyline

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
mecoman6GF27
356 Views, 4 Replies

How to copy lines and arcs and transform them into a closed polyline

Hi everyone.
As per the title of the post I have to make a copy of lines and arcs and transform them into a closed polyline. I have no problem copying lines and arcs but I don't know how to join them into a closed polyline.

This is what I wrote.

        Dim doc As Document = Application.DocumentManager.MdiActiveDocument
        Dim db As Database = doc.Database
        Dim ed As Editor = doc.Editor

        Dim Filter_List1 As TypedValue() = New TypedValue(1) {}
        Filter_List1(0) = New TypedValue(0, "LINE")
        Filter_List1(1) = New TypedValue(8, "Line_Layer")

        Dim Sel_Filter1 As SelectionFilter = New SelectionFilter(Filter_List1)
        Dim Ris_Sel_Filter1 As PromptSelectionResult = ed.SelectAll(Sel_Filter1)

        If Ris_Sel_Filter1.Status <> PromptStatus.OK Then
            MsgBox("Error!!!")
            Return
        End If

        Dim Object_ID As ObjectId() = Ris_Sel_Filtri1.Value.GetObjectIds()

        Dim Obj_Entity_Entity As Entity
        Dim Obj_Entity_Line As Line
        Dim Obj_Entity_Line_Copy As Line

        Using tr1 As Transaction = db.TransactionManager.StartTransaction
            Dim Block_Tab1 As BlockTable = tr1.GetObject(db.BlockTableId, OpenMode.ForRead)
            Dim Block_Tab_Rec1 As BlockTableRecord = tr1.GetObject(Block_Tab1(BlockTableRecord.ModelSpace), OpenMode.ForWrite)

            For i As Integer = 0 To Object_ID.Length - 1
                Obj_Entity_Entity = Object_ID(i).GetObject(OpenMode.ForRead)
                Obj_Entity_Linee = TryCast(Obj_Entity_Entity, Line)

                Obj_Entity_Linee_Copy = Obj_Entity_Linee.Clone()
                Obj_Entity_Linee_Copy.Layer = "Line_Layer_Copy"
                Obj_Entity_Linee_Copy.SetDatabaseDefaults()
                Block_Tab_Rec1.AppendEntity(Obj_Entity_Linee_Copy)

                tr1.AddNewlyCreatedDBObject(Obj_Entity_Linee_Copy, True)
            Next

            tr1.Commit()
        End Using

Can anyone give me some advice on how to do this?
Thanks a lot, bye.

4 REPLIES 4
Message 2 of 5
_gile
in reply to: mecoman6GF27

Hi,

You could use the Entity.JoinEntities method but to get it work, you have to convert the line or arc the method applies to into a Polyline.

Another way should be using the PolylineSegmentCollection.Join method from the PolylineSegmentCollection class after having converted the line and Arcs into PolylineSegment instances. These classes are part of the GeometryExtensions library (this library is written in C# but it can be referenced as is in a VB project).



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 5
mecoman6GF27
in reply to: mecoman6GF27

Hi, thanks for the reply.

I tried using the Entity.JoinEntities method but it partially works.

I tried this:

        Dim doc As Document = Application.DocumentManager.MdiActiveDocument
        Dim db As Database = doc.Database

        Using tr As Transaction = db.TransactionManager.StartTransaction()
            Dim bt As BlockTable = tr.GetObject(db.BlockTableId, OpenMode.ForRead)
            Dim btr As BlockTableRecord = tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)

            Dim Poly As Polyline = New Polyline
            btr.AppendEntity(Poly)
            tr.AddNewlyCreatedDBObject(Poly, True)

            Dim line1 As Line = New Line(New Point3d(0, 0, 0), New Point3d(100, 0, 0))
            btr.AppendEntity(line1)
            tr.AddNewlyCreatedDBObject(line1, True)

            Dim line2 As Line = New Line(New Point3d(0, 0, 0), New Point3d(0, 100, 0))
            btr.AppendEntity(line2)
            tr.AddNewlyCreatedDBObject(line2, True)

            Dim arc1 As Arc = New Arc(New Point3d(0, 0, 0), 100, 0, Math.PI / 2)
            btr.AppendEntity(arc1)
            tr.AddNewlyCreatedDBObject(arc1, True)

            Poly.JoinEntities(New Entity() {line1, line2, arc1})

            tr.Commit()
        End Using

This code creates a polyline composed of line2 and arc1 but without line1.

If I change the line of code "Poly.JoinEntities(New Entity() {line1, line2, arc1})" with "Poly.JoinEntities(New Entity() {line1, arc1, line2})" the polyline is composed of line1 and arc1 but without line2.

Where do you think I'm wrong?

Thanks a lot, bye.

Message 4 of 5
_gile
in reply to: mecoman6GF27

As I said upper, you have to convert one of the lines or arcs into a polyline and call the JoinEntities on this polyline.

        [CommandMethod("TEST")]
        public static void Test()
        {
            var db = HostApplicationServices.WorkingDatabase;
            using (var tr = db.TransactionManager.StartTransaction())
            using (var line = new Line(Point3d.Origin, new Point3d(100.0, 0.0, 0.0)))
            using (var arc = new Arc(Point3d.Origin, 100.0, 0.0, Math.PI * 0.5))
            using (var pline = new Polyline())
            {
                pline.AddVertexAt(0, Point2d.Origin, 0.0, 0.0, 0.0);
                pline.AddVertexAt(1, new Point2d(0.0, 100.0), 0.0, 0.0, 0.0);
                pline.JoinEntities(new Entity[2] { line, arc });
                var modelSpace = (BlockTableRecord)tr.GetObject(
                    SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite);
                modelSpace.AppendEntity(pline);
                tr.AddNewlyCreatedDBObject(pline, true);
                tr.Commit();
            }
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 5 of 5
mecoman6GF27
in reply to: mecoman6GF27

Hi Gil, thank you very much.

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

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report