Solidworks Macro - Add dimension to Sketch Entities
Introduction
In this post, I tell you about how to Add dimension to Sketch Entities using Solidworks VBA Macros in a Sketch.
In this post, I explain about 2 different methods from which are listed below.
-
From
ModelDoc2
object -
From
ModelDocExtension
object
Methods from these objects are updated methods, hence use any one of them for adding dimension.
Method From ModelDoc2
object
Below is the code
sample to Add
dimension to Sketch Entities using ModelDoc2
object’s AddDimension2
method.
Option Explicit
' Create variable for Solidworks application
Dim swApp As SldWorks.SldWorks
' Create variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2
' Boolean Variable
Dim BoolStatus As Boolean
' Create variable for Solidworks Sketch Manager
Dim swSketchManager As SldWorks.SketchManager
' Create Variable for Solidworks Sketch Segment
Dim swSketchSegment As SldWorks.SketchSegment
' Main function of our VBA program
Sub main()
' Set Solidworks variable to Solidworks application
Set swApp = Application.SldWorks
' Create string type variable for storing default part location
Dim defaultTemplate As String
' Set value of this string type variable to "Default part template"
defaultTemplate = swApp.GetUserPreferenceStringValue(swUserPreferenceStringValue_e.swDefaultTemplatePart)
' Set Solidworks document to new part document
Set swDoc = swApp.NewDocument(defaultTemplate, 0, 0, 0)
'-----------------------UNIT CONVERSION----------------------------------------
' Local variables used as Conversion Factors
Dim LengthConversionFactor As Double
Dim AngleConversionFactor As Double
' Use a Select Case, to get the length of active Unit and set the different factors
Select Case swDoc.GetUnits(0) ' GetUnits function gives us, active unit
Case swMETER ' If length is in Meter
LengthConversionFactor = 1
AngleConversionFactor = 1
Case swMM ' If length is in MM
LengthConversionFactor = 1 / 1000
AngleConversionFactor = 1 * 0.01745329
Case swCM ' If length is in CM
LengthConversionFactor = 1 / 100
AngleConversionFactor = 1 * 0.01745329
Case swINCHES ' If length is in INCHES
LengthConversionFactor = 1 * 0.0254
AngleConversionFactor = 1 * 0.01745329
Case swFEET ' If length is in FEET
LengthConversionFactor = 1 * (0.0254 * 12)
AngleConversionFactor = 1 * 0.01745329
Case swFEETINCHES ' If length is in FEET & INCHES
LengthConversionFactor = 1 * 0.0254 ' For length we use sama as Inch
AngleConversionFactor = 1 * 0.01745329
Case swANGSTROM ' If length is in ANGSTROM
LengthConversionFactor = 1 / 10000000000#
AngleConversionFactor = 1 * 0.01745329
Case swNANOMETER ' If length is in NANOMETER
LengthConversionFactor = 1 / 1000000000
AngleConversionFactor = 1 * 0.01745329
Case swMICRON ' If length is in MICRON
LengthConversionFactor = 1 / 1000000
AngleConversionFactor = 1 * 0.01745329
End Select
'----------------------------------------------------------------
' Select Front Plane
BoolStatus = swDoc.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
' Set Sketch manager for our sketch
Set swSketchManager = swDoc.SketchManager
' Insert a sketch into selected plane
swSketchManager.InsertSketch True
' Create a circle of diameter 10
Set swSketchSegment = swSketchManager.CreateCircle(0, 0, 0, 10 * LengthConversionFactor, 0, 0)
' Disable dimension input box before adding dimension
swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swInputDimValOnCreate, False
' Add dimension to circle
swDoc.AddDimension2 10 * LengthConversionFactor, 10 * LengthConversionFactor, 0
' Enable dimension input box after adding dimension
swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swInputDimValOnCreate, True
' De-select all after creation
swDoc.ClearSelection2 True
' Show Front View after Circular Sketch Pattern
swDoc.ShowNamedView2 "", swStandardViews_e.swFrontView
' Zoom to fit screen in Solidworks Window
swDoc.ViewZoomtofit2
End Sub
Understanding ModelDoc2
object method
Now let us walk through each line in the above code, and understand the meaning and purpose of every line.
I also give some link so that you can go through them if there are anything I explained in previous posts.
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.
' Create variable for Solidworks application
Dim swApp As SldWorks.SldWorks
In this line, we create a variable which we named as swApp
and the type of this swApp
variable is SldWorks.SldWorks
.
' Create variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2
In this line, we create a variable which we named as swDoc
and the type of this swDoc
variable is SldWorks.ModelDoc2
.
' Boolean Variable
Dim BoolStatus As Boolean
In this line, we create a variable named BoolStatus
as Boolean
object type.
' Create 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
' Create variable for Solidworks Sketch Segment
Dim swSketchSegment As SldWorks.SketchSegment
In this line, we Create a variable which we named as swSketchSegment
and the type of this swSketchSegment
variable is SldWorks.SketchSegment
.
We create variable swSketchSegment
for
Solidworks Sketch Segments.
To see methods and properties related to swSketchSegment
object, please visit
this
page of Solidworks API Help
These all are our global variables.
As you can see in code sample, they are Solidworks API Objects.
So basically I group all the Solidworks API Objects in one place.
I have also place boolean
type object
at top also, because after certain point we will need this variable frequently.
Thus, I have started placing it here.
Next is our Sub
procedure which has
name of main
.
This procedure hold all the statements (instructions) we give to computer.
' Set Solidworks variable to Solidworks application
Set swApp = Application.SldWorks
In this line, we set the value of our Solidworks variable swApp
; which we define earlier; to
Solidworks application.
' Create string type variable for storing default part location
Dim defaultTemplate As String
' Set 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
, hold 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 GetUserPreferenceStringValue()
method is a part of our main Solidworks variable swApp
.
' Set 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 detailed information about these 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 that post if you want to understand above code in more detail.
'-----------------------UNIT CONVERSION----------------------------------------
' Local variables used as Conversion Factors
Dim LengthConversionFactor As Double
Dim AngleConversionFactor As Double
' Use a Select Case, to get the length of active Unit and set the different factors
Select Case swDoc.GetUnits(0) ' GetUnits function gives us, active unit
Case swMETER ' If length is in Meter
LengthConversionFactor = 1
AngleConversionFactor = 1
Case swMM ' If length is in MM
LengthConversionFactor = 1 / 1000
AngleConversionFactor = 1 * 0.01745329
Case swCM ' If length is in CM
LengthConversionFactor = 1 / 100
AngleConversionFactor = 1 * 0.01745329
Case swINCHES ' If length is in INCHES
LengthConversionFactor = 1 * 0.0254
AngleConversionFactor = 1 * 0.01745329
Case swFEET ' If length is in FEET
LengthConversionFactor = 1 * (0.0254 * 12)
AngleConversionFactor = 1 * 0.01745329
Case swFEETINCHES ' If length is in FEET & INCHES
LengthConversionFactor = 1 * 0.0254 ' For length we use sama as Inch
AngleConversionFactor = 1 * 0.01745329
Case swANGSTROM ' If length is in ANGSTROM
LengthConversionFactor = 1 / 10000000000#
AngleConversionFactor = 1 * 0.01745329
Case swNANOMETER ' If length is in NANOMETER
LengthConversionFactor = 1 / 1000000000
AngleConversionFactor = 1 * 0.01745329
Case swMICRON ' If length is in MICRON
LengthConversionFactor = 1 / 1000000
AngleConversionFactor = 1 * 0.01745329
End Select
'----------------------------------------------------------------
Above code sample shows how to fix Solidworks API Unit issue.
We 1st get the current unit of the part and apply the switch statements to update our Length and Angle Conversion factors.
I have already explained in detail about Fixing Solidworks API Unit Issue in General - Fix Unit Issue blog post.
Do checkout above post for Fixing Solidworks API Issue.
' Select Front Plane
BoolStatus = swDoc.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
In above 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.
' Set Sketch manager for our sketch
Set swSketchManager = swDoc.SketchManager
In above line, we set the Sketch manager variable to current document’s sketch manager.
' Insert 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.
' Create a circle of diameter 10
Set swSketchSegment = swSketchManager.CreateCircle(0, 0, 0, 10 * LengthConversionFactor, 0, 0)
In above line, we set the value of Solidworks Sketch Segment variable swSketchSegment
by CreateCircle
method from Solidworks
Sketch Manager.
This CreateCircle
method creates a
Circle between “2 given point distance as diameter”.
For more information about CreateCircle
method, you can read my Solidworks Macro -
Create Circle post.
That post describe all the parameters we need for this CreateCircle
method in details.
In above line, we create a Circle with:
-
Circle Centerpoint : At origin i.e. (0, 0, 0)
-
Circle Diameter : 10 unit length
' Disable dimension input box before adding dimension
swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swInputDimValOnCreate, False
In above line of code, we Disable dimension input box before adding dimension.
' Add dimension to circle
swDoc.AddDimension2 10 * LengthConversionFactor, 10 * LengthConversionFactor, 0
For “adding dimension” to an sketch entity, we need AddDimension2
method from
Solidworks ModelDoc2
object.
This AddDimension2
method takes
following parameters as explained:
-
X : X coordinate of Dimension text location in meters.
-
Y : y coordinate of Dimension text location in meters.
-
Z : z coordinate of Dimension text location in meters.
After the function complete following are the results:
Return Value:
- IDisplayDimension: Newly created dimension.
In our code, I have used following values:
-
X : X coordinate of Dimension text location =
10 * LengthConversionFactor
. -
Y : y coordinate of Dimension text location =
10 * LengthConversionFactor
. -
Z : z coordinate of Dimension text location =
0
.
Below image shows before and after Circular Sketch Pattern.
Before Circular Sketch Pattern
After Circular Sketch Pattern
' Enable dimension input box after adding dimension
swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swInputDimValOnCreate, True
In above line of code, we Enable dimension input box after adding dimension.
' De-select the Sketch after creation
swDoc.ClearSelection2 True
In the above line of code, we deselect the Sketch after the Linear Sketch Pattern operation.
For de-selecting, we use ClearSelection2
method from our Solidworks
document name swDoc
.
' Show Front View after Linear Sketch Pattern
swDoc.ShowNamedView2 "", swStandardViews_e.swFrontView
In the above line of code, we update the view orientation to Front View.
In my machine, after inserting a sketch view orientation does not changed.
Because of this I have to update the view to Front view.
For showing Front View we used ShowNamedView2
method from our Solidworks
document name swDoc
.
This method takes 2 parameter described as follows:
-
VName : Name of the view to display or an empty string to use ViewId instead
-
ViewId : ID of the view to display as defined by
swStandardViews_e
or -1 to use the VName argument instead.
NOTE: If you specify both VName and ViewId, then ViewId takes precedence if the two arguments do not resolve to the same view.
swStandardViews_e
has following
Standard View Types:
-
swBackView
-
swBottomView
-
swDimetricView
-
swFrontView
-
swIsometricView
-
swLeftView
-
swRightView
-
swTopView
-
swExtendetricView
In our code, we did not use VName instead I used empty string in form of ”“ symbol.
I used ViewId value to specify view and used swStandardViews_e.swFrontView
value to use
Standard Front View.
' 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
.
Method From ModelDocExtension
object
Below is the code
sample to Add
dimension to Sketch Entities using ModelDocExtension
object’s AddDimension
method
Option Explicit
' Create variable for Solidworks application
Dim swApp As SldWorks.SldWorks
' Create variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2
' Boolean Variable
Dim BoolStatus As Boolean
' Create variable for Solidworks Sketch Manager
Dim swSketchManager As SldWorks.SketchManager
' Create Variable for Solidworks Sketch Segment
Dim swSketchSegment As SldWorks.SketchSegment
' Create Variable for Solidworks Display Dimension
Dim swDisplayDim As SldWorks.DisplayDimension
' Main function of our VBA program
Sub main()
' Set Solidworks variable to Solidworks application
Set swApp = Application.SldWorks
' Create string type variable for storing default part location
Dim defaultTemplate As String
' Set value of this string type variable to "Default part template"
defaultTemplate = swApp.GetUserPreferenceStringValue(swUserPreferenceStringValue_e.swDefaultTemplatePart)
' Set Solidworks document to new part document
Set swDoc = swApp.NewDocument(defaultTemplate, 0, 0, 0)
'-----------------------UNIT CONVERSION----------------------------------------
' Local variables used as Conversion Factors
Dim LengthConversionFactor As Double
Dim AngleConversionFactor As Double
' Use a Select Case, to get the length of active Unit and set the different factors
Select Case swDoc.GetUnits(0) ' GetUnits function gives us, active unit
Case swMETER ' If length is in Meter
LengthConversionFactor = 1
AngleConversionFactor = 1
Case swMM ' If length is in MM
LengthConversionFactor = 1 / 1000
AngleConversionFactor = 1 * 0.01745329
Case swCM ' If length is in CM
LengthConversionFactor = 1 / 100
AngleConversionFactor = 1 * 0.01745329
Case swINCHES ' If length is in INCHES
LengthConversionFactor = 1 * 0.0254
AngleConversionFactor = 1 * 0.01745329
Case swFEET ' If length is in FEET
LengthConversionFactor = 1 * (0.0254 * 12)
AngleConversionFactor = 1 * 0.01745329
Case swFEETINCHES ' If length is in FEET & INCHES
LengthConversionFactor = 1 * 0.0254 ' For length we use sama as Inch
AngleConversionFactor = 1 * 0.01745329
Case swANGSTROM ' If length is in ANGSTROM
LengthConversionFactor = 1 / 10000000000#
AngleConversionFactor = 1 * 0.01745329
Case swNANOMETER ' If length is in NANOMETER
LengthConversionFactor = 1 / 1000000000
AngleConversionFactor = 1 * 0.01745329
Case swMICRON ' If length is in MICRON
LengthConversionFactor = 1 / 1000000
AngleConversionFactor = 1 * 0.01745329
End Select
'----------------------------------------------------------------
' Select Front Plane
BoolStatus = swDoc.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
' Set Sketch manager for our sketch
Set swSketchManager = swDoc.SketchManager
' Insert a sketch into selected plane
swSketchManager.InsertSketch True
' Create a circle of diameter 10
Set swSketchSegment = swSketchManager.CreateCircle(0, 0, 0, 10 * LengthConversionFactor, 0, 0)
' Disable dimension input box before adding dimension
swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swInputDimValOnCreate, False
' Add dimension to circle
Set swDisplayDim = swDoc.Extension.AddDimension(0, 12 * LengthConversionFactor, 0, swSmartDimensionDirection_e.swSmartDimensionDirection_Right)
' Enable dimension input box after adding dimension
swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swInputDimValOnCreate, True
' De-select all after creation
swDoc.ClearSelection2 True
' Show Front View after Circular Sketch Pattern
swDoc.ShowNamedView2 "", swStandardViews_e.swFrontView
' Zoom to fit screen in Solidworks Window
swDoc.ViewZoomtofit2
End Sub
Understanding ModelDocExtension
object method
Now let us walk through each line in the above code, and understand the meaning and purpose of every line.
I also give some link so that you can go through them if there are anything I explained in previous posts.
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.
' Create variable for Solidworks application
Dim swApp As SldWorks.SldWorks
In this line, we create a variable which we named as swApp
and the type of this swApp
variable is SldWorks.SldWorks
.
' Create variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2
In this line, we create a variable which we named as swDoc
and the type of this swDoc
variable is SldWorks.ModelDoc2
.
' Boolean Variable
Dim BoolStatus As Boolean
In this line, we create a variable named BoolStatus
as Boolean
object type.
' Create 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
' Create variable for Solidworks Sketch Segment
Dim swSketchSegment As SldWorks.SketchSegment
In this line, we Create a variable which we named as swSketchSegment
and the type of this swSketchSegment
variable is SldWorks.SketchSegment
.
We create variable swSketchSegment
for
Solidworks Sketch Segments.
To see methods and properties related to swSketchSegment
object, please visit
this
page of Solidworks API Help
' Create Variable for Solidworks Display Dimension
Dim swDisplayDim As SldWorks.DisplayDimension
In this line, we Create a variable which we named as swDisplayDim
and the type of this swDisplayDim
variable is SldWorks.DisplayDimension
.
We create variable swDisplayDim
for
dimension we create.
These all are our global variables.
As you can see in code sample, they are Solidworks API Objects.
So basically I group all the Solidworks API Objects in one place.
I have also place boolean
type object
at top also, because after certain point we will need this variable frequently.
Thus, I have started placing it here.
Next is our Sub
procedure which has
name of main
.
This procedure hold all the statements (instructions) we give to computer.
' Set Solidworks variable to Solidworks application
Set swApp = Application.SldWorks
In this line, we set the value of our Solidworks variable swApp
; which we define earlier; to
Solidworks application.
' Create string type variable for storing default part location
Dim defaultTemplate As String
' Set 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
, hold 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 GetUserPreferenceStringValue()
method is a part of our main Solidworks variable swApp
.
' Set 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 detailed information about these 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 that post if you want to understand above code in more detail.
'-----------------------UNIT CONVERSION----------------------------------------
' Local variables used as Conversion Factors
Dim LengthConversionFactor As Double
Dim AngleConversionFactor As Double
' Use a Select Case, to get the length of active Unit and set the different factors
Select Case swDoc.GetUnits(0) ' GetUnits function gives us, active unit
Case swMETER ' If length is in Meter
LengthConversionFactor = 1
AngleConversionFactor = 1
Case swMM ' If length is in MM
LengthConversionFactor = 1 / 1000
AngleConversionFactor = 1 * 0.01745329
Case swCM ' If length is in CM
LengthConversionFactor = 1 / 100
AngleConversionFactor = 1 * 0.01745329
Case swINCHES ' If length is in INCHES
LengthConversionFactor = 1 * 0.0254
AngleConversionFactor = 1 * 0.01745329
Case swFEET ' If length is in FEET
LengthConversionFactor = 1 * (0.0254 * 12)
AngleConversionFactor = 1 * 0.01745329
Case swFEETINCHES ' If length is in FEET & INCHES
LengthConversionFactor = 1 * 0.0254 ' For length we use sama as Inch
AngleConversionFactor = 1 * 0.01745329
Case swANGSTROM ' If length is in ANGSTROM
LengthConversionFactor = 1 / 10000000000#
AngleConversionFactor = 1 * 0.01745329
Case swNANOMETER ' If length is in NANOMETER
LengthConversionFactor = 1 / 1000000000
AngleConversionFactor = 1 * 0.01745329
Case swMICRON ' If length is in MICRON
LengthConversionFactor = 1 / 1000000
AngleConversionFactor = 1 * 0.01745329
End Select
'----------------------------------------------------------------
Above code sample shows how to fix Solidworks API Unit issue.
We 1st get the current unit of the part and apply the switch statements to update our Length and Angle Conversion factors.
I have already explained in detail about Fixing Solidworks API Unit Issue in General - Fix Unit Issue blog post.
Do checkout above post for Fixing Solidworks API Issue.
' Select Front Plane
BoolStatus = swDoc.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
In above 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.
' Set Sketch manager for our sketch
Set swSketchManager = swDoc.SketchManager
In above line, we set the Sketch manager variable to current document’s sketch manager.
' Insert 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.
' Create a circle of diameter 10
Set swSketchSegment = swSketchManager.CreateCircle(0, 0, 0, 10 * LengthConversionFactor, 0, 0)
In above line, we set the value of Solidworks Sketch Segment variable swSketchSegment
by CreateCircle
method from Solidworks
Sketch Manager.
This CreateCircle
method creates a
Circle between “2 given point distance as diameter”.
For more information about CreateCircle
method, you can read my Solidworks Macro -
Create Circle post.
That post describe all the parameters we need for this CreateCircle
method in details.
In above line, we create a Circle with:
-
Circle Centerpoint : At origin i.e. (0, 0, 0)
-
Circle Diameter : 10 unit length
' Disable dimension input box before adding dimension
swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swInputDimValOnCreate, False
In above line of code, we Disable dimension input box before adding dimension.
' Add dimension to circle
Set swDisplayDim = swDoc.Extension.AddDimension(0, 12 * LengthConversionFactor, 0, swSmartDimensionDirection_e.swSmartDimensionDirection_Right)
For “adding dimension” to an sketch entity, we need AddDimension
method from
Solidworks ModelDocExtension
object.
This AddDimension
method takes
following parameters as explained:
-
X : X coordinate of Dimension text location in meters.
-
Y : y coordinate of Dimension text location in meters.
-
Z : z coordinate of Dimension text location in meters.
-
Direction : Direction of dimensioning extension line or rapid dimensioning quadrant as defined in
swSmartDimensionDirection_e
.
There are 4 values we can use from swSmartDimensionDirection_e
enum.
They are as follows:
-
swSmartDimensionDirection_Down
-
swSmartDimensionDirection_Left
-
swSmartDimensionDirection_Right
-
swSmartDimensionDirection_Up
After the function complete following are the results:
Return Value:
- IDisplayDimension: Newly created dimension.
In our code, I have used following values:
-
X : X coordinate of Dimension text location =
0
. -
Y : y coordinate of Dimension text location =
12 * LengthConversionFactor
. -
Z : z coordinate of Dimension text location =
0
. -
Direction : Direction of dimensioning extension line or rapid dimensioning quadrant =
swSmartDimensionDirection_e.swSmartDimensionDirection_Right
.
Below image shows before and after Circular Sketch Pattern.
Before Circular Sketch Pattern
After Circular Sketch Pattern
' Enable dimension input box after adding dimension
swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swInputDimValOnCreate, True
In above line of code, we Enable dimension input box after adding dimension.
' De-select the Sketch after creation
swDoc.ClearSelection2 True
In the above line of code, we deselect the Sketch after the Linear Sketch Pattern operation.
For de-selecting, we use ClearSelection2
method from our Solidworks
document name swDoc
.
' Show Front View after Linear Sketch Pattern
swDoc.ShowNamedView2 "", swStandardViews_e.swFrontView
In the above line of code, we update the view orientation to Front View.
In my machine, after inserting a sketch view orientation does not changed.
Because of this I have to update the view to Front view.
For showing Front View we used ShowNamedView2
method from our Solidworks
document name swDoc
.
This method takes 2 parameter described as follows:
-
VName : Name of the view to display or an empty string to use ViewId instead
-
ViewId : ID of the view to display as defined by
swStandardViews_e
or -1 to use the VName argument instead.
NOTE: If you specify both VName and ViewId, then ViewId takes precedence if the two arguments do not resolve to the same view.
swStandardViews_e
has following
Standard View Types:
-
swBackView
-
swBottomView
-
swDimetricView
-
swFrontView
-
swIsometricView
-
swLeftView
-
swRightView
-
swTopView
-
swExtendetricView
In our code, we did not use VName instead I used empty string in form of ”“ symbol.
I used ViewId value to specify view and used swStandardViews_e.swFrontView
value to use
Standard Front View.
' 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
.
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 Add dimension to Sketch Entities with Solidworks VBA Macros.
For more such tutorials on Solidworks VBA Macro, do come to this blog 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!!!