macro for hiding geometric constraints

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
aster94
Posts: 31
Joined: Sat Oct 26, 2019 10:43 am
Location: Etna
Contact:

macro for hiding geometric constraints

Post by aster94 »

hello,

I wrote a macro to hide all geometric constraints and show only measures (i.e. angle, radius, diameter, distances)
here a video showing its use: https://streamable.com/jhtdu

Code: Select all

import FreeCAD
__title__="Macro_HideGeometryConstraints"
__author__ = "aster94"
__url__     = "https://github.com/aster94"
__version__ = "0.0.1"
__date__    = "26/10/2019"

try:
	sketch_name = Gui.ActiveDocument.getInEdit().Object.Name
	valid_constraints = ['Diameter', 'Radius', 'Angle', 'Distance']
	constraints = list(Gui.ActiveDocument.getInEdit().Object.Constraints) # Get all the constraints of the active sketch

	for cnt, constraint in enumerate(constraints):
		#print(constraint)
		if any([c in str(constraint) for c in valid_constraints]):
			#print(constraint)
			App.ActiveDocument.getObject(sketch_name).setVirtualSpace(cnt, True) # Show
		else:
			App.ActiveDocument.getObject(sketch_name).setVirtualSpace(cnt, False) # Hide
		
except Exception:
    None

and to show again all constraints

Code: Select all

import FreeCAD
__title__="Macro_ShowAllConstraints"
__author__ = "aster94"
__url__     = "https://github.com/aster94"
__version__ = "0.0.1"
__date__    = "26/10/2019"

try:
	sketch_name = Gui.ActiveDocument.getInEdit().Object.Name
	constraint_count = App.ActiveDocument.Sketch001.ConstraintCount

	for c in range(constraint_count):
		App.ActiveDocument.getObject(sketch_name).setVirtualSpace(c, True)

except Exception:
    None
Any suggestion is well accepted since it is my very first script in FreeCAD
I would like to submit to https://github.com/FreeCAD/FreeCAD-macros what do you think about?
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: macro for hiding geometric constraints

Post by openBrain »

As a blocking point, your 2nd macro will only work on Sketch001, which isn't convenient. :)
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: macro for hiding geometric constraints

Post by openBrain »

aster94 wrote: Sat Oct 26, 2019 4:30 pm
Here a feedback where I commented changes. ;)

Code: Select all

__title__="Macro_HideGeometryConstraints"
__author__ = "aster94"
__url__     = "https://github.com/aster94"
__version__ = "0.0.1"
__date__    = "26/10/2019"

try:
	sk = Gui.ActiveDocument.getInEdit().Object # instanciate sketch itself for future use
	valid_constraints = ['Diameter', 'Radius', 'Angle', 'Distance']
	constraints = list(sk.Constraints) # to be checked but I think '.Constraints' already returns a list

	for cnt, constraint in enumerate(constraints):
		if any([c in str(constraint) for c in valid_constraints]):
			sk.setVirtualSpace(cnt, True) # Show
		else:
			sk.setVirtualSpace(cnt, False) # Hide
		
except Exception:
    pass # instead of 'None'

Code: Select all

import FreeCAD
__title__="Macro_ShowAllConstraints"
__author__ = "aster94"
__url__     = "https://github.com/aster94"
__version__ = "0.0.1"
__date__    = "26/10/2019"

try:
	sk = Gui.ActiveDocument.getInEdit().Object # instanciate sketch itself for future use
	constraint_count = sk.ConstraintCount

	for c in range(constraint_count):
		sk.setVirtualSpace(c, True)

except Exception:
    pass
I didn't check this code, but you should be able to see the idea. ;)
aster94
Posts: 31
Joined: Sat Oct 26, 2019 10:43 am
Location: Etna
Contact:

Re: macro for hiding geometric constraints

Post by aster94 »

your 2nd macro will only work on Sketch001, which isn't convenient. :)
ahahahah yep, I missed it :lol:

here the updated versions

Code: Select all

import FreeCAD
__title__="Macro_HideGeometryConstraints"
__author__ = "aster94"
__url__     = "https://github.com/aster94"
__version__ = "0.0.1"
__date__    = "26/10/2019"

valid_constraints = ['Diameter', 'Radius', 'Angle', 'Distance']

try:
	sk = Gui.ActiveDocument.getInEdit().Object
	constraints = sk.Constraints # Get all the constraints of the active sketch

	for cnt, constraint in enumerate(constraints):
		#print(constraint)
		if any([item in str(constraint) for item in valid_constraints]):
			sk.setVirtualSpace(cnt, True) # Show
		else:
			sk.setVirtualSpace(cnt, False) # Hide
		
except Exception:
    pass

Code: Select all

import FreeCAD
__title__="Macro_ShowAllConstraints"
__author__ = "aster94"
__url__     = "https://github.com/aster94"
__version__ = "0.0.1"
__date__    = "26/10/2019"

try:
	sk = Gui.ActiveDocument.getInEdit().Object
	constraint_count = sk.ConstraintCount

	for c in range(constraint_count):
		sk.setVirtualSpace(c, True)

except Exception:
    pass
aster94
Posts: 31
Joined: Sat Oct 26, 2019 10:43 am
Location: Etna
Contact:

Re: macro for hiding geometric constraints

Post by aster94 »

may i upload these to github?
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: macro for hiding geometric constraints

Post by Kunda1 »

aster94 wrote: Sun Nov 10, 2019 6:12 pm may i upload these to github?
yes please
upload them to https://github.com/FreeCAD/FreeCAD-macros/
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: macro for hiding geometric constraints

Post by openBrain »

Well read the Readme there so your header is compliant with requirements. ;)
Also the Wiki page shall exists. Better also if you propose icons with your macros.
IMO it would be good that both macros are grouped in only one. ;)
aster94
Posts: 31
Joined: Sat Oct 26, 2019 10:43 am
Location: Etna
Contact:

Re: macro for hiding geometric constraints

Post by aster94 »

openBrain wrote: Sun Nov 10, 2019 7:20 pm Well read the Readme there so your header is compliant with requirements. ;)
Also the Wiki page shall exists. Better also if you propose icons with your macros.
IMO it would be good that both macros are grouped in only one. ;)
mmmmm the only way i can think about to make the two macro in only one would be to have a flag variable outside the macro, but i guess that this is not possible, is it?
Also i wasn't able to find a way from python to know if a constrain is visible or not. I mean that there are cases in which

Code: Select all

setVirtualSpace(cnt, True)
would make a constraint not visible. Is it possible to check if a constraint is visible or not?
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: macro for hiding geometric constraints

Post by openBrain »

Code: Select all

isHidden=sk.getVirtualSpace(cnt)
To get both in one macro, I'd implement a logic like : if at least one constraint hidden then show all else hide geometric constraints. ;)
aster94
Posts: 31
Joined: Sat Oct 26, 2019 10:43 am
Location: Etna
Contact:

Re: macro for hiding geometric constraints

Post by aster94 »

openBrain wrote: Sun Nov 10, 2019 8:59 pm

Code: Select all

isHidden=sk.getVirtualSpace(cnt)
To get both in one macro, I'd implement a logic like : if at least one constraint hidden then show all else hide geometric constraints. ;)
Great, far away easier, i was trying to access the same with

Code: Select all

sk.Constraints[1].IsInVirtualSpace
but it wasn't working for some strange reason :?
During the next days I will send an updated version of the macro

EDIT 1:Anyway your isHidden unfortunatelly won't work in 100% of cases, if this button (https://www.freecadweb.org/wiki/Sketche ... rtualSpace) is pressed the result would be anyway true. Do you see any work around?

EDIT 2: from the wiki it says "move some constraints to virtual space two" then i guess (i am not checking the code) that the python function getVirtualSpace can return only boolean values and 2 is a valid true. Would it be possible to make this function return an int?
Post Reply