Adding Parameters with iLogic

Adding Parameters with iLogic

Anonymous
Not applicable
5,985 Views
13 Replies
Message 1 of 14

Adding Parameters with iLogic

Anonymous
Not applicable

Happy hump day everyone!

my question for today: I am migrating my files from Inventor 2016 to 2017 and would like to add parameters to my existing files without actually adding parameters by opening the parameters dialog and typing them in. Can I do this task via iLogic?

I would like to add following parameters + values

 

Costlb  (ul)   (Multi value list: 0.65, 1.65, 2.05 + allow custom values)

Material  (text)  (Multi value list)

By   (text)   FJNII

Company   (text)  Company Name

 

Any and all help appreciated.

 

Frank

Inventor 2017

Accepted solutions (1)
5,986 Views
13 Replies
Replies (13)
Message 2 of 14

ekinsb
Alumni
Alumni

Below is some code you can put in an iLogic rule.  I would recommend an external rule since it's not document specific.  The first variable defines a folder that you want to process.  This will process all of the part files in that folder and any subfolders.  By editing the code you can change the contents of the lists.

 

Dim pathToProcess As String = "C:\Temp\Files\"

Dim files() As String = System.IO.Directory.GetFiles(pathToProcess, "*.ipt", IO.SearchOption.AllDirectories)
Dim cnt As Integer = 0
For Each file As String In files
	cnt += 1
	ThisApplication.StatusBarText = "Processing file " & cnt & " of " & files.Length & "..."
	Dim doc As PartDocument
	doc = ThisApplication.Documents.Open(file, False)
	
	' Add the parameters.
	Dim userParams As UserParameters
	userParams = doc.ComponentDefinition.Parameters.UserParameters
	
	Dim param As UserParameter = userParams.AddByValue("Costlb", 0, "ul")
	Dim values(2) As String
	values(0) = "0.65 ul"
	values(1) = "1.65 ul"
	values(2) = "2.05 ul"
	Dim expList As ExpressionList = param.ExpressionList
	expList.SetExpressionList(values, True, 0)
	
	param = userParams.AddByValue("Material", "", "Text")
	ReDim values(3)
	values(0) = """Steel"""
	values(1) = """Bubble Gum"""
	values(2) = """Potato Salad"""
	values(3) = """Titanium"""
	expList = param.ExpressionList
	expList.SetExpressionList(values, True, 0)
	
	param = userParams.AddByValue("By", "FJNII", "Text")
	
	param = userParams.AddByValue("Company", "Company Name", "Text")
	
	doc.Save()
	doc.Close()
Next

Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 3 of 14

Anonymous
Not applicable

Brian,

 

Tried to run this rule from inside a file and got following error:

 

Error in rule: Add Parameters, in document: IN-049.ipt

Value cannot be null.
Parameter name: path

 

Do I need to make a form for this rule? Reading the first line of this rule makes me think this was written to process a complete folder?

 

Hope you had a great weekend. Thanks for your help.

 

Frank

0 Likes
Message 4 of 14

ekinsb
Alumni
Alumni

It does process all of the part files in the directory specified by the pathToProcess variable.  Did you set that to a valid directory?  There might also be a problem with it if those parameters already exist in the file.  I didn't add any checks for that because I assumed that they wouldn't.


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 5 of 14

Anonymous
Not applicable

Brian,

 I saw that after I posted the last question. So I put in the path and it still threw an error. Not sure how to run a rule outside of a file, so I was running it from an open part file. (not empty) I did save it as an external rule. Was also running it from the same directory I wanted to add the parameters to. Will try other options when I get a chance. Thanks again for your help.

 

 

Frank

0 Likes
Message 6 of 14

ekinsb
Alumni
Alumni

iLogic is always the best tool for automating Inventor and in many cases, even when it is you'll still be able to prototype code easier and faster in VBA or Visual Studio.  What you're trying to do is a case where I don't think iLogic is the best choice.  Here's a version of the same thing in VBA.  This will also process a directory of part files, but this version won't handle sub-directories.  I also added a check to handle if the file already has the parameters.  The previous version still worked but you would end up with multiple parameters with numbers appended to the names to make them unique.

 

Public Sub AddProperties()
    Dim pathToProcess As String
    pathToProcess = "C:\Temp\Files\"
      
    Dim fileName As String
    fileName = Dir(pathToProcess & "*.ipt", vbNormal)
    Do While fileName <> ""
        ThisApplication.StatusBarText = "Processing file: " & fileName & "..."
        Dim doc As PartDocument
        Set doc = ThisApplication.Documents.Open(pathToProcess & fileName, False)
        
        ' Add the parameters, if they don't already exist.
        Dim userParams As UserParameters
        Set userParams = doc.ComponentDefinition.Parameters.UserParameters
        
        ' Check for the existence of the parameter "Costlb" and exit the program
        ' if it does exist. It's assuming they all exist if "Costlb" is there.
        Dim param As UserParameter
        On Error Resume Next
        Set param = userParams.Item("Costlb")
        If Err.Number = 0 Then
            MsgBox "The parameters already exist.  Exiting the program."
            Return
        End If
        On Error GoTo 0
        
        Set param = userParams.AddByValue("Costlb", 0, "ul")
        Dim values() As String
        ReDim values(2) As String
        values(0) = "0.65 ul"
        values(1) = "1.65 ul"
        values(2) = "2.05 ul"
        Dim expList As ExpressionList
        Set expList = param.ExpressionList
        Call expList.SetExpressionList(values, True, 0)
        
        Set param = userParams.AddByValue("Material", "", "Text")
        ReDim values(3)
        values(0) = """Steel"""
        values(1) = """Bubble Gum"""
        values(2) = """Potato Salad"""
        values(3) = """Titanium"""
        Set expList = param.ExpressionList
        Call expList.SetExpressionList(values, True, 0)
        
        Set param = userParams.AddByValue("By", "FJNII", "Text")
        
        Set param = userParams.AddByValue("Company", "Company Name", "Text")
        
        Call doc.Save
        Call doc.Close
    
        fileName = Dir()
    Loop
    
    ThisApplication.StatusBarText = "Finished."
End Sub

 


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 7 of 14

Anonymous
Not applicable

Brian,

    You're probably right if I was fluent or even experienced in VBA or Visual Studio. But alas, I'm not, no experience with either or VB.Net. I've managed to pick up enough iLogic to get myself in trouble. As far as this piece of code (Thank you) How do I use it to process a folder/directory to get my parameters into existing part files? Can I do it with a "Custom Task" from the Inventor Task Scheduler?

  While I really appreciate all your help, assume I know nothing ( which is almost true) so I would need step by step instructions on how to best use the help you are providing. Other wise I'm just poking and hoping. Thanks again.

 

 

Frank

0 Likes
Message 8 of 14

Anonymous
Not applicable

And thanks for your continued patience.

 

 

Frank

0 Likes
Message 9 of 14

ekinsb
Alumni
Alumni
Accepted solution

I can't edit my most recent post but I assume you understood that I really meant that "iLogic is NOT always the best tool".

 

Here's a posting on how to use a VBA script.  

 

http://modthemachine.typepad.com/my_weblog/2010/05/using-vba-sample-code-for-the-non-programmer.html

 

And I hate to break it to you, but you are already programming if you're using iLogic, so it's not a big leap to use some of these other tools. 🙂


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 10 of 14

Anonymous
Not applicable

Brian,

  Thank you for all your help. I'm finally making some headway! Missed a few things along the way but have been figuring them out. Had to edit the value (3) for the material list, had me stumped for a bit, but figured it out. Also missed the edit for my company name, that was easy. Got another question for you. On the multi value list for my materials (we don't make anything out of potato salad BTW) how do I "allow custom values"? Otherwise I'm good to go for now. Thanks again for you patience and help.

 

 

Frank

0 Likes
Message 11 of 14

ekinsb
Alumni
Alumni

Hi Frank,

 

Unfortunately, it looks like that setting is missing from the API.  I think it was added later after the initial implementation of lists and has been overlooked. I can't think of any kind of work around.  The best thing to do would be to post it on the Idea Station with API as a tag.  It's certainly a reasonable request.


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 12 of 14

DRoam
Mentor
Mentor

For anyone who would like to vote, here is @Anonymous's suggestion in the IdeaStation: IdeaStation: API Improvement

Message 13 of 14

Anonymous
Not applicable

Hello Frank,

 

I posted this on the Idea Station too (Idea) , but if someone wouldn't hit it: the option you're looking for, is available:

 

 

2017-03-14_14h00_53.png

 

as soon as the parameter has an ExpressionList, inventor saves an attribute to it, which controls this.

 

HTH!

BR,

Daniel

Message 14 of 14

Anonymous
Not applicable

I'm wondering if there is a separate idea station for Inventor API... a had a few... Smiley Happy

BR,

Daniel

0 Likes