'June 16, 2015
' Update to process instance bodies in the current display part (the assembly)
' instead of looping through the solid bodies in each component.
'
Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Assemblies
Module Module2
Dim theSession As Session = Session.GetSession()
Dim ufs As UFSession = UFSession.GetUFSession()
Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
Dim theUfSession As UFSession = UFSession.GetUFSession()
Dim theUISession As UI = UI.GetUI
Dim response As Integer
Sub Main()
If IsNothing(theSession.Parts.BaseWork) Then
'active part required
Return
End If
Dim workPart As Part = theSession.Parts.Work
Dim displayPart As Part = theSession.Parts.Display
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()
Const undoMarkName As String = "NXJ journal"
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)
Dim theAttributes() As NXObject.AttributeInformation
theAttributes = displayPart.GetUserAttributes
lw.Open()
Dim myMeasure As MeasureManager = theSession.Parts.Display.MeasureManager()
Dim massUnits(4) As Unit
massUnits(0) = theSession.Parts.Display.UnitCollection.GetBase("Area")
massUnits(1) = theSession.Parts.Display.UnitCollection.GetBase("Volume")
massUnits(2) = theSession.Parts.Display.UnitCollection.GetBase("Mass")
massUnits(3) = theSession.Parts.Display.UnitCollection.GetBase("Length")
Dim theBodies As New List(Of Body)
Dim row as integer
theBodies = AskAllBodies(theSession.Parts.Display)
Dim MdlPrt As String
Dim c As ComponentAssembly = workPart.ComponentAssembly
Dim rootDispName As String = (c.RootComponent.DisplayName).Replace("/", "_")
For Each tempBody As Body In theBodies
'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
Dim YG as String
Dim XG as String
Dim ZG as String
Dim parentTag As Tag
theUfSession.Assem.AskParentComponent(tempBody.Tag, parentTag)
'theUfSession.Assem.AskComponentData(parentTag, partName, refSetName, instanceName, origin, csysMatrix, transform)
Dim bodyComp As Component = Utilities.NXObjectManager.Get(parentTag)
lw.WriteLine("")
MdlPrt = instanceName
lw.WriteLine(MdlPrt)
lw.WriteLine("")
'lengths returned in part units
Dim bodyLengths(2) As Double
bodyLengths = GetBoundingBox(tempBody)
lw.WriteLine("Extents")
lw.WriteLine("")
lw.WriteLine("X length: " & bodyLengths(0).ToString)
lw.WriteLine("Y length: " & bodyLengths(1).ToString)
lw.WriteLine("Z length: " & bodyLengths(2).ToString)
'Measures Min/Max of all model parts in assembly
Dim bbox(5) as double
Dim tagList(0) As NXOpen.Tag
ufs.Modl.AskBoundingBox(tempBody.Tag,bbox)
lw.WriteLine("Mininum and Maximum Extents")
lw.WriteLine("")
lw.WriteLine("min X: " & bbox(0) & " max X: " & bbox(3))
lw.WriteLine("min Y: " & bbox(1) & " max Y: " & bbox(4))
lw.WriteLine("min Z: " & bbox(2) & " max Z: " & bbox(5))
'lw.WriteLine("X dim: " & bbox(3) - bbox(0))
'lw.WriteLine("Y dim: " & bbox(4) - bbox(1))
'lw.WriteLine("Z dim: " & bbox(5) - bbox(2))
row += 1
Next
End Sub
Function AskAllBodies(ByVal thePart As Part) As List(Of Body)
Dim theBodies As New List(Of Body)
Dim aBodyTag As Tag = Tag.Null
Do
theUfSession.Obj.CycleObjsInPart(thePart.Tag, _
UFConstants.UF_solid_type, aBodyTag)
If aBodyTag = Tag.Null Then
Exit Do
End If
Dim theType As Integer, theSubtype As Integer
theUfSession.Obj.AskTypeAndSubtype(aBodyTag, theType, theSubtype)
If theSubtype = UFConstants.UF_solid_body_subtype Then
theBodies.Add(Utilities.NXObjectManager.Get(aBodyTag))
End If
Loop While True
Return theBodies
End Function
Private Function GetBoundingBox(ByVal solidBody As NXOpen.Body) As Double()
'AskBoundingBox returns min and max coordinates
'this function will simply return the box lengths (x, y, z)
Dim bboxCoordinates(5) As Double
Dim bboxLengths(2) As Double
Try
'get solid body bounding box extents
theUFSession.Modl.AskBoundingBox(solidBody.Tag, bboxCoordinates)
bboxLengths(0) = bboxCoordinates(3) - bboxCoordinates(0)
bboxLengths(1) = bboxCoordinates(4) - bboxCoordinates(1)
bboxLengths(2) = bboxCoordinates(5) - bboxCoordinates(2)
Return bboxLengths
Catch ex As NXException
MsgBox(ex.GetType.ToString & " : " & ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Exclamation, "Solid Body Bounds Error!")
bboxLengths(0) = 0
bboxLengths(1) = 0
bboxLengths(2) = 0
Return bboxLengths
End Try
End Function
Public Function GetUnloadOption(ByVal dummy As String) As Integer
'Unloads the image when the NX session terminates
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
End Function
End Module