Solidworks VBA Macro - Get View Outer Boundary
Objective
In this article, we understand “how to” Get View Outer Boundary in Drawing document from VBA macro.
This is most updated method of Get View Outer Boundary in an drawing document.
Results We Can Get
Below image shows the result we get.
We Get View Outer Boundary in simple manners.
There are no extra steps required.
To get the correct result, please follow the steps correctly.
Macro Video
Below 🎬 video shows how to Get View Outer Boundary from SOLIDWORKS VBA Macros.
Above video is just for visualization and there is no explanation.
I have explained every line in this article.
It is advisable to watch video, since it helps you to better understand the process.
VBA Macro
Below is the VBA macro for Get View Outer Boundary.
Option Explicit
' Creating variable for Solidworks application
Dim swApp As SldWorks.SldWorks
' Creating variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2
' Creating variable for Solidworks Drawing
Dim swDrawing As SldWorks.DrawingDoc
' Creating variable for Solidworks View
Dim swView As SldWorks.View
' Program to Get View Outer Boundary
Sub main()
' Setting Solidworks variable to Solidworks application
Set swApp = Application.SldWorks
' Set Solidworks document variable to currently opened document
Set swDoc = swApp.ActiveDoc
' Check if Solidworks document is opened or not
If swDoc Is Nothing Then
MsgBox "Solidworks document is not opened."
Exit Sub
End If
' Set Solidworks Drawing document variable
Set swDrawing = swDoc
' Get activate view
Set swView = swDrawing.ActiveDrawingView
' Variable for view's outline position
Dim viewOutlinePosition() As Double
' Get view's outline position
viewOutlinePosition = swView.GetOutline
' Print view's outline position co-ordinates
Debug.Print "View's Left X: " & viewOutlinePosition(0)
Debug.Print "View's Right X: " & viewOutlinePosition(2)
Debug.Print "View's Bottom Y: " & viewOutlinePosition(1)
Debug.Print "View's Top Y: " & viewOutlinePosition(3)
End Sub
Prerequisite
There are some prerequisites for this article.
- Knowledge of VBA programming language is ❗required.
- We get view outer boundary from an existing (Base) view.
- We already select an existing (Base) view.
We will apply checks in this article, so the code we write, should be error free mostly.
Steps To Follow
This VBA macro can be divided into following sections:
- Create Global Variables
- Initialize Variables
- Get View Outer Boundary
Every section with each line is explained below.
I also give some links (see icon 🚀) so that you can go through them if there are anything I explained in previous articles.
Create Global Variables
In this section, we create global variables.
Option Explicit
- Purpose: Above line forces us to define every variable we are going to use.
- Reference: 🚀 SOLIDWORKS Macros - Open new Part document article.
' Variable for Solidworks application
Dim swApp As SldWorks.SldWorks
- Purpose: In above line, we create a variable for Solidworks application.
- Variable Name:
swApp
- Type:
SldWorks.SldWorks
- Reference: Please visit 🚀 online SOLIDWORKS API Help.
' Variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2
- Purpose: In above line, we create a variable for Solidworks document.
- Variable Name:
swDoc
- Type:
SldWorks.ModelDoc2
- Reference: Please visit 🚀 online SOLIDWORKS API Help.
' Creating variable for Solidworks Drawing
Dim swDrawing As SldWorks.DrawingDoc
- Purpose: In above line, we create a variable for Solidworks Drawing.
- Variable Name:
swDrawing
- Type:
SldWorks.DrawingDoc
- Reference: Please visit 🚀 online SOLIDWORKS API Help.
' Creating variable for Solidworks View
Dim swView As SldWorks.View
- Purpose: In above line, we create a variable for Solidworks View.
- Variable Name:
swView
- Type:
SldWorks.View
- Reference: Please visit 🚀 online SOLIDWORKS API Help.
These all are our global variables.
They are SOLIDWORKS API Objects.
' Program to Get View Outer Boundary
Sub main()
End Sub
- In above line, we create Program to Get View Outer Boundary.
- This is a
Sub
procedure which has name ofmain
. - This procedure hold all the statements (instructions) we give to computer.
- Reference: Detailed information 🚀 VBA Sub and Function Procedures article of this website.
Initialize Variables
In this section, we initialize Variables.
' Set Solidworks Application variable to current application
Set swApp = Application.SldWorks
- In above line, we set value of
swApp
variable. - This value is currently opened Solidworks application.
' Set Solidworks document variable to currently opened document
Set swDoc = swApp.ActiveDoc
- In above line, we set value of
swDoc
variable. - This value is currently opened part document.
' Check if Solidworks document is opened or not
If swDoc Is Nothing Then
MsgBox ("Solidworks document is not opened.")
Exit Sub
End If
- In above code block, we check if we successfully set the value of
swDoc
variable. - We use 🚀 IF statement for checking.
- Condition:
swDoc Is Nothing
- When this condition is
True
,- We show and 🚀 message window to user.
- Message: SOLIDWORKS document is not opened.
- Then we stop our macro here.
' Set Solidworks Drawing document
Set swDrawing = swDoc
- In above line, we set value of
swDrawing
variable. - This value is
swDoc
variable.
Get View Outer Boundary
In this section, we Get View Outer Boundary.
' Get activate view
Set swView = swDrawing.ActiveDrawingView
- In above code, we Get activate view from Drawing.
- For this, we use
ActiveDrawingView
property. - This
ActiveDrawingView
property is part ofswDrawing
variable.
' Variable for view's outline position
Dim viewOutlinePosition() As Double
- In above line, we create an Array variable for view’s outline position.
- Variable Name:
viewOutlinePosition()
- Type:
Double
' Get view's outline position
viewOutlinePosition = swView.GetOutline
- In above line, we set value of
viewOutlinePosition
variable. - We set the value by
GetOutline()
method ofswView
variable.
' Print view's outline position co-ordinates
Debug.Print "View's Left X: " & viewOutlinePosition(0)
Debug.Print "View's Right X: " & viewOutlinePosition(2)
Debug.Print "View's Bottom Y: " & viewOutlinePosition(1)
Debug.Print "View's Top Y: " & viewOutlinePosition(3)
- In above line, we Print selected View’s outline position co-ordinates in Immediate Window.
- If Immediate Window is not available then press “
Ctrl + G
”. - View’s Left X:
viewOutlinePosition(0)
⬅ This is 0th index. - View’s Right X:
viewOutlinePosition(2)
⬅ This is 2nd index. - View’s Bottom Y:
viewOutlinePosition(1)
⬅ This is 1st index. - View’s Top Y:
viewOutlinePosition(3)
⬅ This is 3rd index.
Please see below image for more details.
Now we run the macro and after running macro we Get View Outer Boundary as shown in below image.
This is it !!!
I hope my efforts will helpful to someone! 😊
If you found anything to add or update, please let me know on my e-mail 📧.
Hope this post helps you to Get View Outer Boundary with SOLIDWORKS VBA Macros.
For more such tutorials on SOLIDWORKS VBA Macro, do come to this website after sometime.
If you like the post then please share it with your friends also. 🙏🏻
Do let me know by you like this post or not!
Till then, Happy learning!!!