|
39 | 39 | #include <Geom_BezierCurve.hxx> |
40 | 40 | #include <Geom_BSplineCurve.hxx> |
41 | 41 | #include <TopExp.hxx> |
| 42 | +#include <TopExp_Explorer.hxx> |
42 | 43 | #include <GProp_GProps.hxx> |
43 | 44 | #include <ShapeAnalysis_Edge.hxx> |
44 | 45 | #include <gp_Circ.hxx> |
@@ -97,6 +98,14 @@ static float getRadius(TopoDS_Shape& edge) |
97 | 98 | gp_Circ circle = adapt.Circle(); |
98 | 99 | return circle.Radius(); |
99 | 100 | } |
| 101 | + if (edge.ShapeType() == TopAbs_FACE) { |
| 102 | + BRepAdaptor_Surface adapt(TopoDS::Face(edge)); |
| 103 | + if (adapt.GetType() != GeomAbs_Cylinder) { |
| 104 | + return 0.0; |
| 105 | + } |
| 106 | + gp_Cylinder cylinder = adapt.Cylinder(); |
| 107 | + return cylinder.Radius(); |
| 108 | + } |
100 | 109 | return 0.0; |
101 | 110 | } |
102 | 111 |
|
@@ -336,36 +345,63 @@ MeasureLengthInfoPtr MeasureLengthHandler(const App::SubObjectT& subject) |
336 | 345 | MeasureRadiusInfoPtr MeasureRadiusHandler(const App::SubObjectT& subject) |
337 | 346 | { |
338 | 347 | Base::Placement placement; // curve center + orientation |
339 | | - Base::Vector3d pointOnCurve; |
| 348 | + Base::Vector3d centerPoint; |
| 349 | + ; |
340 | 350 |
|
341 | 351 | TopoDS_Shape shape = getLocatedShape(subject); |
342 | 352 |
|
343 | 353 | if (shape.IsNull()) { |
344 | | - return std::make_shared<MeasureRadiusInfo>(false, 0.0, pointOnCurve, placement); |
| 354 | + return std::make_shared<MeasureRadiusInfo>(false, 0.0, centerPoint, placement); |
345 | 355 | } |
346 | 356 | TopAbs_ShapeEnum sType = shape.ShapeType(); |
347 | 357 |
|
348 | | - if (sType != TopAbs_EDGE) { |
349 | | - return std::make_shared<MeasureRadiusInfo>(false, 0.0, pointOnCurve, placement); |
| 358 | + if (sType != TopAbs_EDGE && sType != TopAbs_FACE) { |
| 359 | + return std::make_shared<MeasureRadiusInfo>(false, 0.0, centerPoint, placement); |
350 | 360 | } |
351 | 361 |
|
352 | | - // Get Center of mass as the attachment point of the label |
353 | 362 | GProp_GProps gprops; |
354 | | - BRepGProp::LinearProperties(shape, gprops); |
| 363 | + TopoDS_Edge edge; |
| 364 | + TopoDS_Face face; |
| 365 | + gp_Pnt center; |
| 366 | + |
| 367 | + if (sType == TopAbs_EDGE) { |
| 368 | + BRepGProp::LinearProperties(shape, gprops); |
| 369 | + edge = TopoDS::Edge(shape); |
| 370 | + BRepAdaptor_Curve adapt(edge); |
| 371 | + if (adapt.GetType() == GeomAbs_Circle) { |
| 372 | + center = adapt.Circle().Location(); |
| 373 | + } |
| 374 | + } |
| 375 | + else if (sType == TopAbs_FACE) { |
| 376 | + BRepGProp::SurfaceProperties(shape, gprops); |
| 377 | + face = TopoDS::Face(shape); |
| 378 | + TopExp_Explorer exp(face, TopAbs_EDGE); |
| 379 | + if (exp.More()) { |
| 380 | + edge = TopoDS::Edge(exp.Current()); |
| 381 | + } |
| 382 | + if (edge.IsNull()) { |
| 383 | + return std::make_shared<MeasureRadiusInfo>(false, 0.0, centerPoint, placement); |
| 384 | + } |
| 385 | + |
| 386 | + BRepAdaptor_Surface surf(face); |
| 387 | + if (surf.GetType() == GeomAbs_Cylinder) { |
| 388 | + center = surf.Cylinder().Location(); |
| 389 | + } |
| 390 | + } |
| 391 | + // Get Center of mass as the attachment point of the label |
355 | 392 | auto origin = gprops.CentreOfMass(); |
356 | 393 |
|
357 | | - TopoDS_Edge edge = TopoDS::Edge(shape); |
358 | | - gp_Pnt firstPoint = BRep_Tool::Pnt(TopExp::FirstVertex(edge)); |
359 | | - pointOnCurve = Base::Vector3d(firstPoint.X(), firstPoint.Y(), firstPoint.Z()); |
| 394 | + centerPoint = Base::Vector3d(center.X(), center.Y(), center.Z()); |
| 395 | + |
360 | 396 | // a somewhat arbitrary radius from center -> point on curve |
361 | | - auto dir = (firstPoint.XYZ() - origin.XYZ()).Normalized(); |
| 397 | + auto dir = (center.XYZ() - origin.XYZ()).Normalized(); |
362 | 398 | Base::Vector3d elementDirection(dir.X(), dir.Y(), dir.Z()); |
363 | 399 | Base::Vector3d axisUp(0.0, 0.0, 1.0); |
364 | 400 | Base::Rotation rot(axisUp, elementDirection); |
365 | 401 |
|
366 | 402 | placement = Base::Placement(Base::Vector3d(origin.X(), origin.Y(), origin.Z()), rot); |
367 | 403 |
|
368 | | - return std::make_shared<MeasureRadiusInfo>(true, getRadius(shape), pointOnCurve, placement); |
| 404 | + return std::make_shared<MeasureRadiusInfo>(true, getRadius(shape), centerPoint, placement); |
369 | 405 | } |
370 | 406 |
|
371 | 407 |
|
|
0 commit comments