Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Module Module1
Dim theSession As Session = Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession()
Sub Main()
If IsNothing(theSession.Parts.Work) Then
'active part required
Return
End If
Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()
'$$$ file to output measurements, change this before running journal
Dim outputFile As String = "[highlight #FCE94F]C:\temp\prtStockSize[/highlight].csv"
'$$$
Dim bodyToMeasure As Body = Nothing
If SelectSolid("Select a solid body to measure", bodyToMeasure) = Selection.Response.Cancel Then
Return
End If
If Not OrientWCS() Then
Return
End If
Dim stkSize As String = MeasureBody(bodyToMeasure)
Dim output As String = workPart.Leaf & ", " & stkSize
AppendToTextFile(outputFile, output)
lw.Close()
End Sub
Function SelectSolid(ByVal prompt As String, ByRef selObj As TaggedObject) As Selection.Response
Dim theUI As UI = UI.GetUI
Dim title As String = "Select a solid body"
Dim includeFeatures As Boolean = False
Dim keepHighlighted As Boolean = False
Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
Dim cursor As Point3d
Dim scope As Selection.SelectionScope = Selection.SelectionScope.WorkPart
Dim selectionMask_array(0) As Selection.MaskTriple
With selectionMask_array(0)
.Type = UFConstants.UF_solid_type
.SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_BODY
End With
Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
title, scope, selAction, _
includeFeatures, keepHighlighted, selectionMask_array, _
selobj, cursor)
If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
Return Selection.Response.Ok
Else
Return Selection.Response.Cancel
End If
End Function
Function OrientWCS() As Boolean
Dim theUISession As UI = UI.GetUI()
'9 element orientation matrix of specified csys
'arrays are zero based
Dim myCsys(8) As Double
'3 element array: origin point of specified csys
Dim myOrigin(2) As Double
'tag variable used as input/output for the .SpecifyCsys method
'passing a null tag into the function uses the current WCS as the starting csys
Dim newCsysTag As Tag = Tag.Null
'variable to hold the user specified coordinate system
Dim newCsys As CartesianCoordinateSystem
Dim response As Integer
theUISession.LockAccess()
response = theUfSession.Ui.SpecifyCsys("Specify Desired Orientation", 5, myCsys, myOrigin, newCsysTag)
theUISession.UnlockAccess()
If response = Selection.Response.Ok Then
'get CartesianCoordinateSystem object from tag
newCsys = Utilities.NXObjectManager.Get(newCsysTag)
'orient WCS to specified coordinate system
theSession.Parts.Work.WCS.SetCoordinateSystemCartesianAtCsys(newCsys)
Return True
Else
'user pressed back or cancel or the system was in a state that could not allow the dialog to be shown
'1 = back
'2 = cancel
'3 = OK
'7 = no active part
'8 = disallowed state, unable to bring up dialog
Return False
End If
End Function
Function MeasureBody(ByVal theBody As Body) As String
Dim minCorner(2) As Double
Dim boxDirections(2, 2) As Double
Dim boxDistances(2) As Double
Dim boundX As Double
Dim boundY As Double
Dim boundZ As Double
'get solid body bounding box extents aligned to work csys (pass null tag to use work csys)
theUfSession.Modl.AskBoundingBoxAligned(theBody.Tag, Tag.Null, expand:=False, min_corner:=minCorner, directions:=boxDirections, distances:=boxDistances)
boundX = boxDistances(0)
boundY = boxDistances(1)
boundZ = boxDistances(2)
Return boundX.ToString & ", " & boundY.ToString & ", " & boundZ.ToString
End Function
Sub AppendToTextFile(ByVal textFilePath As String, ByVal textLine As String)
'pass False to overwrite existing file, True to append existing file
'if file does not exist, a new file will be created and the True/False value will be ignored
Using myWriter As New IO.StreamWriter(textFilePath, True)
myWriter.WriteLine(textline)
End Using
End Sub
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