cancel
Showing results for 
Search instead for 
Did you mean: 

add features to a group -> translating .Net script to Python

Experimenter
Experimenter

Hello.

 

I am trying to translate a vb net script to Python. I should mention that I am very new to NxOpen and I have no clue of the .Net syntax Smiley Happy

The whole idea of my script is to select all body features in a file and put it a Feature group.

So far I have managed to create a NXopen.BodyCollection with all the bodies that I want to put in a new feature Group. But I dont get any further than that.

The code example is in vb. Works but I can't manage to translate the part of it that is relevant to me. (So to speak, I dont understand what that part of the vb script exactly does.)

 

 

This is the code I was trying to translate into Python:

Option Strict Off
Imports System
Imports System.Collections
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Features
Imports NXOpen.Utilities

Module Module1

Dim theSession As Session = Session.GetSession()
Dim lw As ListingWindow = theSession.ListingWindow
Dim ufs As UFSession = UFSession.GetUFSession()

Sub Main()

Dim toBeGrouped() As Features.Feature = getFeatures
Dim featTags(toBeGrouped.Length - 1) As Tag

For ii As Integer = 0 To toBeGrouped.Length - 1
featTags(ii) = toBeGrouped(ii).Tag

Next

Dim fGroup As Tag = Tag.Null
ufs.Modl.CreateSetOfFeature("TEST", featTags, toBeGrouped.Length, 1, fGroup)

End Sub



Function getFeatures        'Macht ne Liste alle Objecte vom Typ "Extrude" sind

Dim gotFeature As ArrayList = New ArrayList
Dim feats As Features.FeatureCollection = theSession.Parts.Work.Features

For Each feat As Feature In feats
Try

if feat.FeatureType = "EXTRUDE" then
' if feat.FeatureType = "Body" then
gotFeature.add(feat)
End if
Catch ex As Exception

End Try
Next

'do something with exception
return gotfeature.ToArray(GetType(Features.Feature))
End Function

Public Function GetUnloadOption(ByVal dummy As String) As Integer
Return CType(Session.LibraryUnloadOption.Immediately, Integer)
End Function
End Module

http://www.nxjournaling.com/content/add-features-group

 

What I dont get is this part:

Dim toBeGrouped() As Features.Feature = getFeatures

Dim featTags(toBeGrouped.Length - 1) As Tag

 

For ii As Integer = 0 To toBeGrouped.Length - 1

featTags(ii) = toBeGrouped(ii).Tag

 

my first idea of translation whould be: (but that obviously doesn't work):

toBeGrouped = getFeatures()
featTags(len(toBeGrouped)-1) = Tag #..something something, This line I dont understand at all ......

for ii in range(len(toBeGrouped)-1):
	featTags[ii] =  toBeGrouped[ii].Tag

Could anyone give a hint what to do, please?

Thanks in advance

 

 

3 REPLIES

Re: add features to a group -> translating .Net script to Python

Phenom
Phenom

Dim toBeGrouped() As Features.Feature = getFeatures

"Dim" is the VB command for declaring a variable. The variable name, in this case, is toBeGrouped; the parentheses at the end of the name indicate it is an array variable. The type of variable we want to declare follows the "As" keyword; this will be an array of NXOpen.Features.Feature objects. The "getFeatures" is a function defined elsewhere in the code that returns an array of features. So, in this case, we declare the variable and immediately assign it value(s) from another function. In Python, your "toBeGrouped" variable will probably be a list.

 

 

Dim featTags(toBeGrouped.Length - 1) As Tag

 Declaring another array here. This time, the array is of type "Tag". The expression inside the parentheses evaluates to a number, which declares how many elements we want the array to hold. When we declared "toBeGrouped", there was nothing in the parentheses which basically means, we'll figure out the size later. In the case of "toBeGrouped", the size is determined by the array that we assign to it from the "getFeatures" function.

 

 

For ii As Integer = 0 To toBeGrouped.Length - 1

    featTags(ii) = toBeGrouped(ii).Tag

next

This is a for loop. For each feature in the "toBeGrouped" array, we are getting the tag of the feature and saving it in the "featTags" array.

 

Re: add features to a group -> translating .Net script to Python

Phenom
Phenom

Here's some Python code to try:

 

import NXOpen
import NXOpen.UF

theSession = NXOpen.Session.GetSession()
theLw = theSession.ListingWindow
theUfSession = NXOpen.UF.UFSession.GetUFSession()    
    
def main(): 

    workPart = theSession.Parts.Work
    displayPart = theSession.Parts.Display
    
    markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "body feature group")
    theLw.Open()
        
    # initialize list to hold body feature tags
    bodyFeatureTags = []
    
    for x in theSession.Parts.Work.Features:
        #theLw.WriteLine(x.GetFeatureName())
        #theLw.WriteLine(x.FeatureType)
        if x.FeatureType == "BREP":
            bodyFeatureTags.append(x.Tag)
    
    featureSetTag = theUfSession.Modeling.CreateSetOfFeature("setName", bodyFeatureTags,len(bodyFeatureTags),1)
    
    
    # debug
    #theLw.WriteLine("number of bodies: " + str(len(bodyFeatureTags)))
    
    theLw.Close()
    
    
if __name__ == '__main__':
    main()

Re: add features to a group -> translating .Net script to Python

Experimenter
Experimenter

Thanks for your help.

After I got your translation I managed to code something similar like your Python snippet. Works fine now.

 

Cheers.

Arwed