//-------------------------------------------------------------------------------------------------- public Pnt2d?SnapOnPlane(SnapInfo snapInfo, Pln?localPlane = null) { if (snapInfo != null) { var plane = localPlane ?? _WorkspaceController.Workspace.WorkingPlane; switch (snapInfo.SnapMode) { case SnapMode.Grid: case SnapMode.Vertex: case SnapMode.Edge: return(ProjLib.Project(plane, snapInfo.Point)); } } return(null); }
//-------------------------------------------------------------------------------------------------- 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); }