Fri, 08/30/2024 - 16:21
Forums:
How does one call BRepOffsetAPI_ThruSections with a set of TopoDS_Face that are simple shapes, but with holes in them?
My code works well running BRepOffsetAPI_ThruSections twice, with the outside shapes first, and then the inside shapes later and boolean the two together.
But I want to test is running it once, with the holes included would be faster, but I can't figure out how to pass the holes to BRepOffsetAPI_ThruSections.
Here's what I have tried (BTW, I'm using the c# wrapper) This just adds all the TopoDS_Wires, but that of cause can't be right.
I have uploaded a snapshot of my faces for reference, going from left to right.
public static TopoDS_Shape CreateSolidFromSections(List<TopoDS_Shape> shapeSections)
{
try
{
if (shapeSections == null || shapeSections.Count < 2)
{
throw new ArgumentException("At least two shape sections are required to create a solid.");
}
// Validate each shape before proceeding
for (int i = 0; i < shapeSections.Count; i++)
{
var shape = shapeSections[i];
BRepCheck_Analyzer analyzer = new BRepCheck_Analyzer(shape);
if (!analyzer.IsValid())
{
throw new InvalidOperationException($"Shape is not valid: {shape.ShapeType()} - shape#{i}");
}
}
// Initialize the BRepOffsetAPI_ThruSections object
BRepOffsetAPI_ThruSections thruSections = new BRepOffsetAPI_ThruSections(true, false, 0.01);
thruSections.SetSmoothing(true);
thruSections.SetContinuity(GeomAbs_Shape.GeomAbs_C2); // For smooth curvature
thruSections.CheckCompatibility(true); // Ensure the sections are compatible
// Add wires to the ThruSections object
foreach (TopoDS_Shape shape in shapeSections)
{
if (shape.ShapeType() == TopAbs_ShapeEnum.TopAbs_FACE)
{
TopoDS_Face face = TopoDS.ToFace(shape);
TopoDS_Wire outerWire = GetOuterWire(face);
if (outerWire == null || outerWire.IsNull())
{
throw new InvalidOperationException("Failed to get the outer wire from the face.");
}
// Ensure the outer wire is oriented correctly
// BRepLib.OrientClosedWire(face, outerWire);
thruSections.AddWire(outerWire);
// Add inner wires (holes)
TopExp_Explorer explorer = new TopExp_Explorer(face, TopAbs_ShapeEnum.TopAbs_WIRE);
while (explorer.More())
{
TopoDS_Wire wire = TopoDS.ToWire(explorer.Current());
if (!wire.IsNull() && !wire.IsEqual(outerWire))
{
thruSections.AddWire(wire);
}
explorer.Next();
}
}
else
{
throw new ArgumentException("All input shapes must be of type TopoDS_Face.");
}
}
// Build the solid
thruSections.Build();
if (!thruSections.IsDone())
{
throw new InvalidOperationException("Failed to create the solid from the shape sections.");
}
return thruSections.Shape();
}
catch (Exception ex)
{
// Simplified error handling
Debug.LogError($"Error in CreateSolidFromSections: {ex.Message}");
return null;
}
}
PS: how do you tag code as csharp? (or as c++?)
Attachments: