using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
namespace 3dToPline
{
public class ToPline
{
[CommandMethod("ToPline")]
public void Tpolyline()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
Entity entity = null;
double count2d;
double count3d;
count3d = 0;
count2d = 0;
// 创建三维多段线的选择集
TypedValue[] filList = new TypedValue[1];
// 二维三维选择集过滤 "*Polyline" 为多段线,二维多段线,三维多段线 过滤。
filList[0] = new TypedValue((int)DxfCode.Start, "Polyline");
// 建立过滤器
SelectionFilter filter = new SelectionFilter(filList);
PromptSelectionResult ents = ed.SelectAll(filter); //(selops, filter);
ed.WriteMessage("n正在进行三维线转多段线.....");
if (ents.Status == PromptStatus.OK)
{
using (Transaction transaction = db.TransactionManager.StartTransaction())
{
SelectionSet SS = ents.Value;
foreach (ObjectId id in SS.GetObjectIds())
{
entity = (Entity)transaction.GetObject(id, OpenMode.ForWrite, true);
if (entity != null)
{
// ed.WriteMessage(entity.GetType().Name);
if (entity.GetType().Name == "Polyline3d")
{
count3d = count3d + 1;
Polyline3d pl3d = (Polyline3d)transaction.GetObject(id, OpenMode.ForWrite, true);
Point3dCollection pts = new Point3dCollection();
pl3d.GetStretchPoints(pts);
Point2dCollection pt2d = new Point2dCollection();
double Elv;
Elv = 0;
foreach (Point3d pt3d in pts)
{
Point2d p2d = new Point2d(pt3d.X, pt3d.Y);
Elv = Elv + pt3d.Z;
pt2d.Add(p2d);
// ed.WriteMessage("n节点坐标为{0},{1},{2}", pt3d.X, pt3d.Y, pt3d.Z);
}
Polyline pl = Polyline(pt2d, 0);



