Announcements

Starting in December, we will archive content from the community that is 10 years and older. This FAQ provides more information.

Creating a "dynamic" user parameter

mrawesomelemons
Enthusiast
Enthusiast

Creating a "dynamic" user parameter

mrawesomelemons
Enthusiast
Enthusiast

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?

0 Likes
Reply
248 Views
2 Replies
Replies (2)

A.Acheson
Mentor
Mentor

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

WCrihfield
Mentor
Mentor

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)

0 Likes