.NET

Reply
Active Member
wes.newman
Posts: 9
Registered: ‎06-16-2011
Message 1 of 5 (2,033 Views)
Accepted Solution

Convert Arcs to Polylines

2033 Views, 4 Replies
06-25-2011 08:24 AM

I've got ObjectIds of a bunch of arcs that I need to convert to polylines for further processing.

Anyone know of a clean way to do this?

 

I was thinking of using the COM interface and sending the PEDIT command and converting the arcs.

The only problem is I don't know of a way to put the objectids into a selection set, then call the pedit command and use the previous selection.

Anyone know of a way to do this?

 

Thanks,
Wes

Here's something I did when I was learning, hope it helps.

 [CommandMethod("Arc2Poly")]
public void arc2poly()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
PromptSelectionOptions psOpts = new PromptSelectionOptions();
psOpts.MessageForAdding = "\nSelect arcs to convert: ";
psOpts.MessageForRemoval = "\n...Remove arcs: ";
TypedValue[] filter = { new TypedValue(0, "ARC") };
SelectionFilter ssfilter = new SelectionFilter(filter);
PromptSelectionResult psRes = ed.GetSelection(psOpts, ssfilter);
if (psRes.Status != PromptStatus.OK)
return;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)db.BlockTableId.GetObject(OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
foreach (ObjectId id in psRes.Value.GetObjectIds())
{
Arc arc = (Arc)tr.GetObject(id, OpenMode.ForWrite);
Polyline poly = new Polyline();
poly.AddVertexAt(0, new Point2d(arc.StartPoint.X, arc.StartPoint.Y), GetArcBulge(arc), 0, 0);
poly.AddVertexAt(1, new Point2d(arc.EndPoint.X, arc.EndPoint.Y), 0, 0, 0);
poly.LayerId = arc.LayerId;
btr.AppendEntity(poly);
tr.AddNewlyCreatedDBObject(poly, true);
arc.Erase();
}
tr.Commit();
}
}
private double GetArcBulge(Arc arc)
{
double deltaAng = arc.EndAngle - arc.StartAngle;
if (deltaAng < 0)
deltaAng += 2 * Math.PI;
return Math.Tan(deltaAng * 0.25);
}

 

*Expert Elite*
Jeff_M
Posts: 4,519
Registered: ‎07-22-2003
Message 2 of 5 (2,026 Views)

Re: Convert Arcs to Polylines

06-25-2011 09:32 AM in reply to: wes.newman

Here's something I did when I was learning, hope it helps.

        [CommandMethod("Arc2Poly")]
        public void arc2poly()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;
            PromptSelectionOptions psOpts = new PromptSelectionOptions();
            psOpts.MessageForAdding = "\nSelect arcs to convert: ";
            psOpts.MessageForRemoval = "\n...Remove arcs: ";
            TypedValue[] filter = { new TypedValue(0, "ARC") };
            SelectionFilter ssfilter = new SelectionFilter(filter);
            PromptSelectionResult psRes = ed.GetSelection(psOpts, ssfilter);
            if (psRes.Status != PromptStatus.OK)
                return;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = (BlockTable)db.BlockTableId.GetObject(OpenMode.ForRead);
                BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                foreach (ObjectId id in psRes.Value.GetObjectIds())
                {
                    Arc arc = (Arc)tr.GetObject(id, OpenMode.ForWrite);
                    Polyline poly = new Polyline();
                    poly.AddVertexAt(0, new Point2d(arc.StartPoint.X, arc.StartPoint.Y), GetArcBulge(arc), 0, 0);
                    poly.AddVertexAt(1, new Point2d(arc.EndPoint.X, arc.EndPoint.Y), 0, 0, 0);
                    poly.LayerId = arc.LayerId;
                    btr.AppendEntity(poly);
                    tr.AddNewlyCreatedDBObject(poly, true);
                    arc.Erase();
                }
                tr.Commit();
            }
        }

        private double GetArcBulge(Arc arc)
        {
            double deltaAng = arc.EndAngle - arc.StartAngle;
            if (deltaAng < 0)
                deltaAng += 2 * Math.PI;
            return Math.Tan(deltaAng * 0.25);
        }

 

Jeff_M, also a frequent Swamper
Moderator
Alexander.Rivilis
Posts: 1,451
Registered: ‎04-09-2008
Message 3 of 5 (2,014 Views)

Re: Convert Arcs to Polylines

06-25-2011 11:14 AM in reply to: Jeff_M

And:

poly.Normal = arc.Normal;

 


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

Active Member
wes.newman
Posts: 9
Registered: ‎06-16-2011
Message 4 of 5 (2,011 Views)

Re: Convert Arcs to Polylines

06-25-2011 11:18 AM in reply to: Jeff_M

Jeff,

Just what I was after.

 

Thanks!!

Wes

Active Member
wes.newman
Posts: 9
Registered: ‎06-16-2011
Message 5 of 5 (2,009 Views)

Re: Convert Arcs to Polylines

06-25-2011 11:21 AM in reply to: Alexander.Rivilis

Thanks for the update.

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.