I want to know how to toggle hide and show a body or an object using a journal. I have found something similar here-
https://www.eng-tips.com/viewthread.cfm?qid=357724 where Cowski says
"Here's some code that should toggle sketch visibility (hidden vs. shown). It uses a part attribute to save what happened the last time it was run. If the attribute is not found, it is created and it will show all available sketches; subsequent runs will toggle the sketch visibility. The journal does not change layer state: if a sketch is on an invisible layer, it will not make the layer visible, but it will alter the sketch hidden status."
But the code may be in vb or C#, so I couldn't understand that. Can anyone help me on this issue i.e. How to do that using NXOpen Python API?
re: toggle sketch visibility (python)
Below is a python version of the "toggle sketch visibility" journal.
#NXJournaling.com
#March 28, 2023
#
#Toggle sketch visibility based on part attribute value.
import NXOpen
import NXOpen.UF
theSession = NXOpen.Session.GetSession()
theUfSession = NXOpen.UF.UFSession.GetUFSession()
theLw = theSession.ListingWindow
sketchAttribute = "SketchVisibility"
def ShowSketches():
for aSketch in theSession.Parts.Work.Sketches:
aSketch.Unblank()
theSession.Parts.Work.SetBooleanUserAttribute(sketchAttribute, -1, True, NXOpen.UpdateOption.Later)
def HideSketches():
for aSketch in theSession.Parts.Work.Sketches:
aSketch.Blank()
theSession.Parts.Work.SetBooleanUserAttribute(sketchAttribute, -1, False, NXOpen.UpdateOption.Later)
def main():
theLw.Open()
if theSession.Parts.Work is None:
#no active part
theLw.WriteLine("no active part")
return
undoMarkName = "toggle sketch visibility"
markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, undoMarkName)
sketchesVisible = True
if theSession.Parts.Work.HasUserAttribute(sketchAttribute, NXOpen.NXObjectAttributeType.Boolean, -1):
#work part has the attribute, get the current value
sketchesVisible = theSession.Parts.Work.GetBooleanUserAttribute(sketchAttribute, -1)
else:
#work part does not have the attribute, create it
theSession.Parts.Work.SetBooleanUserAttribute(sketchAttribute, -1, sketchesVisible, NXOpen.UpdateOption.Later)
if sketchesVisible:
HideSketches()
else:
ShowSketches()
theLw.Close()
if __name__ == '__main__':
main()
Temporary attribute
Also I want that this attribute shouldn't be saved with the part. This attribute only be available as long as the part window is running. If I close the window the attribute will be deleted.
re: temporary attribute
The journal uses a part attribute to save state information between runs. You'd need a separate way to delete the attribute when you are done with it; perhaps using a separate journal that is automatically run by the "close part" user exit.
Alternately, you could save this state information in a different location such as a file or the registry, separate from the NX part file.
"Close Part" User exit example
Can you please give me an example of "Close Part" User exit in python?
re: user exit
My mistake, there is not a "close part" user exit. You can find out more about user exits here:
https://docs.plm.automation.siemens.com/tdoc/nx/1899/nx_api#uid:xid11624...
If the link doesn't work for you, you can find the info in the NX programming help -> programmer's guide -> execution methods.
I might have confused the user exits with the menuscript pre/post actions. You can customize menu items in NX to perform a given action in addition to the normal action. An example can be found here:
https://docs.plm.automation.siemens.com/tdoc/nx/1899/nx_api#uid:index_me...
If the link doesn't work, check out the NX help -> programming help -> menuscript user guide -> sample exercises -> add pre action
Thanks I have reached my goal.
Thanks. Using your help and after a lot of trials and errors I have developed the initial version of my program. I was trying this for almost two months. As a beginner in python and in NXOpen as well, and lack of resources on NXOpen python API, it was really difficult for me.
Hopefully, soon I will share it with you.
Thanks. Got it. But In my
Thanks. Got it. But In my program there is a little change. I want to do the followings-
1. Create user attribute with list as value when the journal runs first time.
2. then to detect whether the attribute has a list value or not and get the list from the attribute when the journal runs second time.
Is it possible to pass a list in the userattribute? and then to detect and get it?
re: list attribute
To the best of my current knowledge, there is not a specific list type attribute. However, you might be able to save your list in a string attribute; as long as you use a consistent format for your list data, your code could extract the string attribute value and parse the list.
Alternately, an attribute can reference an expression. I've not tried it, but you might be able to create a list expression and reference it in the attribute. You might still have to parse the list yourself, but it would at least enforce the NX list style format for your data.