public void GenerateFromConnectedPipes() { this.SortConnectedPipeLine(); int n = this.ConnectedPipes.Count; List <Vector3d> ds = new List <Vector3d>(); List <Point3d> ePoints = new List <Point3d>(); // 线段截断的两点 List <Point3d> cPoints = new List <Point3d>(); Polyline newPolyline = new Polyline(); List <Line> newLines = new List <Line>(); foreach (var item in this.ConnectedPipes) { Vector3d v = (item.Point(0) - this.CenterPoint).GetNormal(); ds.Add(v); Vector3d vn = v.RotateBy(0.5 * Math.PI, Vector3d.ZAxis); Point3d vp = this.CenterPoint + v * Length; cPoints.Add(vp); Point3d pl = vp - PipeLine.GetScale() / 2 * vn; Point3d pr = vp + PipeLine.GetScale() / 2 * vn; ePoints.Add(pl); ePoints.Add(pr); } int index = 0; for (int i = 0; i < n; i++) { int j = (i + 1) % n; newPolyline.AddVertexAt(index++, Utility.Point3DTo2D(ePoints[i * 2]), 0, 0, 0); newPolyline.AddVertexAt(index++, Utility.Point3DTo2D(ePoints[i * 2 + 1]), 0, 0, 0); if (!Utility.Parallel(this.CenterPoint, cPoints[i], this.CenterPoint, cPoints[j])) { Point3d intersect = Utility.Intersect(ePoints[i * 2 + 1], ds[i], ePoints[j * 2], ds[j]); newPolyline.AddVertexAt(index++, Utility.Point3DTo2D(intersect), 0, 0, 0); } } //闭合 newPolyline.AddVertexAt(index++, newPolyline.GetPoint2dAt(0), 0, 0, 0); // foreach (var p in cPoints) { Line l = new Line(); l.StartPoint = this.CenterPoint; l.EndPoint = p; newLines.Add(l); } if (this.BaseObjectId != ObjectId.Null) { this.RemoveEntity(); } this.pline = newPolyline; this.lines = newLines; this.SaveEntity(); }
public BaseModel DownGrade(PipeLine line) { // if (this.ConnectedPipes.Contains(line)) { this.RemovePipeLine(line); if (this.ConnectedPipes.Count == 2) { //退化成SimpleJoint 或者 PipeLine PipeLine line1 = this.ConnectedPipes[0]; PipeLine line2 = this.ConnectedPipes[1]; //退化成新的PipeLine if (line1.PipeLineCrossTest(line2) == PipeLine.CrossType.ParallelNoConnect) { Point3d p1, p2; PipeJoint co1, co2; if ((line1.Point(0) - this.CenterPoint).Length > (line1.Point(1) - this.CenterPoint).Length) { p1 = line1.Point(0); co1 = line1.HeadConnectedObject; } else { p1 = line1.Point(1); co1 = line1.TailConnectedObject; } if ((line2.Point(0) - this.CenterPoint).Length > (line2.Point(1) - this.CenterPoint).Length) { p2 = line2.Point(0); co2 = line2.HeadConnectedObject; } else { p2 = line2.Point(1); co2 = line2.TailConnectedObject; } PipeLine pLine = new PipeLine(p1, p2, OwnSolution, true); line1.Delete(); line2.Delete(); pLine.HeadConnectedObject = co1; if (co1 != null) { co1.RemovePipeLine(line1); co1.AddPipeLine(pLine); } pLine.TailConnectedObject = co2; if (co2 != null) { co2.RemovePipeLine(line2); co2.AddPipeLine(pLine); } this.RemoveEntity(); } else { Point3d p1, p2, p3, p4; bool ht1, ht2; // head or tail, true for head, false for tail if ((line1.Point(0) - this.CenterPoint).Length > (line1.Point(1) - this.CenterPoint).Length) { p1 = line1.Point(0); p3 = line1.Point(1); ht1 = false; } else { p1 = line1.Point(1); p3 = line1.Point(0); ht1 = true; } if ((line2.Point(0) - this.CenterPoint).Length > (line2.Point(1) - this.CenterPoint).Length) { p2 = line2.Point(0); p4 = line2.Point(1); ht2 = false; } else { p2 = line2.Point(1); p4 = line2.Point(0); ht2 = true; } Point3d intersect = Utility.Intersect(p1, p3 - p1, p2, p4 - p2); SimplePipeJoint simplePipeJoint = new SimplePipeJoint(p1, intersect, p2, PipeLine.GetScale() / 2, OwnSolution, true); Point3d p5 = simplePipeJoint.CalculateLastMlineEndPoint(); Point3d p6 = simplePipeJoint.CalculateMlineStartPoint(); if (ht1) { line1.UpdateStartPoint(p5); line1.HeadConnectedObject = simplePipeJoint; } else { line1.UpdateEndPoint(p5); line1.TailConnectedObject = simplePipeJoint; } if (ht2) { line2.UpdateStartPoint(p6); line2.HeadConnectedObject = simplePipeJoint; } else { line2.UpdateEndPoint(p6); line2.TailConnectedObject = simplePipeJoint; } simplePipeJoint.AddPipeLine(line1); simplePipeJoint.AddPipeLine(line2); this.RemoveEntity(); } } else { this.GenerateFromConnectedPipes(); } } return(null); }
/// <summary> /// 管道相交测试 /// </summary> /// <param name="pipeLine">测试管道</param> /// <param name="isFirst">相交测试是一个递归的过程,在一层递归里面PipeLine并没有被实际创建,而是在测试结束后才创建,下面的递归 /// 层,测试的管道都是已经被创建了的</param> /// <returns></returns> public Point3d?PipeLineCrossTest(PipeLine pipeLine) { Point3d?res; Dictionary <ObjectId, PipeLine> copyPipelines = new Dictionary <ObjectId, PipeLine>(); foreach (var item in PipeLines) { copyPipelines.Add(item.Key, item.Value); } //和管道测试相交 foreach (var item in copyPipelines) { PipeLine cItem = (PipeLine)item.Value; if (cItem == pipeLine) { continue; } PipeLine.CrossType type = cItem.PipeLineCrossTest(pipeLine); if (type == PipeLine.CrossType.CrossNone) { continue; } else if (type == PipeLine.CrossType.ParallelHead) { Point3d p1 = pipeLine.Point(0); Point3d p2 = pipeLine.Point(1); Point3d p3; bool result = false; if ((p1 - cItem.Point(0)).Length < (p2 - cItem.Point(0)).Length) { result = false; p3 = p2; } else { result = true; p3 = p1; } Vector3d v = cItem.Point(0) - cItem.Point(1); Vector3d v1 = p3 - cItem.Point(0); Point3d p = cItem.Point(0) + v1.DotProduct(v.GetNormal()) * v.GetNormal(); // pipeLine = new PipeLine(p, cItem.Point(1), false, this); pipeLine.TailConnectedObject = cItem.TailConnectedObject; if (pipeLine.TailConnectedObject != null) { pipeLine.TailConnectedObject.RemovePipeLine(cItem); pipeLine.TailConnectedObject.AddPipeLine(pipeLine); } cItem.Delete(); //递归调用 res = PipeLineCrossTest(pipeLine); if (result) { return(null); } else { return(res); } } else if (type == PipeLine.CrossType.ParallelTail) { Point3d p1 = pipeLine.Point(0); Point3d p2 = pipeLine.Point(1); Point3d p3; bool result; if ((p1 - cItem.Point(1)).Length < (p2 - cItem.Point(1)).Length) { result = false; p3 = p2; } else { result = true; p3 = p1; } Vector3d v = cItem.Point(1) - cItem.Point(0); Vector3d v1 = p3 - cItem.Point(1); Point3d p = cItem.Point(1) + v1.DotProduct(v.GetNormal()) * v.GetNormal(); // pipeLine = new PipeLine(cItem.Point(0), p, false, this); pipeLine.HeadConnectedObject = cItem.HeadConnectedObject; if (pipeLine.HeadConnectedObject != null) { pipeLine.HeadConnectedObject.RemovePipeLine(cItem); pipeLine.HeadConnectedObject.AddPipeLine(pipeLine); } cItem.Delete(); //递归调用 res = PipeLineCrossTest(pipeLine); if (result) { return(null); } else { return(res); } } else if (type == PipeLine.CrossType.NonParallelHead) { Point3d p1 = pipeLine.Point(0); Point3d p2 = pipeLine.Point(1); Point3d p3; bool result; if ((p1 - cItem.Point(0)).Length < (p2 - cItem.Point(0)).Length) { result = false; p3 = p2; } else { result = true; p3 = p1; } SimplePipeJoint simplePipeJoint = new SimplePipeJoint(cItem.Point(1), cItem.Point(0), p3, PipeLine.GetScale() / 2, this, true); cItem.UpdateStartPoint(simplePipeJoint.CalculateLastMlineEndPoint()); Point3d startPoint = simplePipeJoint.CalculateMlineStartPoint(); simplePipeJoint.AddPipeLine(cItem); PipeLine newLine = new PipeLine(p3, startPoint, false, this); simplePipeJoint.AddPipeLine(newLine); cItem.HeadConnectedObject = simplePipeJoint; newLine.TailConnectedObject = simplePipeJoint; if (pipeLine.HeadConnectedObject != null) { pipeLine.HeadConnectedObject.RemovePipeLine(pipeLine); pipeLine.HeadConnectedObject.AddPipeLine(newLine); newLine.HeadConnectedObject = pipeLine.HeadConnectedObject; } //递归调用 res = PipeLineCrossTest(newLine); if (result) { return(null); } else { return(res); } } else if (type == PipeLine.CrossType.NonParallelTail) { Point3d p1 = pipeLine.Point(0); Point3d p2 = pipeLine.Point(1); Point3d p3; bool result; if ((p1 - cItem.Point(1)).Length < (p2 - cItem.Point(1)).Length) { result = false; p3 = p2; } else { result = true; p3 = p1; } SimplePipeJoint simplePipeJoint = new SimplePipeJoint(cItem.Point(0), cItem.Point(1), p3, PipeLine.GetScale() / 2, this, true); cItem.UpdateEndPoint(simplePipeJoint.CalculateLastMlineEndPoint()); cItem.TailConnectedObject = simplePipeJoint; Point3d startPoint = simplePipeJoint.CalculateMlineStartPoint(); simplePipeJoint.AddPipeLine(cItem); PipeLine newLine = new PipeLine(startPoint, p3, false, this); simplePipeJoint.AddPipeLine(newLine); newLine.HeadConnectedObject = simplePipeJoint; if (pipeLine.TailConnectedObject != null) { pipeLine.TailConnectedObject.RemovePipeLine(pipeLine); pipeLine.TailConnectedObject.AddPipeLine(newLine); newLine.TailConnectedObject = pipeLine.TailConnectedObject; } res = PipeLineCrossTest(newLine); if (result) { return(null); } else { return(res); } } else if (type == PipeLine.CrossType.CrossHead) { MultiPipeJoint mj = new MultiPipeJoint(this, true); List <Point3d> ps = mj.GenerateFromCrossPipe(cItem, pipeLine.Point(0), pipeLine.Point(1), PipeLine.GetScale() / 2); PipeLine p1 = new PipeLine(cItem.Point(0), ps[0], this, true); PipeLine p2 = new PipeLine(ps[1], cItem.Point(1), this, true); // if (cItem.HeadConnectedObject != null) { cItem.HeadConnectedObject.RemovePipeLine(cItem); cItem.HeadConnectedObject.AddPipeLine(p1); } if (cItem.TailConnectedObject != null) { cItem.TailConnectedObject.RemovePipeLine(cItem); cItem.TailConnectedObject.AddPipeLine(p2); } p1.HeadConnectedObject = cItem.HeadConnectedObject; p1.TailConnectedObject = mj; p2.HeadConnectedObject = mj; p2.TailConnectedObject = cItem.TailConnectedObject; Point3d startPoint = ps[2]; cItem.Delete(); PipeLine pLine = new PipeLine(startPoint, pipeLine.Point(1), false, this); pLine.HeadConnectedObject = mj; if (pipeLine.TailConnectedObject != null) { pipeLine.TailConnectedObject.RemovePipeLine(pipeLine); pipeLine.TailConnectedObject.AddPipeLine(pLine); pLine.TailConnectedObject = pipeLine.TailConnectedObject; } mj.AddPipeLine(p1); mj.AddPipeLine(p2); mj.AddPipeLine(pLine); res = PipeLineCrossTest(pLine); return(res); } else if (type == PipeLine.CrossType.CrossTail) { // MultiPipeJoint mj = new MultiPipeJoint(this, true); List <Point3d> ps = mj.GenerateFromCrossPipe(cItem, pipeLine.Point(1), pipeLine.Point(0), PipeLine.GetScale() / 2); PipeLine p1 = new PipeLine(cItem.Point(0), ps[0], this, true); PipeLine p2 = new PipeLine(ps[1], cItem.Point(1), this, true); // if (cItem.HeadConnectedObject != null) { cItem.HeadConnectedObject.RemovePipeLine(cItem); cItem.HeadConnectedObject.AddPipeLine(p1); } if (cItem.TailConnectedObject != null) { cItem.TailConnectedObject.RemovePipeLine(cItem); cItem.TailConnectedObject.AddPipeLine(p2); } p1.HeadConnectedObject = cItem.HeadConnectedObject; p1.TailConnectedObject = mj; p2.HeadConnectedObject = mj; p2.TailConnectedObject = cItem.TailConnectedObject; Point3d startPoint = ps[2]; cItem.Delete(); PipeLine pLine = new PipeLine(pipeLine.Point(0), startPoint, false, this); pLine.TailConnectedObject = mj; if (pipeLine.HeadConnectedObject != null) { pipeLine.HeadConnectedObject.RemovePipeLine(pipeLine); pipeLine.HeadConnectedObject.AddPipeLine(pLine); pLine.HeadConnectedObject = pipeLine.HeadConnectedObject; } mj.AddPipeLine(p1); mj.AddPipeLine(p2); mj.AddPipeLine(pLine); PipeLineCrossTest(pLine); return(mj.CenterPoint); } else if (type == PipeLine.CrossType.CrossOver) { MultiPipeJoint mj = new MultiPipeJoint(this, true); List <Point3d> ps = mj.GenerateFromCrossPipe(cItem, pipeLine.Point(1), pipeLine.Point(0), PipeLine.GetScale() / 2); PipeLine p1 = new PipeLine(cItem.Point(0), ps[0], this, true); PipeLine p2 = new PipeLine(ps[1], cItem.Point(1), this, true); if (cItem.HeadConnectedObject != null) { cItem.HeadConnectedObject.RemovePipeLine(cItem); cItem.HeadConnectedObject.AddPipeLine(p1); } if (cItem.TailConnectedObject != null) { cItem.TailConnectedObject.RemovePipeLine(cItem); cItem.TailConnectedObject.AddPipeLine(p2); } p1.HeadConnectedObject = cItem.HeadConnectedObject; p1.TailConnectedObject = mj; p2.HeadConnectedObject = mj; p2.TailConnectedObject = cItem.TailConnectedObject; Point3d startPoint = ps[2]; cItem.Delete(); PipeLine pLine = new PipeLine(pipeLine.Point(0), startPoint, false, this); pLine.TailConnectedObject = mj; if (pipeLine.HeadConnectedObject != null) { pipeLine.HeadConnectedObject.RemovePipeLine(pipeLine); pipeLine.HeadConnectedObject.AddPipeLine(pLine); pLine.HeadConnectedObject = pipeLine.HeadConnectedObject; } mj.AddPipeLine(p1); mj.AddPipeLine(p2); mj.AddPipeLine(pLine); PipeLineCrossTest(pLine); //插入新的点 PipeLine se = mj.UpdateMultiJoint(pipeLine.Point(1)); se.HeadConnectedObject = mj; if (pipeLine.TailConnectedObject != null) { pipeLine.TailConnectedObject.RemovePipeLine(pipeLine); pipeLine.TailConnectedObject.AddPipeLine(se); se.TailConnectedObject = pipeLine.TailConnectedObject; } res = PipeLineCrossTest(se); return(res); } } foreach (var item in SimplePipeJoints) { SimplePipeJoint pItem = (SimplePipeJoint)item.Value; if (pItem.PointInJoint(pipeLine.Point(0)) && !pItem.ConnectedPipes.Contains(pipeLine)) { MultiPipeJoint mpj = new MultiPipeJoint(this, true); PipeLine ps = mpj.GenerateFromSimpleJoint(pItem, pipeLine.Point(1), PipeLine.GetScale() / 2); ps.HeadConnectedObject = mpj; res = PipeLineCrossTest(ps); return(res); } if (pItem.PointInJoint(pipeLine.Point(1)) && !pItem.ConnectedPipes.Contains(pipeLine)) { MultiPipeJoint mpj = new MultiPipeJoint(this, true); PipeLine ps = mpj.GenerateFromSimpleJoint(pItem, pipeLine.Point(0), PipeLine.GetScale() / 2); ps.TailConnectedObject = mpj; res = PipeLineCrossTest(ps); return(null); } } foreach (var item in MultiPipeJoints) { MultiPipeJoint pItem = (MultiPipeJoint)item.Value; if (pItem.PointInJoint(pipeLine.Point(0)) && !pItem.ConnectedPipes.Contains(pipeLine)) { PipeLine se = pItem.UpdateMultiJoint(pipeLine.Point(1)); se.HeadConnectedObject = pItem; res = PipeLineCrossTest(se); return(res); } if (pItem.PointInJoint(pipeLine.Point(1)) && !pItem.ConnectedPipes.Contains(pipeLine)) { PipeLine se = pItem.UpdateMultiJoint(pipeLine.Point(0)); se.HeadConnectedObject = pItem; res = PipeLineCrossTest(se); return(null); } } pipeLine.SaveEntity(); return(pipeLine.Point(1)); }