Cancel
Showing results for 
Search instead for 
Did you mean: 

Call Python script within NX Block Styler UI

Experimenter
Experimenter

Hi All,

 

Bear with me, I'm quite new at NX customization. In NX 11, I've created a Block UI and added a button to it (all python output). When I go to edit the callback and define what happens when I push the button, I want to reference a seperate .py file inwhich I programmatically build geometry. Is there a way to call/reference the external file within the UI application script? Any help would be appreciated. See below for a snippet of what I would like to do. Thanks!

 

...

def update_cb(self, block):
        try:
            if block == self.button0:
                # ---- Enter your code here ---

                # I would like to have seperate code referenced here when I push the button....(customScript.py)
                pass
            elif block == self.button01:

1 REPLY

Re: Call Python script within NX Block Styler UI

Siemens Valued Contributor Siemens Valued Contributor
Siemens Valued Contributor

This can be done in following, key is to add file path of second script to sys.path before you import it.

 

File1.py

#File1.py
import NXOpen

import os
import sys


sys.path.append(os.path.dirname(__file__))
import File2

def main():
    NXOpen.UI.GetUI().NXMessageBox.Show("Test Code", NXOpen.NXMessageBox.DialogType.Information, "Called main from File1")
    try:
        File2.SomeFunction()        
    except Exception as ex:
        NXOpen.UI.GetUI().NXMessageBox.Show("Test Code", NXOpen.NXMessageBox.DialogType.Information, "Exception in main")


if __name__ == '__main__':
    main()
    

File2.py

#File2.py
import NXOpen

def SomeFunction():
    NXOpen.UI.GetUI().NXMessageBox.Show("Test Code", NXOpen.NXMessageBox.DialogType.Information, "Called SomeFunction from File2")
    return
SE-Advanced
Siemens Industry Software