//--------------------------------------------------------------------------------------------------

        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 Geom_Curve Curve(this TopoDS_Edge edge, out double firstParam, out double lastParam)
        {
            firstParam = lastParam = 0;
            if (edge == null)
            {
                return(null);
            }

            return(BRep_Tool.Curve(edge, ref firstParam, ref lastParam));
        }
Beispiel #3
0
        //--------------------------------------------------------------------------------------------------

        double _FindEdgeParam(TopoDS_Edge edge, SelectSubshapeAction selectAction)
        {
            // Calculate parameter on the edge
            double umin = 0, umax = 0;
            var    curve    = BRep_Tool.Curve(edge, ref umin, ref umax);
            var    viewAxis = selectAction.LastMouseEventData.PickAxis;
            var    extrema  = new GeomAPI_ExtremaCurveCurve(curve, new Geom_Line(viewAxis));

            if (extrema.NbExtrema() > 0)
            {
                double param1 = 0, param2 = 0;
                extrema.LowerDistanceParameters(ref param1, ref param2);
                return(param1);
            }
            else
            {
                return(Taper.CalculateBaseParameter(edge, 0.5));
            }
        }
Beispiel #4
0
        //--------------------------------------------------------------------------------------------------

        void _AddEdgeProperties(TopoDS_Edge edge)
        {
            const string edgecat  = "Edge";
            const string curvecat = "Curve";

            if (Shape != null)
            {
                var subshapeRef = Shape.GetSubshapeReference(_TopLevelShape, edge);
                _AddProperty(edgecat, "SubshapeRef", subshapeRef?.ToString() ?? "null");
            }

            var flags = "";

            if (BRep_Tool.Degenerated(edge))
            {
                flags += "Degenerated ";
            }
            if (BRep_Tool.SameParameter(edge))
            {
                flags += "SameParameter ";
            }
            if (BRep_Tool.SameRange(edge))
            {
                flags += "SameRange ";
            }

            _AddProperty(edgecat, "Is Closed", $"{(BRep_Tool.IsClosed(edge) ? "Yes" : "No")}");
            _AddProperty(edgecat, "Curve Type", $"{(BRep_Tool.IsGeometric(edge) ? "Geometric Curve" : "Curve on Surface")}");

            var props = new GProp_GProps();

            BRepGProp.LinearProperties(BrepShape, props);
            _AddProperty(edgecat, "Length", $"{props.Mass()}");

            _AddProperty(edgecat, "Tolerance", $"{BRep_Tool.Tolerance(edge)}");
            if (!flags.IsEmpty())
            {
                _AddProperty(edgecat, "Flags", flags);
            }

            if (BRep_Tool.IsGeometric(edge))
            {
                // 3D curve
                double first = 0, last = 0;
                var    curve = BRep_Tool.Curve(edge, ref first, ref last);
                if (curve != null)
                {
                    _AddProperty(edgecat, "Parameter", $"({first}, {last})");
                    _AddProperty(curvecat, "Class", curve.GetType().Name.Replace("Geom_", ""));
                    _AddProperty(curvecat, "Is Closed", $"{(curve.IsClosed() ? "Yes" : "No")}");
                    if (curve.IsPeriodic())
                    {
                        _AddProperty(curvecat, "Period", $"{curve.Period()}");
                    }
                    _AddProperty(curvecat, "Continuity", curve.Continuity().ToString().Replace("GeomAbs_", ""));

                    switch (curve)
                    {
                    case Geom_Line line:
                        const string linecat = "Line";
                        var          lineLoc = line.Position().Location;
                        _AddProperty(linecat, "Location", $"({lineLoc.X.ToRoundedString()}, {lineLoc.Y.ToRoundedString()}, {lineLoc.Z.ToRoundedString()})");
                        var lineDir = line.Position().Direction;
                        _AddProperty(linecat, "Direction", $"({lineDir.X.ToRoundedString()}, {lineDir.Y.ToRoundedString()}, {lineDir.Z.ToRoundedString()})");
                        break;

                    case Geom_Circle circle:
                        const string circlecat = "Circle";
                        _AddProperty(circlecat, "Radius", $"{circle.Radius().ToRoundedString()}");
                        var circleLoc = circle.Position().Location;
                        _AddProperty(circlecat, "Location", $"({circleLoc.X.ToRoundedString()}, {circleLoc.Y.ToRoundedString()}, {circleLoc.Z.ToRoundedString()})");
                        var circleDir = circle.Position().Direction;
                        _AddProperty(circlecat, "Direction", $"({circleDir.X.ToRoundedString()}, {circleDir.Y.ToRoundedString()}, {circleDir.Z.ToRoundedString()})");
                        var circleXDir = circle.Position().XDirection;
                        _AddProperty(circlecat, "X-Direction", $"({circleXDir.X.ToRoundedString()}, {circleXDir.Y.ToRoundedString()}, {circleXDir.Z.ToRoundedString()})");
                        var circleYDir = circle.Position().YDirection;
                        _AddProperty(circlecat, "Y-Direction", $"({circleYDir.X.ToRoundedString()}, {circleYDir.Y.ToRoundedString()}, {circleYDir.Z.ToRoundedString()})");
                        break;

                    case Geom_Ellipse ellipse:
                        const string ellipsecat = "Ellipse";
                        _AddProperty(ellipsecat, "Major Radius", $"{ellipse.MajorRadius().ToRoundedString()}");
                        _AddProperty(ellipsecat, "Minor Radius", $"{ellipse.MinorRadius().ToRoundedString()}");
                        _AddProperty(ellipsecat, "Eccentricity", $"{ellipse.Eccentricity().ToRoundedString()}");
                        _AddProperty(ellipsecat, "Focal", $"{ellipse.Focal().ToRoundedString()}");
                        var ellipseFocus = ellipse.Focus1();
                        _AddProperty(ellipsecat, "Focus 1", $"({ellipseFocus.X.ToRoundedString()}, {ellipseFocus.Y.ToRoundedString()}, {ellipseFocus.Z.ToRoundedString()})");
                        ellipseFocus = ellipse.Focus2();
                        _AddProperty(ellipsecat, "Focus 2", $"({ellipseFocus.X.ToRoundedString()}, {ellipseFocus.Y.ToRoundedString()}, {ellipseFocus.Z.ToRoundedString()})");
                        var ellipseLoc = ellipse.Position().Location;
                        _AddProperty(ellipsecat, "Location", $"({ellipseLoc.X.ToRoundedString()}, {ellipseLoc.Y.ToRoundedString()}, {ellipseLoc.Z.ToRoundedString()})");
                        var ellipseDir = ellipse.Position().Direction;
                        _AddProperty(ellipsecat, "Direction", $"({ellipseDir.X.ToRoundedString()}, {ellipseDir.Y.ToRoundedString()}, {ellipseDir.Z.ToRoundedString()})");
                        var ellipseXDir = ellipse.Position().XDirection;
                        _AddProperty(ellipsecat, "X-Direction", $"({ellipseXDir.X.ToRoundedString()}, {ellipseXDir.Y.ToRoundedString()}, {ellipseXDir.Z.ToRoundedString()})");
                        var ellipseYDir = ellipse.Position().YDirection;
                        _AddProperty(ellipsecat, "Y-Direction", $"({ellipseYDir.X.ToRoundedString()}, {ellipseYDir.Y.ToRoundedString()}, {ellipseYDir.Z.ToRoundedString()})");
                        break;

                    case Geom_BezierCurve bezier:
                        const string beziercat = "Bézier Curve";
                        _AddProperty(beziercat, "Degree", $"{bezier.Degree()}");
                        _AddProperty(beziercat, "Pole Count", $"{bezier.NbPoles()}");
                        _AddProperty(beziercat, "Is Rational", $"{(bezier.IsRational() ? "Yes" : "No")}");
                        break;

                    case Geom_BSplineCurve bspline:
                        const string bsplinecat = "B-Spline Curve";
                        _AddProperty(bsplinecat, "Degree", $"{bspline.Degree()}");
                        _AddProperty(bsplinecat, "Pole Count", $"{bspline.NbPoles()}");
                        _AddProperty(bsplinecat, "Knoe Count", $"{bspline.NbKnots()}");
                        _AddProperty(bsplinecat, "Knot Distrib.", bspline.KnotDistribution().ToString().Replace("GeomAbs_", ""));
                        _AddProperty(bsplinecat, "Is Rational", $"{(bspline.IsRational() ? "Yes" : "No")}");
                        break;
                    }
                }
            }
            else
            {
                // Curve on surface, currently not supported
            }

            // Get continuity information
            var(face1, face2) = EdgeAlgo.FindAdjacentFaces(_TopLevelShape, edge);
            if (face1 != null && face2 != null)
            {
                _AddProperty(edgecat, "Face Contin.", BRep_Tool.Continuity(edge, face1, face2).ToString().Replace("GeomAbs_", ""));
            }
        }
Beispiel #5
0
        //--------------------------------------------------------------------------------------------------

        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);
        }