Set Custom Parameter Precision

Set Custom Parameter Precision

arkelec
Collaborator Collaborator
2,030 Views
16 Replies
Message 1 of 17

Set Custom Parameter Precision

arkelec
Collaborator
Collaborator

Is there some obvious error in the code below:

	Try	
		Parameter("xLength") = Parameter("xLength")
	Catch
		xLength = oDoc.ComponentDefinition.Parameters.UserParameters.AddByValue("xLength", 0, kMillimeterLengthUnits)		xLength.CustomPropertyFormat.Precision=Inventor.CustomPropertyPrecisionEnum.kZeroDecimalPlacePrecision
	End Try

 

If the line below is commented out, the code runs, but the precision is 8 .

xLength.CustomPropertyFormat.Precision=Inventor.CustomPropertyPrecisionEnum.kZeroDecimalPlacePrecision

 

I can't find an example of how to set the units when creating Custom Parameters using Catch Try.

 

0 Likes
Accepted solutions (1)
2,031 Views
16 Replies
Replies (16)
Message 2 of 17

rhasell
Advisor
Advisor

Hi

 

Try this for the formatting

oParameter = Parameter.Param("xLength")
 		oFormat=oParameter.CustomPropertyFormat
		oParameter.ExposedAsProperty=True
		oFormat.PropertyType=Inventor.CustomPropertyTypeEnum.kTextPropertyType
		oFormat.Precision=Inventor.CustomPropertyPrecisionEnum.kZeroDecimalPlacePrecision
		oFormat.Units="mm"
		oFormat.ShowUnitsString=False
		oFormat.ShowLeadingZeros=False
		oFormat.ShowTrailingZeros=False
Reg
2026.1
0 Likes
Message 3 of 17

arkelec
Collaborator
Collaborator

Thanks for replying.

 

I modified the code to:

 

 

	Try	
		Parameter("xLength") = Parameter("xLength")
	Catch
		xLength = oDoc.ComponentDefinition.Parameters.UserParameters.AddByValue("xLength", 0, kMillimeterLengthUnits)
		oParameter = Parameter.Param("xLength")
			oFormat=oParameter.CustomPropertyFormat
			oParameter.ExposedAsProperty=True
			oFormat.PropertyType=Inventor.CustomPropertyTypeEnum.kTextPropertyType
			oFormat.Precision=Inventor.CustomPropertyPrecisionEnum.kZeroDecimalPlacePrecision
			oFormat.Units="mm"
			oFormat.ShowUnitsString=False
			oFormat.ShowLeadingZeros=False
			oFormat.ShowTrailingZeros=False
	End Try

 

 

 

The issue still persists: 

 

 

0 Likes
Message 4 of 17

arkelec
Collaborator
Collaborator

The image would not post in my original reply.

 

Object Variable or With block variable not set.png

0 Likes
Message 5 of 17

rhasell
Advisor
Advisor
Accepted solution

Hi

 

I am not the greatest in coding, I just muddle along, but my understanding is that the the try statement in your example is incorrect, for two reasons.

1:  If it cant find the parameter, there is an error in creating it.

2: If the parameter does exist, then it will be skipped, and the formatting will not be applied.

 

Are you creating a new parameter? If so, then try this,

 

Dim oPartDoc as PartDocument = ThisDoc.Document
Dim userParams As UserParameters = oPartDoc.ComponentDefinition.Parameters.UserParameters
Dim newParam As UserParameter
Dim oFormat As CustomPropertyFormat

'xLength
Try
	oParam = oPartDoc.ComponentDefinition.Parameters("xLength")
Catch 	'If the parameter was not found, then create a new one.
	newParam = userParams.AddByExpression("xLength", 0, "mm") ' Create the Parameter as per above
	newParam.ExposedAsProperty = True 'Flag for Export
	oFormat = newParam.CustomPropertyFormat
	oFormat.PropertyType = Inventor.CustomPropertyTypeEnum.kTextPropertyType
	oFormat.Precision = Inventor.CustomPropertyPrecisionEnum.kZeroDecimalPlacePrecision
	oFormat.ShowUnitsString = False
	oFormat.ShowLeadingZeros = False
	oFormat.ShowTrailingZeros = False
End Try

 

Reg
2026.1
Message 6 of 17

arkelec
Collaborator
Collaborator

The Try Catch in my op works fine, it's the formatting of the decimal places that doesn't.

 

Is it not possible to add format precision within:

Try	
	Parameter("xLength") = Parameter("xLength")
Catch
	xLength = oDoc.ComponentDefinition.Parameters.UserParameters.AddByValue("xLength", 0, kMillimeterLengthUnits)
End Try
0 Likes
Message 7 of 17

JhoelForshav
Mentor
Mentor

@arkelec 

The code provided by @rhasell works fine for me.

Just to make sure, you know it's the format of the property exported by the parameter you're modifying, right?

propertyformat.PNG

0 Likes
Message 8 of 17

rhasell
Advisor
Advisor

Hi

 

Does my code work for you?

 

If yes, then half the battle is won. First get to a working point, which in this case is the snippet from my code, then you can slowly start customizing the code to suit your environment.

 

That way you will always have something to fall back on, and is much easier to troubleshoot.

 

If my code is not working in your environment, then I have misunderstood the problem all along.

 

Reg
2026.1
0 Likes
Message 9 of 17

arkelec
Collaborator
Collaborator

Those are Custom Properties, are they not?  I am adding Custom Parameters:

 

Custom Parameters.png

0 Likes
Message 10 of 17

arkelec
Collaborator
Collaborator

No, it doesn't work, it throws up the error:

 

Object Variable or With block variable not set.png

0 Likes
Message 11 of 17

rhasell
Advisor
Advisor

Okay, that is odd.

 

I copied the code from the forum thread, just to ensure that nothing was lost in translation, and then ran it multiple times to ensure that the Try/Catch worked correctly, and it works for me.

Annotation 2020-05-14 210307.png

Reg
2026.1
0 Likes
Message 12 of 17

rhasell
Advisor
Advisor

Just remember that it will not change the precision of the parameter if it already exists, for that you will need to adjust the code as per my first post.

 

Reg
2026.1
Message 13 of 17

arkelec
Collaborator
Collaborator

OK, Thanks.

I will have another look later & report back.

0 Likes
Message 14 of 17

rhasell
Advisor
Advisor

Did you perhaps switch off the export option?

 

I think you need that option in order to change the precision, same as you would with the right click options.

 

I did some testing and it generates an error if the export flag is set to false, or you comment out the line.

 

Reg
2026.1
Message 15 of 17

JhoelForshav
Mentor
Mentor

@arkelec wrote:

Those are Custom Properties, are they not?  I am adding Custom Parameters:

 

Custom Parameters.png


You are adding a user parameter, then you are exporting it as a custom property (.ExposedAsProperty = True)

Parameter.CustomPropertyFormat is called CustomPropertyFormat because its the format of the property that is exportet from the parameter. Thats why I asked to make sure you knew. The formatting this code is doing has no effect on what you see in the parameters window.

 

Try this code and have a look at your custom Properties. You'll se it formats the property that is created when the parameter is exported.

 

Dim oDoc As PartDocument = ThisDoc.Document
Dim oParam As Inventor.Parameter
Try
	oParam = oDoc.ComponentDefinition.Parameters.UserParameters.Item("xLength")
	oParam.Expression = "1.12345678 mm" '8 decimals
Catch
	oParam = oDoc.ComponentDefinition.Parameters.UserParameters.AddByExpression("xLength", "1.12345678 mm", "mm")
End Try

oParam.ExposedAsProperty = True

Dim oFormat As CustomPropertyFormat = oParam.CustomPropertyFormat 'This is the format of the property, not the parameter
oFormat.PropertyType = CustomPropertyTypeEnum.kNumberPropertyType

'-----------------Try switching between these and see the difference--------------------------
oFormat.Precision = CustomPropertyPrecisionEnum.kZeroDecimalPlacePrecision
'oFormat.Precision = CustomPropertyPrecisionEnum.kEightDecimalPlacesPrecision
'---------------------------------------------------------------------------------------------

iLogicVB.UpdateWhenDone = True

 

If you just want to change what's in the parameters window though, you can set the expression of the parameter with a string.

 

See the difference between these two:

Dim oDoc As PartDocument = ThisDoc.Document
Dim oParam As Inventor.Parameter
Try
	oParam = oDoc.ComponentDefinition.Parameters.UserParameters.Item("xLength")
	oParam.Expression = "0 mm" '0 decimals
Catch
	oParam = oDoc.ComponentDefinition.Parameters.UserParameters.AddByExpression("xLength", "0 mm", "mm")
End Try

And

Dim oDoc As PartDocument = ThisDoc.Document
Dim oParam As Inventor.Parameter
Try
	oParam = oDoc.ComponentDefinition.Parameters.UserParameters.Item("xLength")
	oParam.Expression = "0.000000 mm" '8 decimals
Catch
	oParam = oDoc.ComponentDefinition.Parameters.UserParameters.AddByExpression("xLength", "0.000000 mm", "mm")
End Try

If Ive misinterpreted what you want to accomplish, could you do manually what you want the code to do and take a screenshot of the result so I'll understand once and for all? 😆

Message 16 of 17

arkelec
Collaborator
Collaborator
Sorry for the delay in replying, when I ran your full code, it worked. The syntax is a bit different to what I was using, which also threw me.

The export option is something I need to look into a bit more, to understand the behaviour.

Anyway, it's working now.

Thanks.
Message 17 of 17

arkelec
Collaborator
Collaborator
Yes, thanks for posting that.
I was the one who misunderstood the initial responses.