diff --git a/src/Mod/Sketcher/Gui/CommandSketcherBSpline.cpp b/src/Mod/Sketcher/Gui/CommandSketcherBSpline.cpp index 326709f3ee..b99c192650 100644 --- a/src/Mod/Sketcher/Gui/CommandSketcherBSpline.cpp +++ b/src/Mod/Sketcher/Gui/CommandSketcherBSpline.cpp @@ -173,7 +173,7 @@ void CmdSketcherConvertToNURBS::activated(int iMsg) bool CmdSketcherConvertToNURBS::isActive() { - return isSketcherBSplineActive(getActiveGuiDocument(), true); + return isCommandNeedingGeometryActive(getActiveGuiDocument()); } // Increase degree of the spline @@ -208,7 +208,7 @@ void CmdSketcherIncreaseDegree::activated(int iMsg) // get the needed lists and objects const std::vector& SubNames = selection[0].getSubNames(); - Sketcher::SketchObject* Obj = static_cast(selection[0].getObject()); + auto* Obj = static_cast(selection[0].getObject()); openCommand(QT_TRANSLATE_NOOP("Command", "Increase B-spline degree")); @@ -221,13 +221,9 @@ void CmdSketcherIncreaseDegree::activated(int iMsg) const Part::Geometry* geo = Obj->getGeometry(GeoId); if (geo->is()) { - Gui::cmdAppObjectArgs(selection[0].getObject(), - "increaseBSplineDegree(%d) ", - GeoId); + Gui::cmdAppObjectArgs(Obj, "increaseBSplineDegree(%d) ", GeoId); // add new control points - Gui::cmdAppObjectArgs(selection[0].getObject(), - "exposeInternalGeometry(%d)", - GeoId); + Gui::cmdAppObjectArgs(Obj, "exposeInternalGeometry(%d)", GeoId); } else { ignored = true; @@ -249,7 +245,7 @@ void CmdSketcherIncreaseDegree::activated(int iMsg) bool CmdSketcherIncreaseDegree::isActive() { - return isSketcherBSplineActive(getActiveGuiDocument(), true); + return isCommandNeedingBSplineActive(getActiveGuiDocument()); } @@ -332,9 +328,39 @@ void CmdSketcherDecreaseDegree::activated(int iMsg) bool CmdSketcherDecreaseDegree::isActive() { - return isSketcherBSplineActive(getActiveGuiDocument(), true); + return isCommandNeedingBSplineActive(getActiveGuiDocument()); } +bool isCommandNeedingBSplineKnotActive(Gui::Document* doc) +{ + if (!isCommandActive(doc)) { + return false; + } + + std::vector sel = + Gui::Selection().getSelectionEx(doc->getDocument()->getName(), + Sketcher::SketchObject::getClassTypeId()); + if (sel.size() == 1) { + const std::vector& names = sel[0].getSubNames(); + if (names.size() != 1) { + return false; + } + + auto* Obj = static_cast(sel[0].getObject()); + const std::string& name = names[0]; + + int geoId {GeoEnum::GeoUndef}; + PointPos posId {PointPos::none}; + getIdsFromName(name, Obj, geoId, posId); + + int splineGeoId {GeoEnum::GeoUndef}; + int knotIndexOCC {-1}; + + return isBsplineKnotOrEndPoint(Obj, geoId, posId) + && findBSplineAndKnotIndex(Obj, geoId, posId, splineGeoId, knotIndexOCC); + } + return false; +} DEF_STD_CMD_A(CmdSketcherIncreaseKnotMultiplicity) @@ -480,7 +506,7 @@ void CmdSketcherIncreaseKnotMultiplicity::activated(int iMsg) bool CmdSketcherIncreaseKnotMultiplicity::isActive() { - return isSketcherBSplineActive(getActiveGuiDocument(), true); + return isCommandNeedingBSplineKnotActive(getActiveGuiDocument()); } DEF_STD_CMD_A(CmdSketcherDecreaseKnotMultiplicity) @@ -615,7 +641,7 @@ void CmdSketcherDecreaseKnotMultiplicity::activated(int iMsg) bool CmdSketcherDecreaseKnotMultiplicity::isActive() { - return isSketcherBSplineActive(getActiveGuiDocument(), true); + return isCommandNeedingBSplineKnotActive(getActiveGuiDocument()); } @@ -717,7 +743,7 @@ void CmdSketcherCompModifyKnotMultiplicity::updateAction(int /*mode*/) bool CmdSketcherCompModifyKnotMultiplicity::isActive() { - return isSketcherBSplineActive(getActiveGuiDocument(), false); + return isCommandNeedingBSplineKnotActive(getActiveGuiDocument()); } class DrawSketchHandlerBSplineInsertKnot: public DrawSketchHandler @@ -940,7 +966,7 @@ void CmdSketcherInsertKnot::activated(int iMsg) bool CmdSketcherInsertKnot::isActive() { - return isSketcherBSplineActive(getActiveGuiDocument(), true); + return isCommandNeedingBSplineActive(getActiveGuiDocument()); } DEF_STD_CMD_A(CmdSketcherJoinCurves) @@ -1096,7 +1122,7 @@ void CmdSketcherJoinCurves::activated(int iMsg) bool CmdSketcherJoinCurves::isActive() { - return isSketcherBSplineActive(getActiveGuiDocument(), true); + return isCommandNeedingBSplineActive(getActiveGuiDocument()); } void CreateSketcherCommandsBSpline() diff --git a/src/Mod/Sketcher/Gui/CommandSketcherOverlay.cpp b/src/Mod/Sketcher/Gui/CommandSketcherOverlay.cpp index f33d11ea98..97af887df9 100644 --- a/src/Mod/Sketcher/Gui/CommandSketcherOverlay.cpp +++ b/src/Mod/Sketcher/Gui/CommandSketcherOverlay.cpp @@ -79,7 +79,7 @@ void CmdSketcherBSplineDegree::activated(int iMsg) bool CmdSketcherBSplineDegree::isActive() { - return isSketcherBSplineActive(getActiveGuiDocument(), false); + return isCommandActive(getActiveGuiDocument()); } // Show/Hide B-spline polygon @@ -108,7 +108,7 @@ void CmdSketcherBSplinePolygon::activated(int iMsg) bool CmdSketcherBSplinePolygon::isActive() { - return isSketcherBSplineActive(getActiveGuiDocument(), false); + return isCommandActive(getActiveGuiDocument()); } // Show/Hide B-spline comb @@ -137,7 +137,7 @@ void CmdSketcherBSplineComb::activated(int iMsg) bool CmdSketcherBSplineComb::isActive() { - return isSketcherBSplineActive(getActiveGuiDocument(), false); + return isCommandActive(getActiveGuiDocument()); } // @@ -166,7 +166,7 @@ void CmdSketcherBSplineKnotMultiplicity::activated(int iMsg) bool CmdSketcherBSplineKnotMultiplicity::isActive() { - return isSketcherBSplineActive(getActiveGuiDocument(), false); + return isCommandActive(getActiveGuiDocument()); } // @@ -195,7 +195,7 @@ void CmdSketcherBSplinePoleWeight::activated(int iMsg) bool CmdSketcherBSplinePoleWeight::isActive() { - return isSketcherBSplineActive(getActiveGuiDocument(), false); + return isCommandActive(getActiveGuiDocument()); } // Composite drop down menu for show/hide BSpline information layer @@ -341,7 +341,7 @@ void CmdSketcherCompBSplineShowHideGeometryInformation::updateAction(int /*mode* bool CmdSketcherCompBSplineShowHideGeometryInformation::isActive() { - return isSketcherBSplineActive(getActiveGuiDocument(), false); + return isCommandActive(getActiveGuiDocument()); } // diff --git a/src/Mod/Sketcher/Gui/CommandSketcherTools.cpp b/src/Mod/Sketcher/Gui/CommandSketcherTools.cpp index d3e261545f..47b3e9e205 100644 --- a/src/Mod/Sketcher/Gui/CommandSketcherTools.cpp +++ b/src/Mod/Sketcher/Gui/CommandSketcherTools.cpp @@ -238,7 +238,7 @@ void CmdSketcherCopyClipboard::activated(int iMsg) bool CmdSketcherCopyClipboard::isActive() { - return isCommandActive(getActiveGuiDocument(), true); + return isCommandNeedingGeometryActive(getActiveGuiDocument()); } // ================================================================================ @@ -278,7 +278,7 @@ void CmdSketcherCut::activated(int iMsg) bool CmdSketcherCut::isActive() { - return isCommandActive(getActiveGuiDocument(), true); + return isCommandNeedingGeometryActive(getActiveGuiDocument()); } // ================================================================================ @@ -327,7 +327,7 @@ void CmdSketcherPaste::activated(int iMsg) bool CmdSketcherPaste::isActive() { - return isCommandActive(getActiveGuiDocument(), false); + return isCommandActive(getActiveGuiDocument()); } // ================================================================================ @@ -411,7 +411,7 @@ void CmdSketcherSelectConstraints::activated(int iMsg) bool CmdSketcherSelectConstraints::isActive() { - return isCommandActive(getActiveGuiDocument(), true); + return isCommandNeedingGeometryActive(getActiveGuiDocument()); } // ================================================================================ @@ -455,7 +455,7 @@ void CmdSketcherSelectOrigin::activated(int iMsg) bool CmdSketcherSelectOrigin::isActive() { - return isCommandActive(getActiveGuiDocument(), false); + return isCommandActive(getActiveGuiDocument()); } // ================================================================================ @@ -496,7 +496,7 @@ void CmdSketcherSelectVerticalAxis::activated(int iMsg) bool CmdSketcherSelectVerticalAxis::isActive() { - return isCommandActive(getActiveGuiDocument(), false); + return isCommandActive(getActiveGuiDocument()); } // ================================================================================ @@ -537,7 +537,7 @@ void CmdSketcherSelectHorizontalAxis::activated(int iMsg) bool CmdSketcherSelectHorizontalAxis::isActive() { - return isCommandActive(getActiveGuiDocument(), false); + return isCommandActive(getActiveGuiDocument()); } // ================================================================================ @@ -595,7 +595,7 @@ void CmdSketcherSelectRedundantConstraints::activated(int iMsg) bool CmdSketcherSelectRedundantConstraints::isActive() { - return isCommandActive(getActiveGuiDocument(), false); + return isCommandActive(getActiveGuiDocument()); } // ================================================================================ @@ -650,7 +650,7 @@ void CmdSketcherSelectMalformedConstraints::activated(int iMsg) bool CmdSketcherSelectMalformedConstraints::isActive() { - return isCommandActive(getActiveGuiDocument(), false); + return isCommandActive(getActiveGuiDocument()); } // ================================================================================ @@ -706,7 +706,7 @@ void CmdSketcherSelectPartiallyRedundantConstraints::activated(int iMsg) bool CmdSketcherSelectPartiallyRedundantConstraints::isActive() { - return isCommandActive(getActiveGuiDocument(), false); + return isCommandActive(getActiveGuiDocument()); } // ================================================================================ @@ -763,7 +763,7 @@ void CmdSketcherSelectConflictingConstraints::activated(int iMsg) bool CmdSketcherSelectConflictingConstraints::isActive() { - return isCommandActive(getActiveGuiDocument(), false); + return isCommandActive(getActiveGuiDocument()); } // ================================================================================ @@ -884,7 +884,7 @@ void CmdSketcherSelectElementsAssociatedWithConstraints::activated(int iMsg) bool CmdSketcherSelectElementsAssociatedWithConstraints::isActive() { - return isCommandActive(getActiveGuiDocument(), true); + return isCommandNeedingConstraintActive(getActiveGuiDocument()); } // ================================================================================ @@ -973,7 +973,7 @@ void CmdSketcherSelectElementsWithDoFs::activated(int iMsg) bool CmdSketcherSelectElementsWithDoFs::isActive() { - return isCommandActive(getActiveGuiDocument(), false); + return isCommandActive(getActiveGuiDocument()); } // ================================================================================ @@ -1088,7 +1088,7 @@ void CmdSketcherRestoreInternalAlignmentGeometry::activated(int iMsg) bool CmdSketcherRestoreInternalAlignmentGeometry::isActive() { - return isCommandActive(getActiveGuiDocument(), true); + return isCommandNeedingGeometryActive(getActiveGuiDocument()); } // ================================================================================ @@ -1123,7 +1123,7 @@ void CmdSketcherSymmetry::activated(int iMsg) bool CmdSketcherSymmetry::isActive() { - return isCommandActive(getActiveGuiDocument(), true); + return isCommandNeedingGeometryActive(getActiveGuiDocument()); } // ================================================================================ @@ -1501,7 +1501,7 @@ void CmdSketcherCopy::activate() bool CmdSketcherCopy::isActive() { - return isCommandActive(getActiveGuiDocument(), true); + return isCommandNeedingGeometryActive(getActiveGuiDocument()); } // ================================================================================ @@ -1551,7 +1551,7 @@ void CmdSketcherClone::activate() bool CmdSketcherClone::isActive() { - return isCommandActive(getActiveGuiDocument(), true); + return isCommandNeedingGeometryActive(getActiveGuiDocument()); } class CmdSketcherMove: public SketcherCopy @@ -1598,7 +1598,7 @@ void CmdSketcherMove::activate() bool CmdSketcherMove::isActive() { - return isCommandActive(getActiveGuiDocument(), true); + return isCommandNeedingGeometryActive(getActiveGuiDocument()); } // ================================================================================ @@ -1709,7 +1709,7 @@ void CmdSketcherCompCopy::languageChange() bool CmdSketcherCompCopy::isActive() { - return isCommandActive(getActiveGuiDocument(), true); + return isCommandNeedingGeometryActive(getActiveGuiDocument()); } // ================================================================================ @@ -2058,7 +2058,7 @@ void CmdSketcherRectangularArray::activated(int iMsg) bool CmdSketcherRectangularArray::isActive() { - return isCommandActive(getActiveGuiDocument(), true); + return isCommandNeedingGeometryActive(getActiveGuiDocument()); } // ================================================================================ @@ -2123,7 +2123,7 @@ void CmdSketcherDeleteAllGeometry::activated(int iMsg) bool CmdSketcherDeleteAllGeometry::isActive() { - return isCommandActive(getActiveGuiDocument(), false); + return isCommandActive(getActiveGuiDocument()); } // ================================================================================ @@ -2189,7 +2189,7 @@ void CmdSketcherDeleteAllConstraints::activated(int iMsg) bool CmdSketcherDeleteAllConstraints::isActive() { - return isCommandActive(getActiveGuiDocument(), false); + return isCommandActive(getActiveGuiDocument()); } // ================================================================================ @@ -2310,7 +2310,7 @@ void CmdSketcherRemoveAxesAlignment::activated(int iMsg) bool CmdSketcherRemoveAxesAlignment::isActive() { - return isCommandActive(getActiveGuiDocument(), true); + return isCommandNeedingGeometryActive(getActiveGuiDocument()); } @@ -2393,7 +2393,7 @@ void CmdSketcherOffset::activated(int iMsg) bool CmdSketcherOffset::isActive() { - return isCommandActive(getActiveGuiDocument(), true); + return isCommandNeedingGeometryActive(getActiveGuiDocument()); } // Rotate tool ===================================================================== @@ -2427,7 +2427,7 @@ void CmdSketcherRotate::activated(int iMsg) bool CmdSketcherRotate::isActive() { - return isCommandActive(getActiveGuiDocument(), true); + return isCommandNeedingGeometryActive(getActiveGuiDocument()); } // Scale tool ===================================================================== @@ -2461,7 +2461,7 @@ void CmdSketcherScale::activated(int iMsg) bool CmdSketcherScale::isActive() { - return isCommandActive(getActiveGuiDocument(), true); + return isCommandNeedingGeometryActive(getActiveGuiDocument()); } // Translate / rectangular pattern tool ======================================================= @@ -2495,7 +2495,7 @@ void CmdSketcherTranslate::activated(int iMsg) bool CmdSketcherTranslate::isActive() { - return isCommandActive(getActiveGuiDocument(), true); + return isCommandNeedingGeometryActive(getActiveGuiDocument()); } void CreateSketcherCommandsConstraintAccel() diff --git a/src/Mod/Sketcher/Gui/Utils.cpp b/src/Mod/Sketcher/Gui/Utils.cpp index 485d3fc033..91dfc948fa 100644 --- a/src/Mod/Sketcher/Gui/Utils.cpp +++ b/src/Mod/Sketcher/Gui/Utils.cpp @@ -493,7 +493,7 @@ bool SketcherGui::isSketchInEdit(Gui::Document* doc) return false; } -bool SketcherGui::isCommandActive(Gui::Document* doc, bool actsOnSelection) +bool SketcherGui::isCommandActive(Gui::Document* doc) { if (isSketchInEdit(doc)) { auto mode = @@ -501,29 +501,79 @@ bool SketcherGui::isCommandActive(Gui::Document* doc, bool actsOnSelection) if (mode == ViewProviderSketch::STATUS_NONE || mode == ViewProviderSketch::STATUS_SKETCH_UseHandler) { - - if (!actsOnSelection) { - return true; - } - return Gui::Selection().countObjectsOfType() > 0; + return true; } } return false; } -bool SketcherGui::isSketcherBSplineActive(Gui::Document* doc, bool actsOnSelection) +bool SketcherGui::isCommandNeedingConstraintActive(Gui::Document* doc) { - if (doc) { - // checks if a Sketch Viewprovider is in Edit and is in no special mode - if (doc->getInEdit() - && doc->getInEdit()->isDerivedFrom()) { - if (static_cast(doc->getInEdit())->getSketchMode() - == ViewProviderSketch::STATUS_NONE) { - if (!actsOnSelection) { + if (!isCommandActive(doc)) { + return false; + } + + std::vector sel = + Gui::Selection().getSelectionEx(doc->getDocument()->getName(), + Sketcher::SketchObject::getClassTypeId()); + if (sel.size() == 1) { + for (const std::string& name : sel[0].getSubNames()) { + if (name.starts_with("Constraint")) { + return true; + } + } + } + return false; +} + +bool SketcherGui::isCommandNeedingGeometryActive(Gui::Document* doc) +{ + if (!isCommandActive(doc)) { + return false; + } + + std::vector sel = + Gui::Selection().getSelectionEx(doc->getDocument()->getName(), + Sketcher::SketchObject::getClassTypeId()); + if (sel.size() == 1) { + auto* Obj = static_cast(sel[0].getObject()); + for (const std::string& name : sel[0].getSubNames()) { + int geoId {GeoEnum::GeoUndef}; + PointPos posId {PointPos::none}; + getIdsFromName(name, Obj, geoId, posId); + + if (geoId != GeoEnum::GeoUndef) { + return true; + } + } + } + return false; +} + +bool SketcherGui::isCommandNeedingBSplineActive(Gui::Document* doc) +{ + if (!isCommandActive(doc)) { + return false; + } + + std::vector sel = + Gui::Selection().getSelectionEx(doc->getDocument()->getName(), + Sketcher::SketchObject::getClassTypeId()); + if (sel.size() == 1) { + auto* Obj = static_cast(sel[0].getObject()); + for (const std::string& name : sel[0].getSubNames()) { + + int geoId {GeoEnum::GeoUndef}; + PointPos posId {PointPos::none}; + getIdsFromName(name, Obj, geoId, posId); + + if (geoId != GeoEnum::GeoUndef) { + const Part::Geometry* geo = Obj->getGeometry(geoId); + + if (geo && geo->is()) { return true; } - return Gui::Selection().countObjectsOfType() > 0; } } } diff --git a/src/Mod/Sketcher/Gui/Utils.h b/src/Mod/Sketcher/Gui/Utils.h index 0e354bf06d..f41c23387b 100644 --- a/src/Mod/Sketcher/Gui/Utils.h +++ b/src/Mod/Sketcher/Gui/Utils.h @@ -196,9 +196,10 @@ bool isSketchInEdit(Gui::Document* doc); /// Returns whether an edit mode command should be activated or not. It is only activated if the /// sketcher is no special state or a sketchHandler is active. -bool isCommandActive(Gui::Document* doc, bool actsOnSelection = false); - -bool isSketcherBSplineActive(Gui::Document* doc, bool actsOnSelection); +bool isCommandActive(Gui::Document* doc); +bool isCommandNeedingConstraintActive(Gui::Document* doc); +bool isCommandNeedingGeometryActive(Gui::Document* doc); +bool isCommandNeedingBSplineActive(Gui::Document* doc); SketcherGui::ViewProviderSketch* getInactiveHandlerEditModeSketchViewProvider(Gui::Document* doc);