Option Strict Off
Imports System
Imports NXOpen
Module Module1
Sub Main()
Dim theSession As Session = Session.GetSession()
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()
Const undoMarkName As String = "sheet size expressions"
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)
'lw.WriteLine("base unit of length: " & workPart.UnitCollection.GetBase("length").Abbreviation)
Dim sheetLength As Double
Dim sheetHeight As Double
Dim unit1 As Unit = workPart.UnitCollection.GetBase("length")
For Each tempSheet As Drawings.DrawingSheet In workPart.DrawingSheets
If tempSheet.Units = Drawings.DrawingSheet.Unit.Inches Then
unit1 = CType(workPart.UnitCollection.FindObject("Inch"), Unit)
Else
unit1 = CType(workPart.UnitCollection.FindObject("MilliMeter"), Unit)
End If
sheetLength = tempSheet.Length
sheetHeight = tempSheet.Height
Exit For
Next
Dim markId2 As Session.UndoMarkId
markId2 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Expression")
Try
'look for existing expression and update it
Dim expression1 As Expression = CType(workPart.Expressions.FindObject("SheetHeight"), Expression)
workPart.Expressions.EditWithUnits(expression1, unit1, sheetHeight.ToString)
Dim nErrs1 As Integer
nErrs1 = theSession.UpdateManager.DoUpdate(markId2)
Catch ex As NXException
If ex.ErrorCode = 3520016 Then
'expression does not exist, create it
Dim expression1 As Expression
expression1 = workPart.Expressions.CreateWithUnits("SheetHeight=" & sheetHeight.ToString, unit1)
Dim nErrs1 As Integer
nErrs1 = theSession.UpdateManager.DoUpdate(markId2)
Else
theSession.UndoToMark(markId1, undoMarkName)
MsgBox(ex.ErrorCode & ": " & ex.Message)
End If
End Try
Dim markId3 As Session.UndoMarkId
markId3 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Expression")
Try
'look for existing expression and update it
Dim expression1 As Expression = CType(workPart.Expressions.FindObject("SheetLength"), Expression)
workPart.Expressions.EditWithUnits(expression1, unit1, sheetLength.ToString)
Dim nErrs1 As Integer
nErrs1 = theSession.UpdateManager.DoUpdate(markId3)
Catch ex As NXException
If ex.ErrorCode = 3520016 Then
'expression does not exist, create it
Dim expression1 As Expression
expression1 = workPart.Expressions.CreateWithUnits("SheetLength=" & sheetLength.ToString, unit1)
Dim nErrs1 As Integer
nErrs1 = theSession.UpdateManager.DoUpdate(markId3)
Else
'unexpected error, undo all changes made by journal
theSession.UndoToMark(markId1, undoMarkName)
MsgBox(ex.ErrorCode & ": " & ex.Message)
End If
End Try
lw.Close()
End Sub
End Module