From fb9b3a9e9eb155bb413553d1d9c742d06dfdde15 Mon Sep 17 00:00:00 2001 From: wandererfan Date: Sun, 12 Mar 2023 10:33:53 -0400 Subject: [PATCH] [TD]fix centering of shape - centering of shape was not affecting the OCC TShape, so we now make a deep copy of the original --- src/Mod/TechDraw/App/DrawViewPart.cpp | 57 +++++++++++++++++++++------ src/Mod/TechDraw/App/DrawViewPart.h | 4 ++ 2 files changed, 50 insertions(+), 11 deletions(-) diff --git a/src/Mod/TechDraw/App/DrawViewPart.cpp b/src/Mod/TechDraw/App/DrawViewPart.cpp index 062f9f944d..92090d5944 100644 --- a/src/Mod/TechDraw/App/DrawViewPart.cpp +++ b/src/Mod/TechDraw/App/DrawViewPart.cpp @@ -318,11 +318,21 @@ void DrawViewPart::partExec(TopoDS_Shape& shape) //prepare the shape for HLR processing by centering, scaling and rotating it GeometryObjectPtr DrawViewPart::makeGeometryForShape(TopoDS_Shape& shape) { - // Base::Console().Message("DVP::makeGeometryForShape() - %s\n", getNameInDocument()); - gp_Pnt inputCenter = TechDraw::findCentroid(shape, getProjectionCS()); - m_saveCentroid = DU::toVector3d(inputCenter); - m_saveShape = centerScaleRotate(this, shape, m_saveCentroid); - GeometryObjectPtr go = buildGeometryObject(shape, getProjectionCS()); +// Base::Console().Message("DVP::makeGeometryForShape() - %s\n", getNameInDocument()); + + // if we use the passed reference directly, the centering doesn't work. Maybe the underlying OCC TShape + // isn't modified? using a copy works and the referenced shape (from getSourceShape in execute()) + // isn't used for anything anyway. + bool copyGeometry = true; + bool copyMesh = false; + BRepBuilderAPI_Copy copier(shape, copyGeometry, copyMesh); + TopoDS_Shape localShape = copier.Shape(); + + gp_Pnt gCentroid = TechDraw::findCentroid(localShape, getProjectionCS()); + m_saveCentroid = DU::toVector3d(gCentroid); + m_saveShape = centerScaleRotate(this, localShape, m_saveCentroid); + + GeometryObjectPtr go = buildGeometryObject(localShape, getProjectionCS()); return go; } @@ -330,7 +340,7 @@ GeometryObjectPtr DrawViewPart::makeGeometryForShape(TopoDS_Shape& shape) TopoDS_Shape DrawViewPart::centerScaleRotate(DrawViewPart* dvp, TopoDS_Shape& inOutShape, Base::Vector3d centroid) { - // Base::Console().Message("DVP::centerScaleRotate() - %s\n", dvp->getNameInDocument()); +// Base::Console().Message("DVP::centerScaleRotate() - %s\n", dvp->getNameInDocument()); gp_Ax2 viewAxis = dvp->getProjectionCS(); //center shape on origin @@ -345,12 +355,11 @@ TopoDS_Shape DrawViewPart::centerScaleRotate(DrawViewPart* dvp, TopoDS_Shape& in return centeredShape; } - //create a geometry object and trigger the HLR process in another thread TechDraw::GeometryObjectPtr DrawViewPart::buildGeometryObject(TopoDS_Shape& shape, const gp_Ax2& viewAxis) { - // Base::Console().Message("DVP::buildGeometryObject() - %s\n", getNameInDocument()); +// Base::Console().Message("DVP::buildGeometryObject() - %s\n", getNameInDocument()); showProgressMessage(getNameInDocument(), "is finding hidden lines"); TechDraw::GeometryObjectPtr go( @@ -366,6 +375,15 @@ TechDraw::GeometryObjectPtr DrawViewPart::buildGeometryObject(TopoDS_Shape& shap go->projectShapeWithPolygonAlgo(shape, viewAxis); } else { + // TODO: we should give the thread its own copy of the shape because the passed one will be + // destroyed when the call stack we followed to get here unwinds and the thread could still be + // running. + // Should we pass a smart pointer instead of const& ?? + // bool copyGeometry = true; + // bool copyMesh = false; + // BRepBuilderAPI_Copy copier(shape, copyGeometry, copyMesh); + // copier.Shape(); + //projectShape (the HLR process) runs in a separate thread since it can take a long time //note that &m_hlrWatcher in the third parameter is not strictly required, but using the //4 parameter signature instead of the 3 parameter signature prevents clazy warning: @@ -1144,11 +1162,28 @@ Base::Vector3d DrawViewPart::getOriginalCentroid() const { return m_saveCentroid Base::Vector3d DrawViewPart::getCurrentCentroid() const { TopoDS_Shape shape = getSourceShape(); - gp_Ax2 cs = getProjectionCS(Base::Vector3d(0.0, 0.0, 0.0)); - Base::Vector3d center = TechDraw::findCentroidVec(shape, cs); - return center; + if (shape.IsNull()) { + return Base::Vector3d(0.0, 0.0, 0.0); + } + gp_Ax2 cs = getProjectionCS(); + gp_Pnt gCenter = TechDraw::findCentroid(shape, cs); + return DU::toVector3d(gCenter); } + +Base::Vector3d DrawViewPart::getLocalOrigin3d() const +{ + return getCurrentCentroid(); +} + +Base::Vector3d DrawViewPart::getLocalOrigin2d() const +{ + Base::Vector3d centroid = getCurrentCentroid(); + Base::Vector3d localOrigin = projectPoint(centroid, false); + return localOrigin; +} + + std::vector DrawViewPart::getSectionRefs() const { std::vector result; diff --git a/src/Mod/TechDraw/App/DrawViewPart.h b/src/Mod/TechDraw/App/DrawViewPart.h index 24c08c0fc3..780fb671d1 100644 --- a/src/Mod/TechDraw/App/DrawViewPart.h +++ b/src/Mod/TechDraw/App/DrawViewPart.h @@ -167,6 +167,9 @@ public: gp_Ax2 localVectorToCS(const Base::Vector3d localUnit) const; Base::Vector3d localVectorToDirection(const Base::Vector3d localUnit) const; + Base::Vector3d getLocalOrigin3d() const; + Base::Vector3d getLocalOrigin2d() const; + bool handleFaces(); bool newFaceFinder(); @@ -282,6 +285,7 @@ private: QMetaObject::Connection connectFaceWatcher; QFutureWatcher m_faceWatcher; QFuture m_faceFuture; + }; using DrawViewPartPython = App::FeaturePythonT;