Creating User Parameters in Inventor

Creating User Parameters in Inventor

chris
Advisor Advisor
1,918 Views
17 Replies
Message 1 of 18

Creating User Parameters in Inventor

chris
Advisor
Advisor

Is there a way in iLogic to create a set of user-defined parameters within iLogi without first creating them in the Parameters window and then referring them in iLogic? I have a lot to create and I wanted to be able to copy/paste, re-order, ect. (which I can't currently do in the Parameters window).

 

Just to clarify further what I am trying to do, I want to be able to have an easy way to create 10-30 user parameters for a "part" file, which in this case happens to just be vertical and horizontal offsets from the origin for the creation of a bunch of planes used in a skeleton model to control the insertion points of a handful of parts at the assembly level to control a General Arrangement assembly.

 

I don't mind creating these in the parameter window in IV, I just thought there might be an easier way in iLogic, sort of like when you create a list of custom iproperties. I'd like the ability to edit the Parameter Name, Unit/type, Equation/value, and "comment text" if possible, otherwise the parameter name and value are fine.

chris_0-1710007502252.png

 

0 Likes
1,919 Views
17 Replies
Replies (17)
Message 2 of 18

A.Acheson
Mentor
Mentor

Hi @chris 

Here is a few links I put together for another post. These are methods to drive and create parameters via the Inventor API. Sticking all you parameter names in a list source from an excel list/ manual input and looping through the list will be a good way to create the parameters. Assigning values you could do through ilogic multilist method. Alternatively you could pick them up from an excel sheet but that would be more work and complication.

 

Export iproperty from parameter

Create Parameters using vb.net

Create Parameter VBA Sample API Help

Parameter Object API Help

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

chris
Advisor
Advisor

@A.Acheson Thanks for the links, looking through now, I tried this one, but the code returned an error that I am not familiar with how to fix? (this is the 3rd link you provided) 

chris_0-1710010252741.png

 

0 Likes
Message 4 of 18

mat_hijs
Collaborator
Collaborator

In iLogic there needs to be one main sub that will run when running the rule, when not explicitly stating subs the sub called "Main" is implied. If you want to use multiple subs you'll have to explicitly state one as Sub Main. So to solve this error you have two options, either replace "Public Sub CreateParameters()" with "Sub Main()" or remove "Public Sub CreateParameters()" and "End Sub".

You can read more about this here: https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=GUID-32B66838-22E4-4A0A-B5BB-862350C76B36

0 Likes
Message 5 of 18

mat_hijs
Collaborator
Collaborator

I see that your code is VBA (Visual Basic for Applications), so if you want to use it in iLogic you'll have to convert it to VB.Net or you'll get more errors. This code should work in iLogic:

Dim oPartDoc As PartDocument = ThisApplication.ActiveDocument
Dim oPartCompDef As PartComponentDefinition = oPartDoc.ComponentDefinition
Dim oUserParameters As UserParameters = oPartCompDef.Parameters.UserParameters
Dim oParameter As Parameter = oUserParameters.AddByExpression("NewParameter1", "20 mm", UnitsTypeEnum.kMillimeterLengthUnits)

0 Likes
Message 6 of 18

A.Acheson
Mentor
Mentor

Link two has a functioning parameter creation using vb.net which can be used in the iLogic environment. I would suggest to learn how to convert VBA sample to iLogic code because you can then use all of the API samples which unfortunately were only mostly provided in VBA format.  @mat_hijs provided some useful tips. Need any more just post it up. The main thing with VBA conversion is to call the sub routine by using sub Main or remove sub routines entirely. The next thing is drop the word "Set". You can actually declare and set the object in the same line in vb.net where as VBA needs two separate lines. 

 

 

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

chris
Advisor
Advisor

@mat_hijs Thank you, that worked for adding one parameter, I assumed I could just copy/paste line 4, making changes to the "NewParameter1" and the value, but when I did that it gave me an error, so I assumed there is more involved. What I am trying to do is be able to list out several Parameters like this (see image), but this obviously, returns errors.

 

On top of that, once I run the rule, if I come back to it to make a change to the first parameter value, instead of changing it, it creates a new one with a "_1".

 

Perhaps I'm over-thinking this, I just wanted a way to quickly list off a large amount of Parameters, so I could group and re-order them... just like the Inventor Parameters window should allow 

chris_0-1710022396227.png

chris_1-1710022441670.png

 

 

 

 

 

0 Likes
Message 8 of 18

mat_hijs
Collaborator
Collaborator

Like the error says, you're trying to declare a variable that has already been declared. There are two ways around this, either you declare this variable once and set different values to that same variable like in this example:

Dim oPartDoc As PartDocument = ThisApplication.ActiveDocument
Dim oPartCompDef As PartComponentDefinition = oPartDoc.ComponentDefinition
Dim oUserParameters As UserParameters = oPartCompDef.Parameters.UserParameters
Dim oParameter As Parameter
oParameter = oUserParameters.AddByExpression("NewParameter1", "10 mm", UnitsTypeEnum.kMillimeterLengthUnits)
oParameter = oUserParameters.AddByExpression("NewParameter2", "20 mm", UnitsTypeEnum.kMillimeterLengthUnits)
oParameter = oUserParameters.AddByExpression("NewParameter3", "30 mm", UnitsTypeEnum.kMillimeterLengthUnits)

Or you can declare a new variable for each parameter like in this example:

Dim oPartDoc As PartDocument = ThisApplication.ActiveDocument
Dim oPartCompDef As PartComponentDefinition = oPartDoc.ComponentDefinition
Dim oUserParameters As UserParameters = oPartCompDef.Parameters.UserParameters
Dim oParameter1 As Parameter = oUserParameters.AddByExpression("NewParameter1", "10 mm", UnitsTypeEnum.kMillimeterLengthUnits)
Dim oParameter2 As Parameter = oUserParameters.AddByExpression("NewParameter2", "20 mm", UnitsTypeEnum.kMillimeterLengthUnits)
Dim oParameter3 As Parameter = oUserParameters.AddByExpression("NewParameter3", "30 mm", UnitsTypeEnum.kMillimeterLengthUnits)

 If you want to make the code change the parameter value if it already exists and add it if it doesn't you'll first have to check if it exists. This is an example to do this for one parameter, but if you want to do this for multiple parameters you should probably write a function to do all this and then call this function for each parameter.

Dim oPartDoc As PartDocument = ThisApplication.ActiveDocument
Dim oPartCompDef As PartComponentDefinition = oPartDoc.ComponentDefinition
Dim oUserParameters As UserParameters = oPartCompDef.Parameters.UserParameters

Dim oParameter As Parameter
Try
	oParameter = Parameter.Param("NewParameter1")
	oParameter.Expression = "10 mm"
Catch
	oParameter = oUserParameters.AddByExpression("NewParameter1", "10 mm", UnitsTypeEnum.kMillimeterLengthUnits)
End Try

Message 9 of 18

mat_hijs
Collaborator
Collaborator

Also, as a general tip, when posting in this forum always try to add your code as a code snippet instead of a screenshot.

Message 10 of 18

chris
Advisor
Advisor

@mat_hijs Thanks for the help, unfortantly I don't know anything about writing functions, other than using the snippets in iLogic, I did however add your single "Try/Catch" example to multiple lines and it seems to work. I'm sure there's a way to write the code more efficiently, but this is working, so I'll run with it.

 

Dim oPartDoc As PartDocument = ThisApplication.ActiveDocument
Dim oPartCompDef As PartComponentDefinition = oPartDoc.ComponentDefinition
Dim oUserParameters As UserParameters = oPartCompDef.Parameters.UserParameters
Dim oParameter As Parameter

Try
		oParameter = Parameter.Param("Length")
		'Live Dimension
		oParameter.Expression = "333"
Catch
	'Initial Creation of Parameter
	oParameter = oUserParameters.AddByExpression("Length", "60", UnitsTypeEnum.kDefaultDisplayLengthUnits)
End Try

Try
		oParameter = Parameter.Param("Width")
		'Live Dimension
		oParameter.Expression = "333"
Catch
	'Initial Creation of Parameter
	oParameter = oUserParameters.AddByExpression("Width", "30", UnitsTypeEnum.kDefaultDisplayLengthUnits)
End Try

Try
		oParameter = Parameter.Param("Height")
		'Live Dimension
		oParameter.Expression = "333"
Catch
	'Initial Creation of Parameter
	oParameter = oUserParameters.AddByExpression("Height", "20", UnitsTypeEnum.kDefaultDisplayLengthUnits)
End Try

Try
		oParameter = Parameter.Param("Dia")
		'Live Dimension
		oParameter.Expression = "25"
Catch
	'Initial Creation of Parameter
	oParameter = oUserParameters.AddByExpression("Dia", "25", UnitsTypeEnum.kDefaultDisplayLengthUnits)
End Try

 

0 Likes
Message 11 of 18

mat_hijs
Collaborator
Collaborator

Give this code a try, but be aware that this will only work for specific units etc.

Sub Main
	CreateOrChangeParameter("NewParameter1", "10 mm")
	CreateOrChangeParameter("NewParameter2", "20 mm")
	CreateOrChangeParameter("NewParameter3", "15 mm + 15 mm")
	CreateOrChangeParameter("NewParameter4", "NewParameter3")
End Sub

Public Sub CreateOrChangeParameter(ByVal sParameterName As String, ByVal sExpression As String)
	Dim oPartDoc As PartDocument = ThisApplication.ActiveDocument
	Dim oPartCompDef As PartComponentDefinition = oPartDoc.ComponentDefinition
	Dim oUserParameters As UserParameters = oPartCompDef.Parameters.UserParameters
	Dim oParameter As Parameter
	Try
		oParameter = Parameter.Param(sParameterName)
		oParameter.Expression = sExpression
	Catch
		oParameter = oUserParameters.AddByExpression(sParameterName, sExpression, UnitsTypeEnum.kDefaultDisplayLengthUnits)
	End Try
End Sub

Also, this will not give you the ability to reorder, and I'm not aware of a way to be able to do that easily because parameters will be referenced in sketches, features, other parameters, ...

0 Likes
Message 12 of 18

Frederick_Law
Mentor
Mentor

How about just sort the list?

ParametersSort-01.jpg

0 Likes
Message 13 of 18

WCrihfield
Mentor
Mentor

Hi @chris.  If this group of parameters you want to quickly create is stable (same ones each time), then why not simply create the ones you want manually in another part, export them to an XML file right from the Parameters dialog, then when you want to put them into another document, just import that XML file from the other document, which will create them all at once.  This can be done manually without any code, or it can also be cone by code fairly easily.

iLogicVb.Automation.ParametersXmlSave(oDoc, sXMLFileName, XmlSaveOption.KeysOnly)

or

iLogicVb.Automation.ParametersXmlLoad(oDoc, sXMLFileName)

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 14 of 18

chris
Advisor
Advisor

@WCrihfield I was not aware that you could do that. Would you happen to have an example of what you are talking about and how that XML file should be formatted or separated?

0 Likes
Message 15 of 18

WCrihfield
Mentor
Mentor

Hi @chris.  I never had to worry about how the XML file was formatted or separated.  There is only one 'toggle' type option when exporting (KeysOnly or All).  The 'KeysOnly' option just means only export the parameters that are marked for export.  To mark a Parameter for export, just check the checkbox in the column labeled 'Export Parameter'.  We can also check (or uncheck) that box by code (Parameter.ExposedAsProperty).  So, if I want to control which parameters it will export, I either limit all the parameters to only the ones I want, like in a new document, or make sure that only the ones I want to export are set to export, by making sure that property is True for them.  Then use that line of code to export the parameters to an external XML file.  Then when working with another document that I want those parameters to be in (by code), I use that one line of code to import that XML file, and boom...all those parameters are now present in my document.  All the parameter names are the same, values are all still the same, the multi-value lists are preserved, the 'Key' checkbox is still checked, and the comments are still there in the new document.  The only setting that does not copy with each parameter is the checkbox setting for exporting that parameter.  It pretty sweet.

 

Edit:  Here is one possible example code though, just for something to work with.  At the top of the example is a line that might need to be in the 'Header' of the rule.  But only if you did not use certain terms within your rule, such as ThisDoc and others.

'AddReference "Autodesk.Inventor.Interop"
Dim oDoc As Document = ThisDoc.Document
Dim oParams As Inventor.Parameters = Nothing
If oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject OrElse _
	oDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
	oParams = oDoc.ComponentDefinition.Parameters
ElseIf oDoc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then
	oParams = oDoc.Parameters
End If
If oParams Is Nothing Then : Logger.Debug("Parameters Not Found") : Return : End If
Dim oUParams As UserParameters = oParams.UserParameters
For Each oUParam As UserParameter In oUParams
	If Not oUParam.ExposedAsProperty Then oUParam.ExposedAsProperty = True
Next 'oUParam
Dim sProjPath As String = ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath
Dim sDocName As String = ThisDoc.FileName(False)
Dim sXMLFileName As String = sProjPath & "\" & sDocName & ".xml"
iLogicVb.Automation.ParametersXmlSave(oDoc, sXMLFileName, XmlSaveOption.KeysOnly)

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 16 of 18

chris
Advisor
Advisor

@WCrihfield I appreciate the help, but I just want to clarify and make sure I am hearing what you are saying.

First off, I have no idea what you are talking about, lol what I mean is this, (what I heard you say):

 

I can create a blank xml file with something like (notepad). where I can create a list of parameters, similar to what I might do in Excel when linking to Excel. However, in Excel, I have to list the starting cell in order for Inventor to know how to import the data correctly, but you are saying there is no formatting for an XML import? (example) Parameter Name, Units, Value, ect? 

 

Apologies if I am not understanding correctly, Below is an example of a list of user parameters I'd like to import, of course each would contain a units and value:

 

Base_A

Base_B

Base_C

Base_D

Base_E

Base_F

Base_G

Base_H

Base_J

Base_K

Base_L

Base_M

Base_N

Base_P

Base_Q

Base_R

Base_S

Base_T

Base_U

Base_V

Base_W

Base_X

Base_Y

Base_Z

Base_Dia01

Base_Dia02

Base_Dia03

Base_Dia04

Base_WeldGap

Base_Offset01

 

 

0 Likes
Message 17 of 18

WCrihfield
Mentor
Mentor

Sorry if I was not very clear.  I have never created an XML file from scratch like you are talking about for the purpose of importing Parameters into an Inventor document.  The XML file gets created automatically for me when I export the parameters from an existing Inventor document.  Within the Parameters dialog of a model file, you will see something like the following in the lower left corner of that dialog.

WCrihfield_0-1710263204871.png

When I am doing this process manually, I start from an existing model document that already contains all the parameters in it that I want to reuse in another model document, then I click on the 'Export to XML' button (circled in red) above, or I click on the similar button out on the main ribbon by the same name, as seen in the image below.

WCrihfield_1-1710263384318.png

When you click that button, a dialog opens wanting you to specify where to create / save the new XML file, and what to name it.  While that dialog is open, click on the Options button, then choose the 'Key Parameters Only' option (if that is the one you want), then proceed to create the new XML file.

WCrihfield_2-1710263596956.png

The lines of code I showed you in Message 13 were for use within an iLogic rule only, and are for the same functionality as these manual methods shown above.  When you export an XML file this way, you can open that XML file that it created to review how it is formatted, but I have never had to mess with the 'contents' of one of these XML files before.

 

Then, when I have some other model document open (the one I want to put those parameters into), I either use the button on the ribbon, or open the Parameters dialog, click on that 'Import from XML' button, choose the XML file that I exported from the other model document, and click OK.  Now all those parameters are in my current model document.  I would not want to completely, manually type out the whole contents of the XML file in a text editor for a lot of parameters.

I have attached a very simple example XML file that was created this way from a simple test part.

 

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 18 of 18

chris
Advisor
Advisor

@WCrihfield Gotcha... now I know what you are talking about. I'm going to create a couple examples and see if I can come up with an XML template to use, thanks for the direction... I wouldn't have thought about that!

0 Likes