Solidworks VBA Macro - Insert Section View
Objective
In this article, we understand “how to” Insert Section View in Drawing document from VBA macro.
We will insert Section View.
This is most updated method of Insert Section View in an drawing document.
Results We Can Get
Below image shows the result we get.
We Insert Section View 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 Insert Section View 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 Insert Section View.
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 insertView As SldWorks.View
' Program to Insert Section View
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
' Insert Section View
Set insertView = swDrawing.CreateSectionViewAt5(0.2, 0.12, 0, "A", swCreateSectionView_NotAligned, Nothing, 0)
' Check if we successfully insert Section view
If insertView Is Nothing Then
MsgBox "Failed to Insert Section View."
Exit Sub
End If
' Rebuild document
swDoc.ForceRebuild3 False
End Sub
Prerequisite
There are some prerequisites for this article.
- Knowledge of VBA programming language is ❗required.
- We create Section view from an existing (Base) view .
- We already select a line as “Section Line” this 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
- Insert Section 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.
' Creating variable for Solidworks View
Dim insertView As SldWorks.View
- Purpose: In above line, we create a variable for Solidworks View.
- Variable Name:
insertView
- Type:
SldWorks.View
- Reference: Please visit 🚀 online SOLIDWORKS API Help.
These all are our global variables.
They are SOLIDWORKS API Objects.
' Program to Insert Section View
Sub main()
End Sub
- In above line, we create Program to Insert Section View.
- 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.
Insert Section Views
In this section, we Insert Section Views action.
' Insert Section View
Set insertView = swDrawing.CreateSectionViewAt5(0.2, 0.12, 0, "A", swCreateSectionView_NotAligned, Nothing, 0)
- In above code, we Insert Section View into Drawing.
- For this, we use
CreateSectionViewAt5
method. - This
CreateSectionViewAt5
method is part ofswDrawing
variable. - This method takes following parameters.
- X: X position on the drawing sheet for the center of the section view.
- Y: Y position on the drawing sheet for the center of the section view.
- Z: Z position on the drawing sheet for the center of the section view.
- SectionLabel: Letter for the label for the section view.
-
Options: Options that affect the section view as defined in
swCreateSectionViewAtOptions_e
as following.Parameter Name Description swCreateSectionView_ChangeDirection If set, then the direction of this section view is switched; if not set, then the direction of this section view is not switched.
swCreateSectionView_CutSurfaceBodies If set, then shows only the intersecting line of a surface in a section.
swCreateSectionView_DisplaySurfaceCut If set, then only the surfaces cut by the section line apear in the section view; if not set, then all model surfaces appear in the section view.
swCreateSectionView_ExcludeFasteners If set, then fasteners are not included in the section view; if not set, then fasteners are included in the section view.
swCreateSectionView_NotAligned If set, then the section does not snap into alignment with the parent view; if not set, then the section snaps into alignment with the parent view.
swCreateSectionView_OffsetSection If set, then an aligned section view is created (two lines at an angle); if not set, a normal projection section view is created.
swCreateSectionView_Partial If set, then a partial section view is created; if not set, then a complete section view is created.
swCreateSectionView_ScaleWithModel If set, then the section view is scaled with the model; if not set, then the section view is not scaled with the model.
- ExcludedComponents: Array of components to exclude from the section view.
- SectionDepth: Distance from the selected section line.
-
Return Value : This
CreateSectionViewAt5
method return 🚀 View object. -
In our code, I have used following values:
Parameter Name Value Used X 0.2
Y 0.1
Z 0
SectionLabel A
Options swCreateSectionView_NotAligned
ExcludedComponents Nothing
SectionDepth 0
- Reference: For more details please visit 🚀 online SOLIDWORKS API Help.
' Check if we successfully insert view
If insertView Is Nothing Then
MsgBox "Failed to Insert Section View"
Exit Sub
End If
- In above code block, we check if we successfully insert views or not.
- We use 🚀 IF statement for checking.
- Condition:
insertView Is Nothing
- When this condition is
True
,- We show and 🚀 message window to user.
- Message: Failed to Insert Section View.
- Then we stop our macro here.
' Rebuild drawing
swDoc.ForceRebuild3 True
- In above line, we Rebuild drawing.
- For this we use
ForceRebuild3
method which is part of SOLIDWORKS Document variable i.eswDoc
variable.
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 Insert Section View 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!!!