Solidworks vba all sheet scale views

Hello

I import all the parts of an assembly into an MEP, flat

I change the scale of the sheets by hand and I want all the views to take the scale of the current sheet (the radio button checked: "Use the scale of the sheet") so that if I change the scale of the sheet all the views change scale

 

Do you have an idea of macros that process all the tabs of a mep to scale all the views of the tab

Thank you

Hello;

I find it strange to go through a macro for this... But here is an example to work on:

'This example shows how to set the scale of a selected drawing view.

'---------------------------------------------
' Preconditions:
' 1. Open public_documents\introsw\bolt-assembly.slddrw.
' 2. Select a drawing view.
' 3. Open the Immediate window.
'
' Postconditions:
' 1. Increases the selected drawing view's scale.
' 2. Examine the drawing and Immediate window.
'
' NOTE: Because the drawing is used elsewhere, do not
' save changes.
'---------------------------------------------

Option Explicit

Sub main()

    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swDraw As SldWorks.DrawingDoc
    Dim swSelMgr As SldWorks.SelectionMgr
    Dim swView As SldWorks.View
    Dim vScaleRatio As Variant
    Dim bRet As Boolean

    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swDraw = swModel
    Set swSelMgr = swModel.SelectionManager
    Set swView = swSelMgr.GetSelectedObject6(1, -1)

    vScaleRatio = swView.ScaleRatio

    Debug.Print "File = " & swModel.GetPathName
    Debug.Print "  View = " & swView.Name
    Debug.Print "    Use sheet scale = " & CBool(swView.UseSheetScale)
    Debug.Print "    Original scale ratio = " & vScaleRatio(0) & ":" & vScaleRatio(1)
    Debug.Print "    Original decimal scale value = " & swView.ScaleDecimal

    ' Increase scale values
    ' Changing scale sets IView::UseSheetScale to false
    vScaleRatio = swView.ScaleRatio
    swView.ScaleDecimal = swView.ScaleDecimal * 2#
    vScaleRatio = swView.ScaleRatio
    Debug.Print ""
    Debug.Print "    Use sheet scale = " & CBool(swView.UseSheetScale)
    Debug.Print "    New scale ratio = " & vScaleRatio(0) & ":" & vScaleRatio(1)
    Debug.Print "    New decimal scale value = " & swView.ScaleDecimal

    ' Rebuild to see the scaled drawing view
    bRet = swModel.EditRebuild3: Debug.Assert bRet

End Sub

Source: https://help.solidworks.com/2017/english/api/sldworksapi/Set_View_Scale_Example_VB.htm

Kind regards.

1 Like

Thanks for the answer but I don't know how to loop on all the views and apply the tab scale with the 'use sheet scale' radio button option

Dim swApp As Object
Dim Part As DrawingDoc
Dim currentSheet As Sheet
Dim boolstatus As Boolean
Dim SwModel As SldWorks.ModelDoc2
Dim swDraw  As SldWorks.DrawingDoc
Dim swSelMgr As SldWorks.SelectionMgr
Dim swView  As SldWorks.View
Dim vScaleRatio As Variant


Sub Echelle_Feuille_Vues()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc

'Feuille
Set currentSheet = Part.GetCurrentSheet
vScaleRatio = currentSheet.GetProperties
Debug.Print ("Échelle Feuille = " & vScaleRatio(2) & " : " & vScaleRatio(3))
a = vScaleRatio(2)
b = vScaleRatio(3)

'Vue
Set swDraw = SwModel
Set SwModel = swApp.ActiveDoc
Set swSelMgr = SwModel.SelectionManager
Set swView = swSelMgr.GetSelectedObject5(1)
vScaleRatio = swView.ScaleRatio
Debug.Print ("Échelle vue = " & vScaleRatio(0) & " : " & vScaleRatio(1))
swView.ScaleDecimal = a / b


boolstatus = SwModel.EditRebuild

End Sub

I wrote a piece of code but it's not satisfactory

I haven't found how to loop on all the views of a sheet (tab) and check the radio button

if anyone has an idea

 

Here's sample code for looping all the views in a sheet:

'This example shows how to get the display state for each drawing view.

'------------------------------------------------------
' Preconditions:
' 1. Open a drawing.
' 2. Open the Immediate window.
'
' Postconditions:
' 1. Traverses the drawing views on the current sheet and 
'    gets each drawing view's display state.
' 2. Examine the Immediate window.
'
' NOTE: Because the drawing is used elsewhere, do not
' save changes.
'------------------------------------------------------
Option Explicit

Sub main()

    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swDraw As SldWorks.DrawingDoc
    Dim swSheet As SldWorks.Sheet
    Dim swView As SldWorks.View

    Set swApp = CreateObject("SldWorks.Application")
    Set swModel = swApp.ActiveDoc
    Set swDraw = swModel
    Set swSheet = swDraw.GetCurrentSheet
    Set swView = swDraw.GetFirstView

    Debug.Print "File = " & swModel.GetPathName
    Debug.Print "  " & swSheet.GetName

    While Not swView Is Nothing
        Debug.Print "    " & swView.GetName2 & " [" & swView.DisplayState & "]"
        Set swView = swView.GetNextView
    Wend

End Sub

Source: https://help.solidworks.com/2022/english/api/sldworksapi/Get_Display_State_for_Each_Drawing_View_Example_VB.htm

Thank you for the answers, if anyone has an idea for the radio button, I'm interested

Here is the macro that deals with a tab/sheet

Sub Echelle_Feuille_Vues()
'MEP changer l'échelle des vues
'1 : Bouton radio "échelle de la feuille"
'2 : Échelle personnalisée


Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim Part As DrawingDoc
Dim swDraw As SldWorks.DrawingDoc
Dim swSheet As SldWorks.Sheet
Dim swView As SldWorks.View
Dim swSelMgr As SldWorks.SelectionMgr
Dim bRet As Boolean
Dim currentSheet As Sheet
Dim vScaleRatio As Variant
Dim boolstatus As Boolean

Set swApp = CreateObject("SldWorks.Application")
Set swModel = swApp.ActiveDoc
Set Part = swApp.ActiveDoc
Set swDraw = swModel
Set swSheet = swDraw.GetCurrentSheet
Set swView = swDraw.GetFirstView
Set swSelMgr = swModel.SelectionManager

'Feuille
Set currentSheet = Part.GetCurrentSheet
'vScaleRatio = currentSheet.GetProperties '2-Éch. perso.
'Debug.Print ("Échelle Feuille = " & vScaleRatio(2) & " : " & vScaleRatio(3))
'a = vScaleRatio(2) '2-Éch. perso.
'b = vScaleRatio(3) '2-Éch. perso.

'Vue
While Not swView Is Nothing
    vScaleRatio = swView.ScaleRatio
    'Debug.Print ("Échelle vue = " & vScaleRatio(0) & " : " & vScaleRatio(1))
    'swView.ScaleDecimal = a / b '2-Éch. perso.
    'Bouton radio "utiliser l'échelle de la feuille"
    swView.UseSheetScale = True '1-BT radio
    swView.UpdateViewDisplayGeometry '1-BT radio
Set swView = swView.GetNextView
Wend

boolstatus = swModel.EditRebuild
End Sub

 

Hello;

If you are using Ratios:

'Debug.Print ("Échelle vue = " & vScaleRatio(0" : " & vScaleRatio(1))

In place of the value given in:

Debug.Print "    Use sheet scale = " & CBool(swView.UseSheetScale)


The Radio button can never be active....
Kind regards.

Hello

That's why I put the 2 in the macro, either (1-) radio button or (2-) custom scale