文档库 最新最全的文档下载
当前位置:文档库 › CAD C#.NET 三维多段线转换为多段线

CAD C#.NET 三维多段线转换为多段线


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

https://www.wendangku.net/doc/b93613178.html,yer = https://www.wendangku.net/doc/b93613178.html,yer;
pl.Elevation = Elv / pts.Count;
ToModelSpace(pl);
pl3d.Erase();

}
else if (entity.GetType().Name == "Polyline2d")
{
count2d = count2d + 1;
}

}

}
https://www.wendangku.net/doc/b93613178.html,mit();
}

ed.WriteMessage("\n转换完{0}条三维多段线...", count3d.ToString());
}



}

public static ObjectId ToModelSpace(Entity ent)
{
Database db = HostApplicationServices.WorkingDatabase;


ObjectId entId;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
entId = btr.AppendEntity(ent);
trans.AddNewlyCreatedDBObject(ent, true);
https://www.wendangku.net/doc/b93613178.html,mit();
}
return entId;
}
public static Polyline Polyline(Point2dCollection pts, double width)
{
try
{
Polyline ent = new Polyline();
for (int i = 0; i < pts.Count; i++)
{
ent.AddVertexAt(i, pts[i], 0, width, width);
}
return ent;
}
catch
{
return new Polyline();
}
}

}



相关文档