If selected entity xdata contains value Then select additional entity

If selected entity xdata contains value Then select additional entity

SRSDS
Advisor Advisor
2,763 Views
12 Replies
Message 1 of 13

If selected entity xdata contains value Then select additional entity

SRSDS
Advisor
Advisor

Hi,

I would like to add additional entities to the selection if any of the selected entities contains a an xdata value.

If I put the condition into the ImpliedSelectionChanged reactor I imagine will probably loop back on itself.

Is this something that might be possible?

 

0 Likes
Accepted solutions (1)
2,764 Views
12 Replies
Replies (12)
Message 2 of 13

DiningPhilosopher
Collaborator
Collaborator
Accepted solution
The Editor's SelectionAdded and SelectionRemoved events are for that very purpose. They allow you to examine each sub-selection and add/remove entities to/from the selection.

When adding entities based on criteria of other selected entities you can also use SelectionRemoved to remove the added entities based on the same criteria.
Message 3 of 13

SRSDS
Advisor
Advisor

Thanks again!

Editor events don't seem to appear in the AutoCAD NET Developers guide that I've been looking up.

0 Likes
Message 4 of 13

SRSDS
Advisor
Advisor

I have a couple of things to try to bullet proof the event handlers. I keep a count of every transaction open and closed (trcnt as integer).

 

The SelectionAdded event handler is one that doesn't appear to cope with transactions. Must be some internal calls that uses the SelectionAdded event, starts the transaction but doesn't dispose it. This is without any code between StartTransaction and End Using that might cause any disruption.

 

Is there a way to keep an eye on the selected objects xdata without opening a transaction?

 

Dim ss As SelectionSet = e.AddedObjects
For Each so As SelectedObject In ss
?
Next

 

0 Likes
Message 5 of 13

DiningPhilosopher
Collaborator
Collaborator

I think I already mentioned in another thread that StartOpenCloseTransaction() returns a transaction that can be used in event handlers.

Message 6 of 13

SRSDS
Advisor
Advisor

It's a little confusing. The following creates a message box saying that the trcnt variable is not zero when it should be.

I've shelled down to just the one handler to make sure that there were no others that may have been causing it.

 

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput

<Assembly: ExtensionApplication(GetType(testClass))> 

Public Class testClass
    Implements IExtensionApplication
    Private DocMan As DocumentCollection
    Private trcnt As Integer
    Private PauseEvents As Boolean

    Public Sub Initialize() Implements Autodesk.AutoCAD.Runtime.IExtensionApplication.Initialize
        Dim doc As Document = Application.DocumentManager.MdiActiveDocument
        InitializeActiveDocument(doc)
    End Sub

    Public Sub Terminate() Implements Autodesk.AutoCAD.Runtime.IExtensionApplication.Terminate
    End Sub

    Sub InitializeActiveDocument(ByVal doc As Document)
        Dim db As Database = doc.Database
        Dim ed As Editor = doc.Editor
        Using trans As OpenCloseTransaction = db.TransactionManager.StartOpenCloseTransaction() : trcnt = trcnt + 1
            Try
                AddHandler ed.SelectionAdded, AddressOf ed_SelectionAdded
                trans.Commit() : trcnt = trcnt - 1 : If trcnt > 0 Then MsgBox("TRCNT>0")
                If trcnt <> 0 Then MsgBox("TRCNT " & trcnt & " Init active doc")
            Catch ex As Autodesk.AutoCAD.Runtime.Exception
                MsgBox("Exception:" + ex.Message)
            End Try
        End Using
    End Sub

    Private Sub ed_SelectionAdded(ByVal sender As Object, ByVal e As SelectionAddedEventArgs)
        Dim db As Database = HostApplicationServices.WorkingDatabase
        Using tr As OpenCloseTransaction = db.TransactionManager.StartOpenCloseTransaction()
            trcnt = trcnt + 1

            trcnt = trcnt - 1 : If trcnt > 0 Then MsgBox("ed_SelectionAdded TRCNT>0")
        End Using
    End Sub
End Class

 

0 Likes
Message 7 of 13

DiningPhilosopher
Collaborator
Collaborator

Sorry, your code and the test it does which you are confused about makes no sense at all.

 

The only thing you have to do is start the transaction (in the event handler), use it to open objects, and then commit the transaction before the event handler returns, and there is no point to or need for tracking (I'm not sure what it is you're tracking in the first place).

 

 

0 Likes
Message 8 of 13

SRSDS
Advisor
Advisor

My appologies.

 

I discovered that I wasn't disabling the ImpliedSelectionChanged event handler within the SelectionAdded handler.

The transaction count was useful though in that it allerted me that this was the problem.

 

Thanks for your help and sorry for the useless code above.

0 Likes
Message 9 of 13

SRSDS
Advisor
Advisor

I see what you mean by AutoCAD running like a snail on this sort of handler. It reacts to mouseover of every entity. I just want to know if the entity is selected.

Is there another way other than looping back on ImpliedSelectionChanged?

0 Likes
Message 10 of 13

DiningPhilosopher
Collaborator
Collaborator

@SRSDS wrote:

I see what you mean by AutoCAD running like a snail on this sort of handler. It reacts to mouseover of every entity. I just want to know if the entity is selected.

Is there another way other than looping back on ImpliedSelectionChanged?


AFAIK, the SelectionAdded event doesn't fire when hovering over an entity. It fires only when the user has selected some objects, which could be at the Command prompt with the implied selection or within a command that prompts for a selection.

0 Likes
Message 11 of 13

SRSDS
Advisor
Advisor

It seems to unfortunately. e.Selection is contains no objects.

It's a rare case that I need to use it. I'll prompt the user that the other entity needs to be modified and not the one selected.

 

 

 

 

0 Likes
Message 12 of 13

DiningPhilosopher
Collaborator
Collaborator

The selected objects are in the AddedObjects property, not the SelectionSet property.

 

Read the documentation, and search here for examples of its use.

0 Likes
Message 13 of 13

SRSDS
Advisor
Advisor

I 've ended up using the EnteringQuiescentState event to do this.

I couldn't understand the errors the SelectionAdded event was generating or why it was firing so many times.

Thanks again and sorry for any trouble there.

0 Likes