Macro to set a basic dimension

This VBA macro works to set a dimension to basic

Dim swApp As Object

Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long

Sub main()

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc

'EditDimensionProperties2(TolType, TolMax, TolMin, TolMaxFit, TolMinFit, UseDocPrec, Precision, ArrowsIn, UseDocArrows, Arrow1, Arrow2, PrefixText, SuffixText, ShowValue, CalloutText1, CalloutText2, CenterText)

boolstatus = Part.EditDimensionProperties2(1, 0, 0, "", "", True, 9, 2, True, 12, 12, "", "", True, "", "", False)


Part.ClearSelection2 True

End Sub

However, you can see that it also sets the arrows and the suffix text for the selected dimension.

I need to read these values first from the SELECTED dimension, so I can insert the existing values into the "boolstatus" command.

Once I learn how to read all of the parameters for the command, I can then alter only one.

I have seen many web sites on how to loop thru all dimensions in a drawing (see below), but not the selected dimension.

Thanks,

Brian

================================

Option Explicit

Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swDraw As SldWorks.DrawingDoc
Dim swView As SldWorks.View
Dim swDispDim As SldWorks.DisplayDimension
Dim swDim As SldWorks.Dimension
Dim swAnn As SldWorks.Annotation

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swDraw = swModel

Debug.Print "File = " & swModel.GetPathName

Set swView = swDraw.GetFirstView

Do While Not swView Is Nothing


Debug.Print " View = " & swView.Name

Set swDispDim = swView.GetFirstDisplayDimension5

Do While Not swDispDim Is Nothing


Set swAnn = swDispDim.GetAnnotation
Set swDim = swDispDim.GetDimension

Debug.Print " ------------------------------------"


Debug.Print " AnnName = " & swAnn.GetName
Debug.Print " DimFullName = " & swDim.FullName
Debug.Print " DimName = " & swDim.Name
Debug.Print " swDimensionParamType_e type = " & swDim.GetType
Debug.Print " DrivenState = " & swDim.DrivenState
Debug.Print " ReadOnly = " & swDim.ReadOnly
Debug.Print " Value = " & swDim.GetSystemValue2("")
Debug.Print ""
Debug.Print " Arrowside = " & swDispDim.ArrowSide
Debug.Print " TextAll = " & swDispDim.GetText(swDimensionTextAll)
Debug.Print " TextPrefix = " & swDispDim.GetText(swDimensionTextPrefix)
Debug.Print " TextSuffix = " & swDispDim.GetText(swDimensionTextSuffix)
Debug.Print " CalloutAbove = " & swDispDim.GetText(swDimensionTextCalloutAbove)
Debug.Print " CalloutBelow = " & swDispDim.GetText(swDimensionTextCalloutBelow)

Set swDispDim = swDispDim.GetNext3

Loop

Set swView = swView.GetNextView

Loop


End Sub

SolidworksApi/macros
Answers 12
MS
MS 2021-04-09 Best Answer

Brian, here's an example of how to use the sample code with your code.  The key is using the SelectionManager interface to get the selected object.  

Dim swApp As SldWorks.SldWorks<br><br>Dim Part As ModelDoc2<br>Dim boolstatus As Boolean<br>Dim longstatus As Long, longwarnings As Long<br><br><br>Sub main()<br><br>Set swApp = Application.SldWorks<br><br>Set Part = swApp.ActiveDoc<br><br>Dim selMgr As SelectionMgr<br>Set selMgr = Part.SelectionManager<br><br>Dim swDispDim As DisplayDimension<br>Set swDispDim = selMgr.GetSelectedObject6(1, -1)<br>Dim swDIM As Dimension<br>Dim swAnn As Annotation<br><br>Set swAnn = swDispDim.GetAnnotation<br>Set swDIM = swDispDim.GetDimension<br><br>Debug.Print " ------------------------------------"<br><br>Debug.Print " AnnName = " & swAnn.GetName<br>Debug.Print " DimFullName = " & swDIM.FullName<br>Debug.Print " DimName = " & swDIM.Name<br>Debug.Print " swDimensionParamType_e type = " & swDIM.GetType<br>Debug.Print " DrivenState = " & swDIM.DrivenState<br>Debug.Print " ReadOnly = " & swDIM.ReadOnly<br>Debug.Print " Value = " & swDIM.GetSystemValue2("")<br>Debug.Print ""<br>Debug.Print " Arrowside = " & swDispDim.ArrowSide<br>Debug.Print " TextAll = " & swDispDim.GetText(swDimensionTextAll)<br>Debug.Print " TextPrefix = " & swDispDim.GetText(swDimensionTextPrefix)<br>Debug.Print " TextSuffix = " & swDispDim.GetText(swDimensionTextSuffix)<br>Debug.Print " CalloutAbove = " & swDispDim.GetText(swDimensionTextCalloutAbove)<br>Debug.Print " CalloutBelow = " & swDispDim.GetText(swDimensionTextCalloutBelow)<br><br>'EditDimensionProperties2(TolType, TolMax, TolMin, TolMaxFit, TolMinFit, UseDocPrec, Precision, ArrowsIn, UseDocArrows, Arrow1, Arrow2, PrefixText, SuffixText, ShowValue, CalloutText1, CalloutText2, CenterText)<br><br>boolstatus = Part.EditDimensionProperties2(1, 0, 0, "", "", True, 9, 2, True, 12, 12, "", "", True, "", "", False)<br><br>Part.ClearSelection2 True<br><br>End Sub<br><span class="line-numbers-rows"><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span></span>
BA
BA 2021-04-09

All done, thanks to Fifi.

The following code reads all of the variables, you can then change the value of one variable to change only that variable

===============================

Dim swApp As SldWorks.SldWorks
Dim Part As ModelDoc2
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim TolType, TolMax, TolMin, Precision, Arrow1, Arrow2 As Integer
Dim UseDocArrows, UseDocPrec, ShowValue, CenterText As Boolean
Dim TolMaxFit, TolMinFit, PrefixText, SuffixText, CalloutText1, CalloutText2 As String

Dim suffix1 As String

Sub main()

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc

Dim selMgr As SelectionMgr
Set selMgr = Part.SelectionManager

Dim swDispDim As DisplayDimension
Set swDispDim = selMgr.GetSelectedObject6(1, -1)
Dim swDIM As Dimension
Dim swAnn As Annotation
   
Set swAnn = swDispDim.GetAnnotation
Set swDIM = swDispDim.GetDimension
Dim swDimensionTolerance As SldWorks.DimensionTolerance
Set swDimensionTolerance = swDIM.Tolerance

TolType = swDimensionTolerance.Type ' 0 = normal, 1 = basic, 2 = display both plus/minus values, 3 = high/low, 4 = display plus/minus symbol, 5 = MIN, 6 = MAX
TolMax = swDimensionTolerance.GetMaxValue
TolMin = swDimensionTolerance.GetMinValue
TolMaxFit = swDimensionTolerance.GetHoleFitValue
TolMinFit = swDimensionTolerance.GetShaftFitValue

UseDocPrec = False

'Precision = 1 ' number of decimal places with DocPrec = False
Precision = swDispDim.GetPrimaryPrecision2
ArrowsIn = swDispDim.ArrowSide
UseDocArrows = True
Arrow1 = 12
Arrow2 = 12
PrefixText = swDispDim.GetText(swDimensionTextPrefix)
SuffixText = swDispDim.GetText(swDimensionTextSuffix)
ShowValue = True
CalloutText1 = swDispDim.GetText(swDimensionTextCalloutAbove)
CalloutText2 = swDispDim.GetText(swDimensionTextCalloutBelow)
CenterText = True


boolstatus = Part.EditDimensionProperties2(TolType, TolMax, TolMin, TolMaxFit, TolMinFit, UseDocPrec, Precision, ArrowsIn, UseDocArrows, Arrow1, Arrow2, PrefixText, SuffixText, ShowValue, CalloutText1, CalloutText2, CenterText)

Part.ClearSelection2 True


End Sub

BA
BA 2021-04-09

Fifi,

Thanks so much!  

After looking at my new code below, I also need to read the number of decimal places.  Can you reply with that code?   So sorry!  Last variable needed!

Dim swApp As SldWorks.SldWorks
Dim Part As ModelDoc2
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim TolType, TolMax, TolMin, Precision, Arrow1, Arrow2 As Integer
Dim UseDocArrows, UseDocPrec, ShowValue, CenterText As Boolean
Dim TolMaxFit, TolMinFit, PrefixText, SuffixText, CalloutText1, CalloutText2 As String

Dim suffix1 As String

Sub main()

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc

Dim selMgr As SelectionMgr
Set selMgr = Part.SelectionManager

Dim swDispDim As DisplayDimension
Set swDispDim = selMgr.GetSelectedObject6(1, -1)
Dim swDIM As Dimension
Dim swAnn As Annotation
   
Set swAnn = swDispDim.GetAnnotation
Set swDIM = swDispDim.GetDimension
Dim swDimensionTolerance As SldWorks.DimensionTolerance
Set swDimensionTolerance = swDIM.Tolerance

TolType = swDimensionTolerance.Type ' 0 = normal, 1 = basic, 2 = display both plus/minus values, 3 = high/low, 4 = display plus/minus symbol, 5 = MIN, 6 = MAX
TolMax = swDimensionTolerance.GetMaxValue
TolMin = swDimensionTolerance.GetMinValue
TolMaxFit = swDimensionTolerance.GetHoleFitValue
TolMinFit = swDimensionTolerance.GetShaftFitValue

UseDocPrec = False
Precision = 3 ' number of decimal places with DocPrec = False
ArrowsIn = swDispDim.ArrowSide
UseDocArrows = True
Arrow1 = 12
Arrow2 = 12
PrefixText = swDispDim.GetText(swDimensionTextPrefix)
SuffixText = swDispDim.GetText(swDimensionTextSuffix)
ShowValue = True
CalloutText1 = swDispDim.GetText(swDimensionTextCalloutAbove)
CalloutText2 = swDispDim.GetText(swDimensionTextCalloutBelow)
CenterText = True


boolstatus = Part.EditDimensionProperties2(TolType, TolMax, TolMin, TolMaxFit, TolMinFit, UseDocPrec, Precision, ArrowsIn, UseDocArrows, Arrow1, Arrow2, PrefixText, SuffixText, ShowValue, CalloutText1, CalloutText2, CenterText)

Part.ClearSelection2 True


End Sub

FR
FR 2021-04-09

Add this after set swDIM:

Dim swDimensionTolerance As SldWorks.DimensionTolerance
Set swDimensionTolerance = swDIM.Tolerance
Debug.Print "TolType: " & swDimensionTolerance.Type
Debug.Print "TolMax: " & swDimensionTolerance.GetMaxValue
Debug.Print "TolMin: " & swDimensionTolerance.GetMinValue
Debug.Print "TolMaxFit: " & swDimensionTolerance.GetHoleFitValue
Debug.Print "TolMinFit: " & swDimensionTolerance.GetShaftFitValue

BA
BA 2021-04-09

Thanks Fifi

I went back to using EditDimensionProperties2, and I was able to change the dimension to basic, by setting TolType = 1  (see below).

The code below is almost complete.

I just need to know the code to read from the selected dimension

- TolType, as an integer

- TolMax, as an integer

- TolMin, as an integer

- TolMaxFit, as string

- TolMinFit, as string

Brian

======================================

Dim swApp As SldWorks.SldWorks
Dim Part As ModelDoc2
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim TolType, TolMax, TolMin, Precision, Arrow1, Arrow2 As Integer
Dim UseDocArrows, UseDocPrec, ShowValue, CenterText As Boolean
Dim TolMaxFit, TolMinFit, PrefixText, SuffixText, CalloutText1, CalloutText2 As String

Dim suffix1 As String

Sub main()

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc

Dim selMgr As SelectionMgr
Set selMgr = Part.SelectionManager

Dim swDispDim As DisplayDimension
Set swDispDim = selMgr.GetSelectedObject6(1, -1)
Dim swDIM As Dimension
Dim swAnn As Annotation
   
Set swAnn = swDispDim.GetAnnotation
Set swDIM = swDispDim.GetDimension

'Debug.Print suffix1

TolType = 0 ' 0 = normal, 1 = basic, 2 = display both plus/minus values, 3 = high/low, 4 = display plus/minus symbol, 5 = MIN, 6 = MAX
TolMax = 0
TolMin = 0
TolMaxFit = ""
TolMinFit = ""
UseDocPrec = False
Precision = 3 ' number of decimal places with DocPrec = False
ArrowsIn = swDispDim.ArrowSide
UseDocArrows = True
Arrow1 = 12
Arrow2 = 12
PrefixText = swDispDim.GetText(swDimensionTextPrefix)
SuffixText = swDispDim.GetText(swDimensionTextSuffix)
ShowValue = True
CalloutText1 = swDispDim.GetText(swDimensionTextCalloutAbove)
CalloutText2 = swDispDim.GetText(swDimensionTextCalloutBelow)
CenterText = True


'EditDimensionProperties2(TolType, TolMax, TolMin, TolMaxFit, TolMinFit, UseDocPrec, Precision, ArrowsIn, UseDocArrows, Arrow1, Arrow2, PrefixText, SuffixText, ShowValue, CalloutText1, CalloutText2, CenterText)


boolstatus = Part.EditDimensionProperties2(TolType, TolMax, TolMin, TolMaxFit, TolMinFit, UseDocPrec, Precision, ArrowsIn, UseDocArrows, Arrow1, Arrow2, PrefixText, SuffixText, ShowValue, CalloutText1, CalloutText2, CenterText)


Part.ClearSelection2 True


End Sub

FR
FR 2021-04-09

Here is a code to change a selected dimension to basic

Option Explicit
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swSelMgr As SldWorks.SelectionMgr
Dim swDisplayDimension As SldWorks.DisplayDimension
Dim swDimension As SldWorks.Dimension
Dim swDimensionTolerance As SldWorks.DimensionTolerance
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swSelMgr = swModel.SelectionManager
Set swDisplayDimension = swSelMgr.GetSelectedObject6(1, 0)
If swSelMgr.GetSelectedObjectType3(1, -1) <> swSelectType_e.swSelDIMENSIONS Then Exit Sub
Set swDimension = swDisplayDimension.GetDimension2(0)
Set swDimensionTolerance = swDimension.Tolerance
swDimensionTolerance.Type = swTolType_e.swTolBASIC
End Sub

FR
FR 2021-04-09

did you set swDispDim?

check with If swDispDim Is Nothing Then debug.print "swDispDim not set"

BA
BA 2021-04-09

Fifi,

Error on this line

Set swDimension = swDispDim.GetDimension2(0)

error is "Object required"

Brian

BA
BA 2021-04-09

Thanks, Fifi.  Just targeting what I want to change is a better way to go.

Chris, I am in the middle of creating a custom toolbar for drafting, to speed my work.

It will have buttons for basic, reference, 2 PL, and so on.

Brian

CS
CS 2021-04-09

Just curious, why you need a macro to set a dimension to basic?

FR
FR 2021-04-09

You don't need to read all the parameters if you only need to change one of them.

For example to change the tolerance type to basic:

Dim swDimension As SldWorks.Dimension
Dim swDimensionTolerance As SldWorks.DimensionTolerance

Set swDimension = swDispDim.GetDimension2(0)
Set swDimensionTolerance = swDimension.Tolerance 
swDimensionTolerance.Type = swTolBASIC

http://help.solidworks.com/2016/english/api/sldworksapi/change_dimension_tolerance_in_configuration_example_vb.htm

Or you can use "Empty" for the parameters you don't want to change:

boolstatus = Part.EditDimensionProperties2(swTolType_e.swTolBASIC, Empty, Empty, Empty, Empty, Empty, Empty, Empty, Empty, Empty, Empty, Empty, Empty, True, Empty, Empty, Empty)

(Actually doesn't work very well)

BA
BA 2021-04-09

Thank you.

There is one more step, though.

I need to read the values of all of the parameters for the "EditDimensionProperties2" method

TolType, TolMax, TolMin, TolMaxFit, TolMinFit, UseDocPrec, Precision, ArrowsIn, UseDocArrows, Arrow1, Arrow2, PrefixText, SuffixText, ShowValue, CalloutText1, CalloutText2, CenterText

In the code you gave me (or I gave you), it does not have all of these values, and it is not a "one to one" relationship.

In other words, to get prefix text ("PrefixText" in the "EditDimensionProperties2"), the command is

swDispDim.GetText(swDimensionTextPrefix)

How do I get the commands for all of the parameters of "EditDimensionProperties2"?

Brian

San Diego, CA