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

        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();
        }
예제 #2
0
        public static TopoDSShape ExtractSubShape(
            TopoDSShape originalShape,
            int facePosition,
            TopAbsShapeEnum shapeType)
        {
            if (originalShape == null)
            {
                return(null);
            }
            // Find the face
            var baseEx = new TopExpExplorer();

            baseEx.Init(originalShape, shapeType, TopAbsShapeEnum.TopAbs_SHAPE);

            while (baseEx.More && (facePosition != 1))
            {
                baseEx.Next();
                facePosition--;
            }
            //if (!baseEx.More())
            //    return null;
            TopoDSShape shape = null;

            switch (shapeType)
            {
            case TopAbsShapeEnum.TopAbs_VERTEX:
                shape = TopoDS.Vertex(baseEx.Current);
                break;

            case TopAbsShapeEnum.TopAbs_EDGE:
                shape = TopoDS.Edge(baseEx.Current);
                break;

            case TopAbsShapeEnum.TopAbs_WIRE:
                shape = TopoDS.Wire(baseEx.Current);
                break;

            case TopAbsShapeEnum.TopAbs_FACE:
                shape = TopoDS.Face(baseEx.Current);
                break;

            case TopAbsShapeEnum.TopAbs_SOLID:
            case TopAbsShapeEnum.TopAbs_COMPOUND:
                return(originalShape);
            }
            return(shape);
        }
예제 #3
0
        //--------------------------------------------------------------------------------------------------

        #endregion

        #region Property List

        void _InitProperties()
        {
            if (_Properties != null)
            {
                return;
            }

            _Properties = new List <BRepTopologyTreeProperty>();
            try
            {
                _AddDefaultProperties();

                switch (BrepShape.ShapeType())
                {
                case TopAbs_ShapeEnum.TopAbs_SHELL:
                    _AddShellProperties(BrepShape as TopoDS_Shell ?? TopoDS.Shell(BrepShape));
                    break;

                case TopAbs_ShapeEnum.TopAbs_FACE:
                    _AddFaceProperties(BrepShape as TopoDS_Face ?? TopoDS.Face(BrepShape));
                    break;

                case TopAbs_ShapeEnum.TopAbs_WIRE:
                    _AddWireProperties(BrepShape as TopoDS_Wire ?? TopoDS.Wire(BrepShape));
                    break;

                case TopAbs_ShapeEnum.TopAbs_EDGE:
                    _AddEdgeProperties(BrepShape as TopoDS_Edge ?? TopoDS.Edge(BrepShape));
                    break;

                case TopAbs_ShapeEnum.TopAbs_VERTEX:
                    _AddVertexProperties(BrepShape as TopoDS_Vertex ?? TopoDS.Vertex(BrepShape));
                    break;
                }
            }
            catch (Exception e)
            {
                Messages.Exception($"Error getting properties for B-Rep shape {Name}", e);
            }
        }
예제 #4
0
        //--------------------------------------------------------------------------------------------------

        public static List <TopoDS_Vertex> Vertices(this TopoDS_Shape shape, bool distinct = true)
        {
            var faces = new List <TopoDS_Vertex>();

            var exp = new TopExp_Explorer(shape, TopAbs_ShapeEnum.TopAbs_VERTEX, TopAbs_ShapeEnum.TopAbs_SHAPE);

            while (exp.More())
            {
                var vertex = TopoDS.Vertex(exp.Current());
                exp.Next();

                if (distinct)
                {
                    if (faces.Any(e => e.IsSame(vertex)))
                    {
                        continue;
                    }
                }
                faces.Add(vertex);
            }
            return(faces);
        }
예제 #5
0
        /// <summary>
        ///   Apply fillet on a list of wires. The common endpoints of wires are considered the fillet vertexes.
        /// </summary>
        private static TopoDSWire ApplyFilletOnWires(IEnumerable <SceneSelectedEntity> filletNodes, double radius,
                                                     int filletChamferType)
        {
            // This method processes only fillet2D and chamfer2D operations
            if ((filletChamferType != (int)FilletChamferTypes.SimpleFillet2D) &&
                (filletChamferType != (int)FilletChamferTypes.SimpleChamfer2D))
            {
                return(null);
            }

            try
            {
                // Make a face fom the wires
                var wires = new List <SceneSelectedEntity>();
                foreach (var node in filletNodes)
                {
                    wires.Add(node);
                }
                var face = MakeFaceFunction.ComposeWires(wires, true);

                if ((face == null) || (face.IsNull))
                {
                    return(null);
                }

                var fillet = new BRepFilletAPIMakeFillet2d();
                // Initialize a fillet with the face made from the 2 wires
                fillet.Init(face);

                // Fillet the common vertexes
                Node previousNode = null;
                foreach (var node in filletNodes)
                {
                    if (previousNode != null)
                    {
                        var wire1 = previousNode.Get <NamedShapeInterpreter>().Shape;
                        var wire2 = node.Node.Get <NamedShapeInterpreter>().Shape;
                        var listOfCommonVertex = GeomUtils.CommonVertexes(wire1, wire2);
                        if (listOfCommonVertex.Count >= 1)
                        {
                            foreach (var vertex in listOfCommonVertex)
                            {
                                if (filletChamferType == (int)FilletChamferTypes.SimpleFillet2D)
                                {
                                    // If the operation is a fillet
                                    fillet.AddFillet(vertex, radius);
                                }
                                else
                                {
                                    // Map all edges to faces
                                    var map = new TopToolsIndexedDataMapOfShapeListOfShape(1);
                                    TopExp.MapShapesAndAncestors(wire1, TopAbsShapeEnum.TopAbs_VERTEX,
                                                                 TopAbsShapeEnum.TopAbs_EDGE, map);

                                    // Locate an ancestor face
                                    for (var i = 1; i <= map.Extent; i++)
                                    {
                                        var localVertex = TopoDS.Vertex(map.FindKey(i));
                                        if (!vertex.IsSame(localVertex))
                                        {
                                            continue;
                                        }
                                        // We found an ancestor edge
                                        var edge = TopoDS.Edge(map.FindFromIndex(i).First);
                                        // Add the vertex and edge on the chamfer algorithm
                                        //fillet.AddChamfer(TopoDS.Edge(edge), TopoDS.Edge(edge2), radius, radius);
                                        fillet.AddChamfer(TopoDS.Edge(edge), vertex, radius,
                                                          GeomUtils.DegreesToRadians(45));
                                    }
                                }
                            }
                        }
                        else
                        {
                            return(null);
                        }
                    }
                    previousNode = node.Node;
                }

                // Test if the operation succeeded
                if (fillet.Status != ChFi2dConstructionError.ChFi2d_IsDone)
                {
                    return(null);
                }

                var shape = fillet.Shape;
                if ((shape == null) || (shape.IsNull))
                {
                    return(null);
                }

                var aMap = new TopToolsIndexedMapOfShape(1);
                TopExp.MapShapes(fillet.Shape, TopAbsShapeEnum.TopAbs_WIRE, aMap);
                if (aMap.Extent != 1)
                {
                    return(null);
                }

                var newWire = new BRepBuilderAPIMakeWire();
                var ex      = new BRepToolsWireExplorer(TopoDS.Wire(aMap.FindKey(1)));
                for (; ex.More; ex.Next())
                {
                    newWire.Add(ex.Current);
                }

                return(newWire.Wire);
            }
            catch (Exception ex)
            {
                Log.Error("Apply Fillet2D error: " + ex.Message);
            }

            return(null);
        }
예제 #6
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);
        }
예제 #7
0
        //--------------------------------------------------------------------------------------------------

        public static TopoDS_Vertex ToVertex(this TopoDS_Shape shape)
        {
            return(shape == null ? null : TopoDS.Vertex(shape));
        }
예제 #8
0
 public static gpPnt ConvertShapeToPoint(TopoDSShape shape)
 {
     return(shape == null ? null : BRepTool.Pnt(TopoDS.Vertex(shape)));
 }