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