I've written a script to loop over all the bodies in an assembly and identify ones that match a certain NX_material string. These are then exported as STLs later.
However, parts that appear to be links/mirrors (I don't know the right NX terminology) are not exported, only the root part is exported.
As I loop over the bodies, though, and set the setHighlight flag to "1", all the mirrored/linked parts are highlighted, for a given body, but later, the ExportSTL call only exports one of these.
How can I export all occurrences (?) of this part.
Here's the full VB script with a few sensitive pieces of information removed:
Option Strict Off
Imports System
Imports System.ThreadingImports System.Collections.GenericImports NXOpen
Imports NXOpen.UFImports NXOpen.AssembliesImports NXOpen.Features
Module Module1
Dim theSession As Session = Session.GetSession()Dim theUfSession As UFSession = UFSession.GetUFSession()Dim lw As ListingWindow = theSession.ListingWindow
Dim bCheckIfFullyLoaded AsBoolean=FalseDim stlOutFileName AsString="C:/This/is/a/path"Dim qualifiers AsString=""Dim materialList AsNew List(OfString)From{"Material_name_1", "Material_name_2"}
Dim bWriteSTL AsBoolean=FalseDim bWriteAsIndividualFiles AsBoolean=False
Sub Main()
If IsNothing(theSession.Parts.BaseWork)Then'active part requiredReturnEndIf
Dim workParts AsNew List(Of Part)ForEach tempPart As Part In theSession.Parts'Dim c As ComponentAssembly = tempPart.ComponentAssembly
workParts.Add(tempPart)Next
lw.Open()
Const undoMarkName AsString="export solids to STL"Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, undoMarkName)
Dim theSolids AsNew List(Of Body)
'collect the solid bodies in the work part
ForEach part As Part in workParts
Dim isLoadead AsBoolean= part.IsFullyLoaded()Dim partDesc AsString= part.GetStringAttribute("DB_PART_DESC")
lw.WriteLine(part.Name+" "+ partDesc)
If bCheckIfFullyLoaded ThenIf isLoadead Then
lw.WriteLine(" Fully Loaded")Else
lw.WriteLine(" Not Fully Loaded")
lw.WriteLine(" Trying to fully load this part")Try
part.LoadThisPartFully()Catch ex As Exception
lw.WriteLine("Error: "+ ex.Message)EndTryEndIfEndIf
ForEach body As Body In part.Bodies
lw.WriteLine("-"+ body.Name)
theUfSession.Disp.SetHighlight(body.Tag, 0)If body.HasUserAttribute("NX_Material", NXObject.AttributeType.String, 0)ThenDim obj As NXOpen.NXObject.AttributeInformation= body.GetUserAttribute("NX_Material", NXObject.AttributeType.String, -1)Dim material AsString= obj.StringValue
lw.WriteLine("... "+ material)
Dim bKeepThisBody AsBoolean=FalseDim bContinue =False
ForEach mat AsStringIn materialList
If material.Contains(mat)Then
bContinue =TrueEndIf
' HACK to get a portion of the rollbar selectedIf mat ="SPECIAL_NAME"ThenIf partDesc.Contains("ROLLBAR")And material.Contains("ZERO MASS")Then
bContinue =TrueEndIfEndIfNext
If bContinue ThenIf qualifiers <>""ThenForEach feature As Feature in body.GetFeatures()If feature.Name.Contains(qualifiers)
bKeepThisBody =TrueEndIfNextElse
bKeepThisBody =TrueEndIf
If bKeepThisBody Then
theUfSession.Disp.SetHighlight(body.Tag, 1)
theUfSession.Disp.MakeDisplayUpToDate()
System.Threading.Thread.Sleep(200)
theUfSession.Disp.SetHighlight(body.Tag, 0)
theUfSession.Disp.MakeDisplayUpToDate()
lw.WriteLine(body.Name)
theSolids.Add(body)EndIfEndIfEndIfNextNext
If bWriteSTL ThenIf bWriteAsIndividualFiles
ForEach body As Body In theSolids
Dim singleObject AsNew List(Of Body)
singleObject.Add(body)
ExportSTL(stlOutFileName +"_"+CStr(body.Tag), singleObject, 0.03)NextElse
ExportSTL(stlOutFileName, theSolids, 0.03)EndIfEndIf'Try'ExportSTL(workPart.FullPath, theSolids, 0.003, 0.003)'Catch ex As NXException'lw.WriteLine("NX Error: " & ex.Message)'Catch ex As Exception'lw.WriteLine("Error: " & ex.Message)'End Try
lw.Close()
EndSub
Sub ExportSTL(ByVal FileName AsString, ByVal theObjects As List(Of Body), ByVal triangleTolerance AsDouble)
Dim NumErrors AsIntegerDim FileHandle As IntPtr
Dim InfoError()As UFStd.StlErrorDim Header, FileBaseName AsStringDim lw As ListingWindow = theSession.ListingWindowDim theUfSession As UFSession = UFSession.GetUFSession()'Dim numNegated As Integer'Dim Negated() As Tag
lw.Open()'Negated = Nothing
InfoError =Nothing
FileName = IO.Path.ChangeExtension(FileName, ".stl")
FileBaseName = IO.Path.GetFileName(FileName)
Header ="Header: "& FileBaseName
theUfSession.Std.OpenBinaryStlFile(FileName, False, Header, FileHandle)
theUfSession.Ui.SetPrompt("Creating file ... "& FileBaseName &" ...")
ForEach temp As Body In theObjects
If temp.IsSolidBodyThen
lw.WriteLine(" body is occurrence: "+ temp.IsOccurrence.ToString)Try
theUfSession.Std.PutSolidInStlFile(FileHandle, Tag.Null, temp.Tag, 0.0, 5.0, triangleTolerance, NumErrors, InfoError)
theUfSession.Disp.SetHighlight(temp.Tag, 0)Catch ex As Exception
lw.WriteLine("Error with STL : "+ ex.Message)EndTryEndIfNext
theUfSession.Std.CloseStlFile(FileHandle)
theUfSession.Ui.SetStatus("File ... "& FileBaseName &" generated ...")
EndSub
PublicFunction GetUnloadOption(ByVal dummy AsString)AsInteger
'Unloads the image immediately after execution within NX
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately
EndFunction
EndModule