Solidworks Macro - Create Tangent Arc

7 minute read

In this post, I tell you about how to create a Tangent Arc through Solidworks VBA Macros in a sketch.

The process is almost identical with previous Solidworks Sketch Macros - Create Line post.

In this post, I tell you about CreateTangentArc method from Solidworks SketchManager object.

This method is most updated method, I found in Solidworks API Help.

So use this method if you want to create a new Tangent Arc.


Video of Code on YouTube

Please see below video on how to create a Tangent Arc from Solidworks VBA Macros.


Please note that there are no explaination in the video.

Explaination of each line and why we write code this way is given in this post.


Code Sample

Below is the code sample for creating a Tangent Arc.

Option Explicit

' Creating variable for Solidworks application
Dim swApp As SldWorks.SldWorks
' Creating variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2
' Boolean Variable
Dim BoolStatus As Boolean
' Creating variable for Solidworks Sketch Manager
Dim swSketchManager As SldWorks.SketchManager

' Main function of our VBA program
Sub main()

  ' Setting Solidworks variable to Solidworks application
  Set swApp = Application.SldWorks
  
  ' Creating string type variable for storing default part location
  Dim defaultTemplate As String
  ' Setting value of this string type variable to "Default part template"
  defaultTemplate = swApp.GetUserPreferenceStringValue(swUserPreferenceStringValue_e.swDefaultTemplatePart)

  ' Setting Solidworks document to new part document
  Set swDoc = swApp.NewDocument(defaultTemplate, 0, 0, 0)

  ' Selecting Front Plane
  BoolStatus = swDoc.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
  
  ' Setting Sketch manager for our sketch
  Set swSketchManager = swDoc.SketchManager
  
  ' Inserting a sketch into selected plane
  swSketchManager.InsertSketch True
  
  ' Creating Variable for Solidworks Sketch segment
  Dim mySketchSegment As SketchSegment
      
  ' Creating an horizontal line
  Set mySketchSegment = swSketchManager.CreateLine(0, 0, 0, 0, 1, 0)
  
  ' Creating a Tangent Arc
  Set mySketchSegment = swSketchManager.CreateTangentArc(0, 1, 0, 2, 1, 0, swTangentArcTypes_e.swForward)
  
  ' De-select the Arc after creation
  swDoc.ClearSelection2 True
  
  ' Zoom to fit screen in Solidworks Window
  swDoc.ViewZoomtofit

End Sub

Understanding the Code

Now let us walk through each line in the above code, and understand the meaning of every line.

Option Explicit

This line forces us to define every variable we are going to use.

For more information please visit Solidworks Macros - Open new Part document post.

' Creating variable for Solidworks application
Dim swApp As SldWorks.SldWorks

In this line, we are creating a variable which we named as swApp and the type of this swApp variable is SldWorks.SldWorks.

' Creating variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2

In this line, we are creating a variable which we named as swDoc and the type of this swDoc variable is SldWorks.ModelDoc2.

Next is our Sub procedure named as main. This procedure hold all the statements (instructions) we give to computer.

' Setting Solidworks variable to Solidworks application
Set swApp = Application.SldWorks

In this line, we are setting the value of our Solidworks variable swApp which we defined earlier to Solidworks application.

' Creating string type variable for storing default part location
Dim defaultTemplate As String
' Setting value of this string type variable to "Default part template"
defaultTemplate = swApp.GetUserPreferenceStringValue(swUserPreferenceStringValue_e.swDefaultTemplatePart)

In 1st statement of above example, we are defining a variable of string type and named it as defaultTemplate.

This variable defaultTemplate, holds the location the location of Default Part Template.

In 2nd line of above example. we assign value to our newly define defaultTemplate variable.

We assign the value by using a Method named GetUserPreferenceStringValue().

This method is a part of our main Solidworks variable swApp.

' Setting Solidworks document to new part document
Set swDoc = swApp.NewDocument(defaultTemplate, 0, 0, 0)

In this line, we set the value of our swDoc variable to new document.

For more detailed information about above lines please visit Solidworks Macros - Open new Part document post.

I have discussed them thoroghly in Solidworks Macros - Open new Part document post, so do checkout this post if you don’t understand above code.

' Boolean Variable
Dim BoolStatus As Boolean

' Selecting Front Plane
BoolStatus = swDoc.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, swSelectOption_e.swSelectOptionDefault)

In 1st line, we create a variable named BoolStatus as Boolean object.

In next line, we select the front plane by using SelectByID2 method from Extension object.

For more information about selection method please visit Solidworks Macros - Selection Methods post.

I have discussed about different Selection methods in details in Soldworks Macros - Selection Methods post, so do visit this post for more Selection methods.

' Creating variable for Solidworks Sketch Manager
Dim swSketchManager As SldWorks.SketchManager

In above line, we create variable swSketchManager for Solidworks Sketch Manager.

As the name suggested, a Sketch Manager holds variours methods and properties to manage Sketches.

To see methods and properties related to SketchManager object, please visit this page of Solidworks API Help

' Setting Sketch manager for our sketch
Set swSketchManager = swDoc.SketchManager

In above line, we set the Sketch manager variable to current document’s sketch manager.

' Inserting a sketch into selected plane
swSketchManager.InsertSketch True

In above line, we use InsertSketch method of SketchManager and give True value.

This method allows us to insert a sketch in selected plane.

' Creating Variable for Solidworks Sketch segment
Dim mySketchSegment As SketchSegment
    
' Creating an horizontal line
Set mySketchSegment = swSketchManager.CreateLine(0, 0, 0, 0, 1, 0)

' Creating a Tangent Arc
Set mySketchSegment = swSketchManager.CreateTangentArc(0, 1, 0, 2, 1, 0, swTangentArcTypes_e.swForward)

In above sample code, we 1st create a variable named mySketchSegment of type SketchSegment.

A SketchSegment represent a line, ellipse, parabola or spline.

A SketchSegment provides functions that are generic to every type of sketch segment.

For example, every sketch segment has an ID and can be selected programmatically.

Therefore, the SketchSegment interface provides functions to obtain the ID and to select the item.

For detailed information about the SketchSegment please visit this page of Solidworks API Help

In 2nd line, we set the value of sketch segment variable mySketchSegment.

We get this value from CreateLine method which is inside the swSketchManager variable.

swSketchManager variable is a type of SketchManager, hence we used CreateLine method from SketchManager.

For detailed information on Creating Sketch Line visit Solidworks Sketch Macros - Create Line

In 3rd line, we again set the value of sketch segment variable mySketchSegment.

We get this value from CreateTangentArc method which is inside the swSketchManager variable.

swSketchManager variable is a type of SketchManager, hence we used CreateTangentArc method from SketchManager.

This CreateTangentArc method takes following parameters as explained:

  • X1 : X coordinate of the start point, of the arc

  • Y1 : Y coordinate of the start point, of the arc

  • Z1 : Z coordinate of the start point, of the arc

  • X2 : X coordinate of the end point, of the arc

  • Y2 : Y coordinate of the end point, of the arc

  • Z2 : Z coordinate of the end point, of the arc

  • Arc Type : Types of Tangent Arcs as defined in swTangentArcTypes_e enum list.

Below are the values inside swTangentArcTypes_e

  • swBack = 3

  • swForward = 1

  • swLeft = 2

  • swRight = 4

In the above code sample, first I create a Sketch line and then at one end of the line I create a Tangent arc.

For creating a Sketch line, I have used (0, 0, 0) which is at origin as starting point of the line.

For End point of the line, I used (0, 1, 0) which is 1 point distance in Y-direction.

For creating a Tangent arc, I used End point of line i.e. (0, 1, 0) as the Start point for Tangent Arc.

For End point of Tangent arc, I used (2, 1, 0) which is 2 point distance in X-direction and 1 point distance in Y-direction.

Basically, I want to end the Tangent arc at the same height of the Sketch line.

For Arc type, I have used swForward as value.

This creates a forward clockwise Tangent arc.

This CreateTangentArc method returns sketch segments which represent the sides created for this Tangent arc.

A Sketch Segment can represent a sketch arc, line, ellipse, parabola or spline.

Sketch Segment has ISketchSegment Interface, which provides functions that are generic to every type of sketch segment.

For example, every sketch segment has an ID and can be programmatically selected.

Therefore, the ISketchSegment interface provides functions to obtain the ID and to select the item.

NOTE

It is very important to remember that, when you give distance or any other numeric value in Solidworks API, Solidworks takes that numeric value in Meter only.

Solidworks API does not care about your application’s Unit systems.

For example, I works in ANSI system means “inches” for distance.

But when I used Solidworks API through VBA macros or C#, I have to use converted numeric values.

Because Solidworks API output the distance in Meter only; which is not my requirement.

' De-select the Arc after creation
swDoc.ClearSelection2 True

In the this line of code, we de-select the created Tangent Arc.

For de-selecting, we use ClearSelection2 method from our Solidworks document variable swDoc.

' Zoom to fit screen in Solidworks Window
swDoc.ViewZoomtofit

In this last line we use zoom to fit command.

For Zoom to fit, we use ViewZoomtofit method from our Solidworks document variable swDoc.

Hope this post helps you to create Tangent Arc in Sketches with Solidworks VB Macros.

For more such tutorials on Solidworks VBA Macros, do come to this blog after sometime.

Till then, Happy learning!!!

Updated: