Need a macro to lock and unlock the view position of the drawing view instead of doing manually (below step).
- Lock View Position allows you to fix the drawing view in place. Right-click anywhere in the drawing view and select Lock View Position.
Need a macro to lock and unlock the view position of the drawing view instead of doing manually (below step).
While running that receiving Run time error 438, also I don't want to calculate boundary also. I am having set of drawings in Locked view position, I just want to unlock that. If possible can you please share the code here...
1. Placing the drawing view1
2. Creating a section view for that
3. Hiding the drawing view1 alone (without affecting section view)
4. Need to lock / unlock that section view
Here is some simply code to try (toggles if a view is locked or not). You must had the view you want to lock or unlock selected before running.
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swView As SldWorks.View
Set swApp = Application.SldWorks
On Error Resume Next
Set swView = swApp.ActiveDoc.SelectionManager.GetSelectedObject5(1)
If swView Is Nothing Then
MsgBox "View to lock/unlock position not selected. ", vbCritical + vbOKOnly, "Lock/Unlock View Position"
Else
swView.PositionLocked = Not -(swView.PositionLocked)
MsgBox "View [" & swView.Name & "] position " & IIf(swView.PositionLocked, "locked. ", "unlocked. "), vbInformation + vbOKOnly, "Lock/Unlock View Position"
End If
On Error GoTo 0
Set swApp = Nothing
Set swView = Nothing
End Sub
I looked closer at the example and found why it does not work like it should. The line
Set swApp = CreateObject("SldWorks.Application")
Starts a new instance of SOLIDWORKS. That would work for an Excel macro for example but not SOLIDWORKS. It should be
Set swApp = New SldWorks.SldWorks
This will connect the running instance of SOLIDWORKS. If you do not need the position, just comment the lines out by adding apostrophe ' at the start of the line. The below image is of modified macro that will flip the position lock for all views on the active sheet of the active drawing file.
Still I am receiving the run time errors
When I am using the previous code getting the error in Debug.Print "Position locked?" & swView.PositionLocked
I am having the predefined sectioned view in locked position, need to unlock that one...
Try this code and let us know how it works:
Option Explicit
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swDraw As SldWorks.DrawingDoc
Dim swView As SldWorks.View
Dim outline() As Double
Dim pos() As Double
Dim fileName As String
Dim errors As Long
Dim warnings As Long
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
If swModel.GetType = swDocDRAWING Then
Set swDraw = swModel
Else
Exit Sub
End If
Set swView = swDraw.GetFirstView
Do While Not swView Is Nothing
outline = swView.GetOutline
pos = swView.Position
Debug.Print "View = " & swView.Name
Debug.Print " X and Y positions = (" & pos(0) * 1000# & ", " & pos(1) * 1000# & ") mm"
Debug.Print " X and Y bounding box minimums = (" & outline(0) * 1000# & ", " & outline(1) * 1000# & ") mm"
Debug.Print " X and Y bounding box maximums = (" & outline(2) * 1000# & ", " & outline(3) * 1000# & ") mm"
Debug.Print " Position locked? " & swView.PositionLocked
Set swView = swView.GetNextView
Loop
End Sub
Here is example from API help file.
http://help.solidworks.com/2018/English/api/sldworksapi/Get_View_Bounding_Box_and_Position_Example_VB.htm
To lock the view position: