Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Renaming selected Part parameters & set to export from Assembly level

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
andrew.tom.reynolds
325 Views, 5 Replies

Renaming selected Part parameters & set to export from Assembly level

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
5 REPLIES 5
Message 2 of 6

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.

Message 3 of 6
A.Acheson
in reply to: Zach.Stauffer

Another thing it looks like you are using "parameters.item" incorrectly. 

Syntax

Parameters.ItemIndex As Variant ) As Parameter

 

https://help.autodesk.com/view/INVNTOR/2021/ENU/?guid=GUID-9031D871-3BAE-4170-8F69-4E49B23336CD

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
Message 4 of 6

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

 

Message 5 of 6

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.

AAcheson_0-1656382484144.png

 

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

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
Message 6 of 6

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.

Post to forums  

Autodesk Design & Make Report