From a48d34f6c2213ef8266f3b58aee142efc199f667 Mon Sep 17 00:00:00 2001 From: PaddleStroke Date: Mon, 29 Jan 2024 18:20:48 +0100 Subject: [PATCH] Assembly: fixes --- src/Mod/Assembly/CommandCreateJoint.py | 19 ++++++++-- src/Mod/Assembly/CommandInsertLink.py | 51 +++++++++++++++++++++++++- src/Mod/Assembly/JointObject.py | 2 +- 3 files changed, 66 insertions(+), 6 deletions(-) diff --git a/src/Mod/Assembly/CommandCreateJoint.py b/src/Mod/Assembly/CommandCreateJoint.py index 1c6ea545c8..cb710d6aea 100644 --- a/src/Mod/Assembly/CommandCreateJoint.py +++ b/src/Mod/Assembly/CommandCreateJoint.py @@ -225,6 +225,20 @@ class CommandCreateJointDistance: activateJoint(5) +def createGroundedJoint(obj): + assembly = UtilsAssembly.activeAssembly() + if not assembly: + return + + joint_group = UtilsAssembly.getJointGroup(assembly) + + obj.Label = obj.Label + " 🔒" + ground = joint_group.newObject("App::FeaturePython", "GroundedJoint") + JointObject.GroundedJoint(ground, obj) + JointObject.ViewProviderGroundedJoint(ground.ViewObject) + return ground + + class CommandToggleGrounded: def __init__(self): pass @@ -294,10 +308,7 @@ class CommandToggleGrounded: continue # Create groundedJoint. - part_containing_obj.Label = part_containing_obj.Label + " 🔒" - ground = joint_group.newObject("App::FeaturePython", "GroundedJoint") - JointObject.GroundedJoint(ground, part_containing_obj) - JointObject.ViewProviderGroundedJoint(ground.ViewObject) + createGroundedJoint(part_containing_obj) App.closeActiveTransaction() diff --git a/src/Mod/Assembly/CommandInsertLink.py b/src/Mod/Assembly/CommandInsertLink.py index 70e19b11df..c0793a9ae3 100644 --- a/src/Mod/Assembly/CommandInsertLink.py +++ b/src/Mod/Assembly/CommandInsertLink.py @@ -33,6 +33,7 @@ if App.GuiUp: import UtilsAssembly import Preferences +import CommandCreateJoint # translate = App.Qt.translate @@ -106,6 +107,7 @@ class TaskAssemblyInsertLink(QtCore.QObject): self.translation = 0 self.partMoving = False self.totalTranslation = App.Vector() + self.groundedObj = None self.insertionStack = [] # used to handle cancellation of insertions. @@ -261,6 +263,49 @@ class TaskAssemblyInsertLink(QtCore.QObject): self.form.partList.setItemSelected(item, False) + if len(self.insertionStack) == 1 and not UtilsAssembly.isAssemblyGrounded(): + self.handleFirstInsertion() + + def handleFirstInsertion(self): + pref = Preferences.preferences() + fixPart = False + fixPartPref = pref.GetInt("GroundFirstPart", 0) + if fixPartPref == 0: # unset + msgBox = QtWidgets.QMessageBox() + msgBox.setWindowTitle("Ground Part?") + msgBox.setText( + "Do you want to ground the first inserted part automatically?\nYou need at least one grounded part in your assembly." + ) + msgBox.setIcon(QtWidgets.QMessageBox.Question) + + yesButton = msgBox.addButton("Yes", QtWidgets.QMessageBox.YesRole) + noButton = msgBox.addButton("No", QtWidgets.QMessageBox.NoRole) + yesAlwaysButton = msgBox.addButton("Always", QtWidgets.QMessageBox.YesRole) + noAlwaysButton = msgBox.addButton("Never", QtWidgets.QMessageBox.NoRole) + + msgBox.exec_() + + clickedButton = msgBox.clickedButton() + if clickedButton == yesButton: + fixPart = True + elif clickedButton == yesAlwaysButton: + fixPart = True + pref.SetInt("GroundFirstPart", 1) + elif clickedButton == noAlwaysButton: + pref.SetInt("GroundFirstPart", 2) + + elif fixPartPref == 1: # Yes always + fixPart = True + + if fixPart: + # Create groundedJoint. + if len(self.insertionStack) != 1: + return + + self.groundedObj = self.insertionStack[0]["addedObject"] + self.groundedJoint = CommandCreateJoint.createGroundedJoint(self.groundedObj) + self.endMove() + def increment_counter(self, item): text = item.text() match = re.search(r"(\d+) inserted$", text) @@ -373,7 +418,11 @@ class TaskAssemblyInsertLink(QtCore.QObject): self.endMove() self.totalTranslation -= stack_item["translation"] - UtilsAssembly.removeObjAndChilds(stack_item["addedObject"]) + obj = stack_item["addedObject"] + if self.groundedObj == obj: + self.groundedJoint.Document.removeObject(self.groundedJoint.Name) + UtilsAssembly.removeObjAndChilds(obj) + self.decrement_counter(item) del self.insertionStack[i] self.form.partList.setItemSelected(item, False) diff --git a/src/Mod/Assembly/JointObject.py b/src/Mod/Assembly/JointObject.py index 328a7651d7..ab1b0e963c 100644 --- a/src/Mod/Assembly/JointObject.py +++ b/src/Mod/Assembly/JointObject.py @@ -941,7 +941,7 @@ class ViewProviderGroundedJoint: # Remove grounded tag. if hasattr(feature.Object, "ObjectToGround"): obj = feature.Object.ObjectToGround - if obj.Label.endswith(" 🔒"): + if obj is not None and obj.Label.endswith(" 🔒"): obj.Label = obj.Label[:-2] return True # If False is returned the object won't be deleted