Clear all dependent parameters

Post here for help on using FreeCAD's graphical user interface (GUI).
Forum rules
and Helpful information
IMPORTANT: Please click here and read this first, before asking for help

Also, be nice to others! Read the FreeCAD code of conduct!
Post Reply
doubters
Posts: 168
Joined: Fri Mar 18, 2016 12:53 pm

Clear all dependent parameters

Post by doubters »

How to clear all dependent parameters of an object (or several objects) in a project at once? Cleaning manually turns out to be very long and you can miss something.
Attachments
Peek 2023-02-15 12-24.gif
Peek 2023-02-15 12-24.gif (435.52 KiB) Viewed 1096 times
heron
Posts: 307
Joined: Mon Apr 20, 2020 5:32 pm

Re: Clear all dependent parameters

Post by heron »

doubters wrote: Wed Feb 15, 2023 9:44 am Cleaning manually turns out to be very long and you can miss something.
Hello, this task can be achieved with a bit of python code

Code: Select all

for i in App.ActiveDocument.Objects:
    if i.isDerivedFrom("Part::Feature"):
        if i.ExpressionEngine:
            for j in i.ExpressionEngine:
                i.setExpression(j[0], None)
App.ActiveDocument.recompute()
My python knowledge is basic, so surely the code can be improved. However, for simple stuff it works fine.
doubters
Posts: 168
Joined: Fri Mar 18, 2016 12:53 pm

Re: Clear all dependent parameters

Post by doubters »

heron wrote: Fri Feb 24, 2023 12:19 pm My python knowledge is basic, so surely the code can be improved. However, for simple stuff it works fine.
Thanks for the tip! Does this code need to be formatted as a macro, or does it need to be executed in the console?
drmacro
Veteran
Posts: 8866
Joined: Sun Mar 02, 2014 4:35 pm

Re: Clear all dependent parameters

Post by drmacro »

doubters wrote: Fri Mar 24, 2023 12:09 pm
heron wrote: Fri Feb 24, 2023 12:19 pm My python knowledge is basic, so surely the code can be improved. However, for simple stuff it works fine.
Thanks for the tip! Does this code need to be formatted as a macro, or does it need to be executed in the console?
FreeCAD "macros" are just Python. You can save it in a file named mycoolcode.FCMacro or mycoolcode.py. Then you can execute it from the macro menu.

If you need it a lot you can also add it to one or more toolbars from the Tools>Customize menu.
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
edwilliams16
Veteran
Posts: 3107
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: Clear all dependent parameters

Post by edwilliams16 »

Code: Select all

for i in App.ActiveDocument.Objects:
    if i.isDerivedFrom("Part::Feature"):
        if i.ExpressionEngine:
            for j in i.ExpressionEngine:
                i.setExpression(j[0], None)

App.ActiveDocument.recompute()
The python console doesn't handle indentation very well. You need the blank line to make it work there or you'll get "Syntax errors". Code I post will run in the console - but that from others, who normally work via Macros or files may not.
doubters
Posts: 168
Joined: Fri Mar 18, 2016 12:53 pm

Re: Clear all dependent parameters

Post by doubters »

It's worked!
And if you need to clear a specific object, is it enough to substitute its internal name instead of App.ActiveDocument.Objects? Or, the syntax will be much more complicated?
Syres
Veteran
Posts: 2893
Joined: Thu Aug 09, 2018 11:14 am

Re: Clear all dependent parameters

Post by Syres »

doubters wrote: Sat Mar 25, 2023 7:50 am And if you need to clear a specific object
Here's what you really want (I hope):

Code: Select all

import FreeCAD as App
from PySide import QtCore, QtGui
from PySide.QtGui import QLineEdit, QRadioButton
ExpObjects = []

class ExpressionRemover():
    def __init__(self):

        for i in App.ActiveDocument.Objects:
            if i.isDerivedFrom("Part::Feature"):
                if len(i.ExpressionEngine) != 0:
                    ExpObjects.append(i.Name)
        if len(ExpObjects) == 0:
            App.Console.PrintWarning('There are no Expressions to be removed\n')
        else:
            self.dialog = None
            self.s1 = None
		
            # Make dialog box
            self.dialog = QtGui.QDialog()
            self.dialog.resize(300,120)
            self.dialog.setWindowTitle("Expression Remover")
            la = QtGui.QVBoxLayout(self.dialog)
            t1 = QtGui.QLabel("Choose to remove All expressions or specific objects in the combobox")
            la.addWidget(t1)

            # Add radio buttons
            self.radio1 = QtGui.QRadioButton("All Object Expressions")
            self.radio2 = QtGui.QRadioButton("Specific Objects")
            self.radio1.clicked.connect(self.on_radio1_clicked)
            self.radio2.clicked.connect(self.on_radio2_clicked)
            # self.radio2.setChecked(True)
            la.addWidget(self.radio1)
            la.addWidget(self.radio2)

            self.expObjs = QtGui.QComboBox()
            self.expObjs.addItems(ExpObjects)

            self.expObjs.setObjectName("expObjs")
            self.expObjs.currentIndexChanged.connect(self.check_validity)

            la.addWidget(self.expObjs)

            # Add OK / Cancel buttons
            self.okbox = QtGui.QDialogButtonBox(QtGui.QDialogButtonBox.Ok | QtGui.QDialogButtonBox.Cancel)
            la.addWidget(self.okbox)
            QtCore.QObject.connect(self.okbox, QtCore.SIGNAL("accepted()"), self.proceed)
            QtCore.QObject.connect(self.okbox, QtCore.SIGNAL("rejected()"), self.close)
            button = self.okbox.button(QtGui.QDialogButtonBox.Ok)
            button.setEnabled(False)
            QtCore.QMetaObject.connectSlotsByName(self.dialog)
            self.dialog.show()
            self.dialog.exec_()


    def check_validity(self,idx):
        App.Console.PrintLog("Current index %d selection changed %s\r\n" % (idx,self.expObjs.currentText()))        
        self.radio2.setChecked(True)
        button = self.okbox.button(QtGui.QDialogButtonBox.Ok)
        button.setEnabled(True)

    def on_radio1_clicked(self):
        button = self.okbox.button(QtGui.QDialogButtonBox.Ok)
        button.setEnabled(True)

    def on_radio2_clicked(self):
        button = self.okbox.button(QtGui.QDialogButtonBox.Ok)
        button.setEnabled(True)

    def proceed(self):
        if self.radio1.isChecked():
            for i in App.ActiveDocument.Objects:
                if i.isDerivedFrom("Part::Feature"):
                    if i.ExpressionEngine:
                        for j in i.ExpressionEngine:
                            i.setExpression(j[0], None)
            App.ActiveDocument.recompute()
            self.close()

        if self.radio2.isChecked():
            for i in App.ActiveDocument.Objects:
                if i.Name == self.expObjs.currentText():
                    if i.ExpressionEngine:
                        for j in i.ExpressionEngine:
                            i.setExpression(j[0], None)
            App.ActiveDocument.recompute()
            self.close()

    def close(self):
        self.dialog.hide()


ExpressionRemover()
ExpressionsRemover.jpg
ExpressionsRemover.jpg (28.66 KiB) Viewed 662 times


Tested using:

Code: Select all

OS: Windows 7 Version 6.1 (Build 7601: SP 1)
Word size of FreeCAD: 64-bit
Version: 0.20.2.29177 +426 (Git)
Build type: Release
Branch: (HEAD detached from 0.20.2)
Hash: 930dd9a76203a3260b1e6256c70c1c3cad8c5cb8
Python 3.8.10, Qt 5.15.2, Coin 4.0.1, Vtk 8.2.0, OCC 7.6.3
Locale: English/United Kingdom (en_GB)
Installed mods: 
  * A2plus 0.4.60f
  * Alternate_OpenSCAD 1.0.0
  * Assembly3 0.11.4
  * CfdOF 1.20.4
  * Curves 0.6.6
  * fasteners 0.4.54
  * fcgear 1.0.0
  * freecad.xray 2022.4.17
  * FreeCAD_Turning_Addon 0.1.0
  * Help 1.0.3
  * Manipulator 1.5.0
  * Plot 2022.4.17
  * Render 2023.2.2
  * sheetmetal 0.2.59
  * Silk 1.0.0
  * timber

Edit: I even made an icon for your toolbar
Attachments
ExpressionRemover.svg
(10.28 KiB) Downloaded 16 times
doubters
Posts: 168
Joined: Fri Mar 18, 2016 12:53 pm

Re: Clear all dependent parameters

Post by doubters »

This is exactly what was needed - your actions were ahead of my thoughts)
Can I somehow try to thank you?
Syres
Veteran
Posts: 2893
Joined: Thu Aug 09, 2018 11:14 am

Re: Clear all dependent parameters

Post by Syres »

No need for special thanks, it's all part of the community chipping in and anyway it gave my brain a little workout which is no bad thing.
Post Reply