コード例 #1
0
        //--------------------------------------------------------------------------------------------------

        public static double Volume(this TopoDS_Shape shape)
        {
            GProp_GProps massProps = new GProp_GProps();

            BRepGProp.VolumeProperties(shape, massProps);
            return(massProps.Mass());
        }
コード例 #2
0
        //--------------------------------------------------------------------------------------------------

        public static double Area(this TopoDS_Shape shape)
        {
            GProp_GProps massProps = new GProp_GProps();

            BRepGProp.SurfaceProperties(shape, massProps);
            return(massProps.Mass());
        }
コード例 #3
0
        //--------------------------------------------------------------------------------------------------

        static string _CompareProperties(GProp_GProps gprops1, GProp_GProps gprops2, string message)
        {
            if (!gprops1.CentreOfMass().IsEqual(gprops2.CentreOfMass(), 0.00001))
            {
                return($"{message} CentreOfMass is not the same.");
            }
            if (!gprops1.Mass().IsEqual(gprops2.Mass(), 0.0001))
            {
                return($"{message} Mass is not the same.");
            }

            double ix1 = 0, iy1 = 0, iz1 = 0, ix2 = 0, iy2 = 0, iz2 = 0;

            gprops1.PrincipalProperties().Moments(ref ix1, ref iy1, ref iz1);
            gprops2.PrincipalProperties().Moments(ref ix2, ref iy2, ref iz2);
            if (!ix1.IsEqual(ix2, 0.0001))
            {
                return($"{message} MomentIx is not the same. Result: {ix1}  Reference: {ix2}");
            }
            if (!iy1.IsEqual(iy2, 0.0001))
            {
                return($"{message} MomentIy is not the same. Result: {iy1}  Reference: {iy2}");
            }
            if (!iz1.IsEqual(iz2, 0.0001))
            {
                return($"{message} MomentIz is not the same. Result: {iz1}  Reference: {iz2}");
            }

            return(_CompareMatrix(gprops1.MatrixOfInertia(), gprops2.MatrixOfInertia(), message + " MatrixOfInertia"));;
        }
コード例 #4
0
        //--------------------------------------------------------------------------------------------------

        void _AddFaceProperties(TopoDS_Face face)
        {
            const string facecat = "Face";
            const string surfcat = "Surface";

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

            _AddProperty(facecat, "Tolerance", $"{BRep_Tool.Tolerance(face)}");
            _AddProperty(facecat, "Nat.Restrict.", $"{(BRep_Tool.NaturalRestriction(face) ? "Yes" : "No")}");

            var props = new GProp_GProps();

            BRepGProp.SurfaceProperties(BrepShape, props);
            _AddProperty(facecat, "Area", $"{props.Mass()}");

            var surface = BRep_Tool.Surface(face);

            if (surface != null)
            {
                _AddProperty(surfcat, "Class", surface.GetType().Name.Replace("Geom_", ""));
                double u1 = 0, u2 = 0, v1 = 0, v2 = 0;
                surface.Bounds(ref u1, ref u2, ref v1, ref v2);
                _AddProperty(surfcat, "Bounds U", $"({u1}, {u2})");
                _AddProperty(surfcat, "Bounds V", $"({v1}, {v2})");
                _AddProperty(surfcat, "Is Closed", $"U={(surface.IsUClosed() ? "Yes" : "No")}  V={(surface.IsUClosed() ? "Yes" : "No")}");
                if (surface.IsUPeriodic() || surface.IsVPeriodic())
                {
                    var s = "";
                    if (surface.IsUPeriodic())
                    {
                        s += $"U={surface.UPeriod()}  ";
                    }
                    if (surface.IsVPeriodic())
                    {
                        s += $"V={surface.VPeriod()}  ";
                    }
                    _AddProperty(surfcat, "Period", s);
                }
                _AddProperty(surfcat, "Continuity", surface.Continuity().ToString().Replace("GeomAbs_", ""));
            }
        }
コード例 #5
0
        //--------------------------------------------------------------------------------------------------

        /// <summary>
        /// Searches for the smallest and biggest adjacent face
        /// </summary>
        public static (TopoDS_Face smallestFace, TopoDS_Face largestFace) FindSmallestAndLargestAdjacentFaces(TopoDS_Shape shape, TopoDS_Edge edgeShape)
        {
            // Create a Map of Edge and connected Faces
            var mapOfEdgesToFaces = new TopTools_IndexedDataMapOfShapeListOfShape(1);

            TopExp.MapShapesAndAncestors(shape, TopAbs_ShapeEnum.TopAbs_EDGE, TopAbs_ShapeEnum.TopAbs_FACE, mapOfEdgesToFaces);

            var faceDict = new Dictionary <TopoDS_Face, double>();

            var faces = mapOfEdgesToFaces.FindFromKey(edgeShape).ToList();

            foreach (var face in faces)
            {
                var gprops = new GProp_GProps();
                BRepGProp.SurfaceProperties(face, gprops, false);
                faceDict.Add(face.ToFace(), gprops.Mass());
            }

            if (!faceDict.Any())
            {
                return(null, null);
            }

            var min = faceDict.First();
            var max = min;

            foreach (var kvp in faceDict.Skip(1))
            {
                if (kvp.Value < min.Value)
                {
                    min = kvp;
                }
                if (kvp.Value > max.Value)
                {
                    max = kvp;
                }
            }

            return(min.Key, max.Key);
        }
コード例 #6
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_", ""));
            }
        }
コード例 #7
0
        //--------------------------------------------------------------------------------------------------

        protected Dictionary <TopoDS_Edge, TopoDS_Face> FindReferenceFaces(TopoDS_Shape sourceShape, IEnumerable <TopoDS_Edge> edges, bool reverseOrientation)
        {
            var dict = new Dictionary <TopoDS_Edge, TopoDS_Face> ();

            // Create a Map of Edges and connected Faces
            var mapOfEdgesToFaces = new TopTools_IndexedDataMapOfShapeListOfShape(1);

            TopExp.MapShapesAndAncestors(sourceShape, TopAbs_ShapeEnum.TopAbs_EDGE, TopAbs_ShapeEnum.TopAbs_FACE, mapOfEdgesToFaces);

            foreach (var edge in edges)
            {
                if (dict.ContainsKey(edge))
                {
                    continue;
                }

                TopoDS_Face face  = null;
                var         faces = mapOfEdgesToFaces.FindFromKey(edge).ToList();
                if (faces.Count == 0)
                {
                    continue;
                }

                var lastSize = 0.0;
                foreach (var faceShape in faces)
                {
                    var gprops = new GProp_GProps();
                    BRepGProp.SurfaceProperties(faceShape, gprops, false);
                    var size = gprops.Mass() * (reverseOrientation ? -1.0 : 1.0);

                    // Init with the first face
                    if (face == null)
                    {
                        face     = faceShape.ToFace();
                        lastSize = size;
                        continue;
                    }

                    // Take the biggest face.
                    if (size < lastSize)
                    {
                        continue;
                    }
                    if (size > lastSize)
                    {
                        face     = faceShape.ToFace();
                        lastSize = size;
                        continue;
                    }
                    // If all faces are of equal size, take forward orientated face as reference for distance
                    if (faceShape.Orientation() == (reverseOrientation ? TopAbs_Orientation.TopAbs_REVERSED : TopAbs_Orientation.TopAbs_FORWARD))
                    {
                        face     = faceShape.ToFace();
                        lastSize = size;
                    }
                }

                dict.Add(edge, face);
            }
            return(dict);
        }