Hi,
I put together somehow some lines of codes and I do a VBA User Form on the drawing template.
This will change some of the parameter from the part and from the drawing, if Sheet Metal it is the referenced document.
Can this User Form be trigger when the "Template" it is use for Sheet Metal?
Or can be also when "iTrigger" Button it is click?
This is my code:
Private Sub UserForm_Initialize()
CommandButton1.Caption = "SS Sheet Metal" & vbCrLf & "Normal" & vbCrLf & "Ra. 2.5"
CommandButton2.Caption = "SS Sheet Metal" & vbCrLf & "Normal" & vbCrLf & "Electro Polished" & vbCrLf & "Ra. 1.6"
End Sub
'SS Sheet - Normal - 2.5
Private Sub CommandButton1_Click()
Dim oInvApp As Object
Set oInvApp = ThisApplication
Dim oDDoc As DrawingDocument
Set oDDoc = oInvApp.ActiveDocument
If oDDoc Is Nothing Then Exit Sub
Dim oViews As DrawingViews
Set oViews = oDDoc.ActiveSheet.DrawingViews
If oViews.Count = 0 Then Exit Sub
Dim oDoc As PartDocument
Set oDoc = oViews(1).ReferencedDocumentDescriptor.ReferencedDocument
If oDoc Is Nothing Then Exit Sub
Dim desiredMaterialName As String
desiredMaterialName = "SS sheet 1.4301 (AISI 304) 2B Annealed and pickled"
If oDoc.ActiveMaterial.DisplayName <> desiredMaterialName Then
Dim oNewAsset As Object
Set oNewAsset = Nothing
Dim oAssetlib As Object
For Each oAssetlib In oInvApp.AssetLibraries
Dim i As Integer
For i = 1 To oAssetlib.MaterialAssets.Count
If oAssetlib.MaterialAssets(i).DisplayName = desiredMaterialName Then
Set oNewAsset = oAssetlib.MaterialAssets(i)
Exit For
End If
Next i
If Not oNewAsset Is Nothing Then Exit For
Next oAssetlib
If Not oNewAsset Is Nothing Then
On Error Resume Next
oDoc.ActiveMaterial = oNewAsset
On Error GoTo 0
If Err.Number <> 0 Then
MsgBox "Failed to change material!" & vbCrLf & "Error: " & Err.Description, vbExclamation, "Error!"
Err.Clear
End If
Else
MsgBox "Material 'SS sheet 1.4301 (AISI 304) 2B Annealed and pickled' not found in asset libraries.", vbExclamation, "Error!"
End If
End If
oDoc.PropertySets.Item("Inventor User Defined Properties").Item("General surface roughness").Value = "2.5"
oDoc.PropertySets.Item("Inventor User Defined Properties").Item("Treatment").Value = "Chamfering Only"
oDoc.PropertySets.Item("Inventor User Defined Properties").Item("Title_Block_Chamfer").Value = "Chamfering: Machine deburred (Fladder tool)"
'--sheet metal styles--
If TypeOf oDoc Is PartDocument Then
Dim oSheetMetal As SheetMetalComponentDefinition
Set oSheetMetal = oDoc.ComponentDefinition
Dim thicknessParameterValue As Double
On Error Resume Next
thicknessParameterValue = oSheetMetal.Thickness.Value * 10
On Error GoTo 0
If thicknessParameterValue > 0 Then
Dim styleName As String
Select Case thicknessParameterValue
Case "1.5"
styleName = "Stainless Steel - 1.5mm"
Case "2"
styleName = "Stainless Steel - 2mm"
Case "3"
styleName = "Stainless Steel - 3mm"
Case "5"
styleName = "Stainless Steel - 5mm"
Case "8"
styleName = "Stainless Steel - 8mm"
Case "10"
styleName = "Stainless Steel - 10mm"
Case Else
styleName = "DefaultStyle"
End Select
On Error Resume Next
oSheetMetal.SheetMetalStyles.Item(styleName).Activate
On Error GoTo 0
If Err.Number <> 0 Then
MsgBox "Error activating sheet metal style: " & Err.Description, vbExclamation, "Error!"
Err.Clear
End If
Else
MsgBox "Invalid thickness parameter value.", vbExclamation, "Error!"
End If
Else
MsgBox "Active document is not a sheet metal document."
End If
oDDoc.Update
End Sub
'SS Sheet - Normal - Electro polished - 1.6
Private Sub CommandButton2_Click()
Dim oInvApp As Object
Set oInvApp = ThisApplication
Dim oDDoc As DrawingDocument
Set oDDoc = oInvApp.ActiveDocument
If oDDoc Is Nothing Then Exit Sub
Dim oViews As DrawingViews
Set oViews = oDDoc.ActiveSheet.DrawingViews
If oViews.Count = 0 Then Exit Sub
Dim oDoc As PartDocument
Set oDoc = oViews(1).ReferencedDocumentDescriptor.ReferencedDocument
If oDoc Is Nothing Then Exit Sub
Dim desiredMaterialName As String
desiredMaterialName = "SS sheet 1.4301 (AISI 304) 2B Annealed and pickled"
If oDoc.ActiveMaterial.DisplayName <> desiredMaterialName Then
Dim oNewAsset As Object
Set oNewAsset = Nothing
Dim oAssetlib As Object
For Each oAssetlib In oInvApp.AssetLibraries
Dim i As Integer
For i = 1 To oAssetlib.MaterialAssets.Count
If oAssetlib.MaterialAssets(i).DisplayName = desiredMaterialName Then
Set oNewAsset = oAssetlib.MaterialAssets(i)
Exit For
End If
Next i
If Not oNewAsset Is Nothing Then Exit For
Next oAssetlib
If Not oNewAsset Is Nothing Then
On Error Resume Next
oDoc.ActiveMaterial = oNewAsset
On Error GoTo 0
If Err.Number <> 0 Then
MsgBox "Failed to change material!" & vbCrLf & "Error: " & Err.Description, vbExclamation, "Error!"
Err.Clear
End If
Else
MsgBox "Material 'SS sheet 1.4301 (AISI 304) 2B Annealed and pickled' not found in asset libraries.", vbExclamation, "Error!"
End If
End If
oDoc.PropertySets.Item("Inventor User Defined Properties").Item("General surface roughness").Value = "1.6"
oDoc.PropertySets.Item("Inventor User Defined Properties").Item("Treatment").Value = "Electro Polishing"
oDoc.PropertySets.Item("Inventor User Defined Properties").Item("Title_Block_Chamfer").Value = "Chamfering: Break sharp edges 0,2 to 0,5x45° if not otherwise defined."
'--sheet metal styles--
If TypeOf oDoc Is PartDocument Then
Dim oSheetMetal As SheetMetalComponentDefinition
Set oSheetMetal = oDoc.ComponentDefinition
Dim thicknessParameterValue As Double
On Error Resume Next
thicknessParameterValue = oSheetMetal.Thickness.Value * 10
On Error GoTo 0
If thicknessParameterValue > 0 Then
Dim styleName As String
Select Case thicknessParameterValue
Case "1.5"
styleName = "Stainless Steel - 1.5mm"
Case "2"
styleName = "Stainless Steel - 2mm"
Case "3"
styleName = "Stainless Steel - 3mm"
Case "5"
styleName = "Stainless Steel - 5mm"
Case "8"
styleName = "Stainless Steel - 8mm"
Case "10"
styleName = "Stainless Steel - 10mm"
Case Else
styleName = "DefaultStyle"
End Select
On Error Resume Next
oSheetMetal.SheetMetalStyles.Item(styleName).Activate
On Error GoTo 0
If Err.Number <> 0 Then
MsgBox "Error activating sheet metal style: " & Err.Description, vbExclamation, "Error!"
Err.Clear
End If
Else
MsgBox "Invalid thickness parameter value.", vbExclamation, "Error!"
End If
Else
MsgBox "Active document is not a sheet metal document."
End If
oDDoc.Update
End Sub
Can't find what you're looking for? Ask the community or share your knowledge.