[TD]fix dimension autocorrect

- autocorrect was not handling scaled and rotated reference geometry
  properly.
This commit is contained in:
wandererfan
2024-05-05 20:38:14 -04:00
parent 15b612ebca
commit d2d93458e9
17 changed files with 701 additions and 563 deletions

View File

@@ -42,9 +42,11 @@
#include "DrawUtil.h"
#include "DrawViewPart.h"
#include "ShapeUtils.h"
#include "CosmeticVertex.h"
using namespace TechDraw;
using DU = DrawUtil;
using SU = ShapeUtils;
ReferenceEntry::ReferenceEntry( App::DocumentObject* docObject, std::string subName, App::Document* document)
@@ -60,6 +62,7 @@ ReferenceEntry::ReferenceEntry( App::DocumentObject* docObject, std::string subN
}
}
ReferenceEntry::ReferenceEntry(const ReferenceEntry& other)
{
setObject(other.getObject());
@@ -72,6 +75,9 @@ ReferenceEntry::ReferenceEntry(const ReferenceEntry& other)
ReferenceEntry& ReferenceEntry::operator=(const ReferenceEntry& otherRef)
{
if (this == &otherRef) {
return *this;
}
setObject(otherRef.getObject());
setSubName(otherRef.getSubName());
setObjectName(otherRef.getObjectName());
@@ -87,51 +93,19 @@ TopoDS_Shape ReferenceEntry::getGeometry() const
// first, make sure the object has not been deleted!
App::DocumentObject* obj = getDocument()->getObject(getObjectName().c_str());
if (!obj) {
Base::Console().Message("RE::getGeometry - %s no longer exists!\n", getObjectName().c_str());
return {};
}
if (getSubName().empty()) {
Base::Console().Message("RE::getGeometry - Reference has no subelement!\n");
return {};
}
if ( getObject()->isDerivedFrom(TechDraw::DrawViewPart::getClassTypeId()) ) {
// Base::Console().Message("RE::getGeometry - getting 2d geometry\n");
std::string gType;
try {
auto dvp = static_cast<TechDraw::DrawViewPart*>(getObject());
gType = geomType();
if (gType == "Vertex") {
// getVertex throws on not found, but we want to return null
// shape
auto vgeom = dvp->getVertex(getSubName());
if (!vgeom) {
return {};
}
return vgeom->getOCCVertex();
}
if (gType == "Edge") {
auto egeom = dvp->getEdge(getSubName());
if (!egeom) {
return {};
}
return egeom->getOCCEdge();
}
if (gType == "Face") {
auto fgeom = dvp->getFace(getSubName());
if (!fgeom) {
return {};
}
return fgeom->toOccFace();
}
}
catch (...) {
Base::Console().Message("RE::getGeometry - no shape for dimension 2d reference - gType: **%s**\n", gType.c_str());
return {};
}
// 2d geometry from DrawViewPart will be rotated and scaled
return getGeometry2d();
}
// 3d geometry
Part::TopoShape shape = Part::Feature::getTopoShape(getObject());
auto geoFeat = dynamic_cast<App::GeoFeature*>(getObject());
if (geoFeat) {
@@ -145,6 +119,48 @@ TopoDS_Shape ReferenceEntry::getGeometry() const
return shape.getSubShape(getSubName().c_str());
}
//! get a shape for this 2d reference
TopoDS_Shape ReferenceEntry::getGeometry2d() const
{
// Base::Console().Message("RE::getGeometry2d()\n");
std::string gType;
try {
auto dvp = static_cast<TechDraw::DrawViewPart*>(getObject()); //NOLINT cppcoreguidelines-pro-type-static-cast-downcast
gType = geomType();
if (gType == "Vertex") {
// getVertex throws on not found, but we want to return null
// shape
auto vgeom = dvp->getVertex(getSubName());
if (!vgeom) {
return {};
}
return vgeom->getOCCVertex();
}
if (gType == "Edge") {
auto egeom = dvp->getEdge(getSubName());
if (!egeom) {
return {};
}
return egeom->getOCCEdge();
}
if (gType == "Face") {
auto fgeom = dvp->getFace(getSubName());
if (!fgeom) {
return {};
}
return fgeom->toOccFace();
}
}
catch (...) {
Base::Console().Message("RE::getGeometry2d - no shape for dimension 2d reference - gType: **%s**\n", gType.c_str());
return {};
}
return {};
}
std::string ReferenceEntry::getSubName(bool longForm) const
{
if (longForm) {
@@ -158,6 +174,7 @@ std::string ReferenceEntry::getSubName(bool longForm) const
return workingSubName;
}
App::DocumentObject* ReferenceEntry::getObject() const
{
if (!getDocument()) {
@@ -171,13 +188,14 @@ App::DocumentObject* ReferenceEntry::getObject() const
return obj;
}
//! return the reference geometry as a Part::TopoShape.
Part::TopoShape ReferenceEntry::asTopoShape() const
{
// Base::Console().Message("RE::asTopoShape()\n");
// Base::Console().Message("RE::asTopoShape()\n");
TopoDS_Shape geom = getGeometry();
if (geom.IsNull()) {
// throw Base::RuntimeError("Dimension Reference has null geometry");
Base::Console().Message("RE::asTopoShape - reference geometry is null\n");
return {};
}
if (geom.ShapeType() == TopAbs_VERTEX) {
@@ -191,28 +209,48 @@ Part::TopoShape ReferenceEntry::asTopoShape() const
throw Base::RuntimeError("Dimension Reference has unsupported geometry");
}
Part::TopoShape ReferenceEntry::asTopoShapeVertex(TopoDS_Vertex& vert) const
//! returns unscaled, unrotated version of inShape. inShape is assumed to be a 2d shape, but this is not enforced.
Part::TopoShape ReferenceEntry::asCanonicalTopoShape() const
{
Base::Vector3d point = DU::toVector3d(BRep_Tool::Pnt(vert));
if (!is3d()) {
auto dvp = static_cast<TechDraw::DrawViewPart*>(getObject());
point = point / dvp->getScale();
// Base::Console().Message("RE::asCanonicalTopoShape()\n");
if (is3d()) {
return asTopoShape();
}
BRepBuilderAPI_MakeVertex mkVert(DU::togp_Pnt(point));
return { mkVert.Vertex() };
// this is a 2d reference
auto dvp = static_cast<DrawViewPart*>(getObject()); //NOLINT cppcoreguidelines-pro-type-static-cast-downcast
auto rawTopoShape = asTopoShape();
return ReferenceEntry::asCanonicalTopoShape(rawTopoShape, *dvp);
}
Part::TopoShape ReferenceEntry::asTopoShapeEdge(TopoDS_Edge &edge) const
//! static public method returns unscaled, unrotated version of inShape. inShape is assumed to be a 2d shape,
//! but this is not enforced. 3d shapes should not be made canonical.
//! 2d shapes are inverted in Y direction and need to be inverted before and after rotation
//! operations.
Part::TopoShape ReferenceEntry::asCanonicalTopoShape(const Part::TopoShape& inShape, const DrawViewPart& dvp)
{
// 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
auto dvp = static_cast<TechDraw::DrawViewPart*>(getObject());
TopoDS_Shape unscaledShape = ShapeUtils::scaleShape(edge, 1.0 / dvp->getScale());
unscaledEdge = TopoDS::Edge(unscaledShape);
// Base::Console().Message("RE::(static)asCanonicalTopoShape()\n");
gp_Ax2 OXYZ;
auto unscaledShape = SU::scaleShape(inShape.getShape(), 1.0 / dvp.getScale());
if (dvp.Rotation.getValue() != 0.0) {
auto rotationDeg = dvp.Rotation.getValue();
unscaledShape = SU::invertGeometry(unscaledShape);
unscaledShape = SU::rotateShape(unscaledShape, OXYZ, -rotationDeg);
unscaledShape = SU::invertGeometry(unscaledShape);
}
return { unscaledEdge };
return {unscaledShape};
}
Part::TopoShape ReferenceEntry::asTopoShapeVertex(const TopoDS_Vertex& vert)
{
return { vert };
}
Part::TopoShape ReferenceEntry::asTopoShapeEdge(const TopoDS_Edge &edge)
{
return { edge };
}
std::string ReferenceEntry::geomType() const
@@ -226,27 +264,22 @@ bool ReferenceEntry::isWholeObject() const
return getSubName().empty();
}
//! true if this reference point to 3d model geometry
bool ReferenceEntry::is3d() const
{
if (getObject() &&
getObject()->isDerivedFrom(TechDraw::DrawViewPart::getClassTypeId()) &&
!getSubName().empty()) {
// this is a well formed 2d reference
if (!getObject()) {
// we should really fail here?
return false;
}
if (getObject()->isDerivedFrom(TechDraw::DrawViewPart::getClassTypeId())) {
return false;
}
if (getObject() &&
getObject()->isDerivedFrom(TechDraw::DrawViewPart::getClassTypeId()) &&
getSubName().empty()) {
// this is a broken 3d reference, so it should be treated as 3d
return true;
}
// either we have no object or we have an object and it is a 3d object
return true;
}
//! check if this reference has valid geometry in the model
//! true if the target of this reference has a shape
bool ReferenceEntry::hasGeometry() const
{
// Base::Console().Message("RE::hasGeometry()\n");
@@ -256,33 +289,38 @@ bool ReferenceEntry::hasGeometry() const
if ( getObject()->isDerivedFrom(TechDraw::DrawViewPart::getClassTypeId()) ) {
// 2d reference
auto dvp = static_cast<TechDraw::DrawViewPart*>(getObject());
if (getSubName().empty()) {
return false;
}
int geomNumber = DU::getIndexFromName(getSubName());
std::string gType = geomType();
if (gType == "Vertex") {
auto vert = dvp->getProjVertexByIndex(geomNumber);
if (vert) {
return true;
}
} else if (gType == "Edge") {
auto edge = dvp->getGeomByIndex(geomNumber);
if (edge) {
return true;
}
}
// if we ever have dimensions for faces, add something here.
return false;
return hasGeometry2d();
}
// 3d reference
auto shape = Part::Feature::getTopoShape(getObject());
auto subShape = shape.getSubShape(getSubName().c_str());
if (!subShape.IsNull()) {
return true;
}
return !subShape.IsNull();
}
//! check if this 2d reference has valid geometry in the model
bool ReferenceEntry::hasGeometry2d() const
{
auto dvp = static_cast<TechDraw::DrawViewPart*>(getObject()); //NOLINT cppcoreguidelines-pro-type-static-cast-downcast
if (getSubName().empty()) {
return false;
}
int geomNumber = DU::getIndexFromName(getSubName());
std::string gType = geomType();
if (gType == "Vertex") {
auto vert = dvp->getProjVertexByIndex(geomNumber);
if (vert) {
return true;
}
} else if (gType == "Edge") {
auto edge = dvp->getGeomByIndex(geomNumber);
if (edge) {
return true;
}
}
return false;
}