admin 发表于 2013-10-30 13:46:00

NX 二次开发源码:从整个装配上获取所有的实体


Option Strict Off

Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.UI
Imports NXOpen.Utilities
Imports NXOpen.Assemblies

Module create_array_of_all_bodies_in_assembly

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

   Sub Main()
      lw.Open()

      Dim dispPart As Part = s.Parts.Display()

      Dim root As Component = _
                  dispPart.ComponentAssembly.RootComponent

      Dim compArray(-1) As Component

      stuffComponentArray(root, compArray)

      lw.WriteLine("Component Array Size: " & compArray.Length.ToString())
      Dim bodyArray(-1) As Body

      stuffBodyArray(compArray, bodyArray)

      lw.WriteLine("Count of bodies in array: " & _
                   bodyArray.Length.ToString())


   End Sub

   Public Sub stuffBodyArray(ByVal ca As Component(), ByRef ba As Body())

      For Each comp As Component In ca
         Dim c_part As Part = comp.Prototype
         Dim localBodies() As Body = c_part.Bodies.ToArray()
         For Each thisBody As Body In localBodies
            ReDim Preserve ba(ba.GetUpperBound(0) + 1)
            ba(ba.GetUpperBound(0)) = thisBody
         Next

      Next
   End Sub

   Public Sub stuffComponentArray(ByVal cmp As Component, ByRef ca As Component())
      Dim child As Component = Nothing
      For Each child In cmp.GetChildren()
         ReDim Preserve ca(ca.GetUpperBound(0) + 1)
         ca(ca.GetUpperBound(0)) = child
         stuffComponentArray(child, ca)
      Next
   End Sub

   Public Function GetUnloadOption(ByVal dummy As String) As Integer

      Return Session.LibraryUnloadOption.Immediately

   End Function

End Module



页: [1]
查看完整版本: NX 二次开发源码:从整个装配上获取所有的实体