Author Topic: Curve.GetProjectedCurve with polylines  (Read 2584 times)

0 Members and 1 Guest are viewing this topic.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Curve.GetProjectedCurve with polylines
« on: October 26, 2009, 10:51:38 AM »
Hi,

It seems that the GetProjectedCurve (and GetOrthoProjectedCurve too) doesn't works with polylines.
If the curve is a Polyline an error occurs : eNotApplicable
Converting the Polyline into a Polyline2d avoid this error, but with both Polyline and Polyline2d the method only works if the object construction plane is parallel to the projection pane and needs to force the elevation (TEST1 example).

The only way I found to get a "correct" result is to Explode the polyline (TEST2 example)

Code: [Select]
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;

namespace CurveProject
{
    public class Test
    {
        [CommandMethod("Test1")]
        public void Test1()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            PromptEntityResult per = ed.GetEntity("\nSelect a curve: ");
            if (per.Status == PromptStatus.OK)
            {
                try
                {
                    ObjectId id = per.ObjectId;
                    Vector3d normal = new Vector3d(0.0, 0.0, 1.0);
                    Plane plane = new Plane(Point3d.Origin, normal);
                    using (Transaction tr = db.TransactionManager.StartTransaction())
                    {
                        Curve curve;
                        Curve ent = tr.GetObject(id, OpenMode.ForRead) as Curve;
                        if (ent != null)
                        {
                            BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                            BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                            Polyline pl = ent as Polyline;
                            Polyline2d pl2d = ent as Polyline2d;
                            if (pl != null)
                            {
                                curve = pl.ConvertTo(false).GetProjectedCurve(plane, normal);
                                Polyline tmp = new Polyline();
                                tmp.ConvertFrom(curve, false);
                                tmp.Elevation = 0.0;
                                curve = tmp as Curve;
                            }
                            else if (pl2d != null)
                            {
                                pl2d = pl2d.GetProjectedCurve(plane, normal) as Polyline2d;
                                pl2d.Elevation = 0.0;
                                curve = pl2d as Curve;
                            }

                            else
                            {
                                curve = ent.GetProjectedCurve(plane, normal);
                            }
                            btr.AppendEntity(curve);
                            tr.AddNewlyCreatedDBObject(curve, true);
                        }
                        tr.Commit();
                    }
                }
                catch (System.Exception e)
                {
                    ed.WriteMessage("\nError: " + e.Message);
                }
            }
        }

        [CommandMethod("TEST2")]
        public void test2()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            PromptEntityResult per = ed.GetEntity("\nSelect a curve: ");
            if (per.Status == PromptStatus.OK)
            {
                try
                {
                    ObjectId id = per.ObjectId;
                    Vector3d normal = new Vector3d(0.0, 0.0, 1.0);
                    Plane plane = new Plane(Point3d.Origin, normal);
                    using (Transaction tr = db.TransactionManager.StartTransaction())
                    {
                        Curve curve;
                        Curve ent = tr.GetObject(id, OpenMode.ForRead) as Curve;
                        if (ent != null)
                        {
                            BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                            BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                            if (ent is Polyline || ent is Polyline2d)
                            {
                                DBObjectCollection dboc = new DBObjectCollection();
                                ent.Explode(dboc);
                                foreach (DBObject item in dboc)
                                {
                                    Curve crv = item as Curve;
                                    if (crv != null)
                                    {
                                        btr.AppendEntity(crv);
                                        tr.AddNewlyCreatedDBObject(crv, true);
                                        curve = crv.GetProjectedCurve(plane, normal);
                                        btr.AppendEntity(curve);
                                        tr.AddNewlyCreatedDBObject(curve, true);
                                        crv.Erase(true);
                                    }
                                }
                            }
                            else
                            {
                                curve = ent.GetProjectedCurve(plane, normal);
                                btr.AppendEntity(curve);
                                tr.AddNewlyCreatedDBObject(curve, true);
                            }
                            tr.Commit();
                        }
                    }
                }
                catch (System.Exception e)
                {
                    ed.WriteMessage("\nError: " + e.Message);
                }
            }
        }
    }
}
Speaking English as a French Frog

SEANT

  • Bull Frog
  • Posts: 345
Re: Curve.GetProjectedCurve with polylines
« Reply #1 on: October 26, 2009, 02:18:43 PM »
Hmm,

That is interesting.  Actually, it’s a bit irritating, though converting a Polyline to a Polyline2d seems easy enough.

It certainly appears like the Curve.GetProjectedCurve is tripping up on the poly’s ECS.  I wonder if Autodesk left it that way due to the potential issues with Arc projecting to an Ellipse 

An explosion can be avoided by explicitly coordinating the various systems as such; though fillets don’t project properly.



Code: [Select]
        public void Test1()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            PromptEntityResult per = ed.GetEntity("\nSelect a curve: ");
            if (per.Status == PromptStatus.OK)
            {
                try
                {
                    ObjectId id = per.ObjectId;
                    Vector3d normal = new Vector3d(0.0, 0.0, 1.0);
                   
                    using (Transaction tr = db.TransactionManager.StartTransaction())
                    {
                        Curve curve;
                        Curve ent = tr.GetObject(id, OpenMode.ForRead) as Curve;
                        if (ent != null)
                        {
                            BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                            BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                            Polyline pl = ent as Polyline;
                            if (pl != null)
                            {
                                Matrix3d m = Matrix3d.PlaneToWorld(pl.Normal);
                                normal = normal.TransformBy(m);
                                Plane plane = new Plane(Point3d.Origin, normal);
                                Polyline2d pl2d = pl.ConvertTo(false) as Polyline2d;
                                curve = pl2d.GetProjectedCurve(plane, pl.Normal) as Curve;
                                pl2d = curve as Polyline2d;
                                m = Matrix3d.WorldToPlane(pl2d.Normal);
                                curve.TransformBy(m);
                                pl2d.Elevation = 0.0;
                                btr.AppendEntity(curve);
                                tr.AddNewlyCreatedDBObject(curve, true);
                            }
                        }
                        tr.Commit();
                    }
                }
                catch (System.Exception e)
                {
                    ed.WriteMessage("\nError: " + e.Message);
                }
            }
        }
« Last Edit: October 26, 2009, 02:43:17 PM by SEANT »
Sean Tessier
AutoCAD 2016 Mechanical

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Curve.GetProjectedCurve with polylines
« Reply #2 on: October 26, 2009, 05:05:36 PM »
Thanks Seant.
I tried somethings with TransformBy but never could get it work as you do :kewl:
Speaking English as a French Frog