Macro to change Decimal Length

Tera
Posts: 225
Joined: Fri Mar 19, 2021 4:58 am
Answers: 2
x 483
x 91

Macro to change Decimal Length

Unread post by Tera »

No matter how many times I set the length of Decimals to .12 but after a while it reverts to .123456
-
2023-03-09_15-23-44.jpg
-

The templates I use are set to .12 but as soon as I add several features or edit an existing feature, it goes back to .123456

Now 2 questions:
1- Does anybody know why it happens or how to prevent it?
2- I'm tired of opening the options and correct it every several minutes. Is there a macro that can change it to .12

I appreciate any kind of help/advice.
by AlexB » Thu Mar 09, 2023 8:15 am
1. As you mentioned, this is a document template setting so every new part should match your template. I've never seen this change as I'm working on something without me explicitly accessing the menu and changing it.

2. Yes, it's possible. The code below should set it to 2 for your active document. Note that this only affects the linear "Length" decimal places that you've circled in your screenshot.

Go to Tools -> Macro -> New and delete everything in the new macro document and paste the below code in there and save.

Code: Select all

Option Explicit
Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As ModelDoc2
    Dim swModelExtension As ModelDocExtension
    
    Set swApp = Application.SldWorks '
    Set swModel = swApp.ActiveDoc
    
    If Not swModel Is Nothing Then
        Set swModelExtension = swModel.Extension
        swModelExtension.SetUserPreferenceInteger swUserPreferenceIntegerValue_e.swUnitsLinearDecimalPlaces, swUserPreferenceOption_e.swDetailingNoOptionSpecified, 2
    End If
End Sub
Go to full post
User avatar
AlexB
Posts: 498
Joined: Thu Mar 18, 2021 1:38 pm
Answers: 28
x 266
x 444

Re: Macro to change Decimal Length

Unread post by AlexB »

1. As you mentioned, this is a document template setting so every new part should match your template. I've never seen this change as I'm working on something without me explicitly accessing the menu and changing it.

2. Yes, it's possible. The code below should set it to 2 for your active document. Note that this only affects the linear "Length" decimal places that you've circled in your screenshot.

Go to Tools -> Macro -> New and delete everything in the new macro document and paste the below code in there and save.

Code: Select all

Option Explicit
Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As ModelDoc2
    Dim swModelExtension As ModelDocExtension
    
    Set swApp = Application.SldWorks '
    Set swModel = swApp.ActiveDoc
    
    If Not swModel Is Nothing Then
        Set swModelExtension = swModel.Extension
        swModelExtension.SetUserPreferenceInteger swUserPreferenceIntegerValue_e.swUnitsLinearDecimalPlaces, swUserPreferenceOption_e.swDetailingNoOptionSpecified, 2
    End If
End Sub
User avatar
SPerman
Posts: 2040
Joined: Wed Mar 17, 2021 4:24 pm
Answers: 14
x 2212
x 1863
Contact:

Re: Macro to change Decimal Length

Unread post by SPerman »

Is it possible this setting is somehow conflicting with the Units setting?
image.png
-
I may not have gone where I intended to go, but I think I have ended up where I needed to be. -Douglas Adams
TTevolve
Posts: 252
Joined: Wed Jan 05, 2022 10:15 am
Answers: 3
x 84
x 159

Re: Macro to change Decimal Length

Unread post by TTevolve »

Yea, the units precision is usually not much of an issue, it's the setting under dimensions where you usually have to change it on drawings. I wouldn't want to design anything with a 2 decimal precision, everything rounding to the nearest 10th doesn't sound like a great idea.
User avatar
AlexB
Posts: 498
Joined: Thu Mar 18, 2021 1:38 pm
Answers: 28
x 266
x 444

Re: Macro to change Decimal Length

Unread post by AlexB »

SPerman wrote: Thu Mar 09, 2023 8:55 am Is it possible this setting is somehow conflicting with the Units setting?
In my short testing, one appears to control the display dimension precision and the other controls the decimal places of the dimension in other places in the UI like the Edit Dimension window.
User avatar
SPerman
Posts: 2040
Joined: Wed Mar 17, 2021 4:24 pm
Answers: 14
x 2212
x 1863
Contact:

Re: Macro to change Decimal Length

Unread post by SPerman »

I've never seen one conflict with the other, but I've never seen the document unit precision change either.
-
I may not have gone where I intended to go, but I think I have ended up where I needed to be. -Douglas Adams
Tera
Posts: 225
Joined: Fri Mar 19, 2021 4:58 am
Answers: 2
x 483
x 91

Re: Macro to change Decimal Length

Unread post by Tera »

AlexB wrote: Thu Mar 09, 2023 8:15 am 1. As you mentioned, this is a document template setting so every new part should match your template. I've never seen this change as I'm working on something without me explicitly accessing the menu and changing it.

2. Yes, it's possible. The code below should set it to 2 for your active document. Note that this only affects the linear "Length" decimal places that you've circled in your screenshot.

Go to Tools -> Macro -> New and delete everything in the new macro document and paste the below code in there and save.

Code: Select all

Option Explicit
Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As ModelDoc2
    Dim swModelExtension As ModelDocExtension
    
    Set swApp = Application.SldWorks '
    Set swModel = swApp.ActiveDoc
    
    If Not swModel Is Nothing Then
        Set swModelExtension = swModel.Extension
        swModelExtension.SetUserPreferenceInteger swUserPreferenceIntegerValue_e.swUnitsLinearDecimalPlaces, swUserPreferenceOption_e.swDetailingNoOptionSpecified, 2
    End If
End Sub
The macro works just fine.
I really appreciate your help.
Thank you.
Tera
Posts: 225
Joined: Fri Mar 19, 2021 4:58 am
Answers: 2
x 483
x 91

Re: Macro to change Decimal Length

Unread post by Tera »

Thanks to all. For now the macro works great.
I will have an eye on the timing this setting is changed and drill down what is causing this.
Will update this thread if I find anything on this.

Thanks again.
User avatar
Conklin
Posts: 50
Joined: Fri Mar 26, 2021 3:34 pm
Answers: 0
x 10
x 21

Re: Macro to change Decimal Length

Unread post by Conklin »

Hi,
I would like to have a macro (run on a drawing document) change from this:
Document Properties - Dimensions 3 PL.jpg
to this:
Document Properties - Dimensions 2 PL.jpg
Code as posted previously:

Code: Select all

Option Explicit
Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As ModelDoc2
    Dim swModelExtension As ModelDocExtension
    
    Set swApp = Application.SldWorks '
    Set swModel = swApp.ActiveDoc
    
    If Not swModel Is Nothing Then
        Set swModelExtension = swModel.Extension
        swModelExtension.SetUserPreferenceInteger swUserPreferenceIntegerValue_e.swUnitsLinearDecimalPlaces, swUserPreferenceOption_e.swDetailingNoOptionSpecified, 2
    End If
End Sub
I run the posted code, I get this:
Document Properties - Dimensions code results A.jpg
I changed the 2 to a 1 in the above code:
change to 1.jpg
Run it and I get this:
Document Properties - Dimensions code results B.jpg
I am not sure how to do this.
edit:
I would like it to change the Primary & Dual precision. Not the units "Decimals"
Any help would be appreciated.
Thanks
User avatar
zwei
Posts: 701
Joined: Mon Mar 15, 2021 9:17 pm
Answers: 18
Location: Malaysia
x 185
x 600

Re: Macro to change Decimal Length

Unread post by zwei »

Conklin wrote: Mon Apr 17, 2023 12:03 pm I would like it to change the Primary & Dual precision. Not the units "Decimals"
Any help would be appreciated.
Thanks
swUserPreferenceIntegerValue_e.swUnitsLinearDecimalPlaces is for properties under Unit page in Document Properties

If you are planning to change the precision under Dimensions page in Document properties, you will need to use
swUserPreferenceIntegerValue_e.swDetailingLinearDimPrecision and swUserPreferenceIntegerValue_e.swDetailingAltLinearDimPrecision

Ref:
https://help.solidworks.com/2016/englis ... nsions.htm

See code below:

Code: Select all

Option Explicit
Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As ModelDoc2
    Dim swModelExtension As ModelDocExtension
    
    Set swApp = Application.SldWorks '
    Set swModel = swApp.ActiveDoc
    
    If Not swModel Is Nothing Then
        Set swModelExtension = swModel.Extension
        swModelExtension.SetUserPreferenceInteger swUserPreferenceIntegerValue_e.swDetailingLinearDimPrecision, swUserPreferenceOption_e.swDetailingDimension, 2
        swModelExtension.SetUserPreferenceInteger swUserPreferenceIntegerValue_e.swDetailingAltLinearDimPrecision, swUserPreferenceOption_e.swDetailingDimension, 2
    End If
End Sub

Far too many items in the world are designed, constructed and foisted upon us with no understanding-or even care-for how we will use them.
DDDENGR
Posts: 1
Joined: Tue Aug 08, 2023 6:29 pm
Answers: 0

Re: Macro to change Decimal Length

Unread post by DDDENGR »

EXACT same issue. One that I've been fighting since upgrade from 2022 to 2023. You name it we tried it. The macro works but since it happens nearly 2000000 times an hour it was not a viable option for me.

I think it's fixed. Fingers crossed. Two days ago I completely uninstalled SW. Erased all SW registry entries. Re-installed and it's been performing as it should. It did burp once, but I think that was a legacy from a previous model.

You must preform a "clean" uninstall. Find instructions from your VAR and follow them exactly.

Good luck

DDD
john@layketool.com
Posts: 36
Joined: Wed Jul 28, 2021 9:09 am
Answers: 0
x 1
x 31

Re: Macro to change Decimal Length

Unread post by john@layketool.com »

I have had the same issue. There was a bug introduced in 2023 that would change the decimal precision to 6 decimal places at random times and wasn't anything to do with any of the user settings. It appears that they have found that bug and fixed it in 2023 SP3. I have not had any issues with this since updating to 2023 SP3
cagatay85aktas
Posts: 1
Joined: Tue Aug 01, 2023 12:06 am
Answers: 0

Re: Macro to change Decimal Length

Unread post by cagatay85aktas »

hii

How do we change the unit of the parts in the assembly, what kind of macro should be

"
Const UNIT_SYSTEM As Integer = swUnitSystem_e.swUnitSystem_MMGS 'sets the custom units individually as per the constants below

Const MMGS_LENGTH_UNIT As Integer = swLengthUnit_e.swMETER
Const MMGS_ANGLE_UNIT As Integer = swAngleUnit_e.swDEGREES
Const MMGS_MASS_UNIT As Integer = swUnitsMassPropMass_e.swUnitsMassPropMass_Grams
Const MMGS_VOLUME_UNIT As Integer = swUnitsMassPropVolume_e.swUnitsMassPropVolume_Millimeters3
Const MMGS_TIME_UNIT As Integer = swUnitsTimeUnit_e.swUnitsTimeUnit_Second
Dim swApp As SldWorks.SldWorks

Sub main()

Set swApp = Application.SldWorks

Dim swModel As SldWorks.ModelDoc2

Set swModel = swApp.ActiveDoc

If Not swModel Is Nothing Then

swModel.Extension.SetUserPreferenceInteger swUserPreferenceIntegerValue_e.swUnitSystem, swUserPreferenceOption_e.swDetailingNoOptionSpecified, UNIT_SYSTEM

If UNIT_SYSTEM = swUnitSystem_e.swUnitSystem_Custom Then

swModel.Extension.SetUserPreferenceInteger swUserPreferenceIntegerValue_e.swUnitsLinear, swUserPreferenceOption_e.swDetailingNoOptionSpecified, CUSTOM_LENGTH_UNIT
swModel.Extension.SetUserPreferenceInteger swUserPreferenceIntegerValue_e.swUnitsDualLinear, swUserPreferenceOption_e.swDetailingNoOptionSpecified, CUSTOM_LENGTH_UNIT

swModel.Extension.SetUserPreferenceInteger swUserPreferenceIntegerValue_e.swUnitsAngular, swUserPreferenceOption_e.swDetailingNoOptionSpecified, CUSTOM_ANGLE_UNIT

swModel.Extension.SetUserPreferenceInteger swUserPreferenceIntegerValue_e.swUnitsMassPropLength, swUserPreferenceOption_e.swDetailingNoOptionSpecified, CUSTOM_LENGTH_UNIT
swModel.Extension.SetUserPreferenceInteger swUserPreferenceIntegerValue_e.swUnitsMassPropMass, swUserPreferenceOption_e.swDetailingNoOptionSpecified, CUSTOM_MASS_UNIT
swModel.Extension.SetUserPreferenceInteger swUserPreferenceIntegerValue_e.swUnitsMassPropVolume, swUserPreferenceOption_e.swDetailingNoOptionSpecified, CUSTOM_VOLUME_UNIT

swModel.Extension.SetUserPreferenceInteger swUserPreferenceIntegerValue_e.swUnitsTimeUnits, swUserPreferenceOption_e.swDetailingNoOptionSpecified, CUSTOM_TIME_UNIT

End If

Else
Err.Raise vbError, "", "Model is not opened"
End If

End Sub
"
Post Reply