Solidworks Macro - Selection Methods
In this post, we select Default planes with following methods:
-
By
SelectByID
method FromModelDoc2
Interface -
By
SelectByID2
method FromIModelDocExtension
Interface
By SelectByID method From ModelDoc2 Interface
SelectByID
method From ModelDoc2
Interface is the easiest method
for selecting Default plane.
I will explain the use of this method in 2 different scenerio as follows:
-
Using this method in the previous example of creating a new document and then select a Plane.
-
Using this method in an open document.
Using SelectByID method in previous example
In the previous 2 posts, we learned how to create a new part document, an assembly document, and a drawing document.
Now we use the same code and extended it for using selecting planes.
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
' 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.SelectByID("Front Plane", "PLANE", 0, 0, 0)
End Sub
Above code, 1st create a new part document and then select “Front Plane” in VBA macro.
To select the plane, I have to add 2 lines. 1st I create a boolean varible above main function as shown in below code:
' Boolean Variable
Dim BoolStatus As Boolean
Then I use this BoolStatus
variable to
selecting Front Plane as shown in below code:
' Selecting Front Plane
BoolStatus = swDoc.SelectByID("Front Plane", "PLANE", 0, 0, 0)
SelectByID
takes following parameters:
-
Name : Name of the object or an empty string
-
Type : Type of object in uppercase or an empty string
-
X : X selection location
-
Y : Y selection location
-
Z : Z selection location
Return Value -
If the item is select then this method returns True
and otherwise False
.
Since this method returns True
or False
, hence we use a boolean
variable to perfom this method.
If we want to select Right Plane then we just need to replace "Front Plane"
-> "Right Plane"
in previous code sample.
Similar for selecting Top Plane, we need to replace "Front Plane"
-> "Top Plane"
in previous code sample.
Using SelectByID method in an Open document
For using SelectByID
method in an open
document we use differnet code sample.
The code sample is given below:
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
' Main function of our VBA program
Sub main()
' Setting Solidworks variable to Solidworks application
Set swApp = Application.SldWorks
' Setting Solidworks document to active open document
Set swDoc = swApp.ActiveDoc
' Selecting Front Plane
BoolStatus = swDoc.SelectByID("Front Plane", "PLANE", 0, 0, 0)
End Sub
Most of the things in this code sample is already explained previous post and in previous section of this very post.
In this code I have set the Solidworks document variable swDoc
to active open document.
And then we use similar method to select “Front Plane”.
As explained in previous section we can select Right Plane and Top Plane.
By SelectByID2 method From IModelDocExtension Interface
This method is similar to SelectByID
from previous section infact it is the updated version of previous method.
Since it is updated and has additional functionality, I will explain it I frequently use this method. Previous method is easiest method, by far, for selecting objects.
Since SelectByID2
is updated version of
SelectByID
it has similar syntax and
usage.
I will explain the use of SelectByID2
method in 2 different scenerio as done in previous method:
-
Using this method in the previous example of creating a new document and then select a Plane.
-
Using this method in an open document.
Using SelectByID2 method in previous example
Now we use the same code and extended it for using selecting planes by SelectByID2
method.
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
' 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)
End Sub
This method is doing the same thing which I already explained in the previous method.
The difference is in the syntax and input parameters.
' Selecting Front Plane
BoolStatus = swDoc.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
As you can see from the above code sample, this method is a method of Extension
interface/object.
This Extension
interface/object is part
of swDoc
variable. This variable is the
type of ModelDoc2
interface/object.
Apart from that, we need to give additional input parameters also.
Below I have explained about the input parameters of SelectByID2
method:
-
Name : Name of the object or an empty string
-
Type : Type of object in uppercase or an empty string
-
X : X selection location
-
Y : Y selection location
-
Z : Z selection location
-
Append :
True
orFalse
. It is used the adding the selection into selection list. -
Mark : Value you want to use as a Mark; this value is used by other functions that require ordered selection.
-
Callout : Pointer to the
Callout
interface/object. (Default value most of the time isnothing
.) -
SelectOption : Selection option. You can use either
swSelectOptionDefault
orswSelectOptionExtensive
value.
Return Value -
If the item is select then this method returns True
and otherwise False
.
There is a lot more about this method in the Remark section. For more information visit here.
Since this method returns True
or False
, here also we use a boolean
variable to perfom this method.
If we want to select Right Plane then we just need to replace "Front Plane"
-> "Right Plane"
in previous code sample.
Similar for selecting Top Plane, we need to replace "Front Plane"
-> "Top Plane"
in previous code sample.
Using SelectByID2 method in an Open document
For using SelectByID2
method in an open
document we use same code sample as used in previous method.
The code sample is given below:
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
' Main function of our VBA program
Sub main()
' Setting Solidworks variable to Solidworks application
Set swApp = Application.SldWorks
' Setting Solidworks document to active open document
Set swDoc = swApp.ActiveDoc
' Selecting Front Plane
BoolStatus = swDoc.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
End Sub
Most of the things in this code sample is already explained previous post and in previous section of this very post.
In this code I have set the Solidworks document variable swDoc
to active open document.
And then we use similar method to select “Front Plane”.
As explained in previous section we can select Right Plane and Top Plane.
This is all for now. This post is getting too long.
In next post we learn about Open Saved Documents.