niedzviedz
Mechanical
- Apr 1, 2012
- 307
Hello everyone,
I have imported spep file into NX, and now I have each body of each parts in assembly on layer 10. I don't wanna manually place it on layer 1 because there are more than 200 parts in this assembly. So I started to write some code.
But I get some errors. Any one can help me to solve my problem?
With best regards
Michael
I have imported spep file into NX, and now I have each body of each parts in assembly on layer 10. I don't wanna manually place it on layer 1 because there are more than 200 parts in this assembly. So I started to write some code.
Code:
Option Strict Off
Imports System
Imports System.Collections
Imports NXOpen
Imports NXOpen.Utilities
Imports NXOpen.Assemblies
Imports NXOpen.UF
Imports NXOpenUI
Module MoveComponents
'Dim s As Session = Session.GetSession()
Dim theSession As Session = Session.GetSession()
'Dim ufs As UFSession = UFSession.GetUFSession()
Public ufs As UFSession = UFSession.GetUFSession()
Dim lw As ListingWindow = theSession.ListingWindow
Dim blnCancel As Boolean = False
Dim answer as String
dim layerNumber as Integer = 10
dim newlayer as Integer = 1
Dim allObjects as NXObject()
Sub Main()
Dim workPart As Part = theSession.Parts.Work
Dim dispPart As Part = theSession.Parts.Display
Dim HRC1 as string
Dim stock as string
lw.Open
Try
Dim c As ComponentAssembly = dispPart.ComponentAssembly
if not IsNothing(c.RootComponent) then
ReportComponentChildren(c.RootComponent, 0)
end if
Catch e As Exception
theSession.ListingWindow.WriteLine("Failed: " & e.ToString)
End Try
lw.Close
End Sub
'**********************************************************
Sub reportComponentChildren( ByVal comp As Component, ByVal indent As Integer)
Dim workPart As Part = theSession.Parts.Work
Dim dispPart As Part = theSession.Parts.Display
For Each child As Component In comp.GetChildren()
Dim MyPart As Part = child.Prototype.OwningPart
If LoadComponent(child) Then
Dim displayModification1 As DisplayModification
displayModification1 = theSession.DisplayManager.NewDisplayModification()
For Each temp As Features.Feature In myPart.Features
If temp.Layer <> layernumber Then
Exit For
End If
temp.Layer = newLayer
next
myPart.Layers.MoveDisplayableObjects(temp)
With displayModification1
.NewLayer = 1
.Apply(allobjects)
.Dispose
End With
Else
'component could not be loaded
End If
reportComponentChildren(child, indent+1)
Next
End Sub
Private Function LoadComponent(ByVal theComponent As Component) As Boolean
Dim thePart As Part = theComponent.Prototype.OwningPart
Dim partName As String = ""
Dim refsetName As String = ""
Dim instanceName As String = ""
Dim origin(2) As Double
Dim csysMatrix(8) As Double
Dim transform(3, 3) As Double
Try
If thePart.IsFullyLoaded Then
'component is fully loaded
Else
'component is partially loaded
End If
Return True
Catch ex As NullReferenceException
'component is not loaded
Try
ufs.Assem.AskComponentData(theComponent.Tag, partName, refsetName, instanceName, origin,
csysMatrix, transform)
Dim theLoadStatus As PartLoadStatus
theSession.Parts.Open(partName, theLoadStatus)
If theLoadStatus.NumberUnloadedParts > 0 Then
Dim allReadOnly As Boolean = True
For i As Integer = 0 To theLoadStatus.NumberUnloadedParts - 1
If theLoadStatus.GetStatus(i) = 641058 Then
'read-only warning, file loaded ok
Else
'641044: file not found
lw.WriteLine("file not found")
allReadOnly = False
End If
Next
If allReadOnly Then
Return True
Else
'warnings other than read-only...
Return False
End If
Else
Return True
End If
Catch ex2 As NXException
lw.WriteLine("error: " & ex2.Message)
Return False
End Try
Catch ex As NXException
'unexpected error
lw.WriteLine("error: " & ex.Message)
Return False
End Try
End Function
Public Function GetUnloadOption(ByVal dummy As String) As Integer
'Unloads the image immediately after execution within NX
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately
End Function
End Module
With best regards
Michael