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: 

Creating a "dynamic" user parameter

2 REPLIES 2
Reply
Message 1 of 3
mrawesomelemons
213 Views, 2 Replies

Creating a "dynamic" user parameter

I am trying to create a script that turns the part number into a user parameter. The reason is that I want to use the part number in a text field on an .idw. Creating a parameter allows me to do this. Since I do not want endless amounts of these parameters I thought about deleting the parameter if it exists and then create it again.

So far I came up with the following code:

 

Dim PartNumber As Parameter
'Define parameter in the current user parameters of the current document
Param = ThisApplication.ActiveDocument.ComponentDefinition.Parameters
UserParam = Param.UserParameters

For Each UserParam In Param
	'Check if the parameter PartNumber exists
	If UserParam.Name = "PartNumber"
		'Delete the parameter if it does exist
		Parameter.Param("PartNumber").Delete
	End If
Next

'Create the parameter
PartNumber = UserParam.AddByValue("PartNumber", iProperties.Value("Project", "Part Number"), UnitsTypeEnum.kTextUnits)
'Set a comment
Parameter.Param("PartNumber").Comment = "Set by a rule, do not alter"

'Update file
iLogicVb.UpdateWhenDone = True

 

The codes work seperately (meaning the for loop deleting the parameter if it exists works and the creation of the parameter itself works) but together they do not. The error message I get is: "The public member AddByValue for type UserParameter was not found."


What am I doing wrong?

2 REPLIES 2
Message 2 of 3
A.Acheson
in reply to: mrawesomelemons

In the for each loop where you check if it exist you can use instead of the ilogic snippet.

UserParam.Delete

 Later you ask for a part number and take it from the drawing document. Will this be where you want to extract that information.


Also have you tried just using a custom iProperty instead of parameter? This would be a more direct route.

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

Hi @mrawesomelemons.  I agree with that too.  I also created a slightly modified version of your code that you can try out, to see if it works any better for you.

 

Dim oDoc As Document = ThisDoc.Document
Dim UParams As UserParameters = oDoc.ComponentDefinition.Parameters.UserParameters
For Each UserParam As UserParameter In UParams
	If UserParam.Name = "PartNumber" Then UserParam.Delete
Next
Dim PN As String = oDoc.PropertySets.Item(3).Item("Part Number").Value
Dim PNParam As UserParameter = UParams.AddByValue("PartNumber", PN, UnitsTypeEnum.kTextUnits)
PNParam.Comment = "Set by a rule, do not alter"
oDoc.Update

 

Also, keep in mind that if you want to access the parameters in a drawing type document, that type of document does not have a 'ComponentDefinition', so its Parameters are directly under the Document object (DrawingDocument.Parameters.UserParameters).  I assumed this code above was working with a Part or an Assembly.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report