Solidworks VBA Macro - Insert Model View
Objective
In this article, we understand โhow toโ Insert Model View in Drawing document from VBA macro.
We will insert Insert Model View.
This is most updated method of Insert Model View in an drawing document.
Results We Can Get
Below image shows the result we get.
We Insert Model 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 Model 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 Model 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 modelView As SldWorks.View
' Program to Insert Model View
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
' Insert Model View [Front View]
Set modelView = swDrawing.CreateDrawViewFromModelView3(openedFilePath, "*Front", 0.1, 0.1, 0)
' Check if we successfully insert Model view
If modelView Is Nothing Then
MsgBox "Failed to Insert Model View."
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 Model 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 modelView As SldWorks.View
- Purpose: In above line, we create a variable for Solidworks View.
- Variable Name:
modelView
- Type:
SldWorks.View
- Reference: Please visit ๐ online SOLIDWORKS API Help.
These all are our global variables.
They are SOLIDWORKS API Objects.
' Program to Insert Model View
Sub main()
End Sub
- In above line, we create Program to Insert Model 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.
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 Model View
In this section, we Insert Model View 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.
' Insert Model View [Front View]
Set modelView = swDrawing.CreateDrawViewFromModelView3(openedFilePath, "*Front", 0.1, 0.1, 0)
- In above code, we Insert Model View into Drawing.
- For this, we use
CreateDrawViewFromModelView3
method. - This
CreateDrawViewFromModelView3
method is part ofswDrawing
variable. - This method takes following parameters.
- ModelName: Full pathname of the model document for which to create the drawing view.
- ViewName: Name of the model view from which to create the drawing view. For example โFrontโ for Front view.*
- LocX: X location of drawing view center in meters.
- LocY: Y location of drawing view center in meters.
- LocZ: Z location of drawing view center in meters.
-
Return Value : This
CreateDrawViewFromModelView3
method return ๐ View data object. -
In our code, I have used following values:
Parameter Name Value Used ModelName openedFilePath
ViewName *Front
LocX 0.1
LocY 0.1
LocZ 0
- Reference: For more details please visit ๐ online SOLIDWORKS API Help.
' Check if we successfully insert views
If modelView Is Nothing Then
MsgBox "Failed to Insert Model 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:
modelView Is Nothing
- When this condition is
True
,- We show and ๐ message window to user.
- Message: *Failed to Insert Model View.
- 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 Insert Model 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!!!