Page 1 of 1

Macro to change Decimal Length

Posted: Thu Mar 09, 2023 1:31 am
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.

Re: Macro to change Decimal Length

Posted: Thu Mar 09, 2023 8:15 am
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

Re: Macro to change Decimal Length

Posted: Thu Mar 09, 2023 8:55 am
by SPerman
Is it possible this setting is somehow conflicting with the Units setting?
image.png

Re: Macro to change Decimal Length

Posted: Thu Mar 09, 2023 9:58 am
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.

Re: Macro to change Decimal Length

Posted: Thu Mar 09, 2023 9:59 am
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.

Re: Macro to change Decimal Length

Posted: Thu Mar 09, 2023 10:13 am
by SPerman
I've never seen one conflict with the other, but I've never seen the document unit precision change either.

Re: Macro to change Decimal Length

Posted: Thu Mar 09, 2023 7:31 pm
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.

Re: Macro to change Decimal Length

Posted: Thu Mar 09, 2023 7:37 pm
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.

Re: Macro to change Decimal Length

Posted: Mon Apr 17, 2023 12:03 pm
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

Re: Macro to change Decimal Length

Posted: Mon Apr 17, 2023 6:21 pm
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


Re: Macro to change Decimal Length

Posted: Tue Aug 08, 2023 6:35 pm
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

Re: Macro to change Decimal Length

Posted: Wed Aug 09, 2023 8:25 am
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

Re: Macro to change Decimal Length

Posted: Fri Aug 11, 2023 11:51 am
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
"