-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to make unfold sketch updated on base geometry changes #40
Comments
The unfold is a single one time task and not linked to the object. There is no way to update it, you need to delete the existing and create a new one. |
Do you think this is a |
There are 2 reason why it is not connected (and probably will not):
|
Based on this reason, it might be an option to handle the update with a button.
I'm new to FreeCAD, but I think this depends on how correctly you designed a part. So the "update" button might generate an error if name is changed, else it would be performed smoothly. |
If you have to press a button to refresh you can as well press the unfold button again... |
When a TechDraw page is created based on the unfold sketch, the TechDraw requires the same source sketch too. That makes things complex if you have more than one solid to unfold where their TechDraw pages are already generated, because you need to take care of correct namings too. |
This is a good point... |
I was wrong. The name of the face keeps changing even if you never perform an operation that changes the face geometry. However, there might be a way to go with Assembly3: realthunder/FreeCAD_assembly3#236 |
Here is the proof of concept that works only with Assembly3:
diff --git a/SheetMetalUnfolder.py b/SheetMetalUnfolder.py
index d6b8821..05655c3 100644
--- a/SheetMetalUnfolder.py
+++ b/SheetMetalUnfolder.py
@@ -2456,3 +2456,42 @@ class SMUnfoldCommandClass():
Gui.addCommand('SMUnfold',SMUnfoldCommandClass())
+class SMUnfoldUnattendedCommandClass():
+ """Unfold object"""
+
+ def GetResources(self):
+ __dir__ = os.path.dirname(__file__)
+ iconPath = os.path.join( __dir__, 'Resources', 'icons' )
+ return {'Pixmap' : os.path.join( iconPath , 'SMUnfold.svg') , # the name of a svg file available in the resources
+ 'MenuText': "Unfold" ,
+ 'ToolTip' : "Flatten folded sheet metal object"}
+
+ def Activated(self):
+ SMMessage(["unattended unfold..."])
+ taskd = SMUnfoldTaskPanel()
+ pg = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/sheetmetal")
+ if pg.GetBool("bendSketch"):
+ taskd.checkSeparate.setCheckState(QtCore.Qt.CheckState.Checked)
+ else:
+ taskd.checkSeparate.setCheckState(QtCore.Qt.CheckState.Unchecked)
+ if pg.GetBool("genSketch"):
+ taskd.checkSketch.setCheckState(QtCore.Qt.CheckState.Checked)
+ else:
+ taskd.checkSketch.setCheckState(QtCore.Qt.CheckState.Unchecked)
+ taskd.bendColor.setColor(pg.GetString("bendColor"))
+ taskd.genColor.setColor(pg.GetString("genColor"))
+ taskd.internalColor.setColor(pg.GetString("intColor"))
+ taskd.accept()
+ return
+
+ def IsActive(self):
+ if len(Gui.Selection.getSelection()) != 1 or len(Gui.Selection.getSelectionEx()[0].SubElementNames) != 1:
+ return False
+ selobj = Gui.Selection.getSelection()[0]
+ selFace = Gui.Selection.getSelectionEx()[0].SubObjects[0]
+ if type(selFace) != Part.Face:
+ return False
+ return True
+
+
+Gui.addCommand("SMUnfoldUnattended",SMUnfoldUnattendedCommandClass())
"""
Usage:
1. Put your processed object into an assembly container.
2. Create an "Element" for the unfold face
3. Rename the element to "foo_unfold_face"
4. Run this macro
5. See "foo_unfold" sketch is created properly.
6. See any TechDraw ProjGroup source is updated properly
"""
import FreeCAD
import re
echo = FreeCAD.Console.PrintMessage
warn = FreeCAD.Console.PrintWarning
prev_workbench = Gui.activeWorkbench().name()
Gui.activateWorkbench("SMWorkbench")
if prev_workbench:
Gui.activateWorkbench(prev_workbench)
doc = App.ActiveDocument
unfold_sketch_regex = re.compile('.+_unfold_face')
for o in doc.Objects:
if o.TypeId == 'Part::FeaturePython':
match = unfold_sketch_regex.match(o.Label)
if match:
output_name = o.Label[:-5] # remove "_face" postfix
echo("Found unfold job: " + output_name + " \n")
related_views = []
for x in doc.Objects:
if x.TypeId == 'TechDraw::DrawPage':
for view in x.Views:
try:
_src = view.Source[0]
except:
continue
#print("source of ", x.Name, view.Name, " is: ", _src.Label)
if _src.Label == output_name:
# This view uses our unfolded output, update this view at the end
print("Found related TechDraw ProjGroup: " + view.Name)
related_views.append(view)
try:
old = doc.getObjectsByLabel(output_name)[0].Name
doc.removeObject(old)
doc.recompute()
except:
pass
# Get the unfold face id
# TODO: This extraction (achieving `unfold_face_id`) is a very dirty hack. Tidy up ASAP.
face_elem = doc.getObjectsByLabel(output_name + '_face')[0]
asm_container = face_elem.Parents[0][0]
target_obj_path = asm_container.getSubObjects()[0]
unfold_face_id = (asm_container.Name, target_obj_path + face_elem.LinkedObject[1].split('.')[-1])
# Unfold the object
Gui.Selection.clearSelection()
Gui.Selection.addSelection(doc.Name, unfold_face_id[0], unfold_face_id[1])
Gui.runCommand('SMUnfoldUnattended')
# Remove unnecessary unfolded solid
doc.removeObject('Unfold')
# Properly rename the output
sketch = doc.getObjectsByLabel('Unfold_Sketch')[0]
sketch.Label = output_name
sketch.Visibility = False
# Update the source of related views
for view in related_views:
echo("Updating " + view.Name + " source.")
view.Source = [sketch]
doc.recompute()
Gui.Selection.clearSelection() |
There is new topo naming algorithm implemented in my branch, which auto tracks the changing face index. For simple usage, just add a
You can add another property to enable/disable auto update. And maybe offer a context menu action in your ViewObject to let user manually update. |
Related to shaise#40
This is the current unfold output:
When I changed the geometry, for example as in the following way:
I expect the unfold sketch to be updated accordingly. How can I achieve this, either automatically (this is preferred) or by manually running some code?
The text was updated successfully, but these errors were encountered: