Why is the Parameter("parameterName") function slow with many parameters?

Why is the Parameter("parameterName") function slow with many parameters?

casper.vorm
Contributor Contributor
421 Views
4 Replies
Message 1 of 5

Why is the Parameter("parameterName") function slow with many parameters?

casper.vorm
Contributor
Contributor

Hi there.

 

I work with some fairly complex models that have a lot of parameters. Sometimes I need to write a function that iterates through multiple parameters (based on their name) and updates some value, like so for example:

	For i = 0 To list_section_total.Count - 1
		
		Dim result = sum_previous(list_section_total, i)
		Parameter("inlet_" & k & "_offset_" & std_section_lengths(i)) = result
		
	Next

However, when the number of parameters increases, accessing or updating parameter values with the Parameter function takes increasingly longer time. But doing this directly with the "blue parameters" is very fast.

 

Why is there a difference and is there any other, faster way to simply get or set the value of a parameter based on its name?

 

Thanks in advance!

-Casper

0 Likes
Accepted solutions (1)
422 Views
4 Replies
Replies (4)
Message 2 of 5

WCrihfield
Mentor
Mentor
Accepted solution

Hi @casper.vorm.  That is a good question, and maybe one that other users won't be able to fully explain, because we did not write the programming behind that iLogic only shortcut function.  Basically every time you use that function, it has to run a whole other block of code in the background to find/identify things and figure things out, then do the actions.  Since no document reference is given, it has to go through a routine to figure out which document to target, which likely has a whole block of code just for that task.  Then it doesn't know what type of parameter it is, so it must look through all existing parameters of all types to find one with the given name, if it exists.  Then it tries to get the 'document units' version of its value for you, instead of the 'database units' version, which you would normally get when accessing the parameter through normal API means.  There are likely other stuff in that block of code too that I'm not even thinking of right now.

You could most likely access the parameters faster through the normal API means, than with using the Parameter() function, but it would require more code, and as I mentioned before, the units of a Parameter API object's Value or ModelValue properties are in database units (for length/distance it will be centimeters).  If those are not the same as your document units, then you will likely have to convert the units as needed using either some simple math or the ConvertUnits method, which is under the UnitsOfMeasure object.  Even though you may have to use more code within your rule, and may have to use some math to convert units, it will still most likely be faster performing.  I don't know if it will be faster than using the blue local parameter names though.  You would have to test it in different situations.

API route:  PartDocument.ComponentDefinition.Parameters.UserParameters.Item(1).Value

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 5

A.Acheson
Mentor
Mentor

Maybe try and wrap the parameter access in a transaction to avoid recording unecessary undos which will likely result in more time to process. Here is the overview of its use. This is untested but a quick implement might see results for you based on its use in other forum posts. 

 

Dim oTrans As Transaction = ThisApplication.TransactionManager.StartTransaction( Document As Document, DisplayName As String ) 

'The code your using

oTrans.End

 

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

casper.vorm
Contributor
Contributor

Hi @A.Acheson and @WCrihfield, thank you for your replies! I will try both approaches and return once I have compared the runtime.

0 Likes
Message 5 of 5

casper.vorm
Contributor
Contributor

Hi again, apologies for the very late reply, suddenly there were a lot of work I had to take care of first.

 

I didn't get around to try the transactions, but using the API seems to be much faster. I ended up making two functions: get_value and set_value.

Function get_value(param_name As String)
	Dim res As Object
	Dim oDoc = ThisDoc.Document
	Dim uom As UnitsOfMeasure
	uom = oDoc.UnitsOfMeasure
	Dim param As Parameter = param_obj.Item(param_name)
	
	If Not (param.Units = "Text" Or param.Units = "Boolean") Then
		If uom.CompatibleUnits(param.Expression, param.Units, "1", "mm")
			res = param.Value * 10
			
		Else If uom.CompatibleUnits(param.Expression, param.Units, "1", "deg")
			res = param.Value * (180 / PI)
		Else
			res = param.Value
		End If
	Else
		res = param.Value
	End If
	Return res
	
End Function
Function set_value(param_name As String, new_val As Object)
	Dim oDoc = ThisDoc.Document
	Dim uom As UnitsOfMeasure
	uom = oDoc.UnitsOfMeasure
	
	Dim param As Parameter = param_obj.Item(param_name)
	Try
		param.Expression = new_val
	Catch
		param.Value = new_val
	End Try
End Function

Now, accessing or updating parameters is almost instantaneous (between 0.01 and 0.05 seconds), regardless of the number of parameters. Once again thank you for your suggestions!

 

-Casper

0 Likes