Macro view rotation

Hello

In a macro I get the width of the view as well as its height and if the width is greater than the height I want to rotate the view 90°

The macro is almost functional but you have to select the view manually while I would like this view selection to be automatic.

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
    Dim swActiveView    As SldWorks.View
    Dim bRet            As Boolean
    Dim outline()       As Double
    Dim pos()           As Double
    Dim fileName        As String
    Dim errors          As Long
    Dim warnings        As Long
    Dim vSheetProps     As Variant
    Dim width           As Long
    Dim height          As Long
    Dim Pi              As Double
    
  
    'On récupère la MEP active
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swDraw = swModel
    Set swSheet = swDraw.GetCurrentSheet
    


    

    ' Set PI
    Pi = 4 * Atn(1)
    
    'Debug.Print "File = " & swModel.GetPathName
    'Debug.Print "  Sheet = " & swSheet.GetName
    'Debug.Print "    Template = " & swSheet.GetTemplateName



    
    
    

    Set swApp = CreateObject("SldWorks.Application")
    
    fileName = swModel.GetPathName
    Set swDraw = swApp.OpenDoc6(fileName, swDocumentTypes_e.swDocDRAWING, swOpenDocOptions_e.swOpenDocOptions_Silent, "", errors, warnings)
    Set swView = swDraw.GetFirstView


    Do While Not swView Is Nothing
        outline = swView.GetOutline
        pos = swView.Position

        width = (outline(2) * 1000) - outline(0) * 1000
        Debug.Print "width" & width
        height = (outline(3) * 1000) - outline(1) * 1000
        Debug.Print "height" & height
        
        'On vérifie si la pièce est plus large que haute
        If width < height Then
            Debug.Print "Pièce plus large que haute"

            ' Rotation de la vue suivant angle indiqué

            bRet = swDraw.DrawingViewRotate(90 / (180 / Pi))    'Angle de rotation/(180/Pi) pour passer de radian en °

            

            
        Else
            Debug.Print "Pièce plus haute que large"

            
        End If
        
        
        ' On récupère l'échelle
        'vSheetProps = swSheet.GetProperties
        'Debug.Print "      Scale1         = " & vSheetProps(2)
        'Debug.Print "      scale2         = " & vSheetProps(3)
        'On redimensionne la vue

        Set swView = swView.GetNextView
    Loop




End Sub

I tried several things with selection manager and also to make the view active but for now no working solution.

Hello

Try by putting the line:

bRet = swModel.Extension.SelectByID2(swView.Name, "DRAWINGVIEW", 0, 0, 0, False, 0, Nothing, 0)

Just before the line:

bRet = swDraw.DrawingViewRotate(90 / (180 / Pi))

Be careful, changing the angle of one view will affect the other views depending on their positions (for example, the right view will be completely recalculated if the front view is rotated).

Kind regards

1 Like

Or another solution, put the line:

swView.Angle = (90 / (180 / Pi))

In place of the line:

bRet = swDraw.DrawingViewRotate(90 / (180 / Pi))

Kind regards

1 Like

No, still nothing, the view still only rotates if you select the view manually...

I think I had already tried this solution or something very close.

FYI, no worries about recalculating the other views since only one view is inserted on my sheet (the flat-pattern view of  a sheet)

The goal is to automate sheet metal drawings for us.

Weird, the 2 solutions work for me, is it a standard view from a 3D? Can you send me a one-room take-home composition plus its plan on which it doesn't work?

Kind regards

If you load a new plane on which the view needs to rotate, does it work?

Your view, once rotated, is at a 90° angle but this parameter is not incremental (by this I mean that the function does not correspond to "I turn the view 90°" but corresponds to "I put the view to 90°") so if your view has already undergone a rotation it will not change a second time. If you want you can put the line "swView.Angle = 0" in the "Else" block to make it test so that it should rotate every time.

Kind regards

 

1 Like

Hello

Here is an example of an attached file.

For the angle I systematically set to 0° before. but nothing changes (neither with the addition in the else)

I also tried to put the lines "Do while Not" and "Set swView = swView.GetNextView" in comments since I don't have a next view but no better.

 


test_decoupe.zip

Hello

For me it works perfectly with your drawing and with the following macro:

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
    Dim swActiveView    As SldWorks.View
    Dim bRet            As Boolean
    Dim outline()       As Double
    Dim pos()           As Double
    Dim fileName        As String
    Dim errors          As Long
    Dim warnings        As Long
    Dim vSheetProps     As Variant
    Dim width           As Long
    Dim height          As Long
    Dim Pi              As Double

    'On récupère la MEP active
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swDraw = swModel
    Set swSheet = swDraw.GetCurrentSheet

    ' Set PI
    Pi = 4 * Atn(1)
    
    'Debug.Print "File = " & swModel.GetPathName
    'Debug.Print "  Sheet = " & swSheet.GetName
    'Debug.Print "    Template = " & swSheet.GetTemplateName

    Set swApp = CreateObject("SldWorks.Application")
    
    fileName = swModel.GetPathName
    Set swDraw = swApp.OpenDoc6(fileName, swDocumentTypes_e.swDocDRAWING, swOpenDocOptions_e.swOpenDocOptions_Silent, "", errors, warnings)
    Set swView = swDraw.GetFirstView

    Do While Not swView Is Nothing
        outline = swView.GetOutline
        pos = swView.Position

        Debug.Print swView.Name
        
        width = (outline(2) * 1000) - outline(0) * 1000
        Debug.Print "width" & width
        height = (outline(3) * 1000) - outline(1) * 1000
        Debug.Print "height" & height
        
        'On vérifie si la pièce est plus large que haute
        'If width > height Then
        If height < width Then
            Debug.Print "Pièce plus large que haute"

            ' Rotation de la vue suivant angle indiqué

            'bRet = swModel.Extension.SelectByID2(swView.Name, "DRAWINGVIEW", 0, 0, 0, False, 0, Nothing, 0)
            'bRet = swDraw.DrawingViewRotate(90 / (180 / Pi))    'Angle de rotation/(180/Pi) pour passer de radian en °
    
            swView.Angle = (90 / (180 / Pi))
        Else
            swView.Angle = 0
            Debug.Print "Pièce plus haute que large"
        End If

        ' On récupère l'échelle
        'vSheetProps = swSheet.GetProperties
        'Debug.Print "      Scale1         = " & vSheetProps(2)
        'Debug.Print "      scale2         = " & vSheetProps(3)
        'On redimensionne la vue

        Set swView = swView.GetNextView
    Loop

End Sub

To check in the SW options if you don't have a setting that blocks views or MeP, I'm on SW 2019.

Kind regards

1 Like

Thank you, that's right, I had just seen that my condition was not good:

'If width > height Then

And the whole problem was from there from the beginning.