//-------------------------------------------------------------------------------------------------- public static double Area(this TopoDS_Shape shape) { GProp_GProps massProps = new GProp_GProps(); BRepGProp.SurfaceProperties(shape, massProps); return(massProps.Mass()); }
//-------------------------------------------------------------------------------------------------- public static Pnt CenterOfMass(this TopoDS_Shape shape) { GProp_GProps massProps = new GProp_GProps(); BRepGProp.SurfaceProperties(shape, massProps); return(massProps.CentreOfMass()); }
//-------------------------------------------------------------------------------------------------- static bool _CompareSurfaceProperties(TopoDS_Shape shape1, TopoDS_Shape shape2, out string message) { var gprops1 = new GProp_GProps(); var gprops2 = new GProp_GProps(); BRepGProp.SurfaceProperties(shape1, gprops1, false); BRepGProp.SurfaceProperties(shape2, gprops2, false); message = _CompareProperties(gprops1, gprops2, "Surface"); return(message != null); }
//-------------------------------------------------------------------------------------------------- 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_", "")); } }
//-------------------------------------------------------------------------------------------------- /// <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); }
//-------------------------------------------------------------------------------------------------- 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); }