//-------------------------------------------------------------------------------------------------- void _ExportEdges(List <TopoDS_Edge> edges, TopoDS_Face face) { // Order edges var order = new ShapeAnalysis_WireOrder(true, 0.0001); foreach (var edge in edges) { var first = BRep_Tool.Pnt(TopExp.FirstVertex(edge)); var last = BRep_Tool.Pnt(TopExp.LastVertex(edge)); if (edge.Orientation() == TopAbs_Orientation.TopAbs_FORWARD) { order.Add(first.Coord, last.Coord); } else { order.Add(last.Coord, first.Coord); } } order.Perform(true); if (order.IsDone()) { order.SetChains(0.0001); for (int chain = 1; chain <= order.NbChains(); chain++) { int startIndex = 0, endIndex = 0; order.Chain(chain, ref startIndex, ref endIndex); // Process ordered edges for (int i = startIndex; i <= endIndex; i++) { int orderIndex = order.Ordered(i); int originalIndex = Math.Abs(orderIndex) - 1; // order index is 1-based _ExportEdge(edges[originalIndex], orderIndex < 0, face); } // Add path to group ClosePath(); if (!CombineToPath) { FinalizePath(); } } } else { // Cannot sort, just pump out all edges foreach (var edge in edges) { _ExportEdge(edge, false, face); } // Add path to group if ((CurrentPath != null) && CurrentPath.Segments.Any()) { CurrentGroup.Children.Add(CurrentPath); } } }
//-------------------------------------------------------------------------------------------------- void _AddVertexProperties(TopoDS_Vertex vertex) { const string cat = "Vertex"; var pnt = BRep_Tool.Pnt(vertex); _AddProperty(cat, "Pnt", $"({pnt.X.ToRoundedString()}, {pnt.Y.ToRoundedString()}, {pnt.Z.ToRoundedString()})"); _AddProperty(cat, "Tolerance", $"{BRep_Tool.Tolerance(vertex)}"); }
//-------------------------------------------------------------------------------------------------- void _OnActionFinished(ToolAction toolAction) { bool finished = false; var selectAction = toolAction as SelectSubshapeAction; Debug.Assert(selectAction != null); if (selectAction.SelectedSubshapeType == SubshapeTypes.Face) { var face = TopoDS.Face(selectAction.SelectedSubshape); var brepAdaptor = new BRepAdaptor_Surface(face, true); if (brepAdaptor.GetGeomType() != GeomAbs_SurfaceType.GeomAbs_Plane) { StatusText = "Selected face is not a plane type surface."; } else { double centerU = brepAdaptor.FirstUParameter() + (brepAdaptor.LastUParameter() - brepAdaptor.FirstUParameter()) / 2; double centerV = brepAdaptor.FirstVParameter() + (brepAdaptor.LastVParameter() - brepAdaptor.FirstVParameter()) / 2; var centerPnt = brepAdaptor.Value(centerU, centerV); WorkspaceController.Workspace.WorkingPlane = new Pln(centerPnt, brepAdaptor.Plane().Axis.Direction); finished = true; } } else if (selectAction.SelectedSubshapeType == SubshapeTypes.Edge) { var edge = TopoDS.Edge(selectAction.SelectedSubshape); double firstParam = 0, lastParam = 0; var curve = BRep_Tool.Curve(edge, ref firstParam, ref lastParam); if (curve != null) { var midpoint = curve.Value(firstParam + (lastParam - firstParam) / 2); WorkspaceController.Workspace.WorkingPlane = new Pln(midpoint, WorkspaceController.Workspace.WorkingPlane.Axis.Direction); finished = true; } } else if (selectAction.SelectedSubshapeType == SubshapeTypes.Vertex) { var vertex = TopoDS.Vertex(selectAction.SelectedSubshape); WorkspaceController.Workspace.WorkingPlane = new Pln(BRep_Tool.Pnt(vertex), WorkspaceController.Workspace.WorkingPlane.Axis.Direction); finished = true; } if (finished) { selectAction.Stop(); Stop(); } else { selectAction.Reset(); } WorkspaceController.Invalidate(); }
//-------------------------------------------------------------------------------------------------- public static bool RenderEdges(IDrawingRenderer renderer, List <TopoDS_Edge> edges, TopoDS_Face face) { var res = true; // Order edges var order = new ShapeAnalysis_WireOrder(true, 0.0001); foreach (var edge in edges) { var first = BRep_Tool.Pnt(TopExp.FirstVertex(edge)); var last = BRep_Tool.Pnt(TopExp.LastVertex(edge)); if (edge.Orientation() == TopAbs_Orientation.TopAbs_FORWARD) { order.Add(first.Coord, last.Coord); } else { order.Add(last.Coord, first.Coord); } } order.Perform(true); if (order.IsDone()) { order.SetChains(0.0001); for (int chain = 1; chain <= order.NbChains(); chain++) { int startIndex = 0, endIndex = 0; order.Chain(chain, ref startIndex, ref endIndex); if (startIndex > endIndex) { continue; } // Process ordered edges renderer.BeginPathSegment(); for (int index = startIndex; index <= endIndex; index++) { int orderIndex = order.Ordered(index); int originalIndex = Math.Abs(orderIndex) - 1; // order index is 1-based res &= RenderEdge(renderer, edges[originalIndex], orderIndex < 0, face); } renderer.EndPathSegment(); } } else { // Cannot sort, just pump out all edges foreach (var edge in edges) { res &= RenderEdge(renderer, edge, false, face); } } return(res); }
public static FaceIntersectionLineResult IntersectLine(this TopoDS_Face face, TopoDS_Face other, double tol) { var s1 = face.Surface(); var s2 = other.Surface(); var a = new GeomAPI_IntSS(s1, s2, tol); var C = a.Line(1); var edge = new BRepBuilderAPI_MakeEdge(C, C.FirstParameter(), C.LastParameter()); var v1 = edge.Vertex1(); var v2 = edge.Vertex2(); var i1 = BRep_Tool.Pnt(v1); var i2 = BRep_Tool.Pnt(v2); return(new FaceIntersectionLineResult(C, new Line3D(i1.ToVector3D(), i2.ToVector3D()))); }
//-------------------------------------------------------------------------------------------------- public SnapInfo Snap(MouseEventData mouseEvent) { if (!InteractiveContext.Current.EditorState.SnappingEnabled) { return(null); } SnapInfo info = null; if (mouseEvent.DetectedShapes.Count == 1) { var detectedShape = mouseEvent.DetectedShapes[0]; if (SupportedSnapModes.HasFlag(SnapMode.Vertex) && InteractiveContext.Current.EditorState.SnapToVertexSelected && (detectedShape.ShapeType() == TopAbs_ShapeEnum.TopAbs_VERTEX)) { // On Vertex var vertex = TopoDS.Vertex(detectedShape); info = new SnapInfo() { Point = BRep_Tool.Pnt(vertex), SnapMode = SnapMode.Vertex }; } else if (SupportedSnapModes.HasFlag(SnapMode.Edge) && InteractiveContext.Current.EditorState.SnapToEdgeSelected && (detectedShape.ShapeType() == TopAbs_ShapeEnum.TopAbs_EDGE)) { // On Edge var edge = TopoDS.Edge(detectedShape); double umin = 0, umax = 0; var curve = BRep_Tool.Curve(edge, ref umin, ref umax); if (curve != null) { var extrema = new GeomAPI_ExtremaCurveCurve(curve, new Geom_Line(_WorkspaceController.ActiveViewport.ViewAxis(Convert.ToInt32(mouseEvent.ScreenPoint.X), Convert.ToInt32(mouseEvent.ScreenPoint.Y)))); if (extrema.NbExtrema() >= 1) { Pnt p1 = new Pnt(); Pnt p2 = new Pnt(); if (extrema.TotalNearestPoints(ref p1, ref p2)) { info = new SnapInfo() { Point = p1, SnapMode = SnapMode.Edge }; } } } } } else if (mouseEvent.DetectedAisInteractives.Count == 1) { if (SupportedSnapModes.HasFlag(SnapMode.Vertex) && InteractiveContext.Current.EditorState.SnapToVertexSelected && (mouseEvent.DetectedAisInteractives[0] is AIS_Point aisPoint)) { // On Vertex info = new SnapInfo() { Point = aisPoint.Component().Pnt(), SnapMode = SnapMode.Vertex }; } } else if (SupportedSnapModes.HasFlag(SnapMode.Grid) && InteractiveContext.Current.EditorState.SnapToGridSelected && _WorkspaceController.Workspace.V3dViewer.Grid().IsActive()) { // On Grid info = new SnapInfo() { Point = _WorkspaceController.ActiveViewport.ProjectToGrid(Convert.ToInt32(mouseEvent.ScreenPoint.X), Convert.ToInt32(mouseEvent.ScreenPoint.Y)), SnapMode = SnapMode.Grid }; } if (info != null) { _WorkspaceController.CursorPosition = info.Point; } return(info); }
//-------------------------------------------------------------------------------------------------- public static Pnt Pnt(this TopoDS_Vertex vertex) { return(vertex == null ? new Pnt() : BRep_Tool.Pnt(vertex)); }