Macro to retrieve the sketch name in the 1st unfolded state

Hello

I would like to recover the name of the 1st sketch in the unfolded state in order to show or hide it as needed. (here Folding line2 but the name is not always the same.)

 

Here is the code found and working but when you know the name of the sketch:

Dim swApp As Object
Dim boolstatus As Boolean
Sub main()

Set swApp = Application.SldWorks


Set Part = swApp.ActiveDoc
boolstatus = Part.ActivateView("Drawing View1")
boolstatus = Part.Extension.SelectByID2("Bend-Lines2@Part1@Drawing View1", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)
Part.BlankSketch
MsgBox "Ligne de pliage caché"
boolstatus = Part.Extension.SelectByID2("Bend-Lines2@Part1@Drawing View1", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)
Part.UnblankSketch
MsgBox "Ligne de pliage affiché"
End Sub

 

Hello.

You can go through the construction tree by using the following methods:

Features (FirstFeature and GetNextFeature) 

SubFeature (GetFirstSubFeature and GetNextSubFeature)

we test the type by GetTypeName2

On the other hand, I think that you first have to retrieve the file that is attached to the view, open the file and browse its tree, because you only have 2 levels (Feature and subFeature).

But I've never tested on an unfolded state. Maybe you need to activate the unfolded state

Kind regards.

1 Like

Hello. Try this:

Option Explicit
Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swFeat As SldWorks.Feature
    Dim swSubFeat As SldWorks.Feature
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swFeat = swModel.FirstFeature
    While Not swFeat Is Nothing
        'Debug.Print swFeat.Name & " " & swFeat.GetTypeName2'
        If swFeat.GetTypeName2 = "FlatPattern" Then
            Set swSubFeat = swFeat.GetFirstSubFeature
            While Not swSubFeat Is Nothing
                If swSubFeat.GetTypeName2 = "ProfileFeature" Then
                    'Debug.Print swSubFeat.GetNameForSelection(0)'
                    swSubFeat.Select2 False, 0
                    If swSubFeat.Visible = swVisibilityState_e.swVisibilityStateShown Then
                        swModel.BlankSketch
                    Else
                        swModel.UnblankSketch
                    End If
                    swModel.ClearSelection2 True
                    Exit Sub
                End If
                Set swSubFeat = swSubFeat.GetNextSubFeature
            Wend
        End If
        Set swFeat = swFeat.GetNextFeature
    Wend
End Sub

 

1 Like

Hello

The answer is good JeromeP but not the question sorry.

To rephrase, it is a question of doing this macro from a MEP. Otherwise it's perfectly functional.

I tried something like this but  something is missing.

Dim swDraw As SldWorks.ModelDoc2       
Dim swView As SldWorks.View
Dim swFeat As SldWorks.Feature


Set swDraw = swModel
    Set swView = swDraw.GetFirstView
    Set swView = swView.GetNextView
    
    
    'Set swFeat = swModel.FirstFeature
    Set swFeat = swDraw.FirstFeature

 

The code is a little different for a drawing

Option Explicit
Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swDraw As SldWorks.DrawingDoc
    Dim swPartModel As SldWorks.ModelDoc2
    Dim swFeat As SldWorks.Feature
    Dim swSubFeat As SldWorks.Feature
    Dim swView As SldWorks.View
    Dim swSelMgr As SldWorks.SelectionMgr
    Dim boolstatus As Boolean
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swDraw = swModel
    Set swSelMgr = swModel.SelectionManager
    Set swView = swDraw.ActiveDrawingView
    If swView Is Nothing Then
        MsgBox "Sélectionner une vue"
        Exit Sub
    End If

    Set swPartModel = swView.ReferencedDocument
    Set swFeat = swPartModel.FirstFeature
    While Not swFeat Is Nothing
        'Debug.Print swFeat.Name & " " & swFeat.GetTypeName2'
        If swFeat.GetTypeName2 = "FlatPattern" Then
            Set swSubFeat = swFeat.GetFirstSubFeature
            While Not swSubFeat Is Nothing
                If swSubFeat.GetTypeName2 = "ProfileFeature" Then
                    Debug.Print swSubFeat.Name & "@" & swPartModel.GetTitle & "@" & swView.GetName2
                    boolstatus = swModel.Extension.SelectByID2(swSubFeat.Name & "@" & swPartModel.GetTitle & "@" & swView.GetName2, "SKETCH", 0, 0, 0, False, 0, Nothing, 0)
                    swModel.UnblankSketch
                    
                    swModel.ClearSelection2 True
                    Exit Sub
                End If
                Set swSubFeat = swSubFeat.GetNextSubFeature
            Wend
        End If
        Set swFeat = swFeat.GetNextFeature
    Wend
End Sub

Note: Repeating the macro will not change the visibility of the sketch from one state to the other like the previous macro. But it answers the original question.

1 Like

Hello

Indeed it's quite different, I look at it in the afternoon if I have a little time. Thank you.

Perfectly functional, thank you JeromeP .

Now I just have to adapt it in my full macro but it won't be very complicated.