Hi @ocastro63PFF. Sorry, but there is no already existing, short or simple code solution that will instantly work exactly the way you want it to. Partially because of how custom your situation is. However, that does not mean that a solution could not eventually be created for this, with enough time and effort invested into it.
What you are trying to do is very likely possible to do by code. However, since it is a very custom situation, it will require a very custom solution. Since there is unfortunately no short or simple way to accomplish what you want to do by code, some significant investments of time and effort must be made to create a longer and more complex solution. A strategy or plan must be made to utilize either known code resources and/or create new code resources, in an efficient way, to accomplish this task. Then the actual code must be generated and tested.
Custom aspects of this task:
- Multiple specifically named parts that each need to be changed differently from each other.
- The specific names of the assembly parameters do not appear to be logically associated to the names of the specific parts they are supposed to be effecting, so that association between part names and parameter names must be specified within the code somehow, making the code dependent on all those specific, hardcoded names.
- You have a limited set of sheet metal rules / styles, where there may (or may not be) a rule / style for available for every possible thickness variation that you may specify on the assembly side. So, what should happen when one of the assembly parameters is set to a thickness that there is no sheet metal style available for within the part? Should it just not do anything, or should it try to create a new sheet metal style, or something else? And if it should create a new style, then how should all the other settings of the style (besides thickness) be set?
One thing to keep in mind is that, if the names of the parts involved, or the names of the parameters involved, ever change, then those names within the code will also need to be changed, or it will start throwing errors and not work anymore.
I did try to design the code below in a way to minimize the changes that would be needed to the code when the part names or parameter names need to be changed. You may need to change the values in Lines 17 through 19, or...add or remove lines of code in that area to be able to work with more or less part name & parameter name pairs. I also tried to include a lot of comments in this code, to help you read through it and understand what is going on, but those notes may need to be translated. As you can see, it is quite long and complex, so I can only hope that you will eventually be able to fully understand what all it is trying to do along the way. I also have not had the chance to test this code, so be cautious, and initially test it out on files that are not super important.
Sub Main
'make sure this rule only tries to work with an assembly (not a part, drawing, or presentation)
Dim oADoc As AssemblyDocument = TryCast(ThisDoc.Document, Inventor.AssemblyDocument)
If oADoc Is Nothing Then
Logger.Debug("Rule named '" & iLogicVb.RuleName & "' could not obtain an AssemblyDocument!")
Return
End If
'get all referenced documents at all levels of the entire assembly structure
Dim oAllRefDocs As DocumentsEnumerator = oADoc.AllReferencedDocuments
Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
'get access to the UserParameters of the main assembly
Dim oAsmUParams As UserParameters = oADef.Parameters.UserParameters
'<<< specify which part names, and which assembly parameter names to associate with each other >>>
Dim oPairs As New Dictionary(Of String, String)
'first specify part file name (without extension), then specify assembly parameter name
oPairs.Add("PART A", "X")
oPairs.Add("PART B", "Y")
oPairs.Add("PART C", "Z")
For Each oPair As KeyValuePair(Of String, String) In oPairs
'create variable to represent the UserParameter object we are looking for
Dim oPairParam As UserParameter = Nothing
'try to find those specific UserParameter objects by name, then assign them to the variables
Try
oPairParam = oAsmUParams.Item(oPair.Value)
Catch
MsgBox("Could not find UserParameter named '" & oPair.Value & "' in assembly!", vbExclamation, "iLogic")
Continue For 'skip to next Dictionary entry
End Try
If oPairParam Is Nothing Then Return
'try to find the referenced part document by its specified file name (filtered to only sheet metal parts)
Dim oPairPartDoc As PartDocument = Nothing
For Each oRefDoc As Inventor.Document In oAllRefDocs
'get the file name, without file path, and without file extension
Dim sFileName As String = System.IO.Path.GetFileNameWithoutExtension(oRefDoc.FullFileName)
'compare the file name to the specified file name in the current Dictionary entry
If sFileName <> oPair.Key Then Continue For 'if they do not match, then skip to next referenced document
'if it is not a part, then skip to next referenced document
If Not TypeOf oRefDoc Is PartDocument Then Continue For
'declare a PartDocument type variable, then set its value (makes things easier later)
Dim oPDoc As PartDocument = oRefDoc
'if it is not specifically a sheet metal part, then skip directly to next referenced document
If Not TypeOf oPDoc.ComponentDefinition Is SheetMetalComponentDefinition Then Continue For
'<<< matching file name, document type, and document sub type - found the part >>>
oPairPartDoc = oPDoc
Exit For 'exit loop of referenced documents
Next oRefDoc 'go to the next referenced document
If oPairPartDoc Is Nothing Then
MsgBox("Could not find Part file named '" & oPair.Key & "' in assembly referenced documents!", vbCritical, "iLogic")
Continue For
End If
'make sure this referenced part is 'fully' open (but not visible), and make sure it is the active version (if multiple versions)
oPairPartDoc = ThisApplication.Documents.Open(oPairPartDoc.FullDocumentName, False)
'make sure that if this part document requires an update, it gets updated before further inspection / changes
If oPairPartDoc.RequiresUpdate Then oPairPartDoc.Update2(True)
Dim oSMDef As SheetMetalComponentDefinition = oPairPartDoc.ComponentDefinition
'get the collection of all Sheet Metal Rules/Styles that are available for this sheet metal part
Dim oSMStyles As SheetMetalStyles = oSMDef.SheetMetalStyles
'get the active sheet metal rule/style (the value of this property can be read, but not changed directly)
Dim oActiveSMStyle As SheetMetalStyle = oSMDef.ActiveSheetMetalStyle 'ReadOnly property
'get the expression / equation defining the Thickness that this sheet metal rule/style is for (a String)
Dim sActiveSMStyleThickness As String = oActiveSMStyle.Thickness
'get the main Parameter object for this part's Thickness (ReadOnly - we can not set a different Parameter as its value)
Dim oThicknessParam As Inventor.Parameter = oSMDef.Thickness
'check if the style thickness expression matches the part's "Thickness" parameter's expression
'if they do not match, then force the parameter's expression to be the thickness of the sheet metal style
If oThicknessParam.Expression <> sActiveSMStyleThickness Then
'if True, forces the part to use the Thickness defined within the part's active sheet metal style
oSMDef.UseSheetMetalStyleThickness = True 'Read/Write property with Boolean value
End If
'compare the assembly parameter's expression to the part's active sheet metal style thickness String
If sActiveSMStyleThickness = oPairParam.Expression Then
'if the match, then no action is needed, so just skip to the next Dictionary entry
Continue For 'everything is already OK, so skip to next referenced document
Else 'they do not match, so attempt to switch to a different sheet metal style for needed thickness
For Each oSMStyle As SheetMetalStyle In oSMStyles
If oSMStyle.Thickness = oPairParam.Expression Then
'found a sheet metal style with matching thickness, so activate it
Try : oSMStyle.Activate : Catch : End Try
'<<< if no sheet metal style is found with matching thickness, nothing will happen >>>
End If
Next oSMStyle
End If
If oPairPartDoc.RequiresUpdate Then oPairPartDoc.Update2(True)
Next oPair 'go to the next Dictionary entry
'if the main assembly requires an update, then update it
If oADoc.RequiresUpdate Then oADoc.Update2(True) 'True = Accept Errors And Continue
'if the main assembly has been dirtied (changed since last save), then save it now
'If oADoc.Dirty Then oADoc.Save2(True) 'True = also save dependent documents that have changed
End Sub
Wesley Crihfield

(Not an Autodesk Employee)