Example #1
0
        //--------------------------------------------------------------------------------------------------

        public static List <TopoDS_Wire> Wires(this TopoDS_Shape shape, bool distinct = true)
        {
            var wires = new List <TopoDS_Wire>();

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

            while (exp.More())
            {
                var wire = TopoDS.Wire(exp.Current());
                exp.Next();

                if (distinct)
                {
                    var otherEdgeIndex = wires.FindIndex(e => e.IsSame(wire));
                    if (otherEdgeIndex >= 0)
                    {
                        if (wires[otherEdgeIndex].Orientation() == TopAbs_Orientation.TopAbs_REVERSED &&
                            wire.Orientation() == TopAbs_Orientation.TopAbs_FORWARD)
                        {
                            // Replace with forward wire, this is prefered
                            wires[otherEdgeIndex] = wire;
                        }

                        // Edge already present or replaced, skip adding
                        continue;
                    }
                }

                wires.Add(wire);
            }
            return(wires);
        }
Example #2
0
        public static TopoDSFace ComposeWires(List <SceneSelectedEntity> cutShapes, bool skipFaceValidityCheck)
        {
            if (cutShapes.Count > 0)
            {
                alreadyDone.Add(cutShapes[0].Node);
                var mkWire = new BRepBuilderAPIMakeWire();
                foreach (var cutShape in cutShapes)
                {
                    var node  = cutShape.Node;
                    var shape = node.Get <TopoDsShapeInterpreter>().Shape;
                    if (shape == null)
                    {
                        break;
                    }
                    if (shape.ShapeType != TopAbsShapeEnum.TopAbs_WIRE)
                    {
                        continue;
                    }
                    var wire = TopoDS.Wire(shape);
                    mkWire.Add(wire);
                }

                if (mkWire.IsDone)
                {
                    // If the wire generation succeeded generate a Face from it
                    var wireProfile = mkWire.Wire;
                    if (!wireProfile.IsNull)
                    {
                        var faceProfile = new BRepBuilderAPIMakeFace(wireProfile, true).Face;
                        if (!faceProfile.IsNull)
                        {
                            // The face is generated not checking if it is a valid face
                            if (skipFaceValidityCheck)
                            {
                                return(faceProfile);
                            }

                            // Check if it is a valid shape
                            var analyzer = new BRepCheckAnalyzer(faceProfile, true);
                            if (analyzer.IsValid())
                            {
                                return(faceProfile);
                            }
                        }
                    }
                }
            }

            return(null);
        }
 private static TopoDSShape MakePipe(TopoDSShape path, TopoDSShape profile)
 {
     try
     {
         var baseEx = new TopExpExplorer();
         baseEx.Init(path, TopAbsShapeEnum.TopAbs_WIRE, TopAbsShapeEnum.TopAbs_SHAPE);
         var spline = TopoDS.Wire(baseEx.Current);
         return(new BRepOffsetAPIMakePipe(spline, profile).Shape);
     }
     catch (Exception ex)
     {
         Log.Error("Error on making pipe: " + ex.Message);
         return(null);
     }
 }
Example #4
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);
        }
Example #5
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);
            }
        }
Example #6
0
        public override bool Execute()
        {
            // Get the extrusion referenece shape, height and extrusion type
            var offsetShape  = Dependency[0].ReferedShape;
            var offsetLength = Dependency[1].Real;

            // Don't allow 0 offset
            if (Math.Abs(offsetLength) < Precision.Confusion)
            {
                return(false);
            }

            //TopoDSShape resultShape = NodeUtils.GenerateWireOffset(offsetShapes, offsetLength);
            // ! Currently only offset on 2D closed shapes of type Face can be made
            if (offsetShape.ShapeType != TopAbsShapeEnum.TopAbs_FACE)
            {
                return(false);
            }

            var face        = TopoDS.Face(offsetShape);
            var resultShape = GeomUtils.GenerateFaceOffset(face, offsetLength);

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

            var wire       = TopoDS.Wire(resultShape);
            var offsetFace = new BRepBuilderAPIMakeFace(wire, false).Face;

            if (offsetFace.IsNull)
            {
                return(false);
            }

            // Set the result in the NamedShapeInterpreter and make the shape visible
            Shape = offsetFace;

            return(true);
        }
Example #7
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);
        }
Example #8
0
        //--------------------------------------------------------------------------------------------------

        public static TopoDS_Wire ToWire(this TopoDS_Shape shape)
        {
            return(shape == null ? null : TopoDS.Wire(shape));
        }