public override bool Execute()
        {
            var firstShape  = Dependency[0].ReferedShape;
            var secondShape = Dependency[1].ReferedShape;

            if (firstShape.IsSame(secondShape))
            {
                return(false);
            }

            NodeUtils.Hide(Dependency[0].Reference);
            NodeUtils.Hide(Dependency[1].Reference);

            switch (Dependency[2].Integer)
            {
            case (int)BooleanOperations.Substract:
                var cut = new BRepAlgoAPICut(firstShape, secondShape);
                if (!cut.IsDone)
                {
                    return(false);
                }
                if ((cut.HasGenerated == false) && (cut.HasModified == false))
                {
                    return(false);
                }
                Shape = cut.Shape;
                break;

            case (int)BooleanOperations.Add:
                var fuse = new BRepAlgoAPIFuse(firstShape, secondShape);
                if (!fuse.IsDone)
                {
                    return(false);
                }
                if ((fuse.HasGenerated == false) && (fuse.HasModified == false))
                {
                    return(false);
                }
                Shape = fuse.Shape;
                break;

            case (int)BooleanOperations.Intersect:
                var common = new BRepAlgoAPICommon(firstShape, secondShape);
                if (!common.IsDone)
                {
                    return(false);
                }
                if ((common.HasGenerated == false) && (common.HasModified == false))
                {
                    return(false);
                }
                Shape = common.Shape;
                break;
            }
            return(true);
        }
예제 #2
0
        /// <summary>
        ///   Cuts through all childs of the node with the TopoDS_Shape passed as parameter.
        ///   It returns a compund made from the affected shapes
        /// </summary>
        /// <param name = "nodes"></param>
        /// <param name = "cutPrismShape"></param>
        private static TopoDSShape CutThroughAll(IEnumerable <SceneSelectedEntity> nodes, TopoDSShape cutPrismShape)
        {
            var compoundShape = new TopoDSCompound();
            var shapeBuilder  = new BRepBuilder();

            shapeBuilder.MakeCompound(compoundShape);
            var resShape = (TopoDSShape)compoundShape;

            foreach (var cutShape in nodes)
            {
                var childNode = cutShape.Node;
                // Try to Cut through the object
                var topoShape = childNode.Get <NamedShapeInterpreter>().Shape;
                if (topoShape == null)
                {
                    continue;
                }
                var cut = new BRepAlgoAPICut(topoShape, cutPrismShape);
                // Check if the shape was altered by the Cut
                if ((cut.HasGenerated == false) && (cut.HasModified == false))
                {
                    continue;
                }

                TopoDSShape shapeCut = null;
                try
                {
                    shapeCut = cut.Shape;
                }
                catch (Exception)
                {
                    Log.Info("Exception on create cut shape");
                }

                if ((shapeCut != null) && (!shapeCut.IsNull))
                {
                    // Show the new shape as a new Node
                    shapeBuilder.Add(resShape, shapeCut);
                }
            }

            return(resShape);
        }
예제 #3
0
        /// <summary>
        ///   Cuts through all childs of the node with the TopoDS_Shape passed as parameter.
        ///   It returns a compund made from the affected shapes
        /// </summary>
        /// <param name = "nodes"></param>
        /// <param name = "cutPrismShape"></param>
        private static TopoDSShape CutThroughShape(TopoDSShape shapeToCut, TopoDSShape cutPrismShape)
        {
            var cut = new BRepAlgoAPICut(shapeToCut, cutPrismShape);

            // Check if the shape was altered by the Cut
            //if ((cut.HasGenerated == false) && (cut.HasModified == false))
            //    return null;
            if (cut.Shape == null || GeomUtils.GetSolidVolume(cut.Shape) < Precision.Confusion)
            {
                return(null);
            }

            TopoDSShape shapeCut = null;

            try
            {
                shapeCut = cut.Shape;
            }
            catch (Exception)
            {
                Log.Info("Exception on create cut shape");
            }
            return(shapeCut);
        }
예제 #4
0
        /// <summary>
        ///   Cuts through all childs of the node with the TopoDS_Shape passed as parameter.
        ///   It returns a compund made from the affected shapes
        /// </summary>
        /// <param name = "root"></param>
        /// <param name = "cutPrismShape"></param>
        /// <param name = "affectedNodes"></param>
        /// <param name = "shapeList"></param>
        private static TopoDSShape CutThroughAll(Node root, TopoDSShape cutPrismShape,
                                                 ICollection <SceneSelectedEntity> affectedNodes,
                                                 IEnumerable <int> shapeList)
        {
            var compoundShape = new TopoDSCompound();
            var shapeBuilder  = new BRepBuilder();

            shapeBuilder.MakeCompound(compoundShape);
            var resShape = (TopoDSShape)compoundShape;

            foreach (var childNodeIndex in shapeList)
            {
                var childNode = new NodeBuilder(root[childNodeIndex]);

                var topoShape = childNode.Shape;
                if (topoShape == null)
                {
                    continue;
                }
                BRepAlgoAPICut cut;
                var            filler = new BOPToolsDSFiller();
                try
                {
                    filler.SetShapes(topoShape, cutPrismShape);
                    if (!filler.IsDone)
                    {
                        continue;
                    }
                    filler.Perform();
                    cut = new BRepAlgoAPICut(topoShape, cutPrismShape, filler, true);
                    if (!cut.IsDone)
                    {
                        continue;
                    }
                    // Check if the shape was altered by the Cut
                    //if ((cut.HasGenerated == false) && (cut.HasModified == false))
                    //    continue;
                }
                catch (Exception)
                {
                    continue;
                }

                TopoDSShape shapeCut = null;
                try
                {
                    shapeCut = cut.Shape;
                }
                catch (Exception)
                {
                    Log.Info("Exception on create cut shape");
                }

                if ((shapeCut == null) || (shapeCut.IsNull))
                {
                    continue;
                }
                // If Cut succeeded hide the original shape
                NodeUtils.Hide(childNode.Node);
                // Show the new shape as a new Node
                shapeBuilder.Add(resShape, shapeCut);
                // Add the affceted node into the affectedNodes list
                affectedNodes.Add(new SceneSelectedEntity(childNode.Node));
            }

            return(resShape);
        }
예제 #5
0
        public static List <TopoDSShape> BuildAutoFaces(Node sketchNode, Document Document)
        {
            var results       = new List <Node>();
            var root          = Document.Root;
            var circleNodes   = new List <int>();
            var nodesOnSketch = new List <Node>();

            // get all 2D wire-based shapes on the sketch
            foreach (var node in root.ChildrenList)
            {
                var builder = new NodeBuilder(node);
                if (builder.FunctionName == FunctionNames.Circle || builder.FunctionName == FunctionNames.Ellipse || builder.FunctionName == FunctionNames.LineTwoPoints || builder.FunctionName == FunctionNames.Arc || builder.FunctionName == FunctionNames.Arc3P)// || builder.FunctionName == FunctionNames.Point)
                {
                    var currentSketchNode = FindSketchNode(node);
                    if (currentSketchNode.Index == sketchNode.Index)
                    {
                        nodesOnSketch.Add(node);
                    }
                }
            }
            indexesList = new List <List <int> >();
            foreach (var node in nodesOnSketch)
            {
                var builder = new NodeBuilder(node);

                if (builder.FunctionName == FunctionNames.Circle || builder.FunctionName == FunctionNames.Ellipse)
                {
                    // ensure the same circle isn't added more than once - if it were, it would be subtracted from itself and would become invisible
                    if (!circleNodes.Contains(node.Index))
                    {
                        circleNodes.Add(node.Index);
                    }
                }

                if (builder.FunctionName == FunctionNames.LineTwoPoints ||
                    builder.FunctionName == FunctionNames.Arc ||
                    builder.FunctionName == FunctionNames.Arc3P)
                {
                    TryAutoGroup(Document, builder.Node);
                }
            }
            var _candidates = new List <Node>();

            foreach (var indexList in indexesList)
            {
                if (indexList.Count < 3)
                {
                    continue;
                }
                if (indexList.Distinct().Count() != indexList.Count)
                {
                    continue;
                }
                //build face
                var entities = indexList.Select(index => new SceneSelectedEntity {
                    Node = Document.Root[index], ShapeType = TopAbsShapeEnum.TopAbs_WIRE
                }).ToList();

                var nb = new NodeBuilder(Document, FunctionNames.Face);
                nb[0].ReferenceList = entities;
                nb.ExecuteFunction();
                if (nb.Shape != null)
                {
                    if (GeomUtils.GetFaceArea(nb.Shape) > 0)
                    {
                        _candidates.Add(nb.Node);
                    }
                    else
                    {
                        Document.Root.Remove(nb.Node.Index);
                    }
                }
                else
                {
                    Document.Root.Remove(nb.Node.Index);
                }
            }

            var generatedFaceNodes = UpdateFaces(Document, _candidates);

            if (generatedFaceNodes != null)
            {
                results.AddRange(generatedFaceNodes);
            }

            var shapesList = new List <TopoDSShape>();

            foreach (var node in circleNodes)
            {
                var builder    = new NodeBuilder(root[node]);
                var addedEntiy = new SceneSelectedEntity {
                    Node = builder.Node, ShapeType = TopAbsShapeEnum.TopAbs_WIRE
                };
                var builder2 = new NodeBuilder(Document, FunctionNames.Face);
                builder2[0].ReferenceList = new List <SceneSelectedEntity> {
                    addedEntiy
                };
                if (builder2.ExecuteFunction())
                {
                    results.Add(builder2.Node);
                }
                else
                {
                    Document.Root.Remove(builder2.Node.Index);
                }
            }

            if (results.Count > 0)
            {
                var shapes = new Dictionary <int, List <int> >();
                for (int i = 0; i < results.Count; i++)
                {
                    shapes.Add(i, new List <int>());
                }

                for (int i = 0; i < results.Count; i++)
                {
                    for (int j = i + 1; j < results.Count; j++)
                    {
                        //var position = ShapeIsInsideShape(new NodeBuilder(results[i]).Shape,
                        //                                  new NodeBuilder(results[j]).Shape);
                        var newMethodValue =
                            RelativePosition(new NodeBuilder(results[i]).Shape, new NodeBuilder(results[j]).Shape);
                        if (newMethodValue == 2)
                        {
                            // first shape is inside second
                            shapes[j].Add(i);
                            shapes[i].Add(-1);
                        }
                        else
                        {
                            if (newMethodValue == 3)
                            {
                                // second shape is inside first
                                shapes[i].Add(j);
                                shapes[j].Add(-1);
                            }
                        }
                    }
                }

                var facesList = new Dictionary <int, List <int> >();

                for (int i = 0; i < shapes.Count; i++)
                {
                    if (!shapes[i].Contains(-1))
                    {
                        facesList.Add(i, shapes[i]);
                    }
                }

                // end form list
                foreach (int i in facesList.Keys)
                {
                    if (facesList[i].Count == 0)
                    {
                        var nb = new NodeBuilder(results[i]);
                        if (nb.Shape != null)
                        {
                            shapesList.Add(nb.Shape);
                        }
                    }
                    else
                    {
                        var bigShape = new NodeBuilder(results[i]).Shape;
                        foreach (var inner in facesList[i])
                        {
                            var innerShape = new NodeBuilder(results[inner]).Shape;

                            var subtract = new BRepAlgoAPICut(bigShape, innerShape);
                            if (subtract.IsDone)
                            {
                                bigShape = subtract.Shape;
                            }
                        }
                        var face = GeomUtils.ExtractFaces(bigShape);
                        if (face.Count == 1)
                        {
                            shapesList.Add(face[0]);
                        }
                    }
                }

                foreach (Node node in results)
                {
                    Document.Root.Remove(node.Index);
                }
                if (generatedFaceNodes != null)
                {
                    foreach (Node node in generatedFaceNodes)
                    {
                        Document.Root.Remove(node.Index);
                    }
                }

                foreach (var node in _candidates)
                {
                    Document.Root.Remove(node.Index);
                }
            }
            return(shapesList);
        }