Skip to content

Commit daba3e8

Browse files
committed
Measure: handle potential exceptions in MeasureToolBRep::brepMinDistance()
Relates to GitHub #288
1 parent e23a037 commit daba3e8

File tree

3 files changed

+38
-14
lines changed

3 files changed

+38
-14
lines changed

src/measure/measure_tool_brep.cpp

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ gp_Pnt computeShapeCenter(const TopoDS_Shape& shape)
147147
const MeasureCircle circle = MeasureToolBRep::brepCircle(shape);
148148
return circle.value.Location();
149149
}
150-
catch (const BRepMeasureError<ErrorCode::NotCircularEdge>&) {
150+
catch (const IMeasureError&) {
151151
BRepGProp::LinearProperties(shape, shapeProps);
152152
}
153153
}
@@ -276,10 +276,10 @@ MeasureCircle MeasureToolBRep::brepCircleFromGeometricEdge(const TopoDS_Edge& ed
276276
const GCPnts_QuasiUniformAbscissa pnts(curve, 4); // More points to avoid confusion
277277
throwErrorIf<ErrorCode::NotCircularEdge>(!pnts.IsDone() || pnts.NbPoints() < 3);
278278
const GC_MakeCircle makeCirc(
279-
GeomUtils::d0(curve, pnts.Parameter(1)),
280-
GeomUtils::d0(curve, pnts.Parameter(2)),
281-
GeomUtils::d0(curve, pnts.Parameter(3))
282-
);
279+
GeomUtils::d0(curve, pnts.Parameter(1)),
280+
GeomUtils::d0(curve, pnts.Parameter(2)),
281+
GeomUtils::d0(curve, pnts.Parameter(3))
282+
);
283283
throwErrorIf<ErrorCode::NotCircularEdge>(!makeCirc.IsDone());
284284
circle = makeCirc.Value()->Circ();
285285
}
@@ -312,10 +312,10 @@ MeasureCircle MeasureToolBRep::brepCircleFromPolygonEdge(const TopoDS_Edge& edge
312312
throwErrorIf<ErrorCode::NotGeometricOrPolygonEdge>(polyline.IsNull() || polyline->NbNodes() < 7);
313313
// Try to create a circle from 3 sample points
314314
const GC_MakeCircle makeCirc(
315-
polyline->Nodes().First(),
316-
polyline->Nodes().Value(1 + polyline->NbNodes() / 3),
317-
polyline->Nodes().Value(1 + 2 * polyline->NbNodes() / 3)
318-
);
315+
polyline->Nodes().First(),
316+
polyline->Nodes().Value(1 + polyline->NbNodes() / 3),
317+
polyline->Nodes().Value(1 + 2 * polyline->NbNodes() / 3)
318+
);
319319
throwErrorIf<ErrorCode::NotCircularEdge>(!makeCirc.IsDone());
320320
const gp_Circ circle = makeCirc.Value()->Circ();
321321

@@ -345,12 +345,21 @@ MeasureCircle MeasureToolBRep::brepCircle(const TopoDS_Shape& shape)
345345
}
346346

347347
MeasureDistance MeasureToolBRep::brepMinDistance(
348-
const TopoDS_Shape& shape1, const TopoDS_Shape& shape2)
348+
const TopoDS_Shape& shape1, const TopoDS_Shape& shape2
349+
)
349350
{
350351
throwErrorIf<ErrorCode::NotBRepShape>(shape1.IsNull());
351352
throwErrorIf<ErrorCode::NotBRepShape>(shape2.IsNull());
352353

353-
const BRepExtrema_DistShapeShape dist(shape1, shape2);
354+
BRepExtrema_DistShapeShape dist;
355+
try {
356+
dist.LoadS1(shape1);
357+
dist.LoadS2(shape2);
358+
dist.Perform();
359+
} catch (...) {
360+
throw BRepMeasureError<ErrorCode::MinDistanceFailure>();
361+
}
362+
354363
throwErrorIf<ErrorCode::MinDistanceFailure>(!dist.IsDone());
355364

356365
MeasureDistance distResult;
@@ -362,7 +371,8 @@ MeasureDistance MeasureToolBRep::brepMinDistance(
362371
}
363372

364373
MeasureDistance MeasureToolBRep::brepCenterDistance(
365-
const TopoDS_Shape& shape1, const TopoDS_Shape& shape2)
374+
const TopoDS_Shape& shape1, const TopoDS_Shape& shape2
375+
)
366376
{
367377
throwErrorIf<ErrorCode::NotBRepShape>(shape1.IsNull());
368378
throwErrorIf<ErrorCode::NotBRepShape>(shape2.IsNull());
@@ -507,8 +517,8 @@ MeasureArea MeasureToolBRep::brepArea(const TopoDS_Shape& shape)
507517

508518
const BRepAdaptor_Surface surface(face);
509519
areaResult.middlePnt = surface.Value(
510-
(surface.FirstUParameter() + surface.LastUParameter()) / 2.,
511-
(surface.FirstVParameter() + surface.LastVParameter()) / 2.
520+
(surface.FirstUParameter() + surface.LastUParameter()) / 2.,
521+
(surface.FirstVParameter() + surface.LastVParameter()) / 2.
512522
);
513523
}
514524
else {

tests/test_measure.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <BRepAdaptor_Curve.hxx>
1818
#include <BRepBuilderAPI_MakeVertex.hxx>
1919
#include <BRepBuilderAPI_MakeEdge.hxx>
20+
#include <BRepBuilderAPI_MakeFace.hxx>
2021
#include <BRepPrimAPI_MakeBox.hxx>
2122
#include <BRepPrimAPI_MakeSphere.hxx>
2223
#include <Geom_BSplineCurve.hxx>
@@ -146,6 +147,18 @@ void TestMeasure::BRepMinDistance_TwoBoxes_test()
146147
QCOMPARE(UnitSystem::millimeters(minDist.value).value, minDist.pnt1.Distance(minDist.pnt2));
147148
}
148149

150+
void TestMeasure::BRepMinDistance_TwoConfusedFaces_test()
151+
{
152+
const TopoDS_Face face1 = BRepBuilderAPI_MakeFace(gp_Pln(gp::XOY()));
153+
const TopoDS_Face face2 = BRepBuilderAPI_MakeFace(gp_Pln(gp::XOY()));
154+
try {
155+
const MeasureDistance minDist = MeasureToolBRep::brepMinDistance(face1, face2);
156+
QCOMPARE(minDist.value.value(), 0.);
157+
} catch (const IMeasureError& err) {
158+
qDebug() << QString::fromUtf8(err.message().data(), err.message().length());
159+
}
160+
}
161+
149162
void TestMeasure::BRepAngle_TwoLinesIntersect_test()
150163
{
151164
const TopoDS_Shape shape1 = BRepBuilderAPI_MakeEdge(gp_Lin(gp::Origin(), gp::DX()));

tests/test_measure.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ private slots:
2626

2727
void BRepMinDistance_TwoPoints_test();
2828
void BRepMinDistance_TwoBoxes_test();
29+
void BRepMinDistance_TwoConfusedFaces_test();
2930

3031
void BRepAngle_TwoLinesIntersect_test();
3132
void BRepAngle_TwoLinesParallelError_test();

0 commit comments

Comments
 (0)