Macro to change Drafting Standard for all drawings, then set all dimensions and tolerances to use document font

Library for macros
henry14286
Posts: 1
Joined: Wed Oct 25, 2023 2:18 am
Answers: 0
x 1

Macro to change Drafting Standard for all drawings, then set all dimensions and tolerances to use document font

Unread post by henry14286 »

Hello forumers,

I am working on a macro to:
1. Change Drafting Standard for all opened drawings in SolidWorks
2. Set all the dimensions and their tolerances to "Use document font" or "Use document size"

So far the macro is able to do the step 1, but I have no clue how to add the step 2 into the macro.

Can anybody help please?
image.png
Dim swApp As Object
Dim sDrawingCol As New Collection

Sub Main()

Set swApp = Application.SldWorks
Set swDrawModel = swApp.GetFirstDocument

' Check to see if a drawing is loaded.
If swDrawModel Is Nothing Then
MsgBox "There is no active drawing document"
Exit Sub
End If

Do While Not swDrawModel Is Nothing
If swDrawModel.GetType = swDocDRAWING Then
sDrawingCol.Add swDrawModel.GetPathName
Debug.Print swDrawModel.GetPathName
End If
Set swDrawModel = swDrawModel.GetNext
Loop

If sDrawingCol.Count > 0 Then

FileSave = True

Else

MsgBox "There is no active drawing document"
Exit Sub

End If

Set swDrawModel = swApp.GetFirstDocument
Do While Not swDrawModel Is Nothing
If swDrawModel.GetType = swDocDRAWING Then
Set swDraw = swDrawModel

'Update Drafting Standard
Set Ext = swDraw.Extension
Ext.LoadDraftingStandard ("C:\Drafting Standards\ISO-Metric.sldstd")

' set all notes on this drawing sheet to the default font
SetAnnotationsToDefaultFont swDraw

End If


Jump:
Set swDrawModel = swDrawModel.GetNext
Loop

Set sDrawingCol = Nothing

End Sub
User avatar
AlexB
Posts: 501
Joined: Thu Mar 18, 2021 1:38 pm
Answers: 28
x 269
x 445

Re: Macro to change Drafting Standard for all drawings, then set all dimensions and tolerances to use document font

Unread post by AlexB »

Here's a small example of how to do this using a snippet from a macro I wrote to do this with our specific templates. For each annotation, you can get the underlying object and cast it to the expected type. Then you can get the text format and then set it again with the second argument as "TRUE" which tells it to "Use Document Font".

Note, this probably won't run if you just plug it in to your code but should give you an idea on how this is used.

Code: Select all

Dim allSheetViewArrays As Variant
Dim sheetViews As Variant
Dim formatIndex As Integer

allSheetViewArrays = swDraw.GetViews

Dim i As Integer
Dim j As Integer

For i = 0 To UBound(allSheetViewArrays)
    sheetViews = allSheetViewArrays(i)
    For j = 0 To UBound(sheetViews)
        Set swView = sheetViews(j)
        
        Set swAnno = swView.GetFirstAnnotation3
        swViewType = swView.Type
        
        While Not swAnno Is Nothing
		
            For formatIndex = 0 To swAnno.GetTextFormatCount
                If swAnno.GetType = swAnnotationType_e.swNote Then
                    Set swNote = swAnno.GetSpecificAnnotation
                    Set swTextFormat = swAnno.GetTextFormat(formatIndex)
                    swAnno.SetTextFormat formatIndex, True, swTextFormat
                End If
            Next formatIndex
            
            Set swAnno = swAnno.GetNext3
        Wend
        
        Set swView = swView.GetNextView
    Next j
Next i
Post Reply