Assembly Parameters in subparts with iLogic Rule

Assembly Parameters in subparts with iLogic Rule

Anonymous
Not applicable
1,357 Views
6 Replies
Message 1 of 7

Assembly Parameters in subparts with iLogic Rule

Anonymous
Not applicable

Hello,

 

I found how to create a new parameter in a subpart of the assembly:

https://forums.autodesk.com/t5/inventor-forum/create-parameters-in-assembly-with-ilogic-rule/m-p/687...

 

but I would like to create all the user parameters in a subpart that already exist in the assembly, not create new parameters.

Property Type it is important?

I try to modify Curtis_Waguespack code (Link above) and here:

https://forums.autodesk.com/t5/inventor-forum/create-user-parameter-values-with-ilogic/m-p/3677786/h...

 

Unfortunately my codes do not work properly. Too difficult for me.

Any help is greatly appreciated.

 

 

0 Likes
Accepted solutions (2)
1,358 Views
6 Replies
Replies (6)
Message 2 of 7

JaneFan
Autodesk
Autodesk

Here is the code snippet to create all user parameters in top assembly to sub part or sub assembly. I added judgement in the code to avoid creating duplicated ones that there will be dialog pops up to indicate you the parameter already exists. Please remove the filter if you don't want to avoid such case.

 

Sub Main
Dim oValue As Double
dValue = 15 'value to push to each component
Dim asmDoc As AssemblyDocument
asmDoc = ThisApplication.ActiveDocument

Dim oParams As UserParameters
oParams = asmDoc.ComponentDefinition.Parameters.UserParameters

Dim oParam As UserParameter

' Iterate through all of the referenced documents
Dim doc As Document
For i = 1 To oParams.Count
oParam = oParams.Item(i)
For Each doc In asmDoc.AllReferencedDocuments
'update the components
Call UpdateParameter(doc, oParam.Name,oParam.Value)
Next
Next
End Sub

Sub UpdateParameter(oDoc As Document, strName As String, dValue As Double)
' Get the user parameters collection.
Dim userParams As UserParameters
userParams = _
oDoc.ComponentDefinition.Parameters.UserParameters

Dim param As Parameter
If Not ParamExists(strName,userParams) Then
param = userParams.AddByValue(strName,dValue,UnitsTypeEnum.kInchLengthUnits)
Else
MsgBox("This user parameter exists: " & strName)
End If

End Sub

Function ParamExists (strName As String, userParams As UserParameters) As Boolean
ParamExists = False
Dim oTemp As UserParameter
For Each oTemp In userParams
If oTemp.name = strName Then
ParamExists = True
Exit For
End If
Next
End Function




Jane Fan
Inventor/Fusion QA Engineer
Message 3 of 7

Anonymous
Not applicable

Thank you very much, the code is great.

I have a few more questions:

Rule create parameters with one unit (inch). It is possible to create parameters in different units like in assemble?

Rule work when type of parameter is numeric. I would like to add text and true/false parameters. It is possible?

 

Thank you for help

 

 

0 Likes
Message 4 of 7

JaneFan
Autodesk
Autodesk

Hello,

 

The following code snippet should work well for all Number, Text and Boolean User parameters:

Sub Main
Dim asmDoc As AssemblyDocument
asmDoc = ThisApplication.ActiveDocument

Dim oParams As UserParameters
oParams = asmDoc.ComponentDefinition.Parameters.UserParameters

Dim oParam As UserParameter
' Iterate through all of the referenced documents
Dim doc As Document
For i = 1 To oParams.Count
oParam = oParams.Item(i)
For Each doc In asmDoc.AllReferencedDocuments
'update the components
Call UpdateParameter(doc, oParam.Name,oParam.Value,oParam.Units )
Next
Next
End Sub

 

Sub UpdateParameter(oDoc As Document, strName As String, dValue As Object, strUnits As String )
' Get the user parameters collection.
Dim userParams As UserParameters
userParams = _
oDoc.ComponentDefinition.Parameters.UserParameters
'Dim bTemp As Boolean
Dim param As Parameter
If Not ParamExists(strName,userParams) Then
Select Case strUnits
Case "in"
param = userParams.AddByValue(strName,dValue,UnitsTypeEnum.kInchLengthUnits)
Case "Text"
param = userParams.AddByValue(strName,dValue,UnitsTypeEnum.kTextUnits)
Case "Boolean"
' bTemp = dValue
param = userParams.AddByValue(strName,dValue ,UnitsTypeEnum.kBooleanUnits)
End Select
Else
MsgBox("This user parameter exists: " & strName)
End If
End Sub

 

Function ParamExists (strName As String, userParams As UserParameters) As Boolean
ParamExists = False
Dim oTemp As UserParameter
For Each oTemp In userParams
If oTemp.name = strName Then
ParamExists = True
Exit For
End If
Next
End Function




Jane Fan
Inventor/Fusion QA Engineer
Message 5 of 7

Owner2229
Advisor
Advisor
Accepted solution

Hey, I've done some minor tweaks (readability, simplification, error prevention) to your code and updated it to work with any units:

 

Sub Main
    Dim asmDoc As AssemblyDocument = ThisApplication.ActiveDocument
    Dim oParams As UserParameters = asmDoc.ComponentDefinition.Parameters.UserParameters
    ' Iterate through all of the referenced documents
    For Each oParam As UserParameter In oParams
        For Each oDoc As Document In asmDoc.AllReferencedDocuments
            ' Update the components
            Call UpdateParameter(oDoc, oParam) 'add ", True" to update parameters
        Next
    Next 
End Sub

Sub UpdateParameter(oDoc As Document, oParam As UserParameter, Optional bUpdate As Boolean = False)
    Dim strName As String = oParam.Name
    ' Get the user parameters collection
    Dim userParams As UserParameters = oDoc.ComponentDefinition.Parameters.UserParameters
    If ParamExists(strName, userParams) Then
        If bUpdate Then
Dim oTemp As UserParameter = userParams(strName)
oTemp.Units = oParam.Units
oTemp.Value = oParam.Value
Else
MsgBox("User parameter '" & strName & "' already exists in '" & oDoc.DisplayName & "'.")
End If Exit Sub End If
userParams.AddByValue(strName, oParam.Value, oParam.Units) End Sub Function ParamExists(strName As String, userParams As UserParameters) As Boolean Dim bResult As Boolean = False
Try Dim oTemp As UserParameter = userParams(strName) bResult = True Catch
End Try Return bResult End Function 

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 6 of 7

Owner2229
Advisor
Advisor
Accepted solution

And here's a little more intensive rework:

 

Dim bUpdateValues As Boolean = False ' True to update values
Dim bShowExisting As Boolean = False ' True to show existing parameters

Dim aDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oParams As UserParameters = aDoc.ComponentDefinition.Parameters.UserParameters
' Iterate through all of the referenced documents
For Each oParam As UserParameter In oParams
    ' Update the components
    For Each oDoc As Document In aDoc.AllReferencedDocuments
        ' Get the user parameters collection
        Dim userParams As UserParameters = oDoc.ComponentDefinition.Parameters.UserParameters
        Dim oTemp As UserParameter
        Try
            oTemp = userParams(oParam.Name)
        Catch
            userParams.AddByValue(oParam.Name, oParam.Value, oParam.Units)
            Exit Sub
        End Try
        If bUpdateValues Then
            oTemp.Units = oParam.Units
            oTemp.Value = oParam.Value
        End If
If bShowExisting Then MsgBox("User parameter '" & oParam.Name & "' already exists in '" & oDoc.DisplayName & "'.") End If Next Next

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 7 of 7

Anonymous
Not applicable

Thank you very much. Great work! This is what i need.

0 Likes