Macro select all existing rows on a view

Hello

I want to select all the lines added on a view to delete them later.

For the moment I can delete when I know the number of the line (here from 1 to 99) but can we make sure to list all the existing lines in order to delete them later?

Below is the snippet of code that the novice that I am trying to modify to then insert it into a slightly more complex macro.

Dim swApp As Object

Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim Numberline As Integer



Sub main()
Numberline = 0
Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc
Dim myModelView As Object
Set myModelView = Part.ActiveView
myModelView.FrameState = swWindowState_e.swWindowMaximized
boolstatus = Part.ActivateView("Vue de mise en plan1")


  
  Do
    Numberline = Numberline + 1
    'boolstatus = Part.Extension.SelectByID2("Line", "SKETCHSEGMENT", 0, 0, 0, False, 0, Nothing, 0)
    boolstatus = Part.Extension.SelectByID2("Line" & Numberline, "SKETCHSEGMENT", 0, 0, 0, False, 0, Nothing, 0)
    Part.EditDelete
  Loop While Numberline < 100



End Sub

 

Hello

Try with the following code to see if this works for you:

Option Explicit

Dim swApp As Object
Dim Part As Object
Dim swDraw As SldWorks.DrawingDoc
Dim swView As SldWorks.View
Dim swSketch As SldWorks.Sketch
Dim vSkSegArr As Variant
Dim vSkSeg As Variant
Dim swSkSeg  As SldWorks.SketchSegment
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim Numberline As Integer
Dim lNumSegments As Long

Sub main()
    Set swApp = Application.SldWorks
    Set Part = swApp.ActiveDoc
    Set swDraw = Part
    Dim myModelView As Object
    Set myModelView = Part.ActiveView
    myModelView.FrameState = swWindowState_e.swWindowMaximized
    boolstatus = Part.ActivateView("Vue de mise en plan1")
    
    Set swView = swDraw.ActiveDrawingView

    lNumSegments = swView.GetLineCount2(1)

    If lNumSegments > 0 Then
        Set swSketch = swView.GetSketch
        vSkSegArr = swSketch.GetSketchSegments
        For Each vSkSeg In vSkSegArr
            Set swSkSeg = vSkSeg
            boolstatus = Part.Extension.SelectByID2(swSkSeg.GetName, "SKETCHSEGMENT", 0, 0, 0, False, 0, Nothing, 0)
            Part.EditDelete
        Next vSkSeg
    End If
End Sub

 

Kind regards

3 Likes

Does the job perfectly! Thank you very much, I don't think I would have managed to finish this macro without your help.

Just for the record, what does this line do:

myModelView.FrameState = swWindowState_e.swWindowMaximized

 

For the rest I think I understood:

You look if there are more than 0 existing lines in the view, then you loop on each line and delete it.

 

And thank you very much  d.roger the king of macros! If I may!

Hello

I left the line in question in the code because it was already part of your code but it is useless for the line deletion function, it allows you to set the SW window of the plan to maximum dimension (or to minimum dimension if you replace "swWindowMaximized" with "swWindowMinimized").

For the rest you got it right.

Kind regards

1 Like

That's what I thought! Thank you again.