public static TriangleMesh Process(TopoDSShape shapeFused)
        {
            var result  = new TriangleMesh();
            var builder = new BRepBuilder();
            var comp    = new TopoDSCompound();

            builder.MakeCompound(comp);
            BRepMesh.Mesh(shapeFused, 1);
            var ex = new TopExpExplorer(shapeFused,
                                        TopAbsShapeEnum.TopAbs_FACE,
                                        TopAbsShapeEnum.TopAbs_SHAPE);

            while (ex.More)
            {
                var shapeResult = new TriangleMesh();
                var face        = TopoDS.Face(ex.Current);
                var location    = new TopLocLocation();
                var facing      = BRepTool.Triangulation(face, location);
                var tab         = new TColgpArray1OfPnt(1, facing.NbNodes);
                //  facing.Nodes(tab);
                var tri = new PolyArray1OfTriangle(1, facing.NbTriangles);
                //  facing.Triangles(tri);
                var triCount   = facing.NbTriangles;
                var listPoints = new List <Point3D>();
                for (var i = 1; i <= triCount; i++)
                {
                    var trian = tri.Value(i);
                    int index1 = 0, index2 = 0, index3 = 0;
                    trian.Get(ref index1, ref index2, ref index3);

                    var firstPoint  = new Point3D(tab.Value(index1));
                    var secondPoint = new Point3D(tab.Value(index2));
                    var thirdPoint  = new Point3D(tab.Value(index3));
                    listPoints.Add(firstPoint);
                    listPoints.Add(secondPoint);
                    listPoints.Add(thirdPoint);
                }
                shapeResult.Points.Clear();
                foreach (var point in listPoints)
                {
                    shapeResult.Points.Add(point);
                }
                shapeResult.Points = GeomUtils.SortAndCompactListPoints(shapeResult.Points);
                var triangleIndex = 0;
                var triangleArray = new int[3];
                foreach (var point in listPoints)
                {
                    triangleArray[triangleIndex] = shapeResult.ComputePointId(point);
                    triangleIndex = (triangleIndex + 1) % 3;
                    if (triangleIndex == 0)
                    {
                        shapeResult.AddTriangle(triangleArray);
                    }
                }
                ex.Next();
                result = result.Combine(shapeResult);
            }
            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Makes a list with the coliding candidates, generates a hidden fused face from them
        /// then generates visible face nodes pointing to the fused shapes
        /// </summary>
        public static List <Node> UpdateFaces(Document _document, List <Node> _candidates)
        {
            _generated.Clear();
            var colidingShapes = new List <Node>();

            DetectColidingCandidates(_candidates, colidingShapes);
            CreateFusedShape(colidingShapes, _document);

            int faceNumber;
            var faceExplorer = new TopExpExplorer();

            // Build SubShapes with the Faces resulted after intersecting the candidates
            if (_nodeFused != null)
            {
                var bigFace = _nodeFused.Get <TopoDsShapeInterpreter>().Shape;
                faceNumber = 1;

                faceExplorer.Init(bigFace, TopAbsShapeEnum.TopAbs_FACE, TopAbsShapeEnum.TopAbs_SHAPE);

                while (faceExplorer.More)
                {
                    var _faceNode = BuildSubShape(_document, _nodeFused, TopAbsShapeEnum.TopAbs_FACE, faceNumber, true);
                    _generated.Add(_faceNode);
                    faceNumber++;
                    faceExplorer.Next();
                }
            }

            var nonAdjacent = _candidates.Except(colidingShapes);

            foreach (var independent in nonAdjacent)
            {
                faceNumber = 1;
                var shape = independent.Get <TopoDsShapeInterpreter>().Shape;
                faceExplorer = new TopExpExplorer();
                faceExplorer.Init(shape, TopAbsShapeEnum.TopAbs_FACE, TopAbsShapeEnum.TopAbs_SHAPE);
                while (faceExplorer.More)
                {
                    var _faceNode = BuildSubShape(_document, independent, TopAbsShapeEnum.TopAbs_FACE, faceNumber, true);
                    _generated.Add(_faceNode);
                    faceNumber++;
                    faceExplorer.Next();
                }
            }
            if (_nodeFused != null)
            {
                _document.Root.Remove(_nodeFused.Index);
            }
            return(_generated);
        }
 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);
     }
 }
Exemplo n.º 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);
        }
Exemplo n.º 5
0
        private static TopoDSShape ApplyFillet(double thickness, TopoDSShape body, int edgeNumber, int operationType)
        {
            try
            {
                // Create fillet
                var aEdgeExplorer = new TopExpExplorer(body, TopAbsShapeEnum.TopAbs_EDGE,
                                                       TopAbsShapeEnum.TopAbs_SHAPE);

                var         number = 1;
                TopoDSShape shape  = null;
                while (aEdgeExplorer.More)
                {
                    if ((edgeNumber == 0) || (edgeNumber == number))
                    {
                        var anEdge = TopoDS.Edge(aEdgeExplorer.Current);

                        if (operationType == (int)FilletChamferTypes.SimpleFillet)
                        {
                            var fillet = new BRepFilletAPIMakeFillet(body, ChFi3dFilletShape.ChFi3d_Rational);
                            // Add edge to fillet algorithm
                            fillet.Add(thickness, anEdge);
                            // Check if valid contour
                            try
                            {
                                if (fillet.StripeStatus(fillet.Contour(anEdge)) != ChFiDSErrorStatus.ChFiDS_Ok)
                                {
                                    return(null);
                                }
                            }
                            catch (Exception)
                            {
                                Log.Info("Exception on applying fillet");
                            }
                            shape = fillet.Shape;
                        }
                        else
                        {
                            var chamfer = new BRepFilletAPIMakeChamfer(body);
                            var aMap    = new TopToolsIndexedDataMapOfShapeListOfShape(1);
                            TopExp.MapShapesAndAncestors(body, TopAbsShapeEnum.TopAbs_EDGE, TopAbsShapeEnum.TopAbs_FACE,
                                                         aMap);
                            // Locate an ancestor face
                            for (var i = 1; i < aMap.Extent; i++)
                            {
                                var localEdge = TopoDS.Edge(aMap.FindKey(i));
                                if (!anEdge.IsSame(localEdge))
                                {
                                    continue;
                                }
                                // We found an ancestor face
                                var face = TopoDS.Face(aMap.FindFromIndex(i).First);
                                // Add the edge and face on the chmafer algorithm
                                chamfer.Add(thickness, thickness, anEdge, face);
                            }
                            shape = chamfer.Shape;
                        }
                    }

                    aEdgeExplorer.Next();
                    number++;
                }

                // Check the shape validity

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

                return(shape);
            }
            catch (Exception ex)
            {
                Log.Info("Apply fillet error: " + ex.Message);
            }
            return(null);
        }