From 8a6872e69d76cb1c2bf89959335d0acde0a9527c Mon Sep 17 00:00:00 2001 From: PaddleStroke Date: Tue, 3 Feb 2026 23:34:53 +0100 Subject: [PATCH] Merge pull request #25848 from PaddleStroke/sk_scale_distances Sketcher: Distance constraints: scale label distance on creation. --- src/Mod/Sketcher/App/SketchObject.cpp | 4 ++++ src/Mod/Sketcher/App/SketchObject.h | 1 + src/Mod/Sketcher/Gui/CommandConstraints.cpp | 4 ---- src/Mod/Sketcher/Gui/ViewProviderSketch.cpp | 20 ++++++++++++++++++++ src/Mod/Sketcher/Gui/ViewProviderSketch.h | 2 ++ 5 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/Mod/Sketcher/App/SketchObject.cpp b/src/Mod/Sketcher/App/SketchObject.cpp index 92c909b674..d1ee015004 100644 --- a/src/Mod/Sketcher/App/SketchObject.cpp +++ b/src/Mod/Sketcher/App/SketchObject.cpp @@ -2304,6 +2304,8 @@ int SketchObject::addConstraints(const std::vector& ConstraintList) } addGeometryState(cnew); + + signalConstraintAdded(cnew); } this->Constraints.setValues(std::move(newVals)); @@ -2377,6 +2379,8 @@ int SketchObject::addConstraint(std::unique_ptr constraint) addGeometryState(constNew); + signalConstraintAdded(constNew); + newVals.push_back(constNew);// add new constraint at the back this->Constraints.setValues(std::move(newVals)); diff --git a/src/Mod/Sketcher/App/SketchObject.h b/src/Mod/Sketcher/App/SketchObject.h index b868ab5131..de3b6bda8e 100644 --- a/src/Mod/Sketcher/App/SketchObject.h +++ b/src/Mod/Sketcher/App/SketchObject.h @@ -973,6 +973,7 @@ public: // Signaled when solver has done update fastsignals::signal signalSolverUpdate; fastsignals::signal signalElementsChanged; + fastsignals::signal signalConstraintAdded; Part::TopoShape buildInternals(const Part::TopoShape& edges) const; diff --git a/src/Mod/Sketcher/Gui/CommandConstraints.cpp b/src/Mod/Sketcher/Gui/CommandConstraints.cpp index c01f9b93ef..3505e56c8e 100644 --- a/src/Mod/Sketcher/Gui/CommandConstraints.cpp +++ b/src/Mod/Sketcher/Gui/CommandConstraints.cpp @@ -107,7 +107,6 @@ void finishDatumConstraint(Gui::Command* cmd, // Guess some reasonable distance for placing the datum text Gui::Document* doc = cmd->getActiveGuiDocument(); - float scaleFactor = 1.0; double labelPosition = 0.0; float labelPositionRandomness = 0.0; @@ -131,13 +130,10 @@ void finishDatumConstraint(Gui::Command* cmd, && doc->getInEdit()->isDerivedFrom()) { SketcherGui::ViewProviderSketch* vp = static_cast(doc->getInEdit()); - scaleFactor = vp->getScaleFactor(); int firstConstraintIndex = lastConstraintIndex - numberofconstraints + 1; for (int i = lastConstraintIndex; i >= firstConstraintIndex; i--) { - ConStr[i]->LabelDistance = 2. * scaleFactor; - if (lastConstraintType == Radius || lastConstraintType == Diameter) { const Part::Geometry* geo = sketch->getGeometry(ConStr[i]->First); diff --git a/src/Mod/Sketcher/Gui/ViewProviderSketch.cpp b/src/Mod/Sketcher/Gui/ViewProviderSketch.cpp index ff821cdec2..89a06f8f54 100644 --- a/src/Mod/Sketcher/Gui/ViewProviderSketch.cpp +++ b/src/Mod/Sketcher/Gui/ViewProviderSketch.cpp @@ -3314,6 +3314,23 @@ void ViewProviderSketch::slotSolverUpdate() } } +void ViewProviderSketch::slotConstraintAdded(Sketcher::Constraint* constraint) +{ + if (!constraint) { + return; + } + + // Auto-scale label distance for dimensional constraints + if (constraint->Type == Distance || constraint->Type == DistanceX || constraint->Type == DistanceY) { + // If label distance is default (10.0), scale it based on view. + // We use a small epsilon for float comparison. + if (std::abs(constraint->LabelDistance - 10.f) < 1e-5) { + float scale = getScaleFactor(); + constraint->LabelDistance = 2.f * scale; + } + } +} + void ViewProviderSketch::onChanged(const App::Property* prop) { ViewProvider2DObject::onChanged(prop); @@ -3605,6 +3622,8 @@ bool ViewProviderSketch::setEdit(int ModNum) } //NOLINTBEGIN + connectConstraintAdded = getSketchObject()->signalConstraintAdded.connect( + std::bind(&ViewProviderSketch::slotConstraintAdded, this, sp::_1)); connectUndoDocument = getDocument()->signalUndoDocument.connect( std::bind(&ViewProviderSketch::slotUndoDocument, this, sp::_1)); connectRedoDocument = getDocument()->signalRedoDocument.connect( @@ -3821,6 +3840,7 @@ void ViewProviderSketch::unsetEdit(int ModNum) connectUndoDocument.disconnect(); connectRedoDocument.disconnect(); connectSolverUpdate.disconnect(); + connectConstraintAdded.disconnect(); // when pressing ESC make sure to close the dialog Gui::Control().closeDialog(); diff --git a/src/Mod/Sketcher/Gui/ViewProviderSketch.h b/src/Mod/Sketcher/Gui/ViewProviderSketch.h index 86241746fd..0a27cd4f70 100644 --- a/src/Mod/Sketcher/Gui/ViewProviderSketch.h +++ b/src/Mod/Sketcher/Gui/ViewProviderSketch.h @@ -773,6 +773,7 @@ protected: void slotUndoDocument(const Gui::Document&); void slotRedoDocument(const Gui::Document&); void slotSolverUpdate(); + void slotConstraintAdded(Sketcher::Constraint* constraint); void forceUpdateData(); //@} @@ -972,6 +973,7 @@ private: fastsignals::connection connectUndoDocument; fastsignals::connection connectRedoDocument; fastsignals::connection connectSolverUpdate; + fastsignals::connection connectConstraintAdded; QMetaObject::Connection screenChangeConnection;