Found a pretty slick rule for returning the length of a sweep which I'm using to me collect lengths for hoses and tubes however I'm having trouble with handling an error in a try-catch block and was wondering what the right way to handle the error where a sweep is not named Hose Sweep or Tube Sweep as right now it errors out if either of those names aren't found. Secondly the parameter Sweeplength that it creates, I can't get it to round to 2 decimal places?
'Set a reference to the active part document
Dim oDoc As PartDocument
oDoc = ThisApplication.ActiveDocument
Dim oDef As PartComponentDefinition
oDef = oDoc.ComponentDefinition
Dim opath As Path
Try :
opath = oDef.Features.SweepFeatures.Item("Hose Sweep").Path
Catch :
opath = oDef.Features.SweepFeatures.Item("Tube Sweep").Path
Catch :
MsgBox("To get the length of your hose or tube the sweep feature needs to be named explicitly as Hose Sweep or Tube Sweep")
Exit Sub
End Try
Dim TotalLength As Double
Dim roundLength 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
roundLength = Round(TotalLength, 2)
Dim oparams As Parameters
Dim oparam As Parameter
oparams = oDoc.ComponentDefinition.Parameters
Dim exists As Boolean
exists = False
'Find out if parameter exists
For Each oparam In oparams
If oparam.Name = "Sweeplength" Then exists = True
Next oparam
'Change the value if the parameter exists otherwise add the parameter
'MsgBox(roundLength & " " & TotalLength)
If exists Then
oparams.Item("Sweeplength").Value = roundLength
Else
oparams.UserParameters.AddByValue("Sweeplength", roundLength, 11266)
End If
oDoc.Update
Hi @e_frissell . For reliability, I suggest placing the Try-Catch statement in the middle of the Try-Catch. Regarding 2x values after the comma. You use the AddByValue method to create the parameter, which means your value will automatically be multiplied by 10, so you need to round up to 3 decimal places.
'Set a reference to the active part document
Dim oDoc As PartDocument = ThisApplication.ActiveDocument
Dim oDef As PartComponentDefinition = oDoc.ComponentDefinition
Dim opath As Path
Try
Try : opath = oDef.Features.SweepFeatures.Item("Hose Sweep").Path
Catch : opath = oDef.Features.SweepFeatures.Item("Tube Sweep").Path
End Try
Catch
MsgBox("To get the length of your hose or tube the sweep feature needs to be named explicitly as Hose Sweep or Tube Sweep")
Exit Sub
End Try
Dim TotalLength, roundLength As Double
TotalLength = 0
Dim oCurve As Object
For i As Integer = 1 To opath.Count
oCurve = opath.Item(i).Curve
Dim oCurveEval As CurveEvaluator = oCurve.Evaluator
Dim MinParam, MaxParam, length As Double
Call oCurveEval.GetParamExtents(MinParam, MaxParam)
Call oCurveEval.GetLengthAtParam(MinParam, MaxParam, length)
TotalLength = TotalLength + length
Next i
Dim oparams As Parameters = oDoc.ComponentDefinition.Parameters
Try
oparams("Sweeplength").Value = Round(TotalLength, 3)
Catch
oparams.UserParameters.AddByValue("Sweeplength", Round(TotalLength, 3), 11266)
End Try
oDoc.Update
Andrii Humeniuk - Leading design engineer
LinkedIn | My free Inventor Addin | My Repositories
Did you find this reply helpful ? If so please use the Accept as Solution/Like.
Hi @e_frissell. You could just loop through all SweepFeatures, instead of looking for one by a specific name. If there will always be some portion of one of those names in the feature's name, then while looping through them, we could check if their name 'contains' specific pieces of text.
As for the parameter...we do not have control over how many decimal places a parameter shows within the Parameters dialog box. It is a bit unpredictable. We can certainly attempt to round values, but that may not eliminate trailing zeros. You would have to represent the value as a String, then apply a format to the String to get the results you want. Similar to how we can set parameters to export, then set the export properties & formatting.
Below is a very similar example iLogic rule for pretty much the same task, but it does not have the UserParameter creation part in it, because it may encounter multiple results, instead of just one. While looping through multiple possible SweepFeatures, it collects the data into a Dictionary(Of String, Double), where the String is the name of the SweepFeature, and the Double is the rounded total length of its path curves. It also writes each result to the iLogic Logger window.
Sub Main
Dim oPDoc As PartDocument = TryCast(ThisDoc.Document, Inventor.PartDocument)
If oPDoc Is Nothing Then Return
Dim oSweepFeats As SweepFeatures = oPDoc.ComponentDefinition.Features.SweepFeatures
If oSweepFeats.Count = 0 Then Return
Dim oResults As New Dictionary(Of String, Double)
For Each oSweepFeat As SweepFeature In oSweepFeats
Dim dTotalLength, dRoundLength As Double
Dim oPath As Inventor.Path = oSweepFeat.Definition.Path
For Each oPathEntity As PathEntity In oPath
Dim oCurve As Object = oPathEntity.Curve
Dim oCEval As CurveEvaluator = Nothing
Try : oCEval = oCurve.Evaluator : Catch : End Try
If oCEval Is Nothing Then Continue For
Dim dMinParam, dMaxParam, dLength As Double
oCEval.GetParamExtents(dMinParam, dMaxParam)
oCEval.GetLengthAtParam(dMinParam, dMaxParam, dLength)
dTotalLength = dTotalLength + dLength
Next oPathEntity
dRoundLength = Round(dTotalLength, 2)
oResults.Add(oSweepFeat.Name, dRoundLength)
Logger.Info(vbCrLf & oSweepFeat.Name & " sweep length = " & dRoundLength.ToString)
Next oSweepFeat
'now use the data within the oResults collection as you would like
End Sub
If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.
Wesley Crihfield
(Not an Autodesk Employee)
Here's another approach
'Set a reference to the active part document Dim oDoc As PartDocument oDoc = ThisApplication.ActiveDocument Dim oDef As PartComponentDefinition oDef = oDoc.ComponentDefinition Dim oList As New List(Of String) oList.Add("") 'zero in the list oList.Add("Hose Sweep") 'one in the list oList.Add("Tube Sweep") 'two in the list For i = 1 To 2 Try : oSweep = oDef.SweepFeatures.Item(oList.Item(i)) Exit For Catch If i = 2 And oSweep Is Nothing Then MsgBox("Sweep not found") Exit Sub End If End Try Next MsgBox(oSweep.name)
Can't find what you're looking for? Ask the community or share your knowledge.