using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using Autodesk.Civil;
using Autodesk.Civil.DatabaseServices;
using System.Collections.Generic;
using System.Linq;
namespace C3dTest
{
public class FeatureLineTest
{
[CommandMethod("CopyFeatureLine")]
public void CopyFeatureLineCmd()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
PromptEntityOptions entOpt
= new PromptEntityOptions("\nSelect a feature line:");
entOpt.SetRejectMessage("\nIt is not a feature line!");
entOpt.AddAllowedClass(typeof(FeatureLine), false);
PromptEntityResult entRes = ed.GetEntity(entOpt);
if (entRes.Status != PromptStatus.OK) return;
CreateFeatureLineCopy(entRes.ObjectId);
Application.ShowAlertDialog("A copy of the feature line is created!");
}
public static ObjectId CreateFeatureLineCopy(ObjectId sourceFLineId)
{
Database db = sourceFLineId.Database;
ObjectId newFeatureLineId;
Plane xy = new Plane(Point3d.Origin, Vector3d.ZAxis);
List<VertexData> vertices = new List<VertexData>();
ObjectId plineId, siteId, styleId, layerId, lTypeId;
double ltScale;
LineWeight lineWeight;
Color color;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
FeatureLine sourceFeatureLine = tr.GetObject
(sourceFLineId, OpenMode.ForRead) as FeatureLine;
siteId = sourceFeatureLine.SiteId;
styleId = sourceFeatureLine.StyleId;
layerId = sourceFeatureLine.LayerId;
lTypeId = sourceFeatureLine.LinetypeId;
ltScale = sourceFeatureLine.LinetypeScale;
lineWeight = sourceFeatureLine.LineWeight;
color = sourceFeatureLine.Color;
Point3dCollection points = sourceFeatureLine
.GetPoints(FeatureLinePointType.PIPoint);
for (int i = 0; i < points.Count - 1; i++)
{
vertices.Add(new VertexData
{
Point = points[i],
Bulge = sourceFeatureLine.GetBulge(i)
});
}
vertices.Add(new VertexData
{
Point = points[points.Count - 1],
Bulge = 0
});
BlockTableRecord btr = tr.GetObject
(sourceFeatureLine.BlockId, OpenMode.ForWrite)
as BlockTableRecord;
Polyline polyline = new Polyline(points.Count);
for (int i = 0; i < vertices.Count; i++)
{
var data = vertices[i];
polyline.AddVertexAt(i, data.Point.Convert2d(xy), data.Bulge, 0, 0);
}
plineId = btr.AppendEntity(polyline);
tr.AddNewlyCreatedDBObject(polyline, true);
tr.Commit();
}
newFeatureLineId = siteId.IsValid
? FeatureLine.Create(string.Empty, plineId, siteId)
: FeatureLine.Create(string.Empty, plineId);
using (Transaction tr = db.TransactionManager.StartTransaction())
{
FeatureLine newFeatureLine
= tr.GetObject(newFeatureLineId, OpenMode.ForWrite) as FeatureLine;
for (int i = 0; i < vertices.Count; i++)
{
newFeatureLine.SetPointElevation(i, vertices[i].Point.Z);
}
newFeatureLine.StyleId = styleId;
newFeatureLine.LayerId = layerId;
newFeatureLine.LinetypeId = lTypeId;
newFeatureLine.LinetypeScale = ltScale;
newFeatureLine.LineWeight = lineWeight;
newFeatureLine.Color = color;
var pline = tr.GetObject(plineId, OpenMode.ForWrite);
pline.Erase(true);
tr.Commit();
}
return newFeatureLineId;
}
struct VertexData
{
public Point3d Point;
public double Bulge;
}
}
}