Solidworks VBA Macro - Create Standard 3 View [1st Angle]
Objective
In this article, we understand โhow toโ Create Standard 3 View [1st Angle] in Drawing document from VBA macro.
We will insert Standard 3 View of [1st Angle].
This is most updated method of Standard 3 View of [1st Angle] in an drawing document.
Results We Can Get
Below image shows the result we get.
We Create Standard 3 View [1st Angle] 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 Create Standard 3 View [1st Angle] 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 Create Standard 3 View [1st Angle].
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
' Program to Insert Standard 3 View [1st Angle]
Sub main()
' Setting Solidworks variable to Solidworks application
Set swApp = Application.SldWorks
' Get Opened "Demo Assembly" document
Set swDoc = swApp.GetOpenDocument("Demo Assembly")
' Check if Solidworks document is opened or not
If swDoc Is Nothing Then
MsgBox "Solidworks document is not opened."
Exit Sub
End If
' Variable for opened File's full Path
Dim openedFilePath As String
' Get opened File's full Path
openedFilePath = swDoc.GetPathName
' Open Drawing document
Set swDoc = swApp.ActiveDoc
' Set Solidworks Drawing document variable
Set swDrawing = swDoc
' Boolean variable
Dim boolStatus As Boolean
' Insert Standard 3 View [1st Angle]
boolStatus = swDrawing.Create1stAngleViews2(openedFilePath)
' Check if we successfully insert views
If boolStatus = False Then
MsgBox "Failed to insert Standard 3 View [1st Angle]"
Exit Sub
End If
End Sub
Prerequisite
There are some prerequisites for this article.
- Knowledge of VBA programming language is โrequired.
- We create drawing from an already opened Assembly document.
- We will use name of Assembly document name hardcoded.
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
- Get Assembly Full Path
- Insert Standard Views
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.
These all are our global variables.
They are SOLIDWORKS API Objects.
' Program to Insert Standard 3 View [1st Angle]
Sub main()
End Sub
- In above line, we create Program to Insert Standard 3 View [1st Angle].
- 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.
Get Assembly Full Path
In this section, we Get Assembly Full Path.
' 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.
' Get Opened "Demo Assembly" document
Set swDoc = swApp.GetOpenDocument("Demo Assembly")
- In above line, we set value of
swDoc
variable. - This value is currently Opened โDemo Assemblyโ document.
- We use
GetOpenDocument()
method fromswApp
variable. - This
GetOpenDocument()
method takes one parameter.- DocName - Name of the document.
- We pass Opened assembly name i.e. โDemo Assemblyโ as parameter.
' 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.
' Variable for opened File's full Path
Dim openedFilePath As String
- In above line, we create a variable for opened Fileโs full Path.
- Variable Name:
openedFilePath
- Type:
String
' Get opened File's full Path
openedFilePath = swDoc.GetPathName
- In above line, we set value of
openedFilePath
variable. - This value opened Fileโs full Path.
- We set the value by
GetPathName()
method ofswDoc
variable.
Insert Standard Views
In this section, we Insert Standard Views action.
' Open Drawing document
Set swDoc = swApp.ActiveDoc
- In above line, we set value of
swDoc
variable. - This value is currently opened part document.
' Set Solidworks Drawing document
Set swDrawing = swDoc
- In above line, we set value of
swDrawing
variable. - This value is
swDoc
variable.
' Boolean Variable
Dim boolStatus As Boolean
- Purpose: In above line, we create a variable for Boolean selection.
- Variable Name:
boolStatus
- Type:
Boolean
' Insert Standard 3 View [1st Angle]
boolStatus = swDrawing.Create1stAngleViews2(openedFilePath)
- In above code, we Insert Standard 3 View into Drawing.
- For this, we use
Create1stAngleViews2
method. - This
Create1stAngleViews2
method is part ofswDrawing
variable. - This method takes 1 parameter.
- ModelName: Name of the document from which to create views.
-
Return Value : This
Create1stAngleViews2
method return ๐True
if successful,False
if not. -
In our code, I have used following values:
Parameter Name Value Used ModelName openedFilePath
- Reference: For more details please visit ๐ online SOLIDWORKS API Help.
' Check if we successfully insert views
If boolStatus = False Then
MsgBox "Failed to insert Standard 3 View [1st Angle]"
Exit Sub
End If
- In above code block, we check if we successfully insert views or not.
- We use ๐ IF statement for checking.
- Condition:
boolStatus = False
- When this condition is
True
,- We show and ๐ message window to user.
- Message: *Failed to insert Standard 3 View [1st Angle].
- Then we stop our macro here.
Now we run the macro and after running macro we show selected component 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 Create Standard 3 View [1st Angle] 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!!!