道路倒角 三角网 两侧偏移
public void 多段线和直线两侧缓冲区()
{
List<Curve> ents1 = Z.db.SelectEntities<Curve>();
List<Polyline> ents = Z.db.CurvesToPolyLines2(ents1);
//Z.db.SelectEntities<Polyline>();
double offsetDistance = 5.0;//p距离
double offsetDistance2 = 1.0;//p距离
List<Line> rightPolylines = new List<Line>();
List<Line> rightPolylines2 = new List<Line>();
Region myRegion = PolylineToRegion(ents, offsetDistance , offsetDistance2,out rightPolylines);
Region myRegion2 = LineToRegion(rightPolylines, offsetDistance2, out rightPolylines2);
myRegion.BooleanOperation(BooleanOperationType.BoolSubtract, myRegion2); //面域相减
//Z.db.AddEntityToModeSpace(resultPolylines.ToArray());
//Z.db.AddEntityToModeSpace(resultCir.ToArray());
//Z.db.AddEntityToModeSpace(regions.ToArray());
Z.db.AddEntityToModeSpace(myRegion);
//Z.db.AddEntityToModeSpace(rightPolylines.ToArray());
}
public Region PolylineToRegion(List<Polyline> ents, double offsetDistance, double offsetDistance2, out List<Line> rightPolylines)
{
List<Polyline> resultPolylines = new List<Polyline>();
List<Line> rightPolylines2 = new List<Line>();
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;
double radius = 0;
//Circle circle1 = new Circle(polyline.EndPoint, Vector3d.ZAxis, radius);//最后一个点画圆
//circle1.ColorIndex = 1;
//resultCir.Add(circle1);
//allColl.Add(circle1);
for (int i = 0; i < polyline.NumberOfVertices - 1; i++)
{
//if (i == polyline.NumberOfVertices - 2)
//{
// Circle circle1 = new Circle(polyline.EndPoint, Vector3d.ZAxis, radius);//最后一个点画圆
// circle1.ColorIndex = 1;
// resultCir.Add(circle1);
// allColl.Add(circle1);
//}
// polyline.GetOffsetCurvesGivenPlaneNormal(Vector3d.ZAxis, offsetDistance);
// 获取当前段的起点和终点
Point3d startPt = polyline.GetPoint3dAt(i);//.GetPointAt(i);
Point3d endPt = polyline.GetPoint3dAt(i + 1);
//Region region1 = new Region();//创建面域
// 创建偏移的直线段
Line lineSeg = new Line(startPt, endPt);
DBObjectCollection offsetLineSegLeft = lineSeg.GetOffsetCurves(offsetDistance);// (offsetDistance, Vector3d.ZAxis, OffsetCurveExtensionType.Chamfer);
DBObjectCollection offsetLineSegRight = lineSeg.GetOffsetCurves(offsetDistance * (-1.0));
Line leftline = offsetLineSegLeft[0] as Line;
Line rightline = offsetLineSegRight[0] as Line;
radius = leftline.GetClosestPointTo(endPt, false).DistanceTo(endPt)- offsetDistance2; // .GetDistAtPoint(endPt);
//if (i >= 0 && i < polyline.NumberOfVertices - 2)//起始点不画圆圈
if (true)//起始点画圆圈
{
if (i==0)
{
Circle circle1 = new Circle(startPt, Vector3d.ZAxis, radius);
//circle1.ColorIndex = 1;
resultCir.Add(circle1);
allColl.Add(circle1); ;//创建圆
}
Circle circle = new Circle(endPt, Vector3d.ZAxis, radius);//创建圆
//circle.ColorIndex = 1;
resultCir.Add(circle);
allColl.Add(circle);
}
将偏移后的线段转换为多段线
Polyline plLeft = new Polyline();
//plLeft.ColorIndex = 1;
plLeft.AddVertexAt(0, new Point2d(leftline.StartPoint.X, leftline.StartPoint.Y), polyline.GetBulgeAt(i), 0, 0);
plLeft.AddVertexAt(1, new Point2d(leftline.EndPoint.X, leftline.EndPoint.Y), 0, 0, 0);
resultPolylines.Add(plLeft);
Polyline plRight = new Polyline();
//plRight.ColorIndex = 1;
plRight.AddVertexAt(0, new Point2d(rightline.StartPoint.X, rightline.StartPoint.Y), polyline.GetBulgeAt(i), 0, 0);
plRight.AddVertexAt(1, new Point2d(rightline.EndPoint.X, rightline.EndPoint.Y), 0, 0, 0);
resultPolylines.Add(plRight);
// rightPolylines2.Add(plRight);//需要减去的里边线
Polyline rect = new Polyline();
rect.AddVertexAt(0, new Point2d(leftline.StartPoint.X, leftline.StartPoint.Y), polyline.GetBulgeAt(i), 0, 0);
rect.AddVertexAt(1, new Point2d(leftline.EndPoint.X, leftline.EndPoint.Y), 0, 0, 0);
rect.AddVertexAt(2, new Point2d(rightline.EndPoint.X, rightline.EndPoint.Y), polyline.GetBulgeAt(i) * (-1), 0, 0);
rect.AddVertexAt(3, new Point2d(rightline.StartPoint.X, rightline.StartPoint.Y), 0, 0, 0);
//rect.ColorIndex = 1;
rect.Closed = true;
//Z.db.AddEntityToModeSpace(rect);//画多段线
allColl.Add(rect);
}
tempColl = Region.CreateFromCurves(allColl);//region的边界是一个对象集合
for (int j = 0; j < tempColl.Count; j++)//这里不可以-1
{
regions.Add(tempColl[j] as Region);//加入retions
//regions[j].ColorIndex = 1;
}
}
Region combinedRegion = new Region();
foreach (Region region in regions)
{
combinedRegion.BooleanOperation(BooleanOperationType.BoolUnite, region);
}
DBObjectCollection dbc = new DBObjectCollection();
combinedRegion.Explode( dbc );
foreach (var item in dbc)
{
if (item is Line line )
{
//Polyline pl = new Polyline();
//pl.AddVertexAt(0, new Point2d(line.StartPoint.X, line.StartPoint.Y), 0, 0, 0);
rightPolylines2.Add(line);
}
}
rightPolylines = rightPolylines2;
return combinedRegion;
}
public Region LineToRegion(List<Line> ents, double offsetDistance, out List<Line> rightPolylines)
{
List<Polyline> resultPolylines = new List<Polyline>();
List<Line> rightPolylines2 = new List<Line>();
List<Circle> resultCir = new List<Circle>();
List<Region> regions = new List<Region>();
DBObjectCollection tempColl = new DBObjectCollection();//
DBObjectCollection allColl = new DBObjectCollection();//所有对象集合
List<Line> linerep = Outershape.RepairLine(ents);
foreach (var item in linerep)
{
Point3d startPt = item.StartPoint;
Point3d endPt = item.EndPoint;
DBObjectCollection offsetLineSegLeft = item.GetOffsetCurves(offsetDistance);// (offsetDistance, Vector3d.ZAxis, OffsetCurveExtensionType.Chamfer);
DBObjectCollection offsetLineSegRight = item.GetOffsetCurves(offsetDistance * (-1.0));
Line leftline = offsetLineSegLeft[0] as Line;
Line rightline = offsetLineSegRight[0] as Line;
double radius = leftline.GetClosestPointTo(endPt, false).DistanceTo(endPt); // .GetDistAtPoint(endPt); //{
Circle circle = new Circle(endPt, Vector3d.ZAxis, radius);//创建圆
//circle.ColorIndex = 1;
resultCir.Add(circle);
allColl.Add(circle);
}
foreach (var item in ents)
{
Point3d startPt = item.StartPoint;
Point3d endPt = item.EndPoint;
DBObjectCollection offsetLineSegLeft = item.GetOffsetCurves(offsetDistance);// (offsetDistance, Vector3d.ZAxis, OffsetCurveExtensionType.Chamfer);
DBObjectCollection offsetLineSegRight = item.GetOffsetCurves(offsetDistance * (-1.0));
Line leftline = offsetLineSegLeft[0] as Line;
Line rightline = offsetLineSegRight[0] as Line;
//double radius = leftline.GetClosestPointTo(endPt, false).DistanceTo(endPt); // .GetDistAtPoint(endPt); //{
//Circle circle = new Circle(endPt, Vector3d.ZAxis, radius);//创建圆
//circle.ColorIndex = 1;
//resultCir.Add(circle);
//allColl.Add(circle);
//}
将偏移后的线段转换为多段线
Polyline plLeft = new Polyline();//画左边线
//plLeft.ColorIndex = 1;
plLeft.AddVertexAt(0, new Point2d(leftline.StartPoint.X, leftline.StartPoint.Y), 0, 0, 0);
plLeft.AddVertexAt(1, new Point2d(leftline.EndPoint.X, leftline.EndPoint.Y), 0, 0, 0);
resultPolylines.Add(plLeft);
Polyline plRight = new Polyline();//画右边线
//plRight.ColorIndex = 1;
plRight.AddVertexAt(0, new Point2d(rightline.StartPoint.X, rightline.StartPoint.Y),0, 0, 0);
plRight.AddVertexAt(1, new Point2d(rightline.EndPoint.X, rightline.EndPoint.Y), 0, 0, 0);
resultPolylines.Add(plRight);
rightPolylines2.Add(plRight);//需要减去的里边线
Polyline rect = new Polyline();//画矩形
rect.AddVertexAt(0, new Point2d(leftline.StartPoint.X, leftline.StartPoint.Y), 0, 0, 0);
rect.AddVertexAt(1, new Point2d(leftline.EndPoint.X, leftline.EndPoint.Y), 0, 0, 0);
rect.AddVertexAt(2, new Point2d(rightline.EndPoint.X, rightline.EndPoint.Y), 0, 0, 0);
rect.AddVertexAt(3, new Point2d(rightline.StartPoint.X, rightline.StartPoint.Y), 0, 0, 0);
//rect.ColorIndex = 1;
rect.Closed = true;
//Z.db.AddEntityToModeSpace(rect);//画多段线
allColl.Add(rect);
//}
tempColl = Region.CreateFromCurves(allColl);//region的边界是一个对象集合
for (int j = 0; j < tempColl.Count; j++)//这里不可以-1
{
regions.Add(tempColl[j] as Region);//加入retions
//regions[j].ColorIndex = 1;
}
}
Region combinedRegion = new Region();
foreach (Region region in regions)
{
combinedRegion.BooleanOperation(BooleanOperationType.BoolUnite, region);
}
DBObjectCollection dbc = new DBObjectCollection();
combinedRegion.Explode(dbc);
foreach (var item in dbc)
{
if (item is Line line)
{
//Polyline pl = new Polyline();
//pl.AddVertexAt(0, new Point2d(line.StartPoint.X, line.StartPoint.Y), 0, 0, 0);
rightPolylines2.Add(line);
}
}
rightPolylines = rightPolylines2;
return combinedRegion;
}