I had some parts built up by multiple sweep-features. And i needed the length for these parts.
I found this Being Inventive post from 2011. That rule did most of the work for me. But it is hardcoded for 1 sweep with a specific name.
I tried to make it universal and it looks like I succeeded. So use it, steal it, i don't care. But if you improve upon it or, please let me know.
Disclaimer: I'm not much of an iLogic programmer, I much prefer VBA. Debugging in iLogic sucks.
Sub main
'Set a reference to the active part document
Dim oDoc As PartDocument
oDoc = ThisApplication.ActiveDocument
Dim oDef As PartComponentDefinition
oDef = oDoc.ComponentDefinition
Dim LengthTotal As Double
LengthTotal = 0
'add length of all sweeps
Dim oSweep As SweepFeature
For Each oSweep In oDef.Features.SweepFeatures
Dim oPath As Path
oPath = oSweep.path
LengthTotal = LengthTotal + GetLengthOfSweep(oPath)
Next oSweep
Dim oparams As Parameters
Dim oparam As Parameter
oparams = oDoc.ComponentDefinition.Parameters
Dim exists As Boolean
exists = False
'Find out if parameter exists
Dim paraName As String
paraName = "Length"
For Each oparam In oparams
If oparam.Name = paraName Then exists = True
Next oparam
'Change the value if the parameter exists otherwise add the parameter
If exists Then
oparams.Item(paraName).Value = LengthTotal
Else
oparams.UserParameters.AddByValue(paraName, LengthTotal, 11266)
End If
Call ParaExport(oDef.Parameters, paraName, kZeroDecimalPlacePrecision)
oDoc.Update
End Sub
'get length of a sweep
Private Function GetLengthOfSweep(ByVal oPath As Path) As Double
Dim TotalLength As Double
TotalLength = 0
Dim oCurve As Object
Dim i As Integer
For i = 1 To oPath.Count
oCurve = oPath.Item(i).Curve
Dim oCurveEval As CurveEvaluator
oCurveEval = oCurve.Evaluator
Dim MinParam As Double
Dim MaxParam As Double
Dim length As Double
Call oCurveEval.GetParamExtents(MinParam, MaxParam)
Call oCurveEval.GetLengthAtParam(MinParam, MaxParam, length)
TotalLength = TotalLength + length
Next i
Return TotalLength
End Function
'exporting the parameter, optional
Private Sub ParaExport(ByRef oParas As Parameters, _
ByVal oParaName As String, _
ByVal oDecimalPrecision As CustomPropertyPrecisionEnum)
Dim oPara As Parameter
For Each oPara In oParas
If String.Compare(oPara.name, oParaName) = 0 Then
If Not oPara.ExposedAsProperty = True Then oPara.ExposedAsProperty = True
If Not oPara.CustomPropertyFormat.ShowUnitsString = False Then _
oPara.CustomPropertyFormat.ShowUnitsString = False
If Not oPara.CustomPropertyFormat.ShowLeadingZeros = True Then _
oPara.CustomPropertyFormat.ShowLeadingZeros = True
If Not oPara.CustomPropertyFormat.ShowTrailingZeros = False Then _
oPara.CustomPropertyFormat.ShowTrailingZeros = False
If Not oPara.CustomPropertyFormat.Precision = oDecimalPrecision Then _
oPara.CustomPropertyFormat.Precision = oDecimalPrecision
Dim oComment As String
oComment = "Is determined by by iLogic-rule: 'SweepLength'"
If Not oPara.Comment = oComment Then oPara.Comment = oComment
Exit Sub
End If
Next oPara
End Sub
Solved! Go to Solution.
Solved by mr_ensing. Go to Solution.
Solved by mr_ensing. Go to Solution.
Please provide a non-confidential part file to investigate.
Thanks and regards,
It's not that i have a problem. It's just that i'm curious if there is something to improve.
But still, a model is attached.
I don't know if this helps or anything, but here are a couple other similar, but more condensed variations of your code.
This version maintains all the separate Subs & Functions, in case you like that sort of thing.
Sub main
Dim oDoc As PartDocument = ThisApplication.ActiveDocument
Dim oDef As PartComponentDefinition = oDoc.ComponentDefinition
Dim LengthTotal As Double = 0
Dim oPath As Path
For Each oSweep As SweepFeature In oDef.Features.SweepFeatures
oPath = oSweep.Definition.Path
LengthTotal = LengthTotal + GetLengthOfSweep(oPath)
Next
Dim oUParams As UserParameters = oDoc.ComponentDefinition.Parameters.UserParameters
Dim oExists As Boolean = False
Dim oParamName As String = "Length"
For Each oUParam As UserParameter In oUParams
If oUParam.Name = oParamName Then
oUParam.Value = LengthTotal
oExists = True
End If
Next
If oExists = False Then
oUParams.AddByValue(oParamName, LengthTotal, UnitsTypeEnum.kDefaultDisplayLengthUnits)
End If
ParaExport(oDef.Parameters.UserParameters, oParamName, kZeroDecimalPlacePrecision)
oDoc.Update
End Sub
Private Function GetLengthOfSweep(ByVal oPath As Path) As Double
Dim TotalLength As Double = 0
Dim oCurve As Object
Dim oCurveEval As CurveEvaluator
Dim MinParam As Double
Dim MaxParam As Double
Dim length As Double
For i As Integer = 1 To oPath.Count
oCurve = oPath.Item(i).Curve
oCurveEval = oCurve.Evaluator
oCurveEval.GetParamExtents(MinParam, MaxParam)
oCurveEval.GetLengthAtParam(MinParam, MaxParam, length)
TotalLength = TotalLength + length
Next
Return TotalLength
End Function
Private Sub ParaExport(ByRef oUParas As UserParameters, ByVal oParaName As String, _
ByVal oDecimalPrecision As CustomPropertyPrecisionEnum)
For Each oUPara As UserParameter In oUParas
If oUPara.Name = oParaName Then
oUPara.ExposedAsProperty = True
Dim oCPF As CustomPropertyFormat = oUPara.CustomPropertyFormat
oCPF.PropertyType = CustomPropertyTypeEnum.kNumberPropertyType
oCPF.Precision = oDecimalPrecision
oUPara.Comment = "Is determined by by iLogic-rule: 'SweepLength'"
Exit Sub
End If
Next
End Sub
And here is the even further condensed version, that doesn't bother using Subs & Functions to separate everything out:
Dim oDoc As PartDocument = ThisApplication.ActiveDocument
Dim oDef As PartComponentDefinition = oDoc.ComponentDefinition
Dim oTotalLength As Double = 0
Dim oPath As Path
Dim oCurve As Object
Dim oCurveEval As CurveEvaluator
Dim MinParam As Double
Dim MaxParam As Double
Dim oLength As Double
For Each oSweep As SweepFeature In oDef.Features.SweepFeatures
oPath = oSweep.Definition.Path
For i As Integer = 1 To oPath.Count
oCurve = oPath.Item(i).Curve
oCurveEval = oCurve.Evaluator
oCurveEval.GetParamExtents(MinParam, MaxParam)
oCurveEval.GetLengthAtParam(MinParam, MaxParam, oLength)
oTotalLength = oTotalLength + oLength
Next
Next
Dim oUParams As UserParameters = oDoc.ComponentDefinition.Parameters.UserParameters
Dim oUParam As UserParameter
Dim oExists As Boolean = False
Dim oParamName As String = "Length"
For Each oUParam In oUParams
If oUParam.Name = oParamName Then
oUParam.Value = oTotalLength
oExists = True
End If
Next
If oExists = False Then
oUParam = oUParams.AddByValue(oParamName, oTotalLength, UnitsTypeEnum.kDefaultDisplayLengthUnits)
End If
oUParam.ExposedAsProperty = True
Dim oCPF As CustomPropertyFormat = oUParam.CustomPropertyFormat
oCPF.PropertyType = CustomPropertyTypeEnum.kNumberPropertyType
oCPF.Precision = CustomPropertyPrecisionEnum.kZeroDecimalPlacePrecision
oUParam.Comment = "Is determined by by iLogic-rule: 'SweepLength'"
RuleParametersOutput()
InventorVb.DocumentUpdate()
I hope this helps.
If this solves your problem, or answers your questions, please click 'Accept As Solution".
Or, if this helps you reach your goal, please click 'LIKES" 👍.
Also, if you're interested, here are a few of the 'Ideas' I'd like to get implemented.
If you agree with any of them, please vote for them.
Inventor 2020 Help | Inventor Forum | Inventor Customization Forum | Inventor Ideas Forum
Wesley Crihfield
As i wrote before, usually i'm writing VBA code, in wich declaring and assigning at once is not possible. That made the code a lot compacter.
I dropped ParaExport function for this use case. But i added the GetPara function and got rid of the Boolean (oExists).
Plus small stuff like this:
oPath = oSweep.Definition.Path
LengthTotal = LengthTotal + GetLengthOfSweep(oPath)
'changed to:
LengthTotal = LengthTotal + GetLengthOfSweep(oSweep.Definition.Path)
My current code:
Sub Main
Dim oDoc As PartDocument = ThisApplication.ActiveDocument
Dim oDef As PartComponentDefinition = oDoc.ComponentDefinition
Dim LengthTotal As Double = 0
'add length of all sweeps
Dim oSweep As SweepFeature
For Each oSweep In oDef.Features.SweepFeatures
LengthTotal = LengthTotal + GetLengthOfSweep(oSweep.path)
Next oSweep
'get(or create) parameter
Dim oPara As Parameter = GetPara(oDoc.ComponentDefinition.Parameters, "SweepLength")
'assign value and comment
oPara.Value = LengthTotal
Dim oComment As String = "Determined by iLogic-rule: 'SweepLength'"
If Not oPara.Comment = oComment Then oPara.Comment = oComment
oDoc.Update
End Sub
'get length of a sweep
Private Function GetLengthOfSweep(ByVal oPath As Path) As Double
Dim oCurve As Object
Dim oCurveEval As CurveEvaluator
Dim MinParam As Double
Dim MaxParam As Double
Dim length As Double
Dim TotalLength As Double = 0
For i As Integer = 1 To oPath.Count
oCurve = oPath.Item(i).Curve
oCurveEval = oCurve.Evaluator
Call oCurveEval.GetParamExtents(MinParam, MaxParam)
Call oCurveEval.GetLengthAtParam(MinParam, MaxParam, length)
TotalLength = TotalLength + length
Next i
Return TotalLength
End Function
Private Function GetPara(ByRef oParas As Parameters, ByVal paraName As String) As Parameter
Dim oPara As Parameter
For Each oPara In oParas
If oPara.Name = paraName Then Return oPara
Next oPara
Return oParas.UserParameters.AddByValue(paraName, 0, 11266)
End Function
I changed one more thing:
'from
Dim oDoc As PartDocument = ThisApplication.ActiveDocument
'to:
Dim oDoc As PartDocument = ThisDoc.Document
I would like to display the above iLogic results to one decimal place.
I would like to add a unit to the end of the result.