SelectionSet - Select on screen gives no result

SelectionSet - Select on screen gives no result

Anonymous
Not applicable
1,939 Views
5 Replies
Message 1 of 6

SelectionSet - Select on screen gives no result

Anonymous
Not applicable

Hi everyone,

 

I wrote some lines to change elements quickly from one to another layer. The elements are collected in a selection set by selecting them by screen. The code works fine. But sometimes after I have used it a few times, the selection on screen is no longer stored in the selection set and there no elements were tranferred.

Public Sub ChangeLayer2(DestLayer As String)
    'FromLayer: Layer auf dem die Objekte ausgewählt werden sollen
    'DestLayer: Ziellayer, auf den umgespeichert werden soll
    
    Dim tCounter As Integer
    Dim tDxfCodes(0) As Integer
    Dim tDxfValues(0) As Variant
    
    'Auswahlfenster deaktivieren
    Unload LayerAuswahl3
    
    'Erstelle ein neues SelectionSet
    On Error Resume Next
    Dim SSet As AcadSelectionSet
    Set SSet = CreateSelectionSet("Layer2")
   
    ' User definiert die Objekte am Bildschirm
    SSet.SelectOnScreen    

   If (Err.Number = 0) And (SSet.Count > 0) Then
        Dim tEnt As AcadEntity
        Dim tEntNew As AcadEntity
  
            'Gehe durch die Auswahl und ändere den Layer
            For Each tEnt In SSet
                Set tEntNew = tEnt.Copy
                tEntNew.Layer = DestLayer
                tCounter = tCounter + 1
            Next
            
            Call MsgBox("Layer gewechselt: " & CStr(tCounter)) 
   Else
      Call MsgBox("Es konnten keine Objekte auf Layer: " & FromLayer & " gefunden werden!" & Err.Description)
   End If
         
   'Aufräumen
   Set SSet = Nothing
   Set tEntNew = Nothing

End Sub

'Funktion zur Definition eines AcadSelectionSet
Function CreateSelectionSet(SSset As String, Optional myDoc As Variant) As AcadSelectionSet
    If IsMissing(myDoc) Then Set myDoc = ThisDrawing
    
    On Error Resume Next
    Set CreateSelectionSet = myDoc.SelectionSets(SSset)
    If Err Then
        Err.Clear
        Set CreateSelectionSet = myDoc.SelectionSets.Add(SSset)
    Else
        CreateSelectionSet.Clear
    End If
End Function

Is there any mistake or inaccurate programming visble, which could result in this behaviour?

 

Thanks in advance!

 

Lars

0 Likes
Accepted solutions (1)
1,940 Views
5 Replies
Replies (5)
Message 2 of 6

norman.yuan
Mentor
Mentor

When you say "used it a few times, ..." do you mean using the code in the same drawing, or different drawings? What is "a few", 10, 50, or more times? How many entities were selected on screen on each use? 

 

Regardless of the answers to my question, I saw a critical flaw in your code: you SHOULD delete the AcadSelectionSet created by your code AFTER being done with it, that is, call SSet.Delete before Set SSet = Nothing (which is not necessary, but harmless). Without going deep into every detail of you case, I suspect adding AcadSelectionSet.Delete would make your issue go away.

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 6

Anonymous
Not applicable

Thanks Norman for your replay.

 

Although I have never counted the times I used the code before it crashes, I would guess that it's about 10-15 times. But I was not able to produce the failure by doing the same for 10 times. I use the code in different drawings, but while working mainly in one main drawing. The number of entities is also different from case to case, sometimes only 5, sometimes more than 1000. Sorry about that vague answer.

 

I will give it at try with your solution, although I thought that it should be enough to check before using the selection set again and if it's exist to clear it in the function.

 

Greets

Lars

0 Likes
Message 4 of 6

norman.yuan
Mentor
Mentor
Accepted solution

@Anonymous wrote:

Thanks Norman for your replay.

 

Although I have never counted the times I used the code before it crashes, ...


Well, you did not say it crashes in your original post, instead you said: "the selection on screen is no longer stored in the selection set...". If the code actually crashes, not deleting the selection set your code creates would be more likely the cause, considering you use the code repeatedly in different drawing. Each AutoCAD session can only have 128 selection sets (not 128 sets per drawing). It is possible that because of the way of error being handled (On Error Resume Next...) leads to too many selection sets being created.

 

So, make sure you delete the Selection Set after using it. Better yet, in the CreateSelectionSet() method, instead of use "On Error Resume Next" to see if a selection set with given name exists, I'd do this:

 

Private Function CreateSelectionSet(name As String) As AcadSelectionSet

  Dim ss As AcadSelectionSet

  '' Make sure if a selection set with the same name exists, delete it

  For Each ss in ThisDrawing.SelectionSets

    If Ucase(ss.Name)=UCase(name) Then

      ss.Delete

      Exit For

    End If

  Next

  Set CreateSelectionSet = ThisDrawing.SelectionSets.Add(name)

End Function

 

However, I'll still call AcadSelectionSet.Delete() whenever I am done with it.

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 5 of 6

maratovich
Advisor
Advisor

I wrong

---------------------------------------------------------------------
Software development
Automatic creation layouts and viewport. Batch printing drawings from model.
www.kdmsoft.net
0 Likes
Message 6 of 6

Anonymous
Not applicable

Thanks Norman,

 

changing the function and deleting the selection set after I'm done with it seems to solve my problems. Up to now I had no further problems with this issue.

 

Greets

Lars

0 Likes