Page 1 of 1

Need Macro to change length units

Posted: Tue May 04, 2021 5:00 pm
by Jaylin Hochstetler
I need a macro to open each component in an assembly and change the document units to 0.123.
I tried but my programming knowledge is simply too limited.

Thanks in advance!

Re: Need Macro to change length units

Posted: Wed May 05, 2021 7:19 am
by artem
Macro recorded should record this for modification of a single part. Then you can run this macro for all components in the assembly using Batch+: https://cadplus.xarial.com/batch/assembly/ (just test it on a spare part before running batch to make sure it is modifying units correctly). Here is the video: The video of a newer version (private beta) so UI may differ a little.

Re: Need Macro to change length units

Posted: Wed May 05, 2021 11:50 am
by berg_lauritz
Would this work on a PDM system too @artem ?

Re: Need Macro to change length units

Posted: Wed May 05, 2021 6:54 pm
by artem
@berg_lauritz, yes, as long as the files are locally cached. The future version will have full support for PDM so no need to cache files locally.

Re: Need Macro to change length units

Posted: Thu May 06, 2021 6:47 am
by Jaylin Hochstetler
@artem I installed CAD+. This is a wonderful tool! Then I recorded a macro to change my units and ran a batch to change all of the units. I had to make the macro save the file after changing the units b/c for some reason the files would fail if I told CAD+ to save it. But, hey, it worked!

Re: Need Macro to change length units

Posted: Thu May 06, 2021 7:01 pm
by artem
@Jaylin Hochstetler , great, glad it helped. Do you remember what was the error when you check 'Automatically Save'? I am now building a new version of CAD+ and will be keen to fix this issue as well.

Re: Need Macro to change length units

Posted: Fri May 07, 2021 6:43 am
by Jaylin Hochstetler
artem wrote: Thu May 06, 2021 7:01 pm @Jaylin Hochstetler , great, glad it helped. Do you remember what was the error when you check 'Automatically Save'? I am now building a new version of CAD+ and will be keen to fix this issue as well.
It didn't give me any errors but in the summary it said the files failed. And if I went into the files I ran the macro on the units weren't changed. Does CAD+ only save it if the SW gives a save warning? The reason I am asking is b/c I noticed if I change the units and close the document it won't give a save warning neither will it save it.

Re: Need Macro to change length units

Posted: Sat Feb 12, 2022 5:37 pm
by loeb
I have the following code that works on one file. It works, but has the strange effect of turning on Scene Shadows and Reflections for inactive configurations. Here's the code

Code: Select all

Option Explicit
Sub main()
    Dim swApp                       As SldWorks.SldWorks
    Dim swModel                     As SldWorks.ModelDoc2
    Dim BoolStatus                  As Boolean
    
    Dim SystemOfUnits               As Integer
    Dim DualSystemOfUnits           As Integer
    Dim PrimaryLengthDecPlaces      As Integer
    Dim AngularUnits                As Integer
    Dim MassDecPlaces               As Integer
    Dim DualLengthDecPlaces         As Integer
    Dim AngularDecPlaces            As Integer
    Dim DecimalRoundingMethod       As Integer
    Dim TimeDecPlaces               As Integer
    
    SystemOfUnits = swUnitSystem_e.swUnitSystem_IPS
    DualSystemOfUnits = swMM
    PrimaryLengthDecPlaces = 3
    DualLengthDecPlaces = 1
    AngularUnits = swDEGREES
    AngularDecPlaces = 0
    MassDecPlaces = 3
    DecimalRoundingMethod = swUnitsDecimalRounding_e.swUnitsDecimalRounding_HalfAway
    TimeDecPlaces = 2
    
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    If swModel Is Nothing Then
        swApp.SendMsgToUser2 "No drawing document open.", swMessageBoxIcon_e.swMbStop, swMessageBoxBtn_e.swMbOkCancel
        Exit Sub
    End If

    BoolStatus = swModel.Extension.SetUserPreferenceInteger(swUserPreferenceIntegerValue_e.swUnitSystem, 0, SystemOfUnits)
    BoolStatus = swModel.Extension.SetUserPreferenceInteger(swUserPreferenceIntegerValue_e.swUnitsDualLinear, 0, DualSystemOfUnits)
    BoolStatus = swModel.Extension.SetUserPreferenceInteger(swUserPreferenceIntegerValue_e.swUnitsLinearDecimalPlaces, 0, PrimaryLengthDecPlaces)
    BoolStatus = swModel.Extension.SetUserPreferenceInteger(swUserPreferenceIntegerValue_e.swUnitsDualLinearDecimalPlaces, 0, DualLengthDecPlaces)
    BoolStatus = swModel.Extension.SetUserPreferenceInteger(swUserPreferenceIntegerValue_e.swUnitsAngular, 0, AngularUnits)
    BoolStatus = swModel.Extension.SetUserPreferenceInteger(swUserPreferenceIntegerValue_e.swUnitsAngularDecimalPlaces, 0, AngularDecPlaces)
    BoolStatus = swModel.Extension.SetUserPreferenceInteger(swUserPreferenceIntegerValue_e.swUnitsMassPropDecimalPlaces, 0, MassDecPlaces)
    BoolStatus = swModel.Extension.SetUserPreferenceInteger(swUserPreferenceIntegerValue_e.swUnitsDecimalRounding, 0, DecimalRoundingMethod)
End Sub

Re: Need Macro to change length units

Posted: Sun Feb 13, 2022 2:34 am
by gupta9665
loebotomy@gmail.com wrote: Sat Feb 12, 2022 5:37 pm I have the following code that works on one file. It works, but has the strange effect of turning on Scene Shadows and Reflections for inactive configurations.
Could you attach your file to check.

Re: Need Macro to change length units

Posted: Sun Feb 13, 2022 3:34 am
by loeb
gupta9665 wrote: Sun Feb 13, 2022 2:34 am Could you attach your file to check.
Sure. Here's the part file I'm testing with:

Re: Need Macro to change length units

Posted: Sun Feb 13, 2022 6:10 am
by gupta9665
No changes on my end. Can you please share a video showing the concern.

Re: Need Macro to change length units

Posted: Sun Feb 13, 2022 6:04 pm
by loeb
gupta9665 wrote: Sun Feb 13, 2022 6:10 am No changes on my end. Can you please share a video showing the concern.
Try this swp with the part I attached earlier. Here it is:

Re: Need Macro to change length units

Posted: Mon Feb 14, 2022 12:27 am
by gupta9665
loebotomy@gmail.com wrote: Sun Feb 13, 2022 6:04 pm Try this swp with the part I attached earlier. Here it is:
Have used the cods you posted earlier and now even with this macro I see no changes in the model. Can you post pictures/video showing before and after?

Re: Need Macro to change length units

Posted: Thu Jun 09, 2022 11:37 am
by MaineSpring
Jaylin Hochstetler wrote: Tue May 04, 2021 5:00 pm I need a macro to open each component in an assembly and change the document units to 0.123.
I tried but my programming knowledge is simply too limited.

Thanks in advance!
I've been unsuccessfully looking for a similar application with a different function.: A simple macro (or something similar) to change all the documents' units of measure in an assembly to MMGS. Any suggestions or pointers?