The correct way to set Doc Visibility (with example macro)

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
freedman
Veteran
Posts: 3477
Joined: Thu Mar 22, 2018 3:02 am
Location: Washington State, USA

The correct way to set Doc Visibility (with example macro)

Post by freedman »

I don't claim to know everything about setting visibility but I have learned alot. My goals here are to set the visibility of the model, I don't want to turn on/off planes and shapebinders ect., just the model shapes. I would like it to run faster if possible.

If you asked on the forum here how to set document visibility you would probably get a response like this:

Code: Select all

for obj in FreeCAD.ActiveDocument.Objects:
    if hasattr(obj,'ViewObject'):
    	obj.ViewObject.Visibility = True
While this does answer the question it is not very useful, the code will turn on everything in the model and frankly can make a big mess. Attached is a macro that is selective about setting visibility in two major ways.
1) Using a bunch of if statements we only process certain objects
2) We determine if the object is of the Part family or a Body object. If the object is a Body then the last feature(Tip) is used to select visibility.

A description of the macro logic.
The "for" loop will run thru the entire document.
1) Use a bunch of "if" statements so we can be selective.
2) Decide if we have a Body object or Part object.
3) If a Body object then find the name of it's "Tip".
5) If we have a Tip-name then go search the entire document and find that feature by name, this is the target to set the object visibility.
6) If not a Body but instead a Part object then we can set visibility directly.

The Print message statement is great, I don't know who wrote but it sure helped me a bunch. If you turn it on you can see the doc structure in the Report view.

All comments are welcome.
Attachments
V_test_mac3.FCMacro
(2.83 KiB) Downloaded 24 times
Last edited by freedman on Wed Aug 19, 2020 9:51 pm, edited 1 time in total.
TheMarkster
Veteran
Posts: 5513
Joined: Thu Apr 05, 2018 1:53 am

Re: The correct way to set Doc Visibility (with example macro)

Post by TheMarkster »

freedman wrote: Wed Aug 19, 2020 7:16 pm All comments are welcome.
If the body is not visible, then setting the tip visible does not help. I would suggest if you are setting the tip visible you should check the body to ensure it is also visible or just set it visible.

There are some useful shortcuts to FreeCAD.PrintMessage to save some typing. Not sure when these were added, so might not work with 0.18.

Code: Select all

Msg("a message\n")
Wrn("a warning\n")
Err("an error\n")
Log("a log\n")
Consider using shift key modifier check to determine True or False. Example shift+click makes it all invisible and a simple click makes it all visible.

Code: Select all

modifiers = QtGui.QApplication.keyboardModifiers()
if modifiers == QtCore.Qt.ShiftModifier:
    vis_state = False
else:
    vis_state = True
freedman
Veteran
Posts: 3477
Joined: Thu Mar 22, 2018 3:02 am
Location: Washington State, USA

Re: The correct way to set Doc Visibility (with example macro)

Post by freedman »

TheMarkster
If the body is not visible, then setting the tip visible does not help. I would suggest if you are setting the tip visible you should check the body to ensure it is also visible or just set it visible.
True, I update the macro.

The macro attached here is just an extract from my soon to be published Vision macro, with multiple levels of saved visibility paging.
Thanks
Post Reply