Author Topic: Tool palette, Object selection and Event  (Read 36579 times)

0 Members and 1 Guest are viewing this topic.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Tool palette, Object selection and Event
« Reply #15 on: June 04, 2007, 06:45:22 PM »
Okay.  I got that part to work correctly.  It had nothing to do with the event part of the code  :ugly:, I had to lock the document when I was creating the controls for the tool palette.  Now I just have to fine tune it.

Thanks for the conversation Luis.  :-)
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

LE

  • Guest
Re: Tool palette, Object selection and Event
« Reply #16 on: June 04, 2007, 06:57:56 PM »
Okay.  I got that part to work correctly.  It had nothing to do with the event part of the code  :ugly:, I had to lock the document when I was creating the controls for the tool palette.  Now I just have to fine tune it.

Thanks for the conversation Luis.  :-)

I was going to mention that, but forgot... when you do any modification from a dialog, the doc must be lock..... sorry... glad you found the way :)

Code: [Select]
        static public void doorModify()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            Document doc = acadApp.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            PromptEntityResult res = ed.GetEntity("\nSelect door to modify: ");
            if (res.Status != PromptStatus.OK) return;
            using (DocumentLock doclock = ed.Document.LockDocument())
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    Entity ename = (Entity)tr.GetObject(res.ObjectId, OpenMode.ForWrite, false);
                    if (ename != null)
                    {
                        BlockReference block;
                        try
                        {
                            block = (BlockReference)ename;
                        }
                        catch { return; }
                        Boolean iDoorXrecValid = true;
                        Xrecord iDoorXRec = null;
                        DBDictionary extDict = null;
                        try
                        {
                            extDict = (DBDictionary)tr.GetObject(block.ExtensionDictionary, OpenMode.ForRead, false);
                        }
                        catch { iDoorXrecValid = false; }
                        try
« Last Edit: June 04, 2007, 07:03:19 PM by LE »

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Tool palette, Object selection and Event
« Reply #17 on: June 04, 2007, 07:24:59 PM »
I create the combo boxes on the fly, and I had to lock the document when I was creating the combo boxes (well not sure if it was just this, or when creating the layout of it).  But it seems to be working now.  I just need to update it so that it will only show the block selected per the current drawing.  Right now when you switch between drawings it will still show the old one selected, but it won't allow you to update it.

Here is the code if anyone is interested.


Edit:  Removed old code.
« Last Edit: June 14, 2007, 01:07:01 PM by T.Willey »
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

TonyT

  • Guest
Re: Tool palette, Object selection and Event
« Reply #18 on: June 05, 2007, 12:14:58 AM »
Hi Tim.

You (and Luis) do understand that the SelectionAdded event fires
for all object selection, including object selection within
built-in commands; LISP programs; etc., right  ?

Also, do you also understand that SelectionAdded is fired once for
each "Select objects: " prompt, during object selection? 

E.g., if you start a command that prompts for multiple selection, and
you select one or more objects using a window/crossing box, then
the SelectionAdded event fires, and the 'Select objects: ' prompt
reappears. If you then add more objects to that same selection by
responding to the new 'Select objects: ' prompt, the SelectionAdded
event fires again.

IOW, In the context of SelectionAdded, each response to a
'Select objects: ' prompt is a 'selection'. And hence, this
event fires once for each 'Select objects: ' prompt that you
respond to by selecting one or more objects.

It seems that what you (and Luis) may be assuming is that this
event fires only once after the user has exited from multiple
object selection and all selection is completed. That's not the
case.

So, it would seem to me that your code is going to be running
inside of many commands, whenever they prompt for a selection.

I create the combo boxes on the fly, and I had to lock the document when I was creating the combo boxes (well not sure if it was just this, or when creating the layout of it).  But it seems to be working now.  I just need to update it so that it will only show the block selected per the current drawing.  Right now when you switch between drawings it will still show the old one selected, but it won't allow you to update it.

« Last Edit: June 05, 2007, 12:25:56 AM by TonyT »

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Tool palette, Object selection and Event
« Reply #19 on: June 05, 2007, 01:47:42 AM »
Hi Tony,

 Thanks for the responds.

  In my testing I noticed that the event fired twice per selection, single window or single crossing or selection of a single object, without a command active, and I did notice that it fired each time I made a window. I noticed that it even fired when I hit escape to deselect all objects.  I assumed that it would fire the same amount during a command also.

Maybe I can add some code to check to see if a command is issued, and if so ignore (or deactivate) my selection event.  Thats a good idea.

I think I will change the code to kick you out once you have more than one block in the selection set, no need to keep checking unless I make it a multi-tab palette, one per block name with the amount in parenthesis.  Just an idea.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

TonyT

  • Guest
Re: Tool palette, Object selection and Event
« Reply #20 on: June 05, 2007, 05:26:12 AM »
Hi Tim.

The reason why you noticed that the event fired twice (or more) is
most-likely because your code is adding the handler to the event
more than once, because that event does not fire more than once
for each selection.

From just a quick glance at your code, it seems that its adding the
event handler in the command method. But, it's never removed, so if
you execute that command 3 times, your event handler is added to
the event 3 times, and the event will fire 3 times for each selection.
If you execute the command 4 times, the event will fire 4 times for
each selection.

From another quick glance at it, your code seems to have much
bigger problems. You are caching references to DBobjects in the Tag
property of controls.  Umm, can't do that. 

A reference to a DBObject is only valid while the transaction it was
obtained from is active. Once that transaction ends, any DBObjects
obtained by calling GetObject() on the transaction, are no longer valid
and should not be used.

You never cache DBObjects for use at a later time, after the
transaction they were obtained from has ended. Doing that will
only lead to a fatal error.

ObjectId's are what you cache and use, not DBObjects. Try
storing the ObjectIds in a variable (or the tag property if you
wish), and then open the objects again using the ObjectId
when you need to access them.

Another problem your code has is that you are developing
it with an SDI mindset. As soon as you try opening more
than one document, then switching to another document
and clicking the Ok button on your tool, you will find that
your code is operating on the wrong document (not the
one containing the objects that you've previously cached
in the Control.Tag property), and kaboom.

Also, if your command method is run in multiple documents,
what do think is going to happen?

I'm afraid that developing an MDI-aware modeless user
interface is not as simple as it may at first appear from
afar.

Hi Tony,

 Thanks for the responds.

  In my testing I noticed that the event fired twice per selection, single window or single crossing or selection of a single object, without a command active, and I did notice that it fired each time I made a window. I noticed that it even fired when I hit escape to deselect all objects.  I assumed that it would fire the same amount during a command also.

Maybe I can add some code to check to see if a command is issued, and if so ignore (or deactivate) my selection event.  Thats a good idea.

I think I will change the code to kick you out once you have more than one block in the selection set, no need to keep checking unless I make it a multi-tab palette, one per block name with the amount in parenthesis.  Just an idea.
« Last Edit: June 05, 2007, 06:36:19 AM by TonyT »

LE

  • Guest
Re: Tool palette, Object selection and Event
« Reply #21 on: June 05, 2007, 10:32:29 AM »
Thank you Tony for your comments;

It is to much there for learning, I started into events but stop, the coding I been doing in C# it is just to keep me busy and to learn the language as much as I can, I have a lot of free time lately. :)

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Tool palette, Object selection and Event
« Reply #22 on: June 05, 2007, 11:03:50 AM »
Thanks again Tony.  I didn't know that you should not cache the DBObjects.  I thought it was a quick way to get the reference again once the user hit the okay button.  I will use the ObjectId.

I know about the problem with MDI mode and my code, that is why I made sure you could edit the attribute, it you can't it will throw a message box saying it can't edit it.

You are right that I add the event more than once, didn't think about that while testing yesterday.  I will look into this as I think I know how to code it.

I'm afraid that developing an MDI-aware modeless user
interface is not as simple as it may at first appear from
afar.
But it will be a good learning experience.  Thanks again for you comments, I really appreciate the time people who know more than I take to point out mistakes I have made.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Tool palette, Object selection and Event
« Reply #23 on: June 05, 2007, 02:00:07 PM »
Man events are harder to work with than reactors in lisp.  I can't seem to find a why to check to see if one is loaded so that it won't get loaded more than once.  If anyone knows a way, or a direction to follow please let me know.  Thanks. 


Off to look in some new places.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

TonyT

  • Guest
Re: Tool palette, Object selection and Event
« Reply #24 on: June 05, 2007, 03:37:36 PM »
Tim - You don't need to find out if the event handler
was added.

The problem isn't that, the problem is that you add
event handlers in commands, which you shouldn't
do. If the event is document-specific, you add the
event handler when the document opens (e.g., in
the DocumentCollection's DocumentAdded event)

Man events are harder to work with than reactors in lisp.  I can't seem to find a why to check to see if one is loaded so that it won't get loaded more than once.  If anyone knows a way, or a direction to follow please let me know.  Thanks. 


Off to look in some new places.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Tool palette, Object selection and Event
« Reply #25 on: June 05, 2007, 04:03:17 PM »
Tim - You don't need to find out if the event handler
was added.

The problem isn't that, the problem is that you add
event handlers in commands, which you shouldn't
do. If the event is document-specific, you add the
event handler when the document opens (e.g., in
the DocumentCollection's DocumentAdded event)

Man events are harder to work with than reactors in lisp.  I can't seem to find a why to check to see if one is loaded so that it won't get loaded more than once.  If anyone knows a way, or a direction to follow please let me know.  Thanks. 


Off to look in some new places.
Tony,

This is just what I did.   :-)

After I couldn't find a way to see if it was loaded, I said to myself "why check?"  So I made three events, right now.  One for when a document is created, one for when it's to be destroyed, and one for when it becomes current.  It seems to be working well right now, but I want to add in the check for a command.  If a command is running, don't do anything.  Edit:  I also add the selection event to all open drawings when the command is issued for the first time.

Thanks again.  Your help pointed me in the right direction.

I will post the code when I figure out the last issue.  I think it's the last issue.
« Last Edit: June 05, 2007, 04:04:35 PM by T.Willey »
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Tool palette, Object selection and Event
« Reply #26 on: June 05, 2007, 06:28:38 PM »
Okay so putting in the command and lisp checks are not as easy as I would have hoped.  I think it won't be until tomorrow or later until I figure them out.  :oops:
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

LE

  • Guest
Re: Tool palette, Object selection and Event
« Reply #27 on: June 05, 2007, 06:41:42 PM »
Okay so putting in the command and lisp checks are not as easy as I would have hoped.  I think it won't be until tomorrow or later until I figure them out.  :oops:

relax my friend, take your time, there is more time than life.... (don't let your hair turn grey)

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Tool palette, Object selection and Event
« Reply #28 on: June 05, 2007, 06:51:27 PM »
Okay so putting in the command and lisp checks are not as easy as I would have hoped.  I think it won't be until tomorrow or later until I figure them out.  :oops:

relax my friend, take your time, there is more time than life.... (don't let your hair turn grey)
:lol: Thanks.  8-)
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Tool palette, Object selection and Event
« Reply #29 on: June 05, 2007, 07:38:53 PM »
Here is the code as is.  When switching between open drawings, when the tool palette is open, it will correct to tool palette to the current drawing.  I haven't figured out how to have the command/lisp events work correctly, but you will see ideas in the code that I was trying.  Comments/opinions/compliments ( :angel: ), all welcomed.


Edit:  Removed old code.
« Last Edit: June 14, 2007, 01:07:32 PM by T.Willey »
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.