Showing results for 
Search instead for 
Do you mean 
Reply

sample Python code for dealing with STL files

[ Edited ]

attribution to

@SteveLabout for writing the original VB code

 

If you are dealing with stl files, here is some python code that you may find useful

 

#import your libraries
import NXOpen
import NXOpen.BlockStyler
import NXOpen.Features
import NXOpen.UF
import NXOpen.GeometricAnalysis
import random
import math




#create your class
class generic_class:
        # Constructor for NX Styler class
    # ------------------------------------------------------------------------------
    def __init__(self):
        
        # class members
        self.theSession = None
        self.theUI = None
        self.theLw= None
        self.theDialogName = ""
        self.theDialog = None
        #Get the UF session
        self.theUfSession = None


        
        

        try:
            self.theSession = NXOpen.Session.GetSession()
            self.theUI = NXOpen.UI.GetUI()
            self.theLw = self.theSession.ListingWindow #create print window
            self.theUfSession = NXOpen.UF.UFSession.GetUFSession() 
            
        except Exception as ex:
            # ---- Enter your exception handling code here -----
            raise ex

    #
    def hello_world(self):
        #open the debug console
        self.theLw.Open()
        self.theLw.WriteLine("Hello NX World!")
        self.theLw.WriteLine("")
        self.theLw.WriteLine("")



    
    def facet_data(self):
        #declare global variables
        global dispPart, workPart, fctFaces, fct_surface_data, fctParam, num_faces

        #need these for each NX session
        dispPart= self.theSession.Parts.Display
        workPart = self.theSession.Parts.Work

        #UF.Facet class instance
        uf_facet=NXOpen.UF.Facet()

        #loop through each facet body
        for thisFctBody in dispPart.FacetedBodies:

            #get the facet body tag for each thisFctBody, will need this for various functions/attributes
            facet_tag=thisFctBody.Tag


            #the following attributes only work on solid bodies, not stl files
            #self.theLw.WriteLine("The Associated body is "+str(thisFctBody.AssociatedBody))
            #self.theLw.WriteLine("Body Type is "+str(thisFctBody.BodyType)+". 0 is undefined, 1 is SolidBody, and 2 is SheetBody")
            self.theLw.WriteLine("")
            self.theLw.WriteLine("")

            #Get parameters method for the faceted body class, documentation link is beow
            #https://docs.plm.automation.siemens.com/data_services/resources/nx/10/nx_api/en_US/custom/nxopen_python_ref/NXOpen.Facet.FacetedBody.html?highlight=facetedbody#nxopen-facet-facetedbody-getparameters
            fctParam=thisFctBody.GetParameters()
            self.theLw.WriteLine("The parameters of this facet body are "+str(fctParam))
            self.theLw.WriteLine("")
            self.theLw.WriteLine("")

            #Uf.Facet function Asknumfaces
            #https://docs.plm.automation.siemens.com/data_services/resources/nx/10/nx_api/en_US/custom/nxopen_python_ref/NXOpen.UF.Facet.AskNumFaces.html?highlight=uf.facet#NXOpen.UF.Facet.AskNumFaces
            
            num_faces=uf_facet.AskNumFaces(facet_tag)
            self.theLw.WriteLine("UF.Facet asknumfaces function is called. # of faces are " + str(num_faces))

            self.theLw.WriteLine("")
            self.theLw.WriteLine("")

            #call the GetFaces function to create a list/array of the faces
            #this works on solid bodies but not on the stl
            #fctFaces=thisFctBody.GetFaces()
            #self.theLw.WriteLine("The faces of this faceted body are "+str(fctFaces))

        #Calling various uf.facet functions, ask
        for i in range(0, num_faces):
            #UF.Facet.AskNormalsOfFacet(model, facetId)
            normals=uf_facet.AskNormalsOfFacet(facet_tag, i)
            self.theLw.WriteLine("UF.Facet asknormals of facet function is called. Normals are " + str(normals))

            uf_facet.AskFaceIdOfFacet(facet_tag, i)
            #this will not work, don't know what facetface parameter should be
            #uf_facet.AskSurfaceDataForFace(facetface)


            self.theLw.WriteLine("")
        ##end of loop

        self.theLw.WriteLine("")
        self.theLw.WriteLine("")
        
        #declare a list/array to store surface data tuples

        fct_surface_data=[]

        #this for loop will work on solid bodies but not on stl files
        #for thisFctFace in fctFaces:

            #This function works on solid bodies but for some reason not on stl facet bodies???
            #fct_surface_data.append(thisFctFace.GetSurfaceData())

        #self.theLw.WriteLine("The total amount of faces are "+str(len(fct_surface_data)))
        self.theLw.WriteLine("")
        self.theLw.WriteLine("")

        #for i in range(len(fct_surface_data)):

            #self.theLw.WriteLine("The surface data for face " + str(i)+" is "+str(fct_surface_data[i]))
            
def main():
    try:

        #create instance of class
        myclass = generic_class()
        #call your functions
        myclass.hello_world()
        myclass.facet_data()
    except Exception as ex:
        # ---- Enter your exception handling code here -----
        NXOpen.UI.GetUI().NXMessageBox.Show("Block Styler", NXOpen.NXMessageBox.DialogType.Error, str(ex))

if __name__ == "__main__":
    main()