.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

If selected entity xdata contains value Then select additional entity

12 REPLIES 12
SOLVED
Reply
Message 1 of 13
SRSDS
883 Views, 12 Replies

If selected entity xdata contains value Then select additional entity

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?

 

12 REPLIES 12
Message 2 of 13
DiningPhilosopher
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.
Message 3 of 13
SRSDS
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.

Message 4 of 13
SRSDS
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

 

Message 5 of 13
DiningPhilosopher
in reply to: SRSDS

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

 

Message 7 of 13
DiningPhilosopher
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).

 

 

Message 8 of 13
SRSDS
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.

Message 9 of 13
SRSDS
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?

Message 10 of 13
DiningPhilosopher
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.

Message 11 of 13
SRSDS
in reply to: DiningPhilosopher

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.

 

 

 

 

Message 12 of 13
DiningPhilosopher
in reply to: SRSDS

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

 

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

Message 13 of 13
SRSDS
in reply to: DiningPhilosopher

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.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost