- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Using iLogic, I want to be able to copy user parameters from part files into an assembly. I found an example that copies user parameters from an assembly into part files and tried modifying it to suit my needs and it runs without errors but does not copy over any of the user parameters. The code I have is below.
Dim asmDoc As Inventor.AssemblyDocument = ThisDoc.Document For Each refDoc As Inventor.Document In asmDoc.AllReferencedDocuments 'Look for part documents. If refDoc.DocumentType = Inventor.DocumentTypeEnum.kPartDocumentObject Then Dim partDoc As Inventor.PartDocument = refDoc Dim refDocUserParams As UserParameters = refDoc.ComponentDefinition.Parameters.UserParameters 'Add the part parameters to the assembly. For Each partUserParam As UserParameter In refDoc.ComponentDefinition.Parameters.UserParameters 'Check to see if the parameter already exists. Dim checkParam As UserParameter = Nothing Try checkParam = refDocUserParams.Item(partUserParam.Name) Catch ex As Exception checkParam = Nothing End Try If checkParam Is Nothing Then 'Create the missing parameter. refDocUserParams.AddByExpression(partUserParam.Name, partUserParam.Expression, partUserParam.Units) Else 'Update the value of the existing parameter. checkParam.Expression = partUserParam.Expression End If Next End If Next
Any help with what I am doing wrong is appreciated.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
change
Dim refDocUserParams As UserParameters = refDoc.ComponentDefinition.Parameters.UserParameters
into
Dim refDocUserParams As UserParameters = asmdoc.ComponentDefinition.Parameters.UserParameters
and move it above
For Each refDoc As Inventor.Document In asmDoc.AllReferencedDocuments
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I know get a 'Parameter is incorrect' error. It runs until the line highlighted below.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi @jishee
Adding by expression might not always work. The expression could contain a name of a parameter that doesn't exist in the assembly. Either a user parameter that hasn't been copied yet or a model parameter.
My suggestion is to try to use the expression but if it fails, use the parameter value instead:
Dim asmDoc As Inventor.AssemblyDocument = ThisDoc.Document For Each refDoc As Inventor.Document In asmDoc.AllReferencedDocuments Dim refDocUserParams As UserParameters = asmDoc.ComponentDefinition.Parameters.UserParameters 'Look for part documents. If refDoc.DocumentType = Inventor.DocumentTypeEnum.kPartDocumentObject Then Dim partDoc As Inventor.PartDocument = refDoc 'Add the part parameters to the assembly. For Each partUserParam As UserParameter In refDoc.ComponentDefinition.Parameters.UserParameters 'Check to see if the parameter already exists. Dim checkParam As UserParameter = Nothing Try checkParam = refDocUserParams.Item(partUserParam.Name) Catch checkParam = Nothing End Try If checkParam Is Nothing Then 'Create the missing parameter. Try refDocUserParams.AddByExpression(partUserParam.Name, partUserParam.Expression, partUserParam.Units) Catch refDocUserParams.AddByValue(partUserParam.Name, partUserParam.Value, partUserParam.Units) End Try Else 'Update the value of the existing parameter. Try checkParam.Expression = partUserParam.Expression Catch checkParam.Value = partUserParam.Value End Try End If Next End If Next
Jhoel Forshav
Download my free Inventor Addin - Hole Projector
LinkedIn | Ideas | Contributions | Blog posts | Website
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
@JhoelForshav have right that reason must be in missing parameter from expression.
If you really must add expression then you can start with this code if you want to determine wchich parameter missing:
Dim asmDoc As Inventor.AssemblyDocument = ThisDoc.Document
Dim refDocUserParams As UserParameters = asmDoc.ComponentDefinition.Parameters.UserParameters
For Each refDoc As Inventor.Document In asmDoc.AllReferencedDocuments
'Look for part documents.
'If refDoc.DocumentType = Inventor.DocumentTypeEnum.kPartDocumentObject Then
Dim partDoc As Inventor.Document = refDoc
'Add the part parameters to the assembly.
For Each partUserParam As UserParameter In refDoc.ComponentDefinition.Parameters.UserParameters
'Check to see if the parameter already exists.
Dim checkParam As UserParameter = Nothing
Try
checkParam = refDocUserParams.Item(partUserParam.Name)
Catch
checkParam = Nothing
End Try
If checkParam Is Nothing Then
'Create the missing parameter.
Dim checkParamdependants As Parameter = Nothing
Dim pardependants As String = ""
For Each param As Parameter In refDoc.ComponentDefinition.Parameters
If param.Type <> 50348800 Then
If param.Dependents.count>0 Then
For Each paramdep As Parameter In param.Dependents
If paramdep.name = partUserParam.Name Then
Try
checkParamdependants = refDocUserParams.Item(param.Name)
Catch
' If pardependants.Contains(param.name)=False Then
pardependants = pardependants & param.name & ";"
' End If
End Try
End If
Next
End If
End If
Next
Try
If pardependants = "" Then
refDocUserParams.AddByExpression(partUserParam.Name, partUserParam.Expression, partUserParam.Units)
Else
MessageBox.Show("Error, in assembly dependant parameters missing: " & pardependants)
End If
Catch
MessageBox.Show ("Error, in assembly dependant parameters missing: " & pardependants )
End Try
Else
'Update the value of the existing parameter.
Try
checkParam.Expression = partUserParam.Expression
Catch
checkParam.Value = partUserParam.Value
End Try
End If
Next
' End If
Next
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
good afternoon @JhoelForshav
Can you tell me which iLogic can transfer all user parameters from the assembly to all the part in this assembly?
in advance
thank you so much
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi @robertast
The type of parameter copy that's done in this thread, but from assembly to all its parts would look something like this:
iLogicVb.UpdateWhenDone = True Dim oAsm As AssemblyDocument = ThisDoc.Document For Each oDoc As Document In oAsm.AllReferencedDocuments If oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject AndAlso oAsm.ComponentDefinition.Occurrences.AllReferencedOccurrences(oDoc).Count > 0 Dim oDef As PartComponentDefinition = oDoc.ComponentDefinition For Each oParam As Inventor.Parameter In oAsm.ComponentDefinition.Parameters.UserParameters Dim paramInPart As Inventor.Parameter Try paramInPart = oDef.Parameters.UserParameters.Item(oParam.Name) Try paramInPart.Expression = oParam.Expression Catch paramInPart.Value = oParam.Value End Try Catch Try oDef.Parameters.UserParameters.AddByExpression(oParam.Name, oParam.Expression, oParam.Units) Catch oDef.Parameters.UserParameters.AddByValue(oParam.Name, oParam.Expression, oParam.Units) End Try End Try Next End If Next
Jhoel Forshav
Download my free Inventor Addin - Hole Projector
LinkedIn | Ideas | Contributions | Blog posts | Website
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
But when it’s good, it wants more.
But is this most likely impossible? ![]()
If SUBASSEMBLY appears in ASSEMBLY, it passes parameters to all parts, including SUBASSEMBLY parts.
And he wants the ASSEMBLY to switch over to its parts, and not to trade SUBASSEMBLY. SUBASSEMBLY on their part.
Is it possible? If yes - the wine is from me...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Thats possible for sure. Only problem will be if the same part exists in multiple subassemblies...
I’ll probably not get home in time to write this code tonight. But if no one beats me to it i’ll fix it tomorrow![]()
Jhoel Forshav
Download my free Inventor Addin - Hole Projector
LinkedIn | Ideas | Contributions | Blog posts | Website
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Here you go ![]()
Sub Main Dim asm As AssemblyDocument = ThisDoc.Document copyParams(asm) iLogicVb.UpdateWhenDone = True End Sub Sub copyParams(oAsm As AssemblyDocument) For Each oDoc As Document In oAsm.ReferencedDocuments If oAsm.ComponentDefinition.Occurrences.AllReferencedOccurrences(oDoc).Count > 0 If oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Dim oDef As PartComponentDefinition = oDoc.ComponentDefinition For Each oParam As Inventor.Parameter In oAsm.ComponentDefinition.Parameters.UserParameters Dim paramInPart As Inventor.Parameter Try paramInPart = oDef.Parameters.UserParameters.Item(oParam.Name) Try paramInPart.Expression = oParam.Expression Catch paramInPart.Value = oParam.Value End Try Catch Try oDef.Parameters.UserParameters.AddByExpression(oParam.Name, oParam.Expression, oParam.Units) Catch oDef.Parameters.UserParameters.AddByValue(oParam.Name, oParam.Expression, oParam.Units) End Try End Try Next ElseIf oDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject 'if the referenced document is an assembly 'the sub will call itself with that document as the argument copyParams(oDoc) End If End If Next End Sub
Jhoel Forshav
Download my free Inventor Addin - Hole Projector
LinkedIn | Ideas | Contributions | Blog posts | Website
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Just come across your reply here, If you only wanted certain parameters from certain parts in the assembly how would you do that please.
Eg, say we have "Parameter1" in the part "PartA" and I would like to copy this "Parameter1" to the assembly that contains "PartA"
Cheers
Donald
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I am having issues with this code, im getting this error:
The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
any idea what this could be?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I'm wondering is there a chance that instead of all the occurrences parameters would be only added from selected one ?