Apply costumer défault to drafting

Hi !
is it possible in a component to check if there are drawings. If so, then select all dimenssions and annotations and update them in function costumer default.

Then check if there is more than one page and if there is more than one page, then do the same thing in the other pages.

Do gave an exemple on VB?

Thank you !

I found this code on GTAC (nx_api6146), it was in C #, I used a code converter to transfer it in .VB.
However, it has some errors, can you help me to make this code functional?
Best Regards!

Option Strict Off

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

Module PrefToDim

Dim theSession As Session = Nothing
Dim theUfSession As UFSession = Nothing
Dim theUI As UI = Nothing
Dim isDebug As Boolean = False

Dim sysInfo As NXOpen.UF.SystemInfo = Nothing

Sub Main()
'Dim workPart As Part '= theSession.Parts.Work
Dim workPart As Part '= Nothing
theSession = Session.GetSession()
theUfSession = UFSession.GetUFSession()

theUI = UI.GetUI()

'If workPart Is Nothing Then
' UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.[Error], "A Part must be Opened.")
' Return
'End If

theSession.EnableRedo(False)
If isDebug Then SystemInfo(theUfSession.Part, workPart)

Try

If CheckValidSheetAndViews() = 0 Then
Dim message As String() = New String(0) {}
message(0) = "Ok to Set Preferences for all Dimension"
If AskYesNo("Preferences Dimension", message) = 2 Then Return
Dim preferencesBuilder As NXOpen.Drafting.PreferencesBuilder = workPart.SettingsManager.CreatePreferencesBuilder()
preferencesBuilder.InheritSettingsFromCustomerDefault()
preferencesBuilder.Commit()
preferencesBuilder.Destroy()
Dim displayableObjects As NXOpen.DisplayableObject() = New DisplayableObject(0) {}
Dim baseEditSettingsBuilders As NXOpen.Drafting.BaseEditSettingsBuilder() = New NXOpen.Drafting.BaseEditSettingsBuilder(0) {}
Dim dimensionCollection As NXOpen.Annotations.DimensionCollection = workPart.Dimensions

For Each dimension As NXOpen.Annotations.Dimension In dimensionCollection

Try
displayableObjects(0) = dimension
Dim editSettingsBuilder As NXOpen.Annotations.EditSettingsBuilder = workPart.SettingsManager.CreateAnnotationEditSettingsBuilder(displayableObjects)
baseEditSettingsBuilders(0) = editSettingsBuilder
workPart.SettingsManager.ProcessForMultipleObjectsSettings(baseEditSettingsBuilders)
editSettingsBuilder.InheritSettingsFromCustomerDefault()
editSettingsBuilder.Commit()
editSettingsBuilder.Destroy()
Catch nXException As NXException
UI.GetUI().NXMessageBox.Show("Settings Dimension", NXMessageBox.DialogType.[Error], "Unable to Set Preference Settings to Dimension. Error : " & nXException.Message.ToString())
End Try
Next
End If

Catch nXException As NXOpen.NXException
UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.[Error], nXException.Message.ToString())
End Try
End Sub

Function CheckValidSheetAndViews() As Integer
Dim errorCode As Integer = 0

Try
Dim draftingDrawingSheetCollection As NXOpen.Drawings.DraftingDrawingSheetCollection = theSession.Parts.Work.DraftingDrawingSheets

If draftingDrawingSheetCollection.ToArray().Length = 0 Then
'If draftingDrawingSheetCollection.ToArray().Length Is 0 Then
UI.GetUI().NXMessageBox.Show("Sheet", NXMessageBox.DialogType.[Error], "No Sheet in this Part.")
errorCode = 1
Return errorCode
End If

Catch nXException As NXOpen.NXException
UI.GetUI().NXMessageBox.Show("Sheet", NXMessageBox.DialogType.[Error], "Unable to Retrieve Sheets in this Part. Error : " & nXException.Message.ToString())
errorCode = 1
Return errorCode
End Try

Dim drawingTag As Tag = Tag.Null

Try
theUfSession.Draw.AskCurrentDrawing(drawingTag)

'If drawingTag Is Tag.Null Then
If drawingTag = 0 Then
UI.GetUI().NXMessageBox.Show("Open Sheet", NXMessageBox.DialogType.[Error], "A Sheet must be Open.")
errorCode = 1
Return errorCode
End If

Catch nXException As NXOpen.NXException
UI.GetUI().NXMessageBox.Show("Open Sheet", NXMessageBox.DialogType.[Error], "Unable to Retrieve Current Sheet. Error : " & nXException.Message.ToString())
errorCode = 1
Return errorCode
End Try

Dim numOfViews As Integer = 0

Try
theUfSession.Draw.AskNumViews(drawingTag, numOfViews)

If numOfViews = 0 Then
UI.GetUI().NXMessageBox.Show("Views", NXMessageBox.DialogType.[Error], "No View in this Sheet.")
errorCode = 1
Return errorCode
End If

Catch nXException As NXOpen.NXException
UI.GetUI().NXMessageBox.Show("Views", NXMessageBox.DialogType.[Error], "Unable to Retrieve Views. Error : " & nXException.Message.ToString())
errorCode = 1
Return errorCode
End Try

Return errorCode
End Function

Function AskYesNo(ByVal title As String, ByVal message As String()) As Integer
Dim response As Integer = 0

Try
Dim messageButtons As UFUi.MessageButtons = Nothing
messageButtons.button1 = True
messageButtons.button2 = False
messageButtons.button3 = True
messageButtons.label1 = "Yes"
messageButtons.label2 = String.Empty
messageButtons.label3 = "No"
messageButtons.response1 = 1
messageButtons.response2 = 0
messageButtons.response3 = 2
theUfSession.Ui.MessageDialog(title, UiMessageDialogType.UiMessageQuestion, message, 1, True, messageButtons, response)
Catch nXException As NXOpen.NXException
UI.GetUI().NXMessageBox.Show("UF Message Button", NXMessageBox.DialogType.[Error], nXException.Message.ToString())
End Try

Return response
End Function

Private Sub SystemInfo(ByVal uFPart As UFPart, ByVal workPart As Part)
Try
''' Cannot convert LocalDeclarationStatementSyntax, System.InvalidCastException: Unable to cast object of type 'Microsoft.CodeAnalysis.VisualBasic.Syntax.UnaryExpressionSyntax' to type 'Microsoft.CodeAnalysis.VisualBasic.Syntax.TypeSyntax'.
''' at ICSharpCode.CodeConverter.VB.CommonConversions.RemodelVariableDeclaration(VariableDeclarationSyntax declaration) in D:\GitWorkspace\CodeConverter\ICSharpCode.CodeConverter\VB\CommonConversions.cs:line 368
''' at ICSharpCode.CodeConverter.VB.MethodBodyVisitor.VisitLocalDeclarationStatement(LocalDeclarationStatementSyntax node) in D:\GitWorkspace\CodeConverter\ICSharpCode.CodeConverter\VB\MethodBodyVisitor.cs:line 65
''' at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor`1.Visit(SyntaxNode node)
''' at ICSharpCode.CodeConverter.VB.CommentConvertingMethodBodyVisitor.ConvertWithTrivia(SyntaxNode node) in D:\GitWorkspace\CodeConverter\ICSharpCode.CodeConverter\VB\CommentConvertingMethodBodyVisitor.cs:line 38
''' at ICSharpCode.CodeConverter.VB.CommentConvertingMethodBodyVisitor.DefaultVisit(SyntaxNode node) in D:\GitWorkspace\CodeConverter\ICSharpCode.CodeConverter\VB\CommentConvertingMethodBodyVisitor.cs:line 28
'''
''' Input:
''' SystemInfo sysInfo = default(SystemInfo);

'''
theUfSession.UF.AskSystemInfo(sysInfo)
Dim partName As String = String.Empty
If workPart IsNot Nothing Then uFPart.AskPartName(workPart.Tag, partName)
If Not theSession.ListingWindow.IsOpen Then theSession.ListingWindow.Open()
theSession.ListingWindow.WriteFullline("============================================================")
theSession.ListingWindow.WriteFullline("Information Listing Created by : " & sysInfo.user_name.ToString())
theSession.ListingWindow.WriteFullline("Date : " & sysInfo.date_buf.ToString())
If workPart IsNot Nothing Then theSession.ListingWindow.WriteFullline("Current Work Part : " & partName)
theSession.ListingWindow.WriteFullline("Node Name : " & sysInfo.node_name.ToString())
theSession.ListingWindow.WriteFullline("============================================================" & vbLf & vbLf)
Catch nXException As NXException
theUI.NXMessageBox.Show("System Information", NXMessageBox.DialogType.[Error], "Unable to Display System Information. Error : " & nXException.Message.ToString())
End Try
End Sub

Function GetUnloadOption(ByVal arg As String) As Integer
Return System.Convert.ToInt32(Session.LibraryUnloadOption.Immediately)
End Function
End Module

Pat

If the GTAC code does what you want, you can run it directly as a journal. There is no need to convert it to VB first.

Thank you for your answer!

I wanted to convert it since I have no knowledge in C # and I had to adapt the code to my needs.

finally, I managed to make the code work and everything works fine.

It allows to :
-Select the drafting mode,
-Check if there are any Sheets
-Select all dimensions and annotations on each sheet.
-To these selections he applies the customer default.

Best Regards!

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

Module PrefToDim
Public theSession As Session = Nothing
Public theUfSession As UFSession = Nothing
Public theUI As UI = Nothing
Public isDebug As Boolean = False

Sub Main()
theSession = Session.GetSession()
theUfSession = UFSession.GetUFSession()
Dim workPart As Part = theSession.Parts.Work
theUI = UI.GetUI()

If workPart Is Nothing Then
UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.[Error], "A Part must be Opened.")
Return
End If

theSession.EnableRedo(False)
If isDebug Then SystemInfo(theUfSession.Part, workPart)

Try

If CheckValidSheetAndViews() = 0 Then
Dim message As String() = New String(0) {}
message(0) = "Ok to Set Preferences for all Dimension"
If AskYesNo("Preferences Dimension", message) = 2 Then Return
Dim preferencesBuilder As NXOpen.Drafting.PreferencesBuilder = workPart.SettingsManager.CreatePreferencesBuilder()
preferencesBuilder.InheritSettingsFromCustomerDefault()
'preferencesBuilder.AnnotationStyle.DimensionStyle.DimensionValuePrecision = 2
preferencesBuilder.Commit()
preferencesBuilder.Destroy()
Dim displayableObjects As NXOpen.DisplayableObject() = New DisplayableObject(0) {}
Dim baseEditSettingsBuilders As NXOpen.Drafting.BaseEditSettingsBuilder() = New NXOpen.Drafting.BaseEditSettingsBuilder(0) {}
Dim dimensionCollection As NXOpen.Annotations.DimensionCollection = workPart.Dimensions

For Each dimension As NXOpen.Annotations.Dimension In dimensionCollection

Try
displayableObjects(0) = dimension
Dim editSettingsBuilder As NXOpen.Annotations.EditSettingsBuilder = workPart.SettingsManager.CreateAnnotationEditSettingsBuilder(displayableObjects)
baseEditSettingsBuilders(0) = editSettingsBuilder
workPart.SettingsManager.ProcessForMultipleObjectsSettings(baseEditSettingsBuilders)
editSettingsBuilder.InheritSettingsFromCustomerDefault()
editSettingsBuilder.Commit()
editSettingsBuilder.Destroy()
Catch nXException As NXException
UI.GetUI().NXMessageBox.Show("Settings Dimension", NXMessageBox.DialogType.[Error], "Unable to Set Preference Settings to Dimension. Error : " & nXException.Message.ToString())
End Try
Next
End If

Catch nXException As NXOpen.NXException
UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.[Error], nXException.Message.ToString())
End Try
End Sub

Function CheckValidSheetAndViews() As Integer
Dim errorCode As Integer = 0

theSession.ApplicationSwitchImmediate("UG_APP_DRAFTING")
Try
Dim draftingDrawingSheetCollection As NXOpen.Drawings.DraftingDrawingSheetCollection = theSession.Parts.Work.DraftingDrawingSheets

If draftingDrawingSheetCollection.ToArray().Length = 0 Then
UI.GetUI().NXMessageBox.Show("Sheet", NXMessageBox.DialogType.[Error], "No Sheet in this Part.")
errorCode = 1
Return errorCode
End If

Catch nXException As NXOpen.NXException
UI.GetUI().NXMessageBox.Show("Sheet", NXMessageBox.DialogType.[Error], "Unable to Retrieve Sheets in this Part. Error : " & nXException.Message.ToString())
errorCode = 1
Return errorCode
End Try

Dim drawingTag As Tag = Tag.Null

Try
theUfSession.Draw.AskCurrentDrawing(drawingTag)

If drawingTag = Tag.Null Then
UI.GetUI().NXMessageBox.Show("Open Sheet", NXMessageBox.DialogType.[Error], "A Sheet must be Open.")
errorCode = 1
Return errorCode
End If

Catch nXException As NXOpen.NXException
UI.GetUI().NXMessageBox.Show("Open Sheet", NXMessageBox.DialogType.[Error], "Unable to Retrieve Current Sheet. Error : " & nXException.Message.ToString())
errorCode = 1
Return errorCode
End Try

Dim numOfViews As Integer = 0

Try
theUfSession.Draw.AskNumViews(drawingTag, numOfViews)

If numOfViews = 0 Then
UI.GetUI().NXMessageBox.Show("Views", NXMessageBox.DialogType.[Error], "No View in this Sheet.")
errorCode = 1
Return errorCode
End If

Catch nXException As NXOpen.NXException
UI.GetUI().NXMessageBox.Show("Views", NXMessageBox.DialogType.[Error], "Unable to Retrieve Views. Error : " & nXException.Message.ToString())
errorCode = 1
Return errorCode
End Try

Return errorCode
End Function

Function AskYesNo(ByVal title As String, ByVal message As String()) As Integer
Dim response As Integer = 0

Try
Dim messageButtons As UFUi.MessageButtons = Nothing
messageButtons.button1 = True
messageButtons.button2 = False
messageButtons.button3 = True
messageButtons.label1 = "Yes"
messageButtons.label2 = String.Empty
messageButtons.label3 = "No"
messageButtons.response1 = 1
messageButtons.response2 = 0
messageButtons.response3 = 2
theUfSession.Ui.MessageDialog(title, UiMessageDialogType.UiMessageQuestion, message, 1, True, messageButtons, response)
Catch nXException As NXOpen.NXException
UI.GetUI().NXMessageBox.Show("UF Message Button", NXMessageBox.DialogType.[Error], nXException.Message.ToString())
End Try

Return response
End Function

Private Sub SystemInfo(ByVal uFPart As UFPart, ByVal workPart As Part)

Dim sysInfo As NXOpen.UF.SystemInfo = Nothing
Try
''' Cannot convert LocalDeclarationStatementSyntax, System.InvalidCastException: Unable to cast object of type 'Microsoft.CodeAnalysis.VisualBasic.Syntax.UnaryExpressionSyntax' to type 'Microsoft.CodeAnalysis.VisualBasic.Syntax.TypeSyntax'.
''' at ICSharpCode.CodeConverter.VB.CommonConversions.RemodelVariableDeclaration(VariableDeclarationSyntax declaration)
''' at ICSharpCode.CodeConverter.VB.MethodBodyVisitor.VisitLocalDeclarationStatement(LocalDeclarationStatementSyntax node)
''' at Microsoft.CodeAnalysis.CSharp.Syntax.LocalDeclarationStatementSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor)
''' at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor`1.Visit(SyntaxNode node)
''' at ICSharpCode.CodeConverter.VB.CommentConvertingMethodBodyVisitor.ConvertWithTrivia(SyntaxNode node)
''' at ICSharpCode.CodeConverter.VB.CommentConvertingMethodBodyVisitor.DefaultVisit(SyntaxNode node)
'''
''' Input:
''' SystemInfo sysInfo = default(SystemInfo);

'''
theUfSession.UF.AskSystemInfo(sysInfo)
Dim partName As String = String.Empty
If workPart IsNot Nothing Then uFPart.AskPartName(workPart.Tag, partName)
If Not theSession.ListingWindow.IsOpen Then theSession.ListingWindow.Open()
theSession.ListingWindow.WriteFullline("============================================================")
theSession.ListingWindow.WriteFullline("Information Listing Created by : " & sysInfo.user_name.ToString())
theSession.ListingWindow.WriteFullline("Date : " & sysInfo.date_buf.ToString())
If workPart IsNot Nothing Then theSession.ListingWindow.WriteFullline("Current Work Part : " & partName)
theSession.ListingWindow.WriteFullline("Node Name : " & sysInfo.node_name.ToString())
theSession.ListingWindow.WriteFullline("============================================================" & vbLf & vbLf)
Catch nXException As NXException
theUI.NXMessageBox.Show("System Information", NXMessageBox.DialogType.[Error], "Unable to Display System Information. Error : " & nXException.Message.ToString())
End Try
End Sub

Function GetUnloadOption(ByVal arg As String) As Integer
Return System.Convert.ToInt32(Session.LibraryUnloadOption.Immediately)
End Function
End Module

Pat