Cancel
Showing results for 
Search instead for 
Did you mean: 

Enum in Python

Solution Partner Valued Contributor Solution Partner Valued Contributor
Solution Partner Valued Contributor

I'm struggling to understand enumerations in Python with NX12.0.2.  Specifically,  I'm working with FaceType, but this is a general problem

 

This expression:

 

NXOpen.Face.FaceType.Planar

 

will return an integer value of 1, which is expected since Planar is the second item on the enumeration list.

 

But I'm trying to test an object to see if it is a planar type face.

Neither of these expressions are working: (cutface is my face object)

 

cutfacetype = cutface.FaceType
test= Enum.ValueOf(cutfacetype)

 

I'm afraid I can't make sense of the documentation here:

 

 

 


2019-03-20_18-57-38.png

Is this necessary?:

 

from enum import Enum

 

Thanks for any suggestions.

4 REPLIES 4
Highlighted

Betreff: Enum in Python

Legend
Legend

Maybe I don't understand the problem, but for me it is simply:

 

    theSession.ListingWindow.Open()
    if cutface.SolidFaceType == NXOpen.Face.FaceType.Cylindrical :
      theSession.ListingWindow.WriteLine("Cylindrical")	
    elif cutface.SolidFaceType == NXOpen.Face.FaceType.Planar :
      theSession.ListingWindow.WriteLine("Planar")
    else:
      theSession.ListingWindow.WriteLine("Other")

Regards,

 

Jan

Jan Böttcher

Betreff: Enum in Python

Solution Partner Valued Contributor Solution Partner Valued Contributor
Solution Partner Valued Contributor

Jan,

That is a solution to my immediate problem.  I will use it, thank you!

 

But in the larger context, I continue to be confused about working with the enumerated objects returned by Python.  I'm still hoping someone has an example.

Betreff: Enum in Python

Legend
Legend

@Andy_Schaefers  schrieb:

Jan,

That is a solution to my immediate problem.  I will use it, thank you!

 

But in the larger context, I continue to be confused about working with the enumerated objects returned by Python.  I'm still hoping someone has an example.


Hi Andy,

don't know exactly what you are missing. I don't think there is a fundamentally different way to deal with the enum objects. The mentioned 'usefullness for bit operations' of ValueOf does not apply in the case of NXOpen.Face.FaceType because the backing int values are no 'flags' (powers of two).

 

    if cutface.SolidFaceType == NXOpen.Face.FaceType.Cylindrical :
      theSession.ListingWindow.WriteLine("Cylindrical")	
    elif cutface.SolidFaceType == NXOpen.Face.FaceType.Planar :
      theSession.ListingWindow.WriteLine("Planar")	
    else:
      theSession.ListingWindow.WriteLine("Other")

is the same as

 

    if NXOpen.Face.FaceType.ValueOf(cutface.SolidFaceType.value) == NXOpen.Face.FaceType.Cylindrical :
      theSession.ListingWindow.WriteLine("Cylindrical")	
    elif NXOpen.Face.FaceType.ValueOf(cutface.SolidFaceType.value) == NXOpen.Face.FaceType.Planar :
      theSession.ListingWindow.WriteLine("Planar")
    else:
      theSession.ListingWindow.WriteLine("Other")

I don't know any NXOpen enum where the members are defined as flags. Maybe there are some. In this case you can hold multiple member values in one variable. Here is an (non NX enum) example for the bit operations:

    # from enum import IntEnum
    class Colors(IntEnum):
      No = 0
      Red = 1
      Blue = 2
      Green = 4
	  
    myCarsColors = Colors.Red | Colors.Blue; # My car is two colored
	 
    theSession.ListingWindow.WriteLine("Value of myCarsColors: " + str(myCarsColors)) #Value of myCarsColors: 3
	
    theSession.ListingWindow.WriteLine("Is my car red?: " + str(myCarsColors & Colors.Red != Colors.No))#Is my car red: True
    theSession.ListingWindow.WriteLine("Is my car blue?: " + str(myCarsColors & Colors.Blue != Colors.No))# Is my car blue: True
    theSession.ListingWindow.WriteLine("Is my car green?: " + str(myCarsColors & Colors.Green != Colors.No))#Is my car green: False

In case of a NX enum you can use ValueOf to get the int values for the operations ('|' and '&') and comparisons.

 

Regards,

Jan

 

 


Jan Böttcher

Betreff: Enum in Python

Solution Partner Valued Contributor Solution Partner Valued Contributor
Solution Partner Valued Contributor

Jan,

Thank you for the reply.  I did not explain my question well, but you answered it in your second example.

The solution is to use the .value parameter with the returned object.  

Thank you!

 

Andy Schaefers

Acuity AE

enum.png