[TD]corrupt dim reference detect and correct

This commit is contained in:
wandererfan
2023-03-13 19:01:31 -04:00
committed by WandererFan
parent 45743d4fbd
commit 8a11528a7e
16 changed files with 859 additions and 64 deletions

View File

@@ -25,18 +25,29 @@
# include <TopoDS_Shape.hxx>
#endif
#include <BRep_Tool.hxx>
#include <BRepAdaptor_Curve.hxx>
#include <BRepBuilderAPI_MakeVertex.hxx>
#include <TopExp.hxx>
#include <App/GeoFeature.h>
#include <Base/Console.h>
#include <Mod/Part/App/TopoShape.h>
#include <Mod/PartDesign/App/Body.h>
#include <Mod/PartDesign/App/Feature.h>
#include "DimensionReferences.h"
#include "DrawUtil.h"
#include "DrawViewPart.h"
#include "GeometryObject.h"
using namespace TechDraw;
using DU = DrawUtil;
TopoDS_Shape ReferenceEntry::getGeometry() const
{
// Base::Console().Message("RE::getGeometry()\n");
if ( getObject()->isDerivedFrom(TechDraw::DrawViewPart::getClassTypeId()) ) {
TechDraw::DrawViewPart* dvp = static_cast<TechDraw::DrawViewPart*>(getObject());
std::string gType = geomType();
@@ -50,6 +61,7 @@ TopoDS_Shape ReferenceEntry::getGeometry() const
auto fgeom = dvp->getFace(getSubName());
return fgeom->toOccFace();
}
//Base::Console().Message("RE::getGeometry - returns null shape! - gType: %s\n", gType.c_str());
return TopoDS_Shape();
}
@@ -62,6 +74,7 @@ TopoDS_Shape ReferenceEntry::getGeometry() const
if (getSubName().empty()) {
return shape.getShape();
}
// TODO: what happens if the subelement is no longer present?
return shape.getSubShape(getSubName().c_str());
}
@@ -78,6 +91,58 @@ std::string ReferenceEntry::getSubName(bool longForm) const
return workingSubName;
}
App::DocumentObject* ReferenceEntry::getObject() const
{
// For PartDesign objects, when the reference is created from a selection,
// the SelectionObject is a Feature within the Body.
PartDesign::Body* pdBody = PartDesign::Body::findBodyOf(m_object);
if (pdBody && pdBody->Tip.getValue()) {
return pdBody->Tip.getValue();
}
return m_object;
}
Part::TopoShape ReferenceEntry::asTopoShape()
{
// Base::Console().Message("RE::asTopoShape()\n");
TopoDS_Shape geom = getGeometry();
if (geom.ShapeType() == TopAbs_VERTEX) {
TopoDS_Vertex vert = TopoDS::Vertex(geom);
return asTopoShapeVertex(vert);
} else if (geom.ShapeType() == TopAbs_EDGE) {
TopoDS_Edge edge = TopoDS::Edge(geom);
return asTopoShapeEdge(edge);
} else {
throw Base::RuntimeError("Dimension Reference has unsupported geometry");
}
return Part::TopoShape();
}
Part::TopoShape ReferenceEntry::asTopoShapeVertex(TopoDS_Vertex vert)
{
Base::Vector3d point = DU::toVector3d(BRep_Tool::Pnt(vert));
if (!is3d()) {
TechDraw::DrawViewPart* dvp = static_cast<TechDraw::DrawViewPart*>(getObject());
point = point / dvp->getScale();
}
BRepBuilderAPI_MakeVertex mkVert(DU::togp_Pnt(point));
return Part::TopoShape(mkVert.Vertex());
}
Part::TopoShape ReferenceEntry::asTopoShapeEdge(TopoDS_Edge edge)
{
// Base::Console().Message("RE::asTopoShapeEdge()\n");
TopoDS_Edge unscaledEdge = edge;
if (!is3d()) {
// 2d reference - projected and scaled. scale might have changed, so we need to unscale
TechDraw::DrawViewPart* dvp = static_cast<TechDraw::DrawViewPart*>(getObject());
TopoDS_Shape unscaledShape = TechDraw::scaleShape(edge, 1.0 / dvp->getScale());
unscaledEdge = TopoDS::Edge(unscaledShape);
}
return Part::TopoShape(unscaledEdge);
}
std::string ReferenceEntry::geomType() const
{
return DrawUtil::getGeomTypeFromName(getSubName());
@@ -87,3 +152,12 @@ bool ReferenceEntry::isWholeObject() const
{
return getSubName().empty();
}
bool ReferenceEntry::is3d() const
{
if ( getObject()->isDerivedFrom(TechDraw::DrawViewPart::getClassTypeId()) ) {
return false;
}
return true;
}