Change user parameter - only length units

Change user parameter - only length units

DewayneH
Advocate Advocate
2,838 Views
12 Replies
Message 1 of 13

Change user parameter - only length units

DewayneH
Advocate
Advocate

 

I'm working on a rule to change the document units and the user parameter "length units" to either inch or mm.

 

I borrowed Curtis Waguespack code "Another Variation 8-18-2014" from  http://inventortrenches.blogspot.com/2012/05/ilogic-rule-to-change-units-of-measure.html. 

 

It handles the document units beautifully, but it changes all the user parameters to inch or mm, even if they are of other units.

 

In my case, I need to identify the user parameter as being a length unit and change the unit accordingly.

I see this topic has been kicked around a bit, bu with my very limited knowledge, I couldn't put a solution together.

 

I'm sure the solution is easy for the experts.

 

Here is my latest attempt....

 

'------- start of ilogic ------
question = MessageBox.Show("This process changes the unit of measure setting of the document. Completing this action in an assembly file will attempt to modify all files contained in the assembly. Are you sure you want to change the documents unit of measure?", _
"Change Document Units",MessageBoxButtons.YesNo,MessageBoxIcon.Warning)

If question = vbNo Then
Return
Else

            'get input from user
                oUnit = InputRadioBox("Select a unit of measure", "Metric", "Imperial", True, "Unit of Measure")
           
            'create precision value list
                oPrecisionArray = New String(){0, 1, 2, 3, 4, 5}

                'get input from user
                oPrecision = InputListBox("Select the number of decimal places to use for the units of length display.",  _
                oPrecisionArray, 3, "Precision", "Decimal Places (Display Only)")
           
            'example UnitsTypeEnum Enumerators
                'kCentimeterLengthUnits = 11268
                'kMillimeterLengthUnits = 11269
                'kInchLengthUnits = 11272
               
                'kKilogramMassUnits = 11283
                'kGramMassUnits = 11284
                'kLbMassMassUnits = 11286
               
                        If oUnit = True Then
                        'set to millimeter
                                oUOM_1 = 11269
                        'set to kilogram
                                oUOM_2 = 11283
                        Else
                        'set to inch
                                oUOM_1 = 11272
                        'set to pounds mass
                                oUOM_2 = 11286                        
                        End If

            'Define the open document
                Dim openDoc As Document
                openDoc = ThisDoc.Document
'I added-------------------------------------
				Dim oParameter As Parameter 
'---------------------------------------------				
            'set length units for the top level assembly
                openDoc.unitsofmeasure.LengthUnits = oUOM_1
                'set mass units for the top level assembly
                openDoc.unitsofmeasure.MassUnits = oUOM_2
                'set precision
                openDoc.unitsofmeasure.LengthDisplayPrecision = oPrecision
				
'I added - Set user param unit equal To document units------------------
					For Each oParameter In openDoc.ComponentDefinition.Parameters.UserParameters
									
						If oParameter.Units = 11269 Or oParameter.Units = 11272 Then
						oParameter.Units = oUOM_1 
						
						End If
					
					Next oParameter				
'------------------------------------------------------------------------               
			   
                'Look at all of the files referenced in the open document
                Dim docFile As Document
                For Each docFile In openDoc.AllReferencedDocuments 
							
				
'I added. Checks if file is able to be modified and skips if not. (Works)---------------------------------
						If docFile.IsModifiable = False Then 'skip
						Else If docFile.IsModifiable = True Then
'--------------------------------------------------------------------------------------------------------
																							
                                'format  file name                   
                                Dim FNamePos As Long
                                FNamePos = InStrRev(docFile.FullFileName, "\", -1)                        
                        Dim docFName As String 
                        docFName = Right(docFile.FullFileName, Len(docFile.FullFileName) - FNamePos)      
								'set length units
                                docFile.unitsofmeasure.LengthUnits = oUOM_1
                                'set mass units
                                docFile.unitsofmeasure.MassUnits = oUOM_2
                                'set precision
                                docFile.unitsofmeasure.LengthDisplayPrecision = oPrecision
								
'I added Change all user parameters units To match document units-------------------------------------
									For Each oParameter In docfile.ComponentDefinition.Parameters.UserParameters  'openDoc.ComponentDefinition.Parameters.UserParameters
									
										If oParameter.Units = 11269 Or oParameter.Units = 11272 Then 
										oParameter.Units = oUOM_1 
										
										End If
										
									Next oParameter				
'----------------------------------------------------------------------------------------------------
								
								'rebuild to update the display
								docFile.Rebuild
								
								End If
                Next    
End If
'update all
iLogicVb.UpdateWhenDone = True

'------- end of ilogic ------

 

Dewayne
Inventor Pro 2023
Vault Pro 2023
0 Likes
Accepted solutions (1)
2,839 Views
12 Replies
Replies (12)
Message 2 of 13

Curtis_Waguespack
Consultant
Consultant

Hi @DewayneH 

 

Give this version a try.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

Sub main
		
	iLogicVb.UpdateWhenDone = True
	
	oCurrentUnits = ThisDoc.Document.UnitsOfMeasure.LengthUnits.ToString
	oCurrentUnits = (oCurrentUnits = "kMillimeterLengthUnits") 'set inputbox default
	
	'Get Input From user
	oUnit = InputRadioBox("Select a units of measure type", "Metric", "Imperial", oCurrentUnits, "ilogic")

	If oUnit = True Then
		'set to millimeter
		oUOM_1 = UnitsTypeEnum.kMillimeterLengthUnits
		'set to kilogram
		oUOM_2 = UnitsTypeEnum.kKilogramMassUnits
	Else
		'set to inch
		oUOM_1 = UnitsTypeEnum.kInchLengthUnits
		'set to pounds mass
		oUOM_2 = UnitsTypeEnum.kLbMassMassUnits                                
	End If

	'Define the open document
	Dim oDoc As Document
	oDoc = ThisDoc.Document

	'run sub to process top level file
	Call SetUnits(oDoc, oUOM_1, oUOM_2)


	'Look at all of the referenced documents
	For Each oDoc In ThisDoc.Document.AllReferencedDocuments
		If oDoc.IsModifiable = True Then    
			'run sub to process referenced file
			Call SetUnits(oDoc, oUOM_1, oUOM_2)
		End If
	Next 
End Sub


Sub SetUnits(oDoc As Document, oUOM_1 As UnitsTypeEnum, oUOM_2 As UnitsTypeEnum)

	'set length units 
	oDoc.UnitsOfMeasure.LengthUnits = oUOM_1
	'set mass units 
	oDoc.UnitsOfMeasure.MassUnits = oUOM_2

	'set list of  units
	Dim oLengthunits = New String() {"mm", "cm", "m", "in", "ft", "micron", "nauticalMile", "mil" }	
	Dim oMassunits = New String() {"g", "kg", "lbmass", "slug", "ouncemass" }	

	For Each oParameter In oDoc.ComponentDefinition.Parameters.UserParameters  

		If oLengthunits.Contains(oParameter.Units)  Then 
			oParameter.Units = oUOM_1 		
		End If
		
		If oMassunits.Contains(oParameter.Units)  Then 
			oParameter.Units = oUOM_2 		
		End If		
	Next	

End Sub

EESignature

0 Likes
Message 3 of 13

DewayneH
Advocate
Advocate

Curtis,

 

Thanks for taking a look.

 

I ran the rule and got an error immediately. The error occurs trying to set the parameter units.

I attempted to figure it out, but I got nothing.

 

 SystemArray Error.png

Dewayne
Inventor Pro 2023
Vault Pro 2023
0 Likes
Message 4 of 13

Curtis_Waguespack
Consultant
Consultant

Hi @DewayneH 

 

ok... well the long story is that error has to with the .net frame work you're running, but that's not worth worrying about... I think this should work for you, post back if it does not.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

Sub main
		
	iLogicVb.UpdateWhenDone = True
	
	oCurrentUnits = ThisDoc.Document.UnitsOfMeasure.LengthUnits.ToString
	oCurrentUnits = (oCurrentUnits = "kMillimeterLengthUnits") 'set inputbox default
	
	'Get Input From user
	oUnit = InputRadioBox("Select a units of measure type", "Metric", "Imperial", oCurrentUnits, "ilogic")

	If oUnit = True Then
		'set to millimeter
		oUOM_1 = UnitsTypeEnum.kMillimeterLengthUnits
		'set to kilogram
		oUOM_2 = UnitsTypeEnum.kKilogramMassUnits
	Else
		'set to inch
		oUOM_1 = UnitsTypeEnum.kInchLengthUnits
		'set to pounds mass
		oUOM_2 = UnitsTypeEnum.kLbMassMassUnits                                
	End If

	'Define the open document
	Dim oDoc As Document
	oDoc = ThisDoc.Document

	'run sub to process top level file
	Call SetUnits(oDoc, oUOM_1, oUOM_2)


	'Look at all of the referenced documents
	For Each oDoc In ThisDoc.Document.AllReferencedDocuments
		If oDoc.IsModifiable = True Then    
			'run sub to process referenced file
			Call SetUnits(oDoc, oUOM_1, oUOM_2)
		End If
	Next 
End Sub


Sub SetUnits(oDoc As Document, oUOM_1 As UnitsTypeEnum, oUOM_2 As UnitsTypeEnum)

	'set length units 
	oDoc.UnitsOfMeasure.LengthUnits = oUOM_1
	'set mass units 
	oDoc.UnitsOfMeasure.MassUnits = oUOM_2

	'set list of  units
	Dim oLengthunits = New String() {"mm", "cm", "m", "in", "ft", "micron", "nauticalMile", "mil" }	
	Dim oMassunits = New String() {"g", "kg", "lbmass", "slug", "ouncemass" }	
	Dim index As Integer

	For Each oParameter In oDoc.ComponentDefinition.Parameters.UserParameters  

		
		index = Array.IndexOf(oLengthunits, oParameter.Units)
		If Not index < 0 Then
		    oParameter.Units = oUOM_1 		
		End If
		
		index = Array.IndexOf(oMassunits, oParameter.Units)
		If Not index < 0 Then
		    oParameter.Units = oUOM_2 		
		End If	
	Next	

End Sub

EESignature

0 Likes
Message 5 of 13

DewayneH
Advocate
Advocate
Accepted solution

Curtis,

 

That did the trick. I added a message box to warn the user of the changes.

It is awesome. Thank you so much.

 

Dewayne
Inventor Pro 2023
Vault Pro 2023
Message 6 of 13

DewayneH
Advocate
Advocate

 

Curtis,

 

Sorry, I may have spoken too soon.

 

I initially tested the rule on four different data sets. All the units updated just as needed on the assembly and the lower level parts. It was beautiful.

 

I was doing some final testing and received an error when I rule ran. Naturally I thought I goofed up your code, so I started over with a fresh version and still got the error. This is the only rule running, and I'm applying it exactly as before, but not getting the same result.

 

Any ideas?

 

Fresh Curtis Rule Error.png

 

 

Dewayne
Inventor Pro 2023
Vault Pro 2023
0 Likes
Message 7 of 13

Curtis_Waguespack
Consultant
Consultant

Hi @DewayneH 

 

Unfortunately the error message is pretty generic, so it doesn't provide much of a clue... can you zip and attach the assembly? or a sample assembly that exhibits the issue?

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

EESignature

0 Likes
Message 8 of 13

DewayneH
Advocate
Advocate

Hi Curtis,

 

The attached zip contains an assembly with a part that exhibits the behavior.

 

Dewayne
Inventor Pro 2023
Vault Pro 2023
0 Likes
Message 9 of 13

Curtis_Waguespack
Consultant
Consultant

Hi @DewayneH , Unfortunately the rule ran for me without issue in that that assembly, so I was not able to see the issue.

 

 

EESignature

0 Likes
Message 10 of 13

Curtis_Waguespack
Consultant
Consultant

...

EESignature

0 Likes
Message 11 of 13

DewayneH
Advocate
Advocate

Curtis,

 

I believe your last post was intend for someone else.

Dewayne
Inventor Pro 2023
Vault Pro 2023
0 Likes
Message 12 of 13

Curtis_Waguespack
Consultant
Consultant

@DewayneH wrote:

Curtis,

 

I believe your last post was intend for someone else.


lol, thanks ... indeed I think I got confused on which thread I was replying to.... thanks

EESignature

0 Likes
Message 13 of 13

DewayneH
Advocate
Advocate

Curtis,

 

Your original fix was the solution.

We had a false alarm. It turns out the error was totally unrelated to the rule or any rule for that matter.

I knew it was working way to well to just stop functioning. I was working on something unrelated and a rule I've used for years caused the same error. That set off bells and whistles.

It appears I lost a directory in my ilogic configuration. Once I added the directory all the rules began working properly.

 

Thanks again for your help.

Dewayne
Inventor Pro 2023
Vault Pro 2023