public static MText FindTextAbove(BlockTableRecord mSpace, Transaction trans, MText text) { LineSegment3d current = new LineSegment3d(text.Bounds.Value.MinPoint, text.Bounds.Value.MaxPoint); Line3d lineUp = new Line3d(current.MidPoint, Vector3d.YAxis); double distance = double.MaxValue; MText ret = null; foreach (ObjectId entId in mSpace) { MText thisText = trans.GetObject(entId, OpenMode.ForRead) as MText; if (thisText == null) { continue; } LineSegment3d thisTextLine = new LineSegment3d(thisText.Bounds.Value.MinPoint, thisText.Bounds.Value.MaxPoint); Point3d[] intersectionPoints = lineUp.IntersectWith(thisTextLine); if (intersectionPoints == null || intersectionPoints.Length != 1) { continue; } double thisDistance = current.MidPoint.DistanceTo(intersectionPoints[0]); if (thisDistance < distance && !thisText.ObjectId.Equals(text.ObjectId) && current.MidPoint.Y < intersectionPoints[0].Y) { distance = thisDistance; ret = thisText; } } return(ret); }
private static Point3d Intersect(Point3d p1, Point3d p2, Point3d p3, Point3d p4) { using (Line3d l1 = new Line3d(p1, p2)) using (Line3d l2 = new Line3d(p3, p4)) { return(l1.IntersectWith(l2)[0]); } }
public double CalculateEx(Point3d pointLocal) { Point3d point2d = new Point3d(pointLocal.X, pointLocal.Y, 0d); Vector3d perp = point2d - point2d.Add(this.AxisVector.GetPerpendicularVector()); Vector3d project = perp.ProjectTo(this.AxisVector.GetNormal(), this.AxisVector); Line3d line3d = new Line3d(point2d, project); var points = line3d.IntersectWith(new Line3d(Point3d.Origin, this.AxisVector), Tolerance.Global); if (points != null && points.Length > 0) { Line line = new Line(point2d, points[0]); this.LastValue = line.Length * (this.AxisVector.GetPerpendicularVector().IsCodirectionalTo(line.GetFirstDerivative(0d)) ? 1 : -1); if (LastValue < 0d) { this.Mirror(); } if (this.IsCodirectional) { if (BaseArrow.IsRedirected && !this.IsRedirected) { this.Redirect(); } if (!this.IsSymbolsMirrored && !BaseArrow.IsSymbolsMirrored) { BaseArrow.MirrorSymbols(); } } else { if (this.BaseArrow != null) { if (this.BaseArrow.IsRedirected) { this.BaseArrow.Redirect(); if (this.BaseArrow.IsSymbolsMirrored) { this.BaseArrow.MirrorSymbols(); } } else { if (this.BaseArrow.IsSymbolsMirrored) { this.BaseArrow.MirrorSymbols(); } } } } return(LastValue.Value); } return(0d); }
private double _calculateValue(Point3d destPoint) { Point3d point2d = new Point3d(destPoint.X, destPoint.Y, 0d); Vector3d perp = point2d - point2d.Add(this.AxisVector.GetPerpendicularVector()); Vector3d project = perp.ProjectTo(this.AxisVector.GetNormal(), this.AxisVector); Line3d line3d = new Line3d(point2d, project); var points = line3d.IntersectWith(new Line3d(Point3d.Origin, this.AxisVector), Tolerance.Global); if (points != null && points.Length > 0) { Line line = new Line(point2d, points[0]); double res = line.Length * (this.AxisVector.GetPerpendicularVector().IsCodirectionalTo(line.GetFirstDerivative(0d)) ? 1 : -1); return(res); } throw new ArgumentOutOfRangeException(); }
public static void Test() { ObjectId[] objectId = Interaction.GetSelection("\n选择多段线", "LWPOLYLINE");//选择多段线 if (objectId.Length == 0) { return; //一个都没选的情况下退出操作 } objectId.QForEach <Polyline>(polyline => { int length = (int)polyline.EndParam; for (int i = 0; i <= length; i++) { Line l1 = NoDraw.Line(polyline.GetPointAtParam(i), polyline.GetPreviousPointAtParam(i)); Line l2 = NoDraw.Line(polyline.GetPointAtParam(i), polyline.GetNextPointAtParam(i)); // Checks if lines intersect Plane plane; Line3d line1 = new Line3d(l1.StartPoint, l1.EndPoint); Line3d line2 = new Line3d(l2.StartPoint, l2.EndPoint); if (!line1.IsCoplanarWith(line2, out plane) || line1.IsParallelTo(line2)) { return; } // Calculates the bisector Point3d inters = line1.IntersectWith(line2)[0]; Vector3d vec1 = line1.Direction; Vector3d vec2 = line2.Direction; Vector3d bisectDir = ((vec1 + vec2) / 2.0).Negate(); var angle = bisectDir.GetAngleTo(Vector3d.XAxis); Xline xline = new Xline(); xline.UnitDir = bisectDir.GetNormal(); xline.BasePoint = inters; Draw.Text((i + 1).ToString(), 2, xline.GetPointAtDist(1)); Draw.Circle(polyline.GetPointAtParam(i), 0.5); } }); }
private static ApartmentContourInfo GetApartmentContour(IEnumerable <ObjectId> linearIds) { var info = ApartmentContour.CalcContour(Application.DocumentManager.MdiActiveDocument, linearIds); var database = Application.DocumentManager.MdiActiveDocument.Database; // Test code ! using (var transaction = database.TransactionManager.StartTransaction()) { var contourSegments = info.Contour; var innerSegments = info.InternalSegments; // find out windows on wall, usually there are 4 or 3 parallel line segments for a window like below. // ----------------------- // ----------------------- // ----------------------- // ----------------------- const double maxWallWidth = 600; // todo Tolerance tol = new Tolerance(0.001, 0.001); List <LineSegment3d> sideWindowSegments = new List <LineSegment3d>(); List <List <LineSegment3d> > windowGroup = new List <List <LineSegment3d> >(); for (var i = 0; i < innerSegments.Count; i++) { var thisSeg = innerSegments[i]; //var startPt = innerSegments[i].StartPoint; //var endPt = innerSegments[i].EndPoint; // do check if the start and end points are on the wall //var cIndex = contourSegments.FindIndex((seg) => { // if (seg.IsOn(startPt, tol) && seg.IsOn(endPt, tol)) // return true; // return false; //}); // find out if the line segment is on the outer contour wall //var cIndex = contourSegments.FindIndex((seg) => //{ // if (seg.IsParallelTo(innerSegments[i], tol)) // { // var dist = seg.GetDistanceTo(thisSeg.MidPoint); // if (dist < maxWallWidth) // return true; // } // return false; //}); //if (cIndex != -1) { // find out all other parallel and equal length segments with this one var startPt = thisSeg.StartPoint; var endPt = thisSeg.EndPoint; double thisLength = thisSeg.Length; Vector3d direction = thisSeg.Direction.RotateBy(Math.PI / 2, Vector3d.ZAxis); Line3d startLine = new Line3d(startPt, direction); Line3d endLine = new Line3d(endPt, direction); List <LineSegment3d> parEqualSegs = new List <LineSegment3d>(); for (var j = 0; j < innerSegments.Count; j++) { if (i == j || innerSegments[j] == null) { continue; // itself } if (thisSeg.IsParallelTo(innerSegments[j]) && (startLine.IsOn(innerSegments[j].StartPoint, tol) && endLine.IsOn(innerSegments[j].EndPoint, tol)) || (startLine.IsOn(innerSegments[j].EndPoint, tol) && endLine.IsOn(innerSegments[j].StartPoint, tol))) { parEqualSegs.Add(innerSegments[j]); } } if (parEqualSegs.Count > 1) { Line3d helper1 = new Line3d(thisSeg.MidPoint, thisSeg.Direction); Line3d helper2 = new Line3d(thisSeg.MidPoint, direction); var intersects = helper1.IntersectWith(helper2); var basePt = intersects[0]; //direction = (thisSeg.MidPoint - basePt).GetNormal(); // sort them by direction parEqualSegs.Add(thisSeg); parEqualSegs.Sort((seg1, seg2) => { Vector3d vector1 = seg1.MidPoint - basePt; Vector3d vector2 = seg2.MidPoint - basePt; if (Math.Abs(vector1.DotProduct(direction) - vector2.DotProduct(direction)) < 0.001) { return(0); } else if (vector1.DotProduct(direction) > vector2.DotProduct(direction)) { return(1); } return(-1); }); List <LineSegment3d> thisWindow = new List <LineSegment3d>(); double distance = parEqualSegs[1].MidPoint.DistanceTo(parEqualSegs[0].MidPoint); if (distance < maxWallWidth / 2) { thisWindow.Add(parEqualSegs[0]); thisWindow.Add(parEqualSegs[1]); for (var k = 2; k < parEqualSegs.Count; k++) { var dist = parEqualSegs[k].MidPoint.DistanceTo(parEqualSegs[k - 1].MidPoint); if (thisWindow.Count > 4 || Math.Abs(dist - distance) > 100) { // we have find out 4 parallel lines or the distance is not equal to previous one break; } thisWindow.Add(parEqualSegs[k]); } if (thisWindow.Count > 2) { // this will be treaded as a valid window windowGroup.Add(thisWindow); } } } } } innerSegments.RemoveAll((seg) => { var index = windowGroup.FindIndex((window) => { return(window.Contains(seg)); }); if (index != -1) { return(true); } return(false); }); // end window var modelspaceId = SymbolUtilityServices.GetBlockModelSpaceId(database); var modelspace = (BlockTableRecord)transaction.GetObject(modelspaceId, OpenMode.ForWrite); var color1 = Color.FromColorIndex(ColorMethod.ByAci, 1); // 1: red ObjectId layerId = LayerUtils.AddNewLayer(database, "temp-poly", "Continuous", color1); for (var i = 0; i < windowGroup.Count; i++) { var window = windowGroup[i]; for (var j = 0; j < window.Count; j++) { var start = window[j].StartPoint; var end = window[j].EndPoint; var line = new Line(start, end); line.Color = color1; line.LayerId = layerId; modelspace.AppendEntity(line); transaction.AddNewlyCreatedDBObject(line, add: true); } } var color = Color.FromColorIndex(ColorMethod.ByAci, 3); // Green var polyline = new Polyline(); for (var i = 0; i < contourSegments.Count; i++) { var segment = contourSegments[i]; var start = segment.StartPoint; polyline.AddVertexAt(i, new Point2d(start.X, start.Y), 0, 0, 0); } polyline.Closed = true; polyline.Color = color; polyline.LayerId = layerId; modelspace.AppendEntity(polyline); transaction.AddNewlyCreatedDBObject(polyline, add: true); foreach (var segment in innerSegments) { var line = new Line(segment.StartPoint, segment.EndPoint); line.Color = color; line.LayerId = layerId; modelspace.AppendEntity(line); transaction.AddNewlyCreatedDBObject(line, add: true); } transaction.Commit(); } return(info); }
public void Bisector() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; PromptEntityOptions peo = new PromptEntityOptions("\nSelect the first line: "); peo.SetRejectMessage("Selected object is not a line !"); peo.AddAllowedClass(typeof(Line), true); PromptEntityResult per = ed.GetEntity(peo); if (per.Status != PromptStatus.OK) { return; } Point3d p1 = per.PickedPoint.TransformBy(ed.CurrentUserCoordinateSystem); ObjectId id1 = per.ObjectId; peo.Message = "\nSelect the second line: "; per = ed.GetEntity(peo); if (per.Status != PromptStatus.OK) { return; } Point3d p2 = per.PickedPoint.TransformBy(ed.CurrentUserCoordinateSystem); ObjectId id2 = per.ObjectId; using (Transaction tr = db.TransactionManager.StartTransaction()) { Line l1 = (Line)tr.GetObject(id1, OpenMode.ForRead); Line l2 = (Line)tr.GetObject(id2, OpenMode.ForRead); // Checks if lines intersect Plane plane; Line3d line1 = new Line3d(l1.StartPoint, l1.EndPoint); Line3d line2 = new Line3d(l2.StartPoint, l2.EndPoint); if (!line1.IsCoplanarWith(line2, out plane) || line1.IsParallelTo(line2)) { return; } // Calculates the bisector Point3d inters = line1.IntersectWith(line2)[0]; Vector3d vec1 = line1.Direction; Vector3d vec2 = line2.Direction; // Corrects the vectors direction according to picked points if (vec1.DotProduct(inters.GetVectorTo(p1)) < 0) { vec1 = vec1.Negate(); } if (vec2.DotProduct(inters.GetVectorTo(p2)) < 0) { vec2 = vec2.Negate(); } Vector3d bisectDir = (vec1 + vec2) / 2.0; // Draws the bisector (XLine) Xline xline = new Xline(); xline.UnitDir = bisectDir.GetNormal(); xline.BasePoint = inters; BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite); btr.AppendEntity(xline); tr.AddNewlyCreatedDBObject(xline, true); tr.Commit(); } }