.NET

Reply
Distinguished Contributor
SRSDS
Posts: 243
Registered: ‎04-15-2011
Message 1 of 13 (390 Views)
Accepted Solution

If selected entity xdata contains value Then select additional entity

390 Views, 12 Replies
05-22-2013 11:45 AM

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?

 

Valued Mentor
DiningPhilosopher
Posts: 370
Registered: ‎05-06-2012
Message 2 of 13 (378 Views)

Re: If selected entity xdata contains value Then select additional entity

05-22-2013 01:55 PM in reply to: SRSDS
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.
Distinguished Contributor
SRSDS
Posts: 243
Registered: ‎04-15-2011
Message 3 of 13 (344 Views)

Re: If selected entity xdata contains value Then select additional entity

05-23-2013 01:07 PM in reply to: DiningPhilosopher

Thanks again!

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

Distinguished Contributor
SRSDS
Posts: 243
Registered: ‎04-15-2011
Message 4 of 13 (338 Views)

Re: If selected entity xdata contains value Then select additional entity

05-23-2013 03:06 PM in reply to: SRSDS

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

 

Valued Mentor
DiningPhilosopher
Posts: 370
Registered: ‎05-06-2012
Message 5 of 13 (333 Views)

Re: If selected entity xdata contains value Then select additional entity

05-23-2013 03:46 PM in reply to: SRSDS

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

Distinguished Contributor
SRSDS
Posts: 243
Registered: ‎04-15-2011
Message 6 of 13 (317 Views)

Re: If selected entity xdata contains value Then select additional entity

05-23-2013 08:22 PM in reply to: DiningPhilosopher

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

 

Valued Mentor
DiningPhilosopher
Posts: 370
Registered: ‎05-06-2012
Message 7 of 13 (313 Views)

Re: If selected entity xdata contains value Then select additional entity

05-23-2013 09:16 PM in reply to: SRSDS

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).

 

 

Distinguished Contributor
SRSDS
Posts: 243
Registered: ‎04-15-2011
Message 8 of 13 (293 Views)

Re: If selected entity xdata contains value Then select additional entity

05-24-2013 10:20 AM in reply to: DiningPhilosopher

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.

Distinguished Contributor
SRSDS
Posts: 243
Registered: ‎04-15-2011
Message 9 of 13 (281 Views)

Re: If selected entity xdata contains value Then select additional entity

05-24-2013 11:48 AM in reply to: SRSDS

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?

Valued Mentor
DiningPhilosopher
Posts: 370
Registered: ‎05-06-2012
Message 10 of 13 (275 Views)

Re: If selected entity xdata contains value Then select additional entity

05-24-2013 04:12 PM in reply to: SRSDS

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.

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

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 to get help installing your software.

Ask the Community