.NET

Reply
Active Contributor
greg
Posts: 33
Registered: ‎02-13-2013
Message 1 of 14 (418 Views)
Accepted Solution

Failed to get selectionresult

418 Views, 13 Replies
08-28-2013 07:07 PM

I have an application that utilizes this code to select entities

 

 

For x = 0 To SelectionSetEntitiesBindingSource.Count - 1
     ObjectIDs(x) = ObjectIdFromHandle(myDocDB, SelectionSetEntitiesBindingSource(x)("ACADHandle").ToString)
Next

Autodesk.AutoCAD.Internal.Utils.SelectObjects(ObjectIDs)

 

 

And then this code to build a selection set from those entities

 

    Public Function GetSelectionSet() As SelectionSet
        Dim myDoc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
        Dim myDocDB As Database = myDoc.Database
        Dim ed As Editor = myDoc.Editor

        Dim prObjectSelectionRes As PromptSelectionResult
        prObjectSelectionRes = ed.SelectImplied()
        If prObjectSelectionRes.Status = PromptStatus.OK Then
            GetSelectionSet = prObjectSelectionRes.Value
        Else
            MsgBox("An error occurred getting the selection set.", vbOKOnly, "Error")
            Exit Function
        End If
    End Function

 

This works fine for a while, but eventually the second part of the code failes at prOjectSelectionRes = ed.selectImplied...or rather not fails but returns error -5001.

 

This appears to be directly related to the number of times I call this function as though there is a limit to the number of sets you can create in an instance of Autocad...or something similar.

 

These entities are being retrieved from a database which is the reason for the loop in the first part.  I execute the code and it will run for a while going through the different tables in the db.

 

Any help is greatly appreciated.

Thanks!

First off by changing one line of code in last example and tested it with looping 300 times and no problems.

 

I have to get going but and I have seen other people have problems that are similiar so will type up a better explanation and concepts to show what is happening.

 

I probably should rephrase the first sentence that I changed one line and no problems. It still a bad approach for the task and there are better ways but understanding why it is not a good approach helps to see what a better one would be.

 

If you want to start taking a look at what is going on look at methods 

with ILSPY or Reflector you see what they do, and notice calls to methods that are explained in docs -  acedSSGet, acedSSAdd, acedSSSetFirst, acedSSFree as they all play a role in it.

 

In the .NET API you actully never will use an instance of a SelectionSet object, one reason would be it is impossible since it is an abstract class.

 

You actually are using a SelectionSetDelayMarshalled object and there use to be a SelectionSetFullyMarshalled object but was removed in 2010 or 2011 or earlier.

 

Except for the SelectionSetDelayMarshalled constuctors that take ObjectIds as a argument, the other constructors pass the selection set to a internal class UnmanagedResources method AddSelectionSet which adds it to a key value pair collection, which passes it into constructor of a DisposableSelectionSet object.

 

 

I know that is a little jumbled but will make a better explanation later tonight or tommorrow .

 

Anyway the only change made was commented out first line and added second.

 'Autodesk.AutoCAD.Internal.Utils.SelectObjects(ObjectIDs)
ed.SetImpliedSelection(ObjectIDs)

 

 

 

 

 

 

 

Active Contributor
greg
Posts: 33
Registered: ‎02-13-2013
Message 2 of 14 (389 Views)

Re: Failed to get selectionresult

08-29-2013 05:03 AM in reply to: greg

Just to add a bit more.

 

It appears when I get out of the command and I select something on screen I can not hit the esc key to cancel the selection...really weird behavior within model space until I change to a layout and back to model space does it fix itself.

*Expert Elite*
arcticad
Posts: 1,285
Registered: ‎06-21-2004
Message 3 of 14 (379 Views)

Re: Failed to get selectionresult

08-29-2013 06:22 AM in reply to: greg

This is the method I've used, it also lets me hit the space bar to repeat the command.

 

Private Sub btnSelectBlock_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelectBlock.Click
    If Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.Count = 0 Then
        Exit Sub
    End If
 
    curDoc.SendStringToExecute(Chr(27), False, False, False)
    curDoc.SendStringToExecute("MyCommand" & vbCr, False, False, False)
    Autodesk.AutoCAD.Internal.Utils.SetFocusToDwgView()

End Sub

<CommandMethod("MyCommand")> _
Public Sub selectBlock()
    ....
End Sub

 

---------------------------



(defun botsbuildbots() (botsbuildbots))
Active Contributor
greg
Posts: 33
Registered: ‎02-13-2013
Message 4 of 14 (371 Views)

Re: Failed to get selectionresult

08-29-2013 06:55 AM in reply to: arcticad

Thanks for the response but not sure how that would help me.

 

Workflow of the application:

  • Application is executed
  • Form opens with various settings.
  • Button is clicked
  • Application makes connection to external database table as a binding source
  • Application loops through binding source where Handle is stored of the drawing objects.
  • Application selects drawing entites based the handles from the bindingsource.
  • Application performs action on entities
  • Application makes another connect to external database for another table table and sets as binding source
  • Application loops through binding source where Handle is stored of the drawing objects.
  • Application selects drawing entites based the handles from the bindingsource.
  • Application performs action on entities
  • This process continues through many different tables..right now about 5 or so but will most likely be added to as time goes on.

 

I dont need to rexecute the command with the space bar..not a concern really.

 

Any other ideas?

 

Thanks

*Expert Elite*
arcticad
Posts: 1,285
Registered: ‎06-21-2004
Message 5 of 14 (360 Views)

Re: Failed to get selectionresult

08-29-2013 07:33 AM in reply to: greg

    <CommandMethod("MyCommand", CommandFlags.Modal Or CommandFlags.UsePickSet Or CommandFlags.Redraw)> _
    Public Sub MyCommand()
        GetSelectionSet()
    End Sub

 

This worked for getting the implied selection set.

 

There is a limit of 128 selection set open at one time.

---------------------------



(defun botsbuildbots() (botsbuildbots))
Active Contributor
greg
Posts: 33
Registered: ‎02-13-2013
Message 6 of 14 (350 Views)

Re: Failed to get selectionresult

08-29-2013 07:46 AM in reply to: arcticad

if you meant I should include "CommandFlags.Modal Or CommandFlags.UsePickSet Or CommandFlags.Redraw" on the original entry...I just did that and it did not fix the issue.

 

Thanks,

*Expert Elite*
arcticad
Posts: 1,285
Registered: ‎06-21-2004
Message 7 of 14 (343 Views)

Re: Failed to get selectionresult

08-29-2013 08:03 AM in reply to: greg

http://spiderinnet1.typepad.com/blog/2012/09/autocad-net-count-limit-of-available-selection-sets.htm...

 

 

---------------------------



(defun botsbuildbots() (botsbuildbots))
Active Contributor
greg
Posts: 33
Registered: ‎02-13-2013
Message 8 of 14 (326 Views)

Re: Failed to get selectionresult

08-29-2013 03:20 PM in reply to: arcticad

Thank you!  It appears this may be the problem.  However I am still in the process of altering my code to explicitly dispose of all selectionsets that are created.  

 

I will let you know.

 

Thanks again.

Active Contributor
greg
Posts: 33
Registered: ‎02-13-2013
Message 9 of 14 (317 Views)

Re: Failed to get selectionresult

08-29-2013 06:25 PM in reply to: greg

So I took the code in the link you provided and modified it a bit to use the general functionality that I am using and this replicates the error I am receiving exactly.  Bombs out at 128 which I suppose makes sense.  The problem is that I am not sure why. I am disposing my selection set with each run through the loop.  Is there something else?

 

    <CommandMethod("TestSelectionSetCountLimit2", CommandFlags.Modal Or CommandFlags.UsePickSet Or CommandFlags.Redraw)> _
    Public Sub TestSElectionSetCountLimit2()
        Dim myDoc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
        Dim db As Database = HostApplicationServices.WorkingDatabase
        Dim ed As Editor = myDoc.Editor

        Try
            For i = 0 To 130
                Dim ObjectIDs(0) As ObjectId

                ObjectIDs(0) = ObjectIdFromHandle(db, "A33DC")

                Autodesk.AutoCAD.Internal.Utils.SelectObjects(ObjectIDs)

                Dim prObjectSelectionRes As PromptSelectionResult
                prObjectSelectionRes = ed.SelectImplied()
                If prObjectSelectionRes.Status = PromptStatus.OK Then
                    Dim ss As SelectionSet
                    ss = prObjectSelectionRes.Value
                    If ss IsNot Nothing Then
                        ed.WriteMessage(vbLf & "The #{0} SS is good and has {1} entities.", i + 1, ss.Count)
                        ss.Dispose()
                    Else
                        ed.WriteMessage(vbLf & "The #{0} SS is bad!", i + 1)
                    End If
                Else
                    ed.WriteMessage(vbLf & "The #{0} SS is bad!", i + 1)

                End If
            Next
        Catch ex As System.Exception
            ed.WriteMessage(ex.ToString())
        End Try
    End Sub

 

Active Contributor
greg
Posts: 33
Registered: ‎02-13-2013
Message 10 of 14 (288 Views)

Re: Failed to get selectionresult

08-30-2013 04:07 AM in reply to: greg

I should also add that you can easily duplicate the error by just replacing the handle with one from your drawing and using the below "ObjectIDfromHandle" function I forgot to include.

    Public Shared Function ObjectIdFromHandle(db As Database, strHandle As String) As ObjectId
        Dim nHandle As Int32 = Int32.Parse(strHandle, Globalization.NumberStyles.AllowHexSpecifier)
        Dim handle As New Handle(nHandle)
        Dim ids(0) As ObjectId
        ids(0) = db.GetObjectId(False, handle, 0)
        Return ids(0)
        'Return db.GetObjectId(False, handle, 0)
    End Function

 Thanks,

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.