Impossible to delete

Hello
I have a 98% working macro and I'm drying a little for the other 2%.
This macro automatically adds markings at the end of the bend line, on a marking layer and in yellow.
The problem comes from some part, when the size of the part is reduced, the marking comes out of the part.
By restarting the macro, it is supposed to erase the marking lines, out of time (rarely) the marking does not erase.
And a message appears in debug mode at the top of the window:
" None of these entities can be deleted "


Here's the part of my code to remove the markup:

Sub suppressMarquage()
    Dim swModel                 As Object
    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
    Dim myModelView             As Object
    
'On supprime le marquage existant
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swDraw = swModel
    
    Set myModelView = swModel.ActiveView
    myModelView.FrameState = swWindowState_e.swWindowMaximized
    Set swView = swDraw.GetFirstView.GetNextView


    lNumSegments = swView.GetLineCount2(1)

    If lNumSegments > 0 Then
        Set swSketch = swView.GetSketch
        vSkSegArr = swSketch.GetSketchSegments
        For Each vSkSeg In vSkSegArr
            Set swSkSeg = vSkSeg
            boolstatus = swModel.Extension.SelectByID2(swSkSeg.GetName, "SKETCHSEGMENT", 0, 0, 0, False, 0, Nothing, 0)
            swModel.EditDelete
        Next vSkSeg
    End If
    
'On supprime l'annotation marquage
    Dim bFind                       As Boolean
    Dim swNote                      As SldWorks.Note
    Dim swAnn                       As SldWorks.Annotation
    Dim bret                        As Boolean
    Set swNote = swView.GetFirstNote
    Do While Not swNote Is Nothing
                            bFind = False
                            Debug.Print "Note:" & swNote.GetText
                            If swNote.GetText Like "*Marquage laser*" Then
                                bFind = True
                                Debug.Print "bFind:" & bFind
                                Set swAnn = swNote.GetAnnotation
                                bret = swAnn.Select2(True, 0)
                                Set swNote = swNote.GetNext
                                swModel.EditDelete
                                
                            End If
        If Not bFind Then Set swNote = swNote.GetNext
    Loop
End Sub

And if necessary an example of MEP that does not work well (sw 2020)
If anyone has a lead, I've been drying up on the subject for too long!:grin:
PI_200416-test. SLDPRT (580.9 KB)
PI_200416-test. SLDDRW (287.8 KB)

Hello

Try replacing this:

    Set myModelView = swModel.ActiveView
    myModelView.FrameState = swWindowState_e.swWindowMaximized
    Set swView = swDraw.GetFirstView.GetNextView

By this:

    Set myModelView = swModel.ActiveView
    myModelView.FrameState = swWindowState_e.swWindowMaximized
    Set swView = swDraw.GetFirstView
    Set swView = swView.GetNextView
    boolstatus = swDraw.ActivateView(swView.GetName2)

Edit: Also declare variables with the right type:

    Dim swModel                 As SldWorks.ModelDoc2
    Dim swDraw                  As SldWorks.DrawingDoc
    Dim swView                  As SldWorks.View

Etc

1 Like

For the variables, they were global, hence the omission in my (partial) code:
image
For the rest it seems perfectly functional, indeed the fact of activating the view seems much better!
Thank you @Cyril.f .
For my culture and for the purpose of my continuous improvement, how did you debug please?

So for the debug I added debug.print on the name of the view and the name of the sketch to see if the selections were good.
What put me on the alert is that during the step-by-step process, it tended to select the sheet in the tree instead of the view (probably related to this line:

Set myModelView = swModel.ActiveView
    myModelView.FrameState = swWindowState_e.swWindowMaximized

Deleting it didn't change anything, I then remembered that the swView.GetNExtView function didn't activate the view of a SW viewpoint so by activating it we force the selection to be at the right level (there can be lines outside the views and the basemap in a drawing).

2 Likes

And to think that this is what I advise in general (adding debug.print), it's sometimes the worst shod shoemaker! :rofl: :rofl: :rofl:

Thanks for the help and explanation!

1 Like