Solidworks Macro - Convert to Construction Sketch
In this post, I tell you about Convert to Construction Sketch in a Sketch.
In this post, I explain about CreateConstructionGeometry
method from
Solidworks’s SketchManager
object.
This method is most updated method, I found in Solidworks API Help.
This post will utilize the methods explained in earlier posts, hence knowledge to those is required but it is not mandatory.
An absolute beginner can follow what is written here.
Code Sample
Below is the code
sample to
Convert Sketch Segment to Construction Sketch.
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
Sub main()
' Set Solidworks variable to Solidworks application
Set swApp = Application.SldWorks
' Set Solidworks document to active part document
Set swDoc = swApp.ActiveDoc
' Select Circle
BoolStatus = swDoc.Extension.SelectByID2("Arc1", "SKETCHSEGMENT", 0, 0, 0, True, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
' Convert to construction circle
swDoc.SketchManager.CreateConstructionGeometry
' De-select the Sketch Segment after Convert to construction
swDoc.ClearSelection2 True
' Show Front View
swDoc.ShowNamedView2 "", swStandardViews_e.swFrontView
' Zoom to fit screen in Solidworks Window
swDoc.ViewZoomtofit2
End Sub
Understanding the Code
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.
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.
' Set Solidworks document to active part document
Set swDoc = swApp.ActiveDoc
In this line, we set the value of our Solidworks variable swDoc
to currently opened part document as
shown below in our post.
' Select Circle we want to Pattern
BoolStatus = swDoc.Extension.SelectByID2("Arc1", "SKETCHSEGMENT", 0, 0, 0, True, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
In above line of code, we select the Circle i.e. Arc 1 and add it to selection list.
' Convert to construction circle
swDoc.SketchManager.CreateConstructionGeometry
In above line, we convert the selected Solidworks Sketch Segment i.e. our circle CreateConstructionGeometry
method from
Solidworks Sketch Manager.
Please note that here we did not create another variable for Sketch Manger like I have done previously.
This CreateConstructionGeometry
method
takes NO parameter and did not return any value.
Before Convert Circle
After Convert Circle
' De-select the Sketch after creation
swDoc.ClearSelection2 True
In the above line of code, we deselect the Sketch after the Circular Sketch Pattern operation.
For de-selecting, we use ClearSelection2
method from our Solidworks
document name swDoc
.
' Show Front View
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 Convert to Construction Sketch 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!!!