Could I ask for your help with some fine tuning?
I hacked together the code below with help from all of you on the forums - thanks!
It takes a list of Parameters and cycles through the assembly looking for parts with the same parameter names. It then copies the value of the parameter from the assembly down to the part.
It occured to be that this would be more efficient if I didn't have to manually create a list of parameters to compare.
What I'd like to do is create a list of all parameters in the assembly file that begin with the prefix 'A_', and compare this list against the parameter list held in each subassmbly and part file in the assembly, copying the values down as before.
Could anyone offer me any advice on how to build the parameter list?
Cheers,
Paul.
' set a reference to the assembly component definintion. ' This assumes an assembly document is open. Dim oAsmCompDef As AssemblyComponentDefinition oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition 'Iterate through all of the occurrences Dim oOccurrence As ComponentOccurrence For Each oOccurrence In oAsmCompDef.Occurrences 'check for and skip virtual components '(in case a virtual component trips things up) If Not TypeOf oOccurrence.Definition Is VirtualComponentDefinition Then Parameter.Quiet = True 'Match up the slab height If Parameter(oOccurrence.Name, "A_CeilingSlab") Then Parameter(oOccurrence.Name, "A_CeilingSlab") = A_CeilingSlab End If 'Match up the coffered ceiling height If Parameter(oOccurrence.Name, "A_CeilingCoffer") Then Parameter(oOccurrence.Name, "A_CeilingCoffer") = A_CeilingCoffer End If 'match up the finished ceiling height If Parameter(oOccurrence.Name, "A_CeilingFinished") Then Parameter(oOccurrence.Name, "A_CeilingFinished") = A_CeilingFinished End If 'Match up the coving height If Parameter(oOccurrence.Name, "A_Coving") Then Parameter(oOccurrence.Name, "A_Coving") = A_Coving End If 'Match up the picture rail height If Parameter(oOccurrence.Name, "A_PictureRail") Then Parameter(oOccurrence.Name, "A_PictureRail") = A_PictureRail End If 'Match up the dado height If Parameter(oOccurrence.Name, "A_DadoHeight") Then Parameter(oOccurrence.Name, "A_DadoHeight") = A_DadoHeight End If 'Match up the datum height If Parameter(oOccurrence.Name, "A_Datum") Then Parameter(oOccurrence.Name, "A_Datum") = A_Datum End If 'Match up the skirting height If Parameter(oOccurrence.Name, "A_SkirtingHeight") Then Parameter(oOccurrence.Name, "A_SkirtingHeight") = A_SkirtingHeight End If 'Match up the finished floor level If Parameter(oOccurrence.Name, "A_FloorFinished") Then Parameter(oOccurrence.Name, "A_FloorFinished") = A_FloorFinished End If 'Match up the slab floor level If Parameter(oOccurrence.Name, "A_FloorSlab") Then Parameter(oOccurrence.Name, "A_FloorSlab") = A_FloorSlab End If 'Match up the slab floor level If Parameter(oOccurrence.Name, "A_RoomLength") Then Parameter(oOccurrence.Name, "A_RoomLength") = A_RoomLength End If 'Match up the slab floor level If Parameter(oOccurrence.Name, "A_RoomWidth") Then Parameter(oOccurrence.Name, "A_RoomWidth") = A_RoomWidth End If Else End If Next 'Uodate the document to see the changes InventorVb.DocumentUpdate()
Autodesk Industry Marketing Manager UK D&M
Opinions are my own and may not reflect those of my company.
Linkedin | Twitter | Instagram | Facebook | Pinterest
Solved! Go to Solution.
Solved by Vladimir.Ananyev. Go to Solution.
Cycling through the assembly parameters collection you may populate y
our own string collection with the names of those parameters,
which names begin with the prefix “A_”.
Dim oAsmDoc As Inventor.AssemblyDocument = ThisDoc.Document Dim oPars As Inventor.Parameters = oAsmDoc.ComponentDefinition.Parameters Dim oList As Collection = New Collection For Each oPar As Inventor.Parameter In oPars If (Left(oPar.Name, 2) = "A_") Then oList.Add(oPar.Name) End If Next
If oList.Count = 0 Then MsgBox("Empty list :(") Else 'print oList content for debug purposes Dim sTotal As String = "Found: " & oList.Count For Each st As String In oList sTotal += vbNewLine & st Next MsgBox(sTotal) 'now we can do something useful with names in the oList For Each oOcc as ComponentOccurrence In oAsmDoc.ComponentDefinition.occurrences For Each ParName As String In oList Try Parameter(oOcc.Name, ParName) = Parameter(ParName) Catch End Try Next Next End If
Cheers,
Hi Vladimir,
Thanks very much for your quick response - this is excellent work, just what I needed!
I'm sorry I didn't get back to you sooner. I've been away at Autodesk University and I've only just got around to testing this code.
I have only one question for you. My origional code runs any time a parameter changes. The code you have supplied me with won't run automatically, even if I set up an iLogic trigger.
Can you think of anything I can do to to get this rull to run anythime a parmeter is changed?
Cheers,
Paul
If your parameters are model parameters then event trigger “Any Model Parameter Change” should run your rule automatically
In case you have a mix of model and user parameters you may use a trick with the dummy variable that depends on the sum of all your model and user parameters (they are blue). In the snippet below variable indicator works as a rule trigger.
indicator = AAA+BBB
'the first line initiate the rule execution
MsgBox("Parameter changed" )
The first line in this rule initiates the execution if at least one of the “blue” user parameters was changed.
This works fine with fixed parameter lists.
cheers,
Thanks Vladimir, It's good to know that Model parameters and Named parameters behave differntly. Do you know why this is?
Regaring your 'Indicator' snippet - I'm not a programmer, how would I impliment this?
Do I have to manually type in a list of all my Named parameters in order to see them highlight in blue? - This is what I was trying to avoid by making a list of parameters, rather than operating on the named parameters themesleves!
If your rule depends on the model parameters only then no problem – use event “Any Model Parameter Change” and everything will be OK.
If you use user parameters as well you may make them exported and try to use event “iProperty Change”.
cheers,
IMHO It looks like the simplest approach for you is fire your main rule before save document.
Alternative method is a little bit tricky. Look at the attached part file.
This part contains 2 rules – MAIN_RULE and Generator.
MAIN_RULE should be initiated by the changes of the subset of model and user parameters.
“Generator” is the auxiliary rule that creates on-the-fly the third iLogic rule named in this sample “MyTrigger”. In this sample “Generator” creates the trigger using all key parameters.
Typical generated code is shown below.
Rule “MyTrigger” consists of the only two executable lines of code that initiates the execution of the MAIN_RULE if at least one of the “blue” parameters changes. The type of these parameters (model or user-defined) does not matter. The only thing is really important – all they are key parameters.
To test this approach open the attached part model and run “Generator” rule. Then open iLogic form and play with the parameter‘s values.
Function “GetParametersList” is responsible for the list of selected parameters. Feel free to modify it to suit your needs.
Hope this helps.
Great stuff Vladimir - thanks for your help 🙂
This works perfectly in the IPT file you sent over. Unfortunately, It doesn't work when I put it into an assembly file. This is the error below - can you help me work out what that means?
Does the method for getting the parameters collection need to be different when working within an assembly?
This approach should work in parts and in assemblies. No difference.
But I found out that my previous version fails if these is no key parameters in the active document.
I slightly changed my code to detect this situation.
Sample assembly document is attached.
Here is the code of "Generator" rule:
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ' === Generator rule === '========================================== 'This rule Main() creates new trigger rule that is initiated by the 'all desired parameters changes (model or user) 'It is convenient to fire this rule by the event BeforeSave '========================================== Sub Main() 'name of trigger rule Dim TriggerRuleName As String = "MyTrigger" 'name of the rule that should be initiated by the trigger rule Dim FiredRuleName As String = "MAIN_RULE" 'reference to iLogic Automation object Dim iLogicAuto As Object = iLogicVb.Automation 'current document Dim oDoc As Inventor.Document = ThisDoc.Document ' Create the trigger rule in auto mode Dim oRule As Object = iLogicAuto.GetRule(oDoc, TriggerRuleName) Dim sText As String = "'create the rule on-the-fly" 'if the rule TriggerRuleName is absent then create it If oRule Is Nothing Then oRule = iLogicAuto.AddRule(oDoc, TriggerRuleName, sText) End If sText = CreateRuleText(oDoc, FiredRuleName) If sText Is Nothing Then MessageBox.Show("Active document has not key parameters", _ "Generator", MessageBoxButtons.OK, MessageBoxIcon.Error) Else oRule.Text = sText oDoc.Save Call iLogicAuto.RunRule(oDoc, TriggerRuleName) End If End Sub 'Main Function CreateRuleText( _ ByVal oDoc As Inventor.Document, _ ByVal FiredRuleName As String) As String 'get the list of key parameters Dim oColl As Collection = GetParametersList(oDoc) If oColl.Count = 0 Then Return Nothing End If ''DummyVar = Cross _ '' & vbNewLine & CrossThickness _ '' & vbNewLine & material _ '' & vbNewLine & Ready _ '' & vbNewLine & InitialLength Dim sText As String sText = "'Automatically generated trigger rule" & vbNewLine sText = sText & "DummyVar = " If oColl.Count = 1 Then sText = sText & oColl.Item(1).tostring Else For i As Integer = 1 To oColl.Count Dim St As String = oColl.Item(i).tostring If i = 1 Then sText = sText & St & " _" ElseIf i < oColl.Count Then sText = sText & vbNewLine & " & vbNewLine & " & St & " _" Else sText = sText & vbNewLine & " & vbNewLine & " & St End If Next End If Return sText & vbNewLine & "iLogicVb.RunRule(""" & FiredRuleName & """)" End Function 'CreateRuleText Function GetParametersList(ByRef oDoc As Inventor.Document) As Collection 'creates the list of parameters that must trigger the rule 'Criteria is up to you. 'In this sample all key parameters are included in the list Dim oColl As New Collection Dim oPar As Inventor.Parameter For Each oPar In oDoc.ComponentDefinition.Parameters If oPar.IsKey Then oColl.Add (oPar.Name) Next Return oColl End Function 'GetParametersList
Generated trigger rule has the following syntax:
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ' === MyTrigger === 'Automatically generated trigger rule DummyVar = InputLength _ & vbNewLine & Ready _ & vbNewLine & Color iLogicVb.RunRule("MAIN_RULE")
In my sample assembly document all these three parameters are user-defined KEY parameters.
Any changes in any of them will fire the execution of the rule "MAIN_RULE".
Hope this helps.
cheers,