Need Macro to change length units

Library for macros
User avatar
Jaylin Hochstetler
Posts: 383
Joined: Sat Mar 13, 2021 8:47 pm
Answers: 4
Location: Michigan
x 375
x 353
Contact:

Need Macro to change length units

Unread post 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!
A goal is only a wish until backed by a plan.
artem
Posts: 26
Joined: Thu Mar 18, 2021 1:31 pm
Answers: 3
x 9
x 73

Re: Need Macro to change length units

Unread post 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.
Thanks,
Artem
xarial.com - making your CAD better
codestack.net - SOLIDWORKS API macros and tutorials
berg_lauritz
Posts: 423
Joined: Tue Mar 09, 2021 10:11 am
Answers: 6
x 441
x 235

Re: Need Macro to change length units

Unread post by berg_lauritz »

Would this work on a PDM system too @artem ?
artem
Posts: 26
Joined: Thu Mar 18, 2021 1:31 pm
Answers: 3
x 9
x 73

Re: Need Macro to change length units

Unread post 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.
Thanks,
Artem
xarial.com - making your CAD better
codestack.net - SOLIDWORKS API macros and tutorials
User avatar
Jaylin Hochstetler
Posts: 383
Joined: Sat Mar 13, 2021 8:47 pm
Answers: 4
Location: Michigan
x 375
x 353
Contact:

Re: Need Macro to change length units

Unread post 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!
A goal is only a wish until backed by a plan.
artem
Posts: 26
Joined: Thu Mar 18, 2021 1:31 pm
Answers: 3
x 9
x 73

Re: Need Macro to change length units

Unread post 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.
Thanks,
Artem
xarial.com - making your CAD better
codestack.net - SOLIDWORKS API macros and tutorials
User avatar
Jaylin Hochstetler
Posts: 383
Joined: Sat Mar 13, 2021 8:47 pm
Answers: 4
Location: Michigan
x 375
x 353
Contact:

Re: Need Macro to change length units

Unread post 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.
A goal is only a wish until backed by a plan.
User avatar
loeb
Posts: 47
Joined: Sun Jan 16, 2022 5:55 pm
Answers: 1
x 35
x 8

Re: Need Macro to change length units

Unread post 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
User avatar
gupta9665
Posts: 359
Joined: Thu Mar 11, 2021 10:20 am
Answers: 20
Location: India
x 383
x 414

Re: Need Macro to change length units

Unread post 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.
Deepak Gupta
SOLIDWORKS Consultant/Blogger
User avatar
loeb
Posts: 47
Joined: Sun Jan 16, 2022 5:55 pm
Answers: 1
x 35
x 8

Re: Need Macro to change length units

Unread post 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:
Attachments
Temp.SLDPRT
(198.72 KiB) Downloaded 50 times
User avatar
gupta9665
Posts: 359
Joined: Thu Mar 11, 2021 10:20 am
Answers: 20
Location: India
x 383
x 414

Re: Need Macro to change length units

Unread post by gupta9665 »

No changes on my end. Can you please share a video showing the concern.
Deepak Gupta
SOLIDWORKS Consultant/Blogger
User avatar
loeb
Posts: 47
Joined: Sun Jan 16, 2022 5:55 pm
Answers: 1
x 35
x 8

Re: Need Macro to change length units

Unread post 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:
Attachments
Macro1.swp
(46.5 KiB) Downloaded 76 times
User avatar
gupta9665
Posts: 359
Joined: Thu Mar 11, 2021 10:20 am
Answers: 20
Location: India
x 383
x 414

Re: Need Macro to change length units

Unread post 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?
Deepak Gupta
SOLIDWORKS Consultant/Blogger
MaineSpring
Posts: 9
Joined: Wed Apr 07, 2021 1:04 pm
Answers: 0
Location: Olar, South Carolina
x 3
Contact:

Re: Need Macro to change length units

Unread post 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?
Post Reply