Monday, May 14, 2012

ilogic Rule To Change Units Of Measure



Issue:
You've created some of your parts in inches and others in millimeters, but now you'd like to convert them all to be the same. Is there a quick way to do this without having to open each file go to the Tools tab > Document Settings button > Units tab?


Solution:
Here's an ilogic rule that can be run in an assembly or a part file that will set the units of measure. In this rule the length and mass units are being set. Set this up as an external iLogic rule to have it available for use in all of your model files.

You can use these links to learn more about creating iLogic rules:


'------- start of ilogic ------
question = MessageBox.Show("Are you sure you want to change the units of measure?", _
"iLogic",MessageBoxButtons.YesNo)

if question = vbno then
Return
Else

            'get input from user
                oUnit = InputRadioBox("Select a units of measure type", "Metric", "Imperial", True, "ilogic")
           
            '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, "iLogic", "Decimal Places ")
           
            '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
            '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
               
                'Look at all of the files referenced in the open document
                Dim docFile As Document
                For Each docFile In openDoc.AllReferencedDocuments                
                                '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
                                'rebuild to update the display
                                docFile.Rebuild
                Next    
End if
'update all
iLogicVb.UpdateWhenDone = True

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


                   A Variation of this rule                   




'------- start of ilogic ------

'get input from user
oUnit = InputRadioBox("Select a units of measure type", "Metric", "Imperial", False, "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 openDoc As Document
openDoc = ThisDoc.Document

'Look at all of the files referenced in the open document
Dim docFile As Document
For Each docFile In openDoc.AllReferencedDocuments
                'look at only part files
                 If docFile.DocumentType = kPartDocumentObject 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
                'rebuild to update the display
                docFile.Rebuild
                End If
Next    
iLogicVb.UpdateWhenDone = True

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


                   Another Variation                               




'------- start of ilogic ------

'get input from user
oUnit = InputRadioBox("Select a units of measure type", "millimeter", "centimeter", True, "ilogic")

If oUnit = True then
'set to millimeter
oUOM_1 = UnitsTypeEnum.kMillimeterLengthUnits
Else
'set to centimeter
oUOM_1 = UnitsTypeEnum.kCentimeterLengthUnits
               
End if

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

'Look at all of the files referenced in the open document
Dim docFile As Document
For Each docFile In openDoc.AllReferencedDocuments
                'look at only part files
                 If docFile.DocumentType = kPartDocumentObject 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
                'rebuild to update the display
                docFile.Rebuild
                End If
Next    
iLogicVb.UpdateWhenDone = True

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


This next variation handles user parameters in the assembly and referenced part files also:
                   Another Variation    8-18-2014                  



'------- start of ilogic ------
'get input from user
oUnit = InputRadioBox("Select a units of measure type", "Metric", "Imperial", True, "ilogic")
         
'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
'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 user parameters
Dim oParameter As Parameter
For Each oParameter In openDoc.ComponentDefinition.Parameters.UserParameters
oParameter.Units = oUOM_1
Next oParameter

'Look at all of the files referenced in the open document
Dim docFile As Document
For Each docFile In openDoc.AllReferencedDocuments
'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 user parameters
Dim oParam As Parameter
For Each oParam In docFile.ComponentDefinition.Parameters.UserParameters
oParam.Units = oUOM_1
Next oParam

'rebuild to update the display
docFile.Rebuild
Next   

'update all
iLogicVb.UpdateWhenDone = True
'------- end of ilogic ------