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

The process is almost identical with previous Sketch - Create Lines post.


Content


Demo Video of Code on YouTube

I have not created the video for this post now.

I will upload the video soon for this post and update the post after that.


For Experience Macro Developer - Create a Point From VBA Macro

If you are an experience Solidworks Macro developer, then you are looking for a specific code sample.

Below is the code for creating A Point from Solidworks VBA Macro.

' Creating Variable for Sketch Point
Dim myPoint As SketchPoint
      
' Creating a Point
Set myPoint = swSketchManager.CreatePoint(0, 1, 0)

For creating a Point first you need to Create a variable of SketchPoint type.

After creating variable, you need to set the value of this variable.

For this you used CreatePoint method from Solidworks Sketch Manager.

This CreatePoint method set the value of SketchPoint type variable.

This CreatePoint method takes following parameters as explained:

X : X Location of Point

Y : Y Location of Point

Z : Z Location of Point

If you want a more detail explaination then please read further otherwise this will help you to Create a Point From VBA Macro.


For Beginners Macro Developers - Create a Point From VBA Macro

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

By this method 1st we create a simple point, after that we create a sequence of points.

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

So use this method if you want to create a new Point or sequence of points.

Below is the code sample for creating a Point.

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 Varient for Polygon
  Dim myPoint As SketchPoint
  
  ' Creating a Point
  Set myPoint = swSketchManager.CreatePoint(0, 1, 0)
  
  ' #########Creating a number of points##############
  
  ' Declaring integer type variable for loop
  Dim i As Integer
  
  ' Looping through 1 to 5
  For i = 0 To 5
  
    ' Declaring integer type variables for X, Y and Z cordinates of point
    Dim x, y, z As Integer
    
    ' Setting values of x, y and z
    x = i
    y = x + i
    z = 0
    
    ' Create points till loop continues
    Set myPoint = swSketchManager.CreatePoint(x, y, z)
    
  Next
  
  ' De-select the Polygon 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

' 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 Sketch Point
Dim myPoint As SketchPoint
      
' Creating a Point
Set myPoint = swSketchManager.CreatePoint(0, 1, 0)

In above sample code, we 1st create a variable named myPoint of type SketchPoint.

In 2nd line, we set the value of SketchPoint variable myPoint.

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

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

This CreatePoint method takes following parameters as explained:

X : X Location of Point

Y : Y Location of Point

Z : Z Location of Point

For creating a Sketch Point, I used following parameter Values:

  • X : 0

  • Y : 1

  • Z : 0

This create a point in Y - Direction at the distance of 1.

Below Image described the Parameters for a Point in more detail.

point_parameters

This CreatePoint method returns Point interface i.e. ISketchPoint interface.

This ISketchPoint interface has various methods and properties for a Point.

For more detail about methods and properties of ISketchPoint interface you can visit this page


Creating sequence of Points

Below code describe how to create Sequence of Points.

' Declaring integer type variable for loop
Dim i As Integer

' Looping through 1 to 5
For i = 0 To 5

  ' Declaring integer type variables for X, Y and Z cordinates of point
  Dim x, y, z As Integer
  
  ' Setting values of x, y and z
  x = i
  y = x + i
  z = 0
  
  ' Create points till loop continues
  Set myPoint = swSketchManager.CreatePoint(x, y, z)
  
Next

Let us understand each line of code.

Above Lines of code creates a number of points.

' Declaring integer type variable for loop
Dim i As Integer

In above line, we create a variable named i of integer type.

' Looping through 1 to 5
For i = 0 To 5
  
Next

In above lines, we create a For loop.

This loop iterate the value of i variable from 0 -> 5.

' Declaring integer type variables for X, Y and Z cordinates of point
Dim x, y, z As Integer

' Setting values of x, y and z
x = i
y = x + i
z = 0

In above lines, we 1st declare 3 variable x, y and z of integer type.

x, y and z are co-ordinates of a single point in X, Y and Z direction.

In next 3 lines, we set the values of x, y and z.

For all points, we set the value of z to 0 because we want to place our points in XY plane.

If the value of i = 0, then we set the value of x equal to i.

This makes x = 0 also.

Now, we set the value of y which is equal to SUM of x and i.

Hence for i = 0, x = 0 and y = 0.

For i = 1, x = 1 and y = 2.

' Create points till loop continues
Set myPoint = swSketchManager.CreatePoint(x, y, z)

In above line, we create a Point using CreatePoint function of swSketchManager variable with the values of x, y and z.

Code sample in this section, create a sequence of points.


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 Point after creation
swDoc.ClearSelection2 True

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

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.


VBA Language feature used in this post

In this post used some features of VBA programming language.

This section of post, has some brief information about the VBA programming language specific features.

  1. We use Option Explicit for capturing un-declared variables.

If you want to read more about Option Explicit then please visit Declaring and Scoping of Variables.

  1. Then we create variable for different data types.

If you don’t know about them, then please visit Variables and Data-types posts of this blog.

These posts will help you to understand what Variables are and how to use them.

  1. Then we create main Sub procedure for our macro.

If you don’t know about the Sub procedure, then I suggest you to visit VBA Sub and Function Procedures and Executing Sub and Function Procedures posts of this blog.

These posts will help you to understand what Procedures are and how to use them.

  1. In most part we create some variables and set their values. We set those values by using some functions provided from objects.

If you don’t know about the functions, then you should visit VBA Functions and VBA Functions that do more posts of this blog.

These posts will help you to understand what functions are and how to use them.

  1. For creating a sequence of points, we use a For-Next loop. We use a loop to set values of x, y and z co-ordinates of each points.

If you don’t know about the For-Next loop, then you should visit VBA Looping post of this blog.

This posts will help you to understand what For-Next loop are and how to use them.


Solidworks API Objects

In this post, for creating a Point, we use Solidworks API objects and their methods.

This section contains the list of all Solidworks Objects used in this post.

I have also attached links of these Solidworks API Objects in API Help website.

If you want to explore those objects, you can use these links.

These Solidworks API Objects are listed below:

  • Solidworks Application Object

If you want explore Properties and Methods/Functions of Solidworks Application Object object you can visit this link.

  • Solidworks Document Object

If you want explore Properties and Methods/Functions of Solidworks Document Object object you can visit this link.

  • Solidworks Sketch Manager Object

If you want explore Properties and Methods/Functions of Solidworks Sketch Manager Object you can visit this link.

  • Solidworks Sketch Point Object

If you want explore Properties and Methods/Functions of Solidworks Sketch Point Object you can visit this link.


Hope this post helps you to create a Point 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!!!