Solidworks VBA Macro - Insert Auxilary View

In this article, we understand β€œhow to” Insert Auxilary View in Drawing document from VBA macro.

We will insert Auxilary View.

This is most updated method of Insert Auxilary View in an drawing document.

Results We Can Get

Below image shows the result we get.

drawing-insert-auxilaryview
Figure 1: Insert Auxilary View in Drawing

We Insert Auxilary View in simple manners.

There are no extra steps required.

Important

To get the correct result, please follow the steps correctly.



Macro Video

Below 🎬 video shows how to Insert Auxilary View from SOLIDWORKS VBA Macros.

How to Insert Auxilary View in Drawing

Important

Please note that there are no explanations in the video.
Explanation of each step and why we write code this way is provided in this post.

VBA Macro

Below is the VBA macro for Insert Auxilary 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 Auxiliary 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 Auxiliary View
  Set modelView = swDrawing.CreateAuxiliaryViewAt2(0.2, 0.1, 0, False, "A", True, True)
  
  ' Check if we successfully insert Auxiliary view
  If modelView Is Nothing Then
    MsgBox "Failed to Insert Auxiliary View."
    Exit Sub
  End If

End Sub

Prerequisite

There are some prerequisites for this article.

  • Knowledge of VBA programming language is ❗required.
  • We create Auxiliary view from an existing (Base) view .
  • We already select an edge in this existing (Base) view.
prerequisite
Figure 2: Prerequisite
Error Prevention

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:

  1. Create Global Variables
  2. Initialize Variables
  3. Insert Auxilary Views

Every section with each line is explained below.

Note

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
' 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 Auxilary View
Sub main()

End Sub
  • In above line, we create Program to Insert Auxilary View.
  • This is a Sub procedure which has name of main.
  • 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 Auxilary Views

In this section, we Insert Auxilary Views action.

' Insert Auxiliary View
Set modelView = swDrawing.CreateAuxiliaryViewAt2(0.2, 0.1, 0, False, "A", True, True)
  • In above code, we Insert Auxilary View into Drawing.
  • For this, we use CreateAuxiliaryViewAt2 method.
  • This CreateAuxiliaryViewAt2 method is part of swDrawing variable.
  • This method takes following parameters.
    • X: X position for the auxiliary view.
    • Y: Y position for the auxiliary view.
    • Z: Z position for the auxiliary view.
    • NotAligned: * True aligns the view from its owner, False does not.*
    • Label: String that holds label of the auxiliary view.
    • Showarrow: * True shows the arrow, False hides the arrow.*
    • Flip: * True flips the side shown in the auxiliary view, False does not.*
  • Return Value : This CreateAuxiliaryViewAt2 method return πŸš€ View data object.

  • In our code, I have used following values:
Table 1: CreateAuxiliaryViewAt2 Parameters Used
Parameter NameValue Used
X 0.2
Y 0.1
Z 0
NotAligned False
Label A
Showarrow True
Flip True
' Check if we successfully insert view
If modelView Is Nothing Then
  MsgBox "Failed to Insert Auxilary 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 Auxilary 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.

drawing-insert-auxilaryview
Figure 1: Insert Auxilary View in Drawing

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 Auxilary 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!!!