Hi @Anonymous & @michalCDNJV . You both seem to be having some troubles with this code. Since it does seem to work part of the time, and you are not sure why it does not work other times, I thought that maybe you could use some extra checks and feedback within the rule to help out. I also excluded checking the TopFace, in favor of just checking the BottomFace of the Flat Pattern, just to help avoid some kinds of features like countersink/counterbore holes, edge chamfers, radiuses, etc that are more likely to be on the top face (if any). I am also using a List(Of Double) to record individual loop lengths for added information, in case it may be desired. It has a Sum property which adds up all numeric values within, so it's still useful there too. I included an InputListBox, just to show you the resulting list of individual loop lengths. If you don't want this, you can just delete that line. Also since a sheet metal part may not already have a flat pattern, I decided to check for this first, before just trying to use it. If it is not found, I let the user know, then ask them if they want the rule to automatically create it, so that the rule can progress and produce the user parameter. If they choose No, it exits the rule without creating it, and without creating/updating the user parameter.
Here's the updated iLogic rule:
If Not TypeOf ThisApplication.ActiveDocument Is PartDocument Then
MsgBox("A Part document must be 'active' for this rule to work. Exiting rule.",,"")
Exit Sub
End If
Dim oPDoc As PartDocument = ThisApplication.ActiveDocument
If Not TypeOf oPDoc.ComponentDefinition Is SheetMetalComponentDefinition Then
MsgBox("This Part is not a Sheet Metal Part. Exiting rule.",,"")
Exit Sub
End If
Dim oSMDef As SheetMetalComponentDefinition = oPDoc.ComponentDefinition
If Not oSMDef.HasFlatPattern Then
oAns = MsgBox("This Sheet Metal Part does not have a Flat Pattern." & vbCrLf & _
"This is needed to calculate 'LaserCutLength'." & vbCrLf & _
"Do you want this rule to automatically create the Flat Pattern, then continue?", vbYesNo + vbQuestion, "")
If oAns = vbNo Then Exit Sub
Try
oSMDef.Unfold
Catch oEx As Exception
MsgBox("Failed to 'Unfold' this Sheet Metal Part, to create a Flat Pattern." & vbCrLf & _
oEx.Message & vbCrLf & oEx.StackTrace, , "")
Exit Sub
End Try
End If
Dim oFP As FlatPattern = oSMDef.FlatPattern
Dim oLengths As New List(Of Double)
For Each oEL As EdgeLoop In oFP.BottomFace.EdgeLoops
oLengths.Add(ThisApplication.MeasureTools.GetLoopLength(oEL))
Next
a = InputListBox("", oLengths, 0.0, "Loop Lengths", "Individual Loop Lengths")
Dim oParam As UserParameter
Try
oParam = oSMDef.Parameters.UserParameters.Item("LaserCutLength")
oParam.Value = oLengths.Sum
Catch
oParam = oSMDef.Parameters.UserParameters.AddByValue("LaserCutLength", oLengths.Sum, "mm")
End Try
If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.
If you want and have time, I would appreciate your Vote(s) for My IDEAS 💡or you can Explore My CONTRIBUTIONS
Wesley Crihfield

(Not an Autodesk Employee)