Macro rotation view 90°

Hello

I would like to add two macro buttons in my command manager, their function: 

After selecting a view in my drawing, I click on one of the buttons that would rotate the active view 90° clockwise or counterclockwise. 

 

I tried to retrieve the rotation code of a view but obviously, it only works for the view with which I made the recording. 

I try to modify this code so that it rotates on the selected view but without success:

Sub main()

Set swApp = _
Application.SldWorks

Set Part = swApp.ActiveDoc
boolstatus = Part.ActivateView("Layout View5")
boolstatus = Part.Extension.SelectByID2("Drawing View5", "DRAWINGVIEW", 8.32576033453152E-02, 0.228631445783132, 0, False, 0, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("Drawing View5", "DRAWINGVIEW", 0, 0, 0, False, 0, Nothing, 0)
boolstatus = Part.DrawingViewRotate(1.5707963267949)
End Sub

 

If anyone had a lead, I'd be interested, I haven't managed to find anything on the net or the forum. 

 

Kind regards.

Just try with

Sub main()

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc
boolstatus = Part.DrawingViewRotate(1.5707963267949)
End Sub

Or rather this

Dim swApp As Object

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

Sub RotateLeft()

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc
Part.ViewRotateminusy
Part.ViewRotateminusy
Part.ViewRotateminusy
Part.ViewRotateminusy
Part.ViewRotateminusy
Part.ViewRotateminusy
End Sub
Sub RotateRight()

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc
Part.ViewRotateplusy
Part.ViewRotateplusy
Part.ViewRotateplusy
Part.ViewRotateplusy
Part.ViewRotateplusy
Part.ViewRotateplusy
End Sub

Hello 

 

First of all, thank you for the answers MaD:

 

I tested both codes, the first one works well. Except that from the 0° position of the view, he tells him to put himself at 90°. That's already not bad, if there was a way to increment the position of the view by 90° each time you click on the button it would be even better. 

The second code seems to be written with this in mind, increment the view by 90° with each click but nothing happens at all when I run it.

We are not far from the goal, I am still looking if you have any leads, don't hesitate. 

I'm not an expert in VBA lagging, I would need something that in the first code does this:

boolstatus = Part.DrawingViewRotate(+90°)

I don't find the structure adequate. 

Thank you

http://help.solidworks.com/2013/English/api/sldworksapi/Rotate_Drawing_View_45_Degrees_Example_VB.htm

You find the line below

status = swDrawing. DrawingViewRotate(45 / 57.3) 'Convert degrees to radians, the default system unit

Thank you for your answer MaD, it does work, but it puts the view in the position it is indicated. 

In this case,  "status = swDrawing.DrawingViewRotate(45 / 57.3" places the view at 45° from its starting position. 

If you click again, the view is already at 45° so don't move. 

If possible, I would like to increment by 45 ° with each click, i.e. tell it current position + 45 °.

I don't know if I'm clear?

Thank you in advance

Hello

You can insert the "RestoreRotation()" function before rotating your view like this if it is already rotated, this allows you to reposition it in the initial position before re-rotating it to the desired value.

In C#, it looks like:

ModelDoc2 swModel = default(ModelDoc2);
ModelDocExtension swModelDocExt = default(ModelDocExtension);
DrawingDoc swDrawing = default(DrawingDoc);
bool status = false;
int errors = 0;
int warnings = 0;

swModel = (ModelDoc2)Program.swapp. OpenDoc6("C:\\Program Files\\SolidWorks Corp\\SolidWorks\\samples\\tutorial\\driveworksxpress\\mobile gantry.slddrw", (int)swDocumentTypes_e.swDocDRAWING, (int)swOpenDocOptions_e. swOpenDocOptions_Silent, "", ref errors, ref warnings);
swModelDocExt = (ModelDocExtension)swModel.Extension;
swModel. ViewZoomtofit2();
swDrawing = (DrawingDoc)swModel;
           
status = swDrawing. ActivateView("Drawing View4");
status = swModelDocExt. SelectByID2("Drawing View4", "DRAWINGVIEW", 0, 0, 0, false, 0, null, 0);
swDrawing. RestoreRotation();
           
status = swDrawing. ActivateView("Drawing View4");
status = swModelDocExt. SelectByID2("Drawing View4", "DRAWINGVIEW", 0, 0, 0, false, 0, null, 0);
status = swDrawing. DrawingViewRotate(30 / 57.3);

Kind regards

Good evening

Below is the code for a macro that allows you to rotate a named view in the active sheet by 45°:

Dim swApp As SldWorks.SldWorks
Dim swDoc As ModelDoc2
Dim swDraw As DrawingDoc
Dim swSheet As Sheet
Dim swView As View
Dim Angle As Double
Dim swViewList As Variant
Dim viewName As String

Sub main()

Set swApp = Application.SldWorks
Set swDoc = swApp.ActiveDoc
If swDoc.GetType = swDocumentTypes_e.swDocDRAWING Then

    'Change the name of the view to be rotated here
    viewName = "Drawing View1"

    Set swDraw = swDoc
    Set swSheet = swDraw.GetCurrentSheet
    swViewList = swSheet.GetViews
    Dim vView As Variant
    For Each vView In swViewList
        Set swView = vView
        If swView.Name = viewName Then
            swDraw.ActivateView (viewName)
            Angle = swView.Angle
            swView.Angle = Angle + (45 / 57.3)
            
        End If
    Next
End If
swDraw.ForceRebuild
End Sub

Kind regards.

In short, and by removing what is useless:

Dim swApp As SldWorks.SldWorks
Dim swDoc As ModelDoc2
Dim swDraw As DrawingDoc
Dim swView As View
Dim viewName As String

Sub main()
'Change the name of the view to be rotated here
viewName = "Drawing View1"

Set swApp = Application.SldWorks
Set swDoc = swApp.ActiveDoc
If swDoc.GetType = swDocumentTypes_e.swDocDRAWING Then
    Set swDraw = swDoc
    Set swView = swDraw.GetFirstView
    While Not swView Is Nothing
        If swView.Name = viewName Then
            swView.Angle = swView.Angle + (45 / 57.3)
        End If
        Set swView = swView.GetNextView
    Wend
End If
swDraw.ForceRebuild
End Sub

Good :) code

1 Like

Hello 

It's perfect, this last code works!!!!  

One last little thing and I could post the final macro, is it possible to apply the macro to the selected view? I tried with things like viewName = swapp. ActiveView or viewName = Activeview.GetName but it doesn't work. 

 

To sum up On the line: 

'Change the name of the view to be rotated here
viewName = "Drawing View1"

I would have to replace "drawing view1" with "selected view"  

I manage to tinker with a few macros in VBA but I don't do them often, it's difficult to keep up. 

A final helping hand would be welcome. 

 

Thank you in advance.  

Kind regards.

Hello

Use the SlectById2 function in addition to activeview in theory it should work.

Hello

The SelectByIDX() function does not return a value.

You have to go through a SelectionManager object to retrieve the selections. Then, you need to check that the selected object is indeed of type View. From there, the code I provided you with remains unchanged.

To help you, search for "Get selected object Solidworks api" in Google.

Good evening

The macro code modified to rotate the selection.

Dim swApp As SldWorks.SldWorks
Dim swDoc As ModelDoc2
Dim swDraw As DrawingDoc
Dim swView As View
Dim viewName As String
Sun swSelMgr As SelectionMgr
Sub main()

Set swApp = Application.SldWorks
Set swDoc = swApp.ActiveDoc
If swDoc.GetType = swDocumentTypes_e.swDocDRAWING Then
    Set swDraw = swDoc
    Set swSelMgr = swDoc.SelectionManager
    Set swView = swSelMgr.GetSelectedObject5(1)
    While Not swView Is Nothing
        If swView.Name = viewName Then
            swView.Angle = swView.Angle + (45 / 57.3)
        End If
        Set swView = swView.GetNextView
    Wend
End If
swDraw.ForceRebuild
End Sub

Be careful, I didn't shield the code, you have to add error handling.

Hello 

 

Thank you very much for your answer, sorry I couldn't answer it before. 

 

I tested the macro you dropped me in the previous message unfortunately it doesn't work, I can't find why. Even after reconstruction, the eyesight does not change position. 

 

I continue to look on my own. Does macro work for you? 

 

Sincerely,

If necessary, the macro I use (a left and a right).

Thank you all for the help


90right.swp

Hello

Thank you but it doesn't increment the angle as requested in one of the posts?

Kind regards

Hello

Here is the code to increment the selected view. On the other hand, you have to reselect the view each time.

Dim swApp As SldWorks.SldWorks
Dim swDoc As ModelDoc2
Dim swDraw As DrawingDoc
Dim swView As View
Sun swSelMgr As SelectionMgr

Sub main()

Set swApp = Application.SldWorks
Set swDoc = swApp.ActiveDoc

If swDoc.GetType = swDocumentTypes_e.swDocDRAWING Then
    Set swDraw = swDoc
    Set swSelMgr = swDoc.SelectionManager
    Set swView = swSelMgr.GetSelectedObject5(1)
    swView.Angle = swView.Angle + (45 * 3.14159265358979 / 180)
End If

'swDraw.ForceRebuild

End Sub