Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Sweep length rule fails when creating/modifying the part within an assembly

lhintonC3ZNZ
Explorer

Sweep length rule fails when creating/modifying the part within an assembly

lhintonC3ZNZ
Explorer
Explorer

Hi all,  

I'n new to Ilogic but seeing the possibilities through it. 

I have a rule (blatantly borrowed off the internet) for calculating the length of a sweep.  I've created a template with the code imbedded with it. 

When I create the part from the part menu (File, new, part), the rule runs as expected, however when I create the part through within an assembly(open assembly, then "file, create in-place component"), when the rule runs it gives me an error "

 

lhintonC3ZNZ_0-1662738502370.png

 

It's very frustrating as the rule runs as it should otherwise, it's only when creating the part within an assembly! Any ideas?  I'd appreciate any help.  Thanks

 

The ilogic is below, however I'm not convinced its the code itself as it runs perfectly as a standalone part.

 

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*1.2

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

 

0 Likes
Reply
Accepted solutions (1)
220 Views
2 Replies
Replies (2)

WCrihfield
Mentor
Mentor
Accepted solution

Hi @lhintonC3ZNZ.  For starters, change this:

Dim oDoc As PartDocument
oDoc = ThisApplication.ActiveDocument

...to this:

Dim oDoc As PartDocument = ThisDoc.Document

I believe what is going on in this situation is the code is targeting the main assembly, because it is the 'active' document in that situation, instead of the part you area creating.  This change will force the code to target the local document (the document that the rule is saved within.).

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

lhintonC3ZNZ
Explorer
Explorer

You're a genius!  That makes perfect sense and it worked.  I've spent the last couple of weeks on and off trying to find the issue.  I'm really grateful, thank you.

0 Likes