public void 交叉多段线容易出错()
{
List<Curve> entse = Z.db.SelectEntities<Curve>();
List<Polyline> ents = Z.db.CurvesToPolyLines(entse);
//Z.db.SelectEntities<Polyline>();
double offsetDistance = 5.0;//偏移距离
List<Polyline> resultPolylines = new List<Polyline>();
List<Circle> resultCir = new List<Circle>();
List<Region> regions = new List<Region>();
DBObjectCollection tempColl = new DBObjectCollection();//
DBObjectCollection allColl = new DBObjectCollection();//所有对象集合
foreach (var item in ents)
{
Polyline polyline = item ;
DBObjectCollection offsetLineSegLeft = polyline.GetOffsetCurves(offsetDistance);
DBObjectCollection offsetLineSegRight = polyline.GetOffsetCurves( (-1)*offsetDistance);
Polyline pltemp1 = offsetLineSegLeft[0] as Polyline;
Polyline pltemp2 = offsetLineSegRight[0] as Polyline;
Polyline pltempxx = new Polyline();
Polyline pltempyy = new Polyline();
pltempxx.AddVertexAt(0,new Point2d(pltemp1.StartPoint.X, pltemp1.StartPoint.Y), 0, 0, 0);
pltempxx.AddVertexAt(1, new Point2d(pltemp2.StartPoint.X, pltemp2.StartPoint.Y), 0, 0, 0);
pltempyy.AddVertexAt(0, new Point2d(pltemp2.EndPoint.X, pltemp2.EndPoint.Y), 0, 0, 0);
pltempyy.AddVertexAt(1, new Point2d(pltemp1.EndPoint.X, pltemp1.EndPoint.Y), 0, 0, 0);
Z.db.AddEntityToModeSpace(pltempxx, pltempyy);
Z.db.AddEntityToModeSpace(offsetLineSegLeft[0] as Polyline);
Z.db.AddEntityToModeSpace(offsetLineSegRight[0] as Polyline);
}
}
public static List<T> SelectEntities<T>(this Database db ) where T : Entity
{
List<T> result = new List<T>();
Editor editor = Application.DocumentManager.MdiActiveDocument.Editor;
var pso = new PromptSelectionOptions();
pso.MessageForAdding = "\n请选择:";
PromptSelectionResult psr = editor.GetSelection(pso);
if (psr.Status == PromptStatus.OK)
{
ObjectId[] objectids = psr.Value.GetObjectIds();
Database database = HostApplicationServices.WorkingDatabase;
using (Transaction tran = database.TransactionManager.StartTransaction())
{
foreach (var item in objectids)
{
Entity entity = item.GetObject(OpenMode.ForRead) as Entity;
if (entity is T)
{
result.Add(entity as T);
}
}
}
}
return result;
}
public static List<Autodesk.AutoCAD.DatabaseServices.Polyline> CurvesToPolyLines(this Database db, List<Curve> ents)
{
List<Autodesk.AutoCAD.DatabaseServices.Polyline> curves = new List<Autodesk.AutoCAD.DatabaseServices.Polyline>();
Spline sp;
foreach (var item in ents)
{
if (item is Polyline oldpl)
{
curves.Add(oldpl);
}
if (item is Line line)
{
Polyline polyline = new Polyline();
polyline.AddVertexAt(0,new Point2d (line .StartPoint.X,line.StartPoint.Y),0,0,0);
polyline.AddVertexAt(1, new Point2d(line.EndPoint.X, line.EndPoint.Y), 0, 0, 0);
polyline.ColorIndex = line.ColorIndex;
curves.Add(polyline);
}
if (item is Circle)
{
Circle temp = (Circle)item;
Point3d pt = temp.Center;
double leftx = pt.X - temp.Radius;
double rightx = pt.X + temp.Radius;
double topx = pt.X;
double bottomx = pt.X;
double lefty = pt.Y;
double righty = pt.Y;
double topy = pt.Y + temp.Radius;
double bottomy = pt.Y - temp.Radius;
Point2d left = new Point2d(leftx, lefty);
Point2d right = new Point2d(rightx, righty);
Point2d top = new Point2d(topx, topy);
Point2d bottom = new Point2d(bottomx, bottomy);
Autodesk.AutoCAD.DatabaseServices.Polyline pl = new Autodesk.AutoCAD.DatabaseServices.Polyline();
pl.AddVertexAt(0, bottom, 1, 0, 0);
pl.AddVertexAt(1, top, 1, 0, 0);
pl.AddVertexAt(2, bottom, 0, 0, 0);
pl.ColorIndex = temp.ColorIndex;
curves.Add(pl);
//pl.Highlight();
}
if (item is Arc arc)
{
double ang = 0;
if (arc.StartAngle > arc.EndAngle)
{
double endangle;
endangle = arc.EndAngle + 2 * Math.PI;
ang = endangle - arc.StartAngle;
}
else
{
ang = arc.EndAngle - arc.StartAngle;
}
//Debugger.Break();
ang = ang / 4;
double td = Math.Tan(ang);
// arc.Spline;
Autodesk.AutoCAD.DatabaseServices.Polyline pl = new Autodesk.AutoCAD.DatabaseServices.Polyline();
// if arc.StartAngle
pl.AddVertexAt(0, new Point2d(item.StartPoint.X, item.StartPoint.Y), td, 0, 0);
pl.AddVertexAt(1, new Point2d(item.EndPoint.X, item.EndPoint.Y), 0, 0, 0);
pl.ColorIndex = arc.ColorIndex;
curves.Add(pl);//Z.db.AddEntityToModeSpace(pl);
}
if (item is Ellipse ellipse)
{
sp = item.Spline;
var curve = sp.ToPolyline();
curve.ColorIndex = ellipse.ColorIndex;
curves.Add(curve as Polyline);
}
if (item is Spline spline)
{
Spline sp2 = (Spline)item;
var curve2 = sp2.ToPolyline();
curve2.ColorIndex = spline.ColorIndex;
curves.Add(curve2 as Polyline);
}
}
List<Curve> eracurves = new List<Curve>();
foreach (var item in ents)
{
if (item is Circle || item is Arc || item is Ellipse || item is Spline|| item is Line)
{
eracurves.Add(item );
}
}
Z.db.Erase(eracurves);
Z.db.AddEntityToModeSpace(curves.ToArray());//转换完加入模型空间
return curves;
}
curve版
public void 交叉多段线容易出错可拟合闭合圆和椭圆()
{
List<Curve> entse = Z.db.SelectEntities<Curve>();
List<Polyline> ents = Z.db.CurvesToPolyLines(entse);
//Z.db.SelectEntities<Polyline>();
//List<Curve> ents = Z.db.SelectEntities<Curve>();
double offsetDistance = 5.0;//偏移距离
List<Polyline> resultPolylines = new List<Polyline>();
List<Circle> resultCir = new List<Circle>();
List<Region> regions = new List<Region>();
DBObjectCollection tempColl = new DBObjectCollection();//
DBObjectCollection allColl = new DBObjectCollection();//所有对象集合
foreach (var item in ents)
{
Curve polyline = item ;
DBObjectCollection offsetLineSegLeft = polyline.GetOffsetCurves(offsetDistance);
DBObjectCollection offsetLineSegRight = polyline.GetOffsetCurves( (-1)*offsetDistance);
Curve pltemp1 = offsetLineSegLeft[0] as Curve; //as Polyline;
Curve pltemp2 = offsetLineSegRight[0] as Curve;//as Polyline;
if (!item.Closed)//如果不闭合
{
Polyline pltempxx = new Polyline();
Polyline pltempyy = new Polyline();
pltempxx.AddVertexAt(0,new Point2d(pltemp1.StartPoint.X, pltemp1.StartPoint.Y), 0, 0, 0);
pltempxx.AddVertexAt(1, new Point2d(pltemp2.StartPoint.X, pltemp2.StartPoint.Y), 0, 0, 0);
pltempyy.AddVertexAt(0, new Point2d(pltemp2.EndPoint.X, pltemp2.EndPoint.Y), 0, 0, 0);
pltempyy.AddVertexAt(1, new Point2d(pltemp1.EndPoint.X, pltemp1.EndPoint.Y), 0, 0, 0);
Curve[] arraypl = new Curve[3];
arraypl[0]= pltempxx as Curve;
arraypl[1]= pltemp2 ;
arraypl[2]= pltempyy as Curve;
pltemp1.ReverseCurve();
//arraypl[3]= pltemp1;
Curve closedpl = pltemp1;
closedpl.JoinEntities(arraypl);
Z.db.AddEntityToModeSpace(closedpl);
//Z.db.AddEntityToModeSpace(pltempxx, pltempyy);
}
else
{
Z.db.AddEntityToModeSpace(offsetLineSegLeft[0] as Polyline);
Z.db.AddEntityToModeSpace(offsetLineSegRight[0] as Polyline);
}
}
}
public static List<Autodesk.AutoCAD.DatabaseServices.Polyline> CurvesToPolyLines(this Database db, List<Curve> ents)
{
List<Autodesk.AutoCAD.DatabaseServices.Polyline> curves = new List<Autodesk.AutoCAD.DatabaseServices.Polyline>();
Spline sp;
foreach (var item in ents)
{
if (item is Polyline oldpl)
{
curves.Add(oldpl);
}
if (item is Line line)
{
Polyline polyline = new Polyline();
polyline.AddVertexAt(0,new Point2d (line .StartPoint.X,line.StartPoint.Y),0,0,0);
polyline.AddVertexAt(1, new Point2d(line.EndPoint.X, line.EndPoint.Y), 0, 0, 0);
polyline.ColorIndex = line.ColorIndex;
curves.Add(polyline);
}
if (item is Circle)
{
Circle temp = (Circle)item;
Point3d pt = temp.Center;
double leftx = pt.X - temp.Radius;
double rightx = pt.X + temp.Radius;
double topx = pt.X;
double bottomx = pt.X;
double lefty = pt.Y;
double righty = pt.Y;
double topy = pt.Y + temp.Radius;
double bottomy = pt.Y - temp.Radius;
Point2d left = new Point2d(leftx, lefty);
Point2d right = new Point2d(rightx, righty);
Point2d top = new Point2d(topx, topy);
Point2d bottom = new Point2d(bottomx, bottomy);
Autodesk.AutoCAD.DatabaseServices.Polyline pl = new Autodesk.AutoCAD.DatabaseServices.Polyline();
pl.AddVertexAt(0, bottom, 1, 0, 0);
pl.AddVertexAt(1, top, 1, 0, 0);
pl.AddVertexAt(2, bottom, 0, 0, 0);
pl.ColorIndex = temp.ColorIndex;
pl.Closed = true;
curves.Add(pl);
//pl.Highlight();
}
if (item is Arc arc)
{
double ang = 0;
if (arc.StartAngle > arc.EndAngle)
{
double endangle;
endangle = arc.EndAngle + 2 * Math.PI;
ang = endangle - arc.StartAngle;
}
else
{
ang = arc.EndAngle - arc.StartAngle;
}
//Debugger.Break();
ang = ang / 4;
double td = Math.Tan(ang);
// arc.Spline;
Autodesk.AutoCAD.DatabaseServices.Polyline pl = new Autodesk.AutoCAD.DatabaseServices.Polyline();
// if arc.StartAngle
pl.AddVertexAt(0, new Point2d(item.StartPoint.X, item.StartPoint.Y), td, 0, 0);
pl.AddVertexAt(1, new Point2d(item.EndPoint.X, item.EndPoint.Y), 0, 0, 0);
pl.ColorIndex = arc.ColorIndex;
curves.Add(pl);//Z.db.AddEntityToModeSpace(pl);
}
if (item is Ellipse ellipse)
{
sp = item.Spline;
var curve = sp.ToPolyline();
curve.ColorIndex = ellipse.ColorIndex;
curves.Add(curve as Polyline);
}
if (item is Spline spline)
{
Spline sp2 = (Spline)item;
var curve2 = sp2.ToPolyline();
curve2.ColorIndex = spline.ColorIndex;
curves.Add(curve2 as Polyline);
}
}
List<Curve> eracurves = new List<Curve>();
foreach (var item in ents)
{
if (item is Circle || item is Arc || item is Ellipse || item is Spline|| item is Line)
{
eracurves.Add(item );
}
}
Z.db.Erase(eracurves);
Z.db.AddEntityToModeSpace(curves.ToArray());//转换完加入模型空间
return curves;
}