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
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:
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;
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)
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
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.
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.
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.
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?
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