Solidworks Macro - Toggle (Hide/Show) Sketch Relations
In this post, I tell you about how to Toggle (Hide/Show) Sketch Relations using Solidworks VBA Macros in a Sketch.
This post is extension to Sketch Transformation - Rotate/Copy Sketch Entities post.
Hence I will explained only Toggle (Hide/Show) Sketch Relations related code.
In this post, I explain about SetUserPreferenceToggle
method from
Solidworks’s ModelDoc2
object.
This method is NOT updated method, but it is easiest way to Toggle (Hide/Show) Sketch Relations.
Video of Code on YouTube
Please see below video on how to Toggle (Hide/Show) Sketch Relations 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 to
Toggle (Hide/Show) Sketch Relations.
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 local variable for CenterPoint ractangle
Dim vSketch As Variant
' Create CenterPoint ractangle
vSketch = swSketchManager.CreateCenterRectangle(0, 0, 0, 1 * LengthConversionFactor, 1 * LengthConversionFactor, 0)
' De-select the lines after creation
swDoc.ClearSelection2 True
' Select all lines of CenterPoint Ractangle
BoolStatus = swDoc.Extension.SelectByID2("Line1", "SKETCHSEGMENT", 0, 0, 0, True, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
BoolStatus = swDoc.Extension.SelectByID2("Line2", "SKETCHSEGMENT", 0, 0, 0, True, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
BoolStatus = swDoc.Extension.SelectByID2("Line3", "SKETCHSEGMENT", 0, 0, 0, True, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
BoolStatus = swDoc.Extension.SelectByID2("Line4", "SKETCHSEGMENT", 0, 0, 0, True, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
' Rotate CenterPoint Ractangle by 45 degree only
swDoc.Extension.RotateOrCopy True, 2, True, 0, 0, 0, 0, 0, 1, 45 * AngleConversionFactor
' Toggle (Hide/Show) Sketch Relations
BoolStatus = swDoc.SetUserPreferenceToggle(swUserPreferenceToggle_e.swViewSketchRelations, 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 the Code
I have already discuss above code in previous Sketch Transformation - Rotate/Copy Sketch Entities post except below line of code.
' Toggle (Hide/Show) Sketch Relations
BoolStatus = swDoc.SetUserPreferenceToggle(swUserPreferenceToggle_e.swViewSketchRelations, True)
For “Toggle (Hide/Show)” Sketch Relations, we need SetUserPreferenceToggle
method from
Solidworks’s ModelDoc2
object.
This SetUserPreferenceToggle
method
takes following parameters as explained:
- UserPreferenceValue : Use Preference Values to toggle as defined in
swUserPreferenceToggle_e
.
NOTE:
swUserPreferenceToggle_e
has many values!!! Hence it is not possible to list all of them here. If you want to check full list, please visit this page of Solidworks API Help.
- OnFlag : True to toggle the value on, false to toggle the value off.
In our code, we used following values:
-
UserPreferenceValue :
swUserPreferenceToggle_e.swViewSketchRelations
-
OnFlag :
True
Return Value:
-
True: If Toggle (Hide/Show) of Sketch Relations is Success.
-
False: If Toggle (Hide/Show) of Sketch Relations is Fail.
Before Toggle (Hide/Show) of Sketch Relations
After Toggle (Hide/Show) of Sketch Relations
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 Toggle (Hide/Show) of Sketch Relations 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!!!