SolidWorks VBA Macro - Hide Sketch Entities

Can anybody help?

I have the following macro....

Dim swApp As Object

Sub HideSketchEntities()
    Dim swModel As ModelDoc2
   
    Dim swModelDocExt As ModelDocExtension

    Dim swSketchMgr As SketchManager

    Set swApp = Application.SldWorks

    Set oDoc = swApp.ActiveDoc

    swModelDocExt = oDoc.Extension

    swSketchMgr = oDoc.SketchManager

    status = swModelDocExt.SelectByID2("Sketch1", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)

    swModel.BlankSketch
End Sub

I want to run (as a test) the macro to hide all sketch1 entities (lines arcs etc)

Can somebody tell me what is wrong with the above macro?

Many thanks in advance!

Darren

SolidworksApi/macros
Answers 5
AB
AB 2021-04-08 Best Answer

To answer your question, you don't actually have swModel defined as anything. You are using oDoc instead so either change your command to oDoc.BlankSketch or define swModel as the active document.

I cobbled this together out of a few examples a while back. See if it does what you want.

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim curFeature As Feature
Dim subFeature As Feature
Dim featureTypeName As String
Dim firstSub As Boolean

Sub main()

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc

If swModel.GetType <> swDocPART Then
Msg = "Only Allowed on Part" ' Define message
Style = vbOKOnly ' OK Button only
Title = "Error" ' Define title
Call MsgBox(Msg, Style, Title) ' Display error message
Exit Sub ' Exit this program
End If

Set curFeature = swModel.FirstFeature()
Do While Not curFeature Is Nothing
featureTypeName = curFeature.GetTypeName2()
If curFeature.Visible = swVisibilityState_e.swVisibilityStateShown And Not curFeature.IsSuppressed Then
Select Case featureTypeName
Case "RefPlane", "RefAxis", "CoordSys", "RefPoint", "ReferenceCurve"
Debug.Print "FeatureTypeName: " & curFeature.Name & " " & featureTypeName
curFeature.Select (False)
swModel.BlankRefGeom
Case "ProfileFeature", "3DProfileFeature"
Debug.Print "FeatureTypeName: " & curFeature.Name & " " & featureTypeName
curFeature.Select (False)
swModel.BlankSketch
End Select
End If

firstSub = True
Set subFeature = curFeature.GetFirstSubFeature()
Do While Not subFeature Is Nothing
featureTypeName = subFeature.GetTypeName2()
If subFeature.Visible = swVisibilityState_e.swVisibilityStateShown And Not subFeature.IsSuppressed Then
Select Case featureTypeName
Case "RefPlane", "RefAxis", "CoordSys", "RefPoint", "ReferenceCurve"
If firstSub Then
Debug.Print "FeatureTypeName: " & subFeature.Name & " " & featureTypeName
firstSub = False
End If
Debug.Print vbTab & "FeatureTypeName: " & subFeature.Name & " " & featureTypeName
curFeature.Select (False)
swModel.BlankRefGeom
Case "ProfileFeature", "3DProfileFeature"
If firstSub Then
Debug.Print "FeatureTypeName: " & subFeature.Name & " " & featureTypeName
firstSub = False
End If
Debug.Print vbTab & "FeatureTypeName: " & subFeature.Name & " " & featureTypeName
curFeature.Select (False)
swModel.BlankSketch
End Select
End If

Set subFeature = subFeature.GetNextSubFeature()
Loop

Set curFeature = curFeature.GetNextFeature()
Loop

End Sub

AB
AB 2021-04-08

Yes, that sorts through the feature tree and selects stand-alone sketches as well as sketches that are part of features (subfeatures) such as boss-extrudes. It looks through the feature tree and finds sketches, planes, axes, points and coordinate systems and hides them if they aren't already hidden.

It's not perfect but it gets the job done for what I needed when I wrote it

SS
SS 2021-04-08

""

Set curFeature = swModel.FirstFeature()
Do While Not curFeature Is Nothing
featureTypeName = curFeature.GetTypeName2()

""

""

Set subFeature = curFeature.GetFirstSubFeature()
Do While Not subFeature Is Nothing
featureTypeName = subFeature.GetTypeName2()

""

Does this helps in auto selection of sketches through out the file (Part/dwg/Assy)? 

DE
DE 2021-04-08

‌ is correct.

also have a look at this macro Hide_and_Show_Sketches_Example_VB

PB
PB 2021-04-08

Thanks for asking this question!

I thought you are looking for what is shown in the below gif file:

I have added a working code for this option below:

Option Explicit<br><br>' Create variable for Solidworks application<br>Dim swApp As SldWorks.SldWorks<br><br>' Create variable for Solidworks document<br>Dim swDoc As SldWorks.ModelDoc2<br><br>' Boolean Variable<br>Dim BoolStatus As Boolean<br><br>' Main function of our VBA program<br>Sub main()<br><br>    ' Set Solidworks variable to Solidworks application<br>    Set swApp = Application.SldWorks<br><br>    ' Set Solidworks document to Active document<br>    Set swDoc = swApp.ActiveDoc<br><br>    ' Turn off visibility of this sketch<br>    BoolStatus = swDoc.SetUserPreferenceToggle(swUserPreferenceToggle_e.swDisplaySketches, False)<br><br>End Sub<span class="line-numbers-rows"><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span></span>

I copied all lines from www.thecadcoder.com except the last line.

I have also commented on all lines so that you can know what are they and why we use it.

I hope this will solve your problem.

Thanks

Prashant Baher

comment imagecomment image