I'm trying to rename a parameter "d2" from a number of selected parts in an Assembly to "LENGTH" and then set to export with a custom property format so I can use it in a BoM export later.
I've gleaned as much as i can from other posts and i'm 99% there. I'm just not sure how to dig in to the selected occurrence parameters properly. Below is my current code. I'm using the same selection structure for another rule and it's working fine. It's just the parameters section i'm not getting right.
Can anyone correct me please?
'Description of the Rule and if you want to continue
Result = MessageBox.Show("Do you want to continue?" _
& vbNewLine &"" _
& vbNewLine & "This rule allows you to browse the tree and select multiple items,then press the Esc Key and have an action carried out" _
& vbNewLine & "", "Browse And Select" _
, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2)
If Result = vbNo Then
MessageBox.Show ("Rule Canceled Exiting", "Title")
ElseIf Result = vbYes Then
Dim oDoc As AssemblyDocument
oDoc = ThisDoc.Document
oCompDef = oDoc.ComponentDefinition
Dim oSet As HighlightSet
oSet = oDoc.CreateHighlightSet
'MessageBox.Show ("Rule will Run", "Title")
'[Pick Assembly Occurrences
Dim comps As ObjectCollection
Dim comp As Object
comps = ThisApplication.TransientObjects.CreateObjectCollection
While True
comp = ThisApplication.CommandManager.Pick(
SelectionFilterEnum.kAssemblyLeafOccurrenceFilter,
"Select a component")
' If nothing gets selected then we're done
If IsNothing(comp) Then Exit While
comps.Add(comp)
oSet.AddItem(comp)
End While
'Description of the Rule and if you want to continue
Result = MessageBox.Show("Do you want to process all Selected?", "Process selected files", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2)
If Result = vbNo Then
MessageBox.Show ("Rule Cancelled", "iLogic")
'If there are selected Components we can Do something
ElseIf Result = vbYes Then
For Each comp In comps
'Pick only parts
If comp.DefinitionDocumentType = kPartDocumentObject Then
'Get document of each occurrence
Dim oOccDoc As Document
oOccDoc = comp.Definition.Document
oOccDoc.Parameter("d2").Name = "LENGTH"
oOccDoc.Parameter("LENGTH").ExposedAsProperty = True
oOccDoc.Parameter("LENGTH").CustomPropertyFormat.ShowUnitsString = False
oOccDoc.Parameter("LENGTH").CustomPropertyFormat.Precision = "0"
']
End If
Next
MessageBox.Show("Files Processed", "iLogic")
'Clear Highlighting
oSet.Clear()
End If
']
End If
Solved! Go to Solution.
Solved by A.Acheson. Go to Solution.
The Parameters object is actually on the PartComponentDefinition, not the PartDocument, so you should just be able to use "comp.Definition" directly if it is the correct type.
Another thing it looks like you are using "parameters.item" incorrectly.
Syntax
Parameters.Item( Index As Variant ) As Parameter
https://help.autodesk.com/view/INVNTOR/2021/ENU/?guid=GUID-9031D871-3BAE-4170-8F69-4E49B23336CD
Thanks for the info, but sadly my knowledge is very basic. Can someone please correct what i've done here?
For Each comp In comps
'Pick only parts
If comp.DefinitionDocumentType = kPartDocumentObject Then
'Get document of each occurrence
'Dim oOccDoc As Document
oOccDoc = comp.Definition
oOccDoc.Parameters.Item("d2").Name = "LENGTH"
oOccDoc.Parameters.Item("LENGTH").ExposedAsProperty = True
oOccDoc.Parameters.Item("LENGTH").CustomPropertyFormat.ShowUnitsString = False
oOccDoc.Parameters.Item("LENGTH").CustomPropertyFormat.Precision = "0"
End If
Next
So in short you have not accessed the location of where the parameters are reached from. This is shown in the error message. If you look at the API help for a document you will see no reference to parameters.
Looking at the API sample for the parameters collection you will see that you can access the collection from all of the below objects.
Accessed From
AssemblyComponentDefinition.Parameters, DrawingDocument.Parameters, FlatPattern.Parameters, PartComponentDefinition.Parameters, SheetMetalComponentDefinition.Parameters, WeldmentComponentDefinition.Parameters
So to this now leaves you with the object you have the occurrence and where you want to get to the parameters collection. If we look at API sample for the occurrence you will find a definition object which is a generic type. Next you may want to only return/work with part documents so we will use the ComponentOccurrence.DefinitionDocumentType As ObjectTypeEnum to filter the part documents or alternately you could use ComponentDefinition.Type() As DocumentTypeEnum to filter the definition type.
@Zach.Stauffer refers to this earlier in the thread.
Here is the working rule.
'Description of the Rule and if you want to continue Result = MessageBox.Show("Do you want to continue?" _ & vbNewLine &"" _ & vbNewLine & "This rule allows you to browse the tree and select multiple items,then press the Esc Key and have an action carried out" _ & vbNewLine & "", "Browse And Select" _ , MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2) If Result = vbNo Then MessageBox.Show ("Rule Canceled Exiting", "Title") ElseIf Result = vbYes Then Dim oDoc As AssemblyDocument oDoc = ThisDoc.Document oCompDef = oDoc.ComponentDefinition Dim oSet As HighlightSet oSet = oDoc.CreateHighlightSet 'MessageBox.Show ("Rule will Run", "Title") '[Pick Assembly Occurrences Dim comps As ObjectCollection Dim comp As Object comps = ThisApplication.TransientObjects.CreateObjectCollection While True comp = ThisApplication.CommandManager.Pick( SelectionFilterEnum.kAssemblyLeafOccurrenceFilter, "Select a component") ' If nothing gets selected then we're done If IsNothing(comp) Then Exit While comps.Add(comp) oSet.AddItem(comp) End While 'Description of the Rule and if you want to continue Result = MessageBox.Show("Do you want to process all Selected?", "Process selected files", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2) If Result = vbNo Then MessageBox.Show ("Rule Cancelled", "iLogic") 'If there are selected Components we can Do something ElseIf Result = vbYes Then For Each comp In comps 'Work with only parts If comp.DefinitionDocumentType = DocumentTypeEnum.kPartDocumentObject Then Dim oPartDef As PartComponentDefinition = comp.Definition Dim oParameters As Parameters = oPartDef.Parameters Try oParameters("d2").Name = "LENGTH" oParameters("LENGTH").ExposedAsProperty = True oParameters("LENGTH").CustomPropertyFormat.ShowUnitsString = False oParameters("LENGTH").CustomPropertyFormat.Precision = "0" Catch MessageBox.Show("Error Getting Parameter", "iLogic") End Try Else MessageBox.Show("Another document type has being chosen", "iLogic") End If Next MessageBox.Show("Files Processed", "iLogic") 'Clear Highlighting oSet.Clear() End If '] End If
Thanks @A.Acheson that works and great explanation.
The only thing I had to change to make it perfect was
oParameters("LENGTH").CustomPropertyFormat.Precision = "0"
Should actually be = kZeroDecimalPlacePrecision
Cheers!
Can't find what you're looking for? Ask the community or share your knowledge.