@JCamper
Hello,
I also added the unfold part with the derived part but it does not unfold my part. Thank you for the help with this one!
Sub Main
'local sheet metal part rule. No document typ checking
Dim oDoc As Document = ThisDoc.Document
Dim oCompDef As SheetMetalComponentDefinition = oDoc.ComponentDefinition
'define the property set
Dim userParams As UserParameters = oDoc.ComponentDefinition.Parameters.UserParameters
Try
BendRadius_Parm = oDoc.ComponentDefinition.Parameters("BendRadius_Ilogic")
Catch
Dim newParam As UserParameter = userParams.AddByExpression("BendRadius_Ilogic", 1, "mm")
End Try
'New Code:
'Set Object to collect values to names
Dim PropList As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
PropList.Add("Length", 20.5)
PropList.Add("Width", 10.5)
PropList.Add("Thickness", 1.5)
PropList.Add("Profile", "Empty String")
Logger.Debug("Setup NameValueMap")
'Fill values based on whether or not there is a derived part
If oCompDef.ReferenceComponents.DerivedPartComponents.Count > 0
Logger.Debug("DerivedPartComponents Count > 0")
Dim ParentDef As SheetMetalComponentDefinition = Nothing
For Each RefDoc As Document In oDoc.ReferencedDocuments
Logger.Debug("DerivedPartComponent Loop")
If RefDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}"
Logger.Debug("Derived Comp Is SM")
ParentDef = RefDoc.ComponentDefinition
Exit For
End If
Next
'I'm exiting here. if you have sheet metal parts with derived parts that are not Sheet Metal, this would be an issue
If IsNothing(ParentDef) Then MessageBox.Show("No Derived Part was a Sheet Metal Part", "Termination") : Exit Sub
'Otherwise it will be fine
'Fill Values from Parent Document
If ParentDef.HasFlatPattern = True
Logger.Debug("Parent HasFlatPattern")
PropList.Value("Length") = (Round(ParentDef.FlatPattern.Length,0))
PropList.Value("Width") = (Round(ParentDef.FlatPattern.Width, 0))
PropList.Value("Thickness") = ParentDef.Parameters("Thickness").Value
PropList.Value("Profile") = "SM " & PropList.Value("Length") & "x" & PropList.Value("Width") & "x" & PropList.Value("Thickness")
Else
Logger.Debug("Parent Not HasFlatPattern")
oCompDef.Unfold
Events
SheetMetal_Hole
PropList.Value("Length") = FindMax(ParentDef.RangeBox, "L")
PropList.Value("Width") = FindMax(ParentDef.RangeBox, "W")
PropList.Value("Thickness") = ParentDef.Parameters("Thickness").Value
PropList.Value("Profile") = "SM " & PropList.Value("Length") & "x" & PropList.Value("Width") & "x" & PropList.Value("Thickness")
End If
Else 'No Referenced Documents
Logger.Debug("DerivedPartComponents Count <= 0")
'Fill Values from open document
If oCompDef.HasFlatPattern = True
Logger.Debug("ThisDoc HasFlatPattern")
SheetMetal_Hole
PropList.Value("Length") = (Round(oCompDef.FlatPattern.Length,0))
PropList.Value("Width") = (Round(oCompDef.FlatPattern.Width, 0))
PropList.Value("Thickness") = oCompDef.Parameters("Thickness").Value
PropList.Value("Profile") = "SM " & PropList.Value("Length") & "x" & PropList.Value("Width") & "x" & PropList.Value("Thickness")
Else
Logger.Debug("ThisDoc Not HasFlatPattern")
oCompDef.Unfold
Events
SheetMetal_Hole
PropList.Value("Length") = FindMax(oCompDef.RangeBox, "L")
PropList.Value("Width") = FindMax(oCompDef.RangeBox, "W")
PropList.Value("Thickness") = oCompDef.Parameters("Thickness").Value
PropList.Value("Profile") = "SM " & PropList.Value("Length") & "x" & PropList.Value("Width") & "x" & PropList.Value("Thickness")
End If
End If
'Set iProperties
Dim CustProps As PropertySet = oDoc.PropertySets.Item("Inventor User Defined Properties")
For i = 1 To PropList.Count
Try
CustProps.Item(PropList.Name(i)).Expression = PropList.Value(PropList.Name(i))
Catch
CustProps.Add(PropList.Value(PropList.Name(i)), PropList.Name(i))
End Try
Next
End Sub
Function FindMax(aBox As Box, Selection As String) As Double
Dim Result As Double = 0
Dim DeltaX As Double = Round((aBox.MaxPoint.X - aBox.MinPoint.X),0)
Dim DeltaY As Double = Round((aBox.MaxPoint.Y - aBox.MinPoint.Y),0)
Dim DeltaZ As Double = Round((aBox.MaxPoint.Z - aBox.MinPoint.Z),0)
If Selection = "L" 'Length is longest
If DeltaX > DeltaY And DeltaX > DeltaZ
Result = DeltaX
Else If DeltaY > DeltaX And DeltaY > DeltaZ
Result = DeltaY
Else If DeltaZ > DeltaX And DeltaZ > DeltaY
Result = DeltaZ
End If
Else If Selection = "W" 'Width is middle value
If DeltaX > DeltaY And DeltaX < DeltaZ
Result = DeltaX
Else If DeltaY > DeltaX And DeltaY < DeltaZ
Result = DeltaY
Else If DeltaZ > DeltaX And DeltaZ < DeltaY
Result = DeltaZ
End If
End If
Return Result
End Function
Sub Events
On Error Resume Next
Dim EventPropSet As Inventor.PropertySet
EventPropSet = GetiLogicEventPropSet(ThisApplication.ActiveDocument)
' To make sure that the document has an iLogic DocumentInterest, add a temporary rule
Dim tempRule = iLogicVb.Automation.AddRule(ThisDoc.Document, "TemporaryRule_392856A2", "")
EventPropSet.Add("file://COS_FlatPattern", "PartBodyChanged", 1250)
EventPropSet.Add("file://BendRadius", "BeforeDocSave1", 701)
EventPropSet.Add("file://COS_Sheet_Metal_Unfol_Rule_Trumpf", "BeforeDocSave2", 702)
iLogicVb.Automation.DeleteRule(ThisDoc.Document, tempRule.Name)
'After Open Document : AfterDocOpen : 400
'Close(Document) : DocClose : 500
'Before Save Document : BeforeDocSave : 700
'After Save Document : AfterDocSave : 800
'Any Model Parameter Change : AfterAnyParamChange : 1000
'Part Geometry Change** : PartBodyChanged : 1200
'Material Change** : AfterMaterialChange : 1400
'Drawing View Change*** : AfterDrawingViewsUpdate : 1500
'iProperty(Change) : AfterAnyiPropertyChange : 1600
'Feature Suppression Change** : AfterFeatureSuppressionChange : 2000
'Component Suppression Change* : AfterComponentSuppressionChange : 2200
'iPart / iAssembly Change Component* : AfterComponentReplace : 2400
'New Document : AfterDocNew : 2600
InventorVb.DocumentUpdate()
End Sub
Function GetiLogicEventPropSet(cDocument As Document) As Inventor.PropertySet
On Error Resume Next
iLogicEventPropSet = cDocument.PropertySets.Item("iLogicEventsRules")
If iLogicEventPropSet Is Nothing Then
iLogicEventPropSet = cDocument.PropertySets.Item("_iLogicEventsRules")
End If
If iLogicEventPropSet.InternalName <> "{2C540830-0723-455E-A8E2-891722EB4C3E}" Then
Call iLogicEventPropSet.Delete
iLogicEventPropSet = cDocument.PropertySets.Add("iLogicEventsRules", "{2C540830-0723-455E-A8E2-891722EB4C3E}")
End If
If iLogicEventPropSet Is Nothing Then
iLogicEventPropSet = cDocument.PropertySets.Add("iLogicEventsRules", "{2C540830-0723-455E-A8E2-891722EB4C3E}")
End If
If iLogicEventPropSet Is Nothing Then
MsgBox ("Unable to create the Event Triggers property for this file!", , "Event Triggers Not Set")
Err.Raise(1)
Exit Function
End If
On Error GoTo 0
Return iLogicEventPropSet
End Function
Sub SheetMetal_Hole
Dim Feature As PartFeature
Dim Features As PartFeatures
Features = ThisDoc.Document.ComponentDefinition.Features
i = 0
j = 0
k = 0
l = 0
For Each Feature In Features
'check if its a hole
If TypeOf Feature Is ExtrudeFeature And Feature.Suppressed = False And Feature.Name.Contains("Chamfer") Then
'increment one
i = i + 1
End If
Next
For Each hole_cb As HoleFeature In Features.HoleFeatures
If hole_cb.HoleType = HoleTypeEnum.kCounterSinkHole
' Dim info_cb As HoleInfo = hole_cb.Info
' If (info_cb IsNot Nothing) Then
j = j + 1
' ' Your hole changing code go's here
End If
Next
For Each hole_tap As HoleFeature In Features.HoleFeatures
' Dim info_tap As HoleTapInfo = hole_tap.TapInfo
If hole_tap.Tapped = True Then
k = k + 1
' Your hole changing code go's here
End If
Next
For Each Feature In Features
'check if its a hole
'TypeOf Feature Is ExtrudeFeature
If Feature.Suppressed = False And Feature.Name.Contains("Flange") Then
'increment one
l = l + 1
End If
If Feature.Suppressed = False And Feature.Name.Contains("Contour Flange") Then
'increment one
l = l + 1
End If
Next
' Get the active part document.
Dim invPartDoc As PartDocument = ThisDoc.Document
'Get the custom property set.
Dim invCustomPropertySet As PropertySet = invPartDoc.PropertySets.Item("Inventor User Defined Properties")
'MessageBox.Show(i, "i")
'MessageBox.Show(j, "j")
'MessageBox.Show(k, "k")
'MessageBox.Show(l, "l")
If i = 0 And j = 0 And k = 0 And l = 0 Then
iProperties.Value("Custom", "Operation_1") = "Laser cutting"
iProperties.Value("Custom", "Operation_2") = ""
iProperties.Value("Custom", "Operation_3") = ""
iProperties.Value("Custom", "Operation_4") = ""
End If
If i = 0 And j = 0 And k = 0 And l > 0 Then
iProperties.Value("Custom", "Operation_1") = "Laser cutting"
iProperties.Value("Custom", "Operation_2") = "Bending"
iProperties.Value("Custom", "Operation_3") = ""
iProperties.Value("Custom", "Operation_4") = ""
End If
If i > 0 And j > 0 And k = 0 And l = 0 Then
iProperties.Value("Custom", "Operation_1") = "Laser cutting"
iProperties.Value("Custom", "Operation_2") = "Countersink"
iProperties.Value("Custom", "Operation_3") = "Chamfer"
iProperties.Value("Custom", "Operation_4") = ""
End If
If i > 0 And j > 0 And k = 0 And l > 0 Then
iProperties.Value("Custom", "Operation_1") = "Laser cutting"
iProperties.Value("Custom", "Operation_2") = "Bending"
iProperties.Value("Custom", "Operation_3") = "Countersink"
iProperties.Value("Custom", "Operation_4") = "Chamfer"
End If
If i = 0 And j > 0 And k = 0 And l = 0 Then
iProperties.Value("Custom", "Operation_1") = "Laser cutting"
iProperties.Value("Custom", "Operation_2") = "Countersink"
iProperties.Value("Custom", "Operation_3") = ""
iProperties.Value("Custom", "Operation_4") = ""
End If
If i = 0 And j > 0 And k = 0 And l > 0 Then
iProperties.Value("Custom", "Operation_1") = "Laser cutting"
iProperties.Value("Custom", "Operation_2") = "Bending"
iProperties.Value("Custom", "Operation_3") = "Countersink"
iProperties.Value("Custom", "Operation_4") = ""
End If
If i > 0 And j = 0 And k = 0 And l = 0 Then
iProperties.Value("Custom", "Operation_1") = "Laser cutting"
iProperties.Value("Custom", "Operation_2") = "Chamfer"
iProperties.Value("Custom", "Operation_3") = ""
iProperties.Value("Custom", "Operation_4") = ""
End If
If i > 0 And j = 0 And k = 0 And l > 0 Then
iProperties.Value("Custom", "Operation_1") = "Laser cutting"
iProperties.Value("Custom", "Operation_2") = "Bending"
iProperties.Value("Custom", "Operation_3") = "Chamfer"
iProperties.Value("Custom", "Operation_4") = ""
End If
If i > 0 And j = 0 And k > 0 And l = 0 Then
iProperties.Value("Custom", "Operation_1") = "Laser cutting"
iProperties.Value("Custom", "Operation_2") = "Tapping"
iProperties.Value("Custom", "Operation_3") = "Chamfer"
iProperties.Value("Custom", "Operation_4") = ""
End If
If i > 0 And j = 0 And k > 0 And l > 0 Then
iProperties.Value("Custom", "Operation_1") = "Laser cutting"
iProperties.Value("Custom", "Operation_2") = "Bending"
iProperties.Value("Custom", "Operation_3") = "Tapping"
iProperties.Value("Custom", "Operation_4") = "Chamfer"
End If
If i = 0 And j = 0 And k > 0 And l = 0 Then
iProperties.Value("Custom", "Operation_1") = "Laser cutting"
iProperties.Value("Custom", "Operation_2") = "Tapping"
iProperties.Value("Custom", "Operation_3") = ""
iProperties.Value("Custom", "Operation_4") = ""
End If
If i = 0 And j = 0 And k > 0 And l > 0 Then
iProperties.Value("Custom", "Operation_1") = "Laser cutting"
iProperties.Value("Custom", "Operation_2") = "Bending"
iProperties.Value("Custom", "Operation_3") = "Tapping"
iProperties.Value("Custom", "Operation_4") = ""
End If
If i = 0 And j > 0 And k > 0 And l = 0 Then
iProperties.Value("Custom", "Operation_1") = "Laser cutting"
iProperties.Value("Custom", "Operation_2") = "Countersink"
iProperties.Value("Custom", "Operation_3") = "Tapping"
iProperties.Value("Custom", "Operation_4") = ""
End If
If i = 0 And j > 0 And k > 0 And l > 0 Then
iProperties.Value("Custom", "Operation_1") = "Laser cutting"
iProperties.Value("Custom", "Operation_2") = "Bending"
iProperties.Value("Custom", "Operation_3") = "Countersink"
iProperties.Value("Custom", "Operation_4") = "Tapping"
End If
End Sub
Sub SheetMetal_Hole_Manueel
Dim Feature As PartFeature
Dim Features As PartFeatures
'Features = Doc.Document.ComponentDefinition.Features
i = False
j = False
k = False
l = False
'Dim oDoc As Document
'oDoc = ThisDoc.Document
'docName0 = oDoc.FullFileName
'ThisApplication.CommandManager.ControlDefinitions.Item("AppZoomallCmd").Execut
'MessageBox.Show(docName0, "We will change the following part:")
i = InputRadioBox("Does the part has chamfered edges?", "Yes", "No", booleanParam, Title := "Chamfer")
j = InputRadioBox("Does the part has countersink holes?", "Yes", "No", booleanParam, Title := "Countersink")
k = InputRadioBox("Does the part has threaded holes?", "Yes", "No", booleanParam, Title := "Thread")
l = InputRadioBox("Does the part has bends?", "Yes", "No", booleanParam, Title := "Bends")
' Get the active part document.
Dim invPartDoc As PartDocument = ThisDoc.Document
'Get the custom property set.
Dim invCustomPropertySet As PropertySet = invPartDoc.PropertySets.Item("Inventor User Defined Properties")
'MessageBox.Show(i, "i")
'MessageBox.Show(j, "j")
'MessageBox.Show(k, "k")
'MessageBox.Show(l, "l")
If i = False And j = False And k = False And l = False Then
iProperties.Value("Custom", "Operation_1") = "Laser cutting"
iProperties.Value("Custom", "Operation_2") = ""
iProperties.Value("Custom", "Operation_3") = ""
iProperties.Value("Custom", "Operation_4") = ""
End If
If i = False And j = False And k = False And l = True Then
iProperties.Value("Custom", "Operation_1") = "Laser cutting"
iProperties.Value("Custom", "Operation_2") = "Bending"
iProperties.Value("Custom", "Operation_3") = ""
iProperties.Value("Custom", "Operation_4") = ""
End If
If i > 0 And j = True And k = False And l = False Then
iProperties.Value("Custom", "Operation_1") = "Laser cutting"
iProperties.Value("Custom", "Operation_2") = "Countersink"
iProperties.Value("Custom", "Operation_3") = "Chamfer"
iProperties.Value("Custom", "Operation_4") = ""
End If
If i = True And j = True And k = False And l = True Then
iProperties.Value("Custom", "Operation_1") = "Laser cutting"
iProperties.Value("Custom", "Operation_2") = "Bending"
iProperties.Value("Custom", "Operation_3") = "Countersink"
iProperties.Value("Custom", "Operation_4") = "Chamfer"
End If
If i = False And j = True And k = False And l = False Then
iProperties.Value("Custom", "Operation_1") = "Laser cutting"
iProperties.Value("Custom", "Operation_2") = "Countersink"
iProperties.Value("Custom", "Operation_3") = ""
iProperties.Value("Custom", "Operation_4") = ""
End If
If i = False And j = True And k = False And l = True Then
iProperties.Value("Custom", "Operation_1") = "Laser cutting"
iProperties.Value("Custom", "Operation_2") = "Bending"
iProperties.Value("Custom", "Operation_3") = "Countersink"
iProperties.Value("Custom", "Operation_4") = ""
End If
If i = True And j = False And k = False And l = False Then
iProperties.Value("Custom", "Operation_1") = "Laser cutting"
iProperties.Value("Custom", "Operation_2") = "Chamfer"
iProperties.Value("Custom", "Operation_3") = ""
iProperties.Value("Custom", "Operation_4") = ""
End If
If i = True And j = False And k = False And l = True Then
iProperties.Value("Custom", "Operation_1") = "Laser cutting"
iProperties.Value("Custom", "Operation_2") = "Bending"
iProperties.Value("Custom", "Operation_3") = "Chamfer"
iProperties.Value("Custom", "Operation_4") = ""
End If
If i = True And j = False And k = True And l = False Then
iProperties.Value("Custom", "Operation_1") = "Laser cutting"
iProperties.Value("Custom", "Operation_2") = "Tapping"
iProperties.Value("Custom", "Operation_3") = "Chamfer"
iProperties.Value("Custom", "Operation_4") = ""
End If
If i = True And j = False And k = True And l = True Then
iProperties.Value("Custom", "Operation_1") = "Laser cutting"
iProperties.Value("Custom", "Operation_2") = "Bending"
iProperties.Value("Custom", "Operation_3") = "Tapping"
iProperties.Value("Custom", "Operation_4") = "Chamfer"
End If
If i = False And j = False And k = True And l = False Then
iProperties.Value("Custom", "Operation_1") = "Laser cutting"
iProperties.Value("Custom", "Operation_2") = "Tapping"
iProperties.Value("Custom", "Operation_3") = ""
iProperties.Value("Custom", "Operation_4") = ""
End If
If i = False And j = False And k = True And l = True Then
iProperties.Value("Custom", "Operation_1") = "Laser cutting"
iProperties.Value("Custom", "Operation_2") = "Bending"
iProperties.Value("Custom", "Operation_3") = "Tapping"
iProperties.Value("Custom", "Operation_4") = ""
End If
If i = False And j = True And k = True And l = False Then
iProperties.Value("Custom", "Operation_1") = "Laser cutting"
iProperties.Value("Custom", "Operation_2") = "Countersink"
iProperties.Value("Custom", "Operation_3") = "Tapping"
iProperties.Value("Custom", "Operation_4") = ""
End If
If i = False And j = True And k = True And l = True Then
iProperties.Value("Custom", "Operation_1") = "Laser cutting"
iProperties.Value("Custom", "Operation_2") = "Bending"
iProperties.Value("Custom", "Operation_3") = "Countersink"
iProperties.Value("Custom", "Operation_4") = "Tapping"
End If
End Sub