IExternalEventHandler run from form Form works well on wall, but not on custom family

IExternalEventHandler run from form Form works well on wall, but not on custom family

bixilix
Contributor Contributor
1,312 Views
6 Replies
Message 1 of 7

IExternalEventHandler run from form Form works well on wall, but not on custom family

bixilix
Contributor
Contributor

Hi everyone 🙂
I´ve got a strange problem with one of my forms / objects.  I have a simple button in a modeless form. From this Form I raise an event.

 

void Button2Click(object sender, EventArgs e)
        {
            Button s = sender as Button;
            SD_SetParameter.value = s.Text;
            eventChangeValuesEventhandler.Raise();
        }

In this test case, I just wish to change a simple parameter.

When I select a wall or another (system?) family it works fine. It changes the value with the click of the button.

When I select a custom (Generic Model) the thread seems to be blocked. I perform the click, and the command is run AFTER I deselect the element.

For this reason, I cannot do "live" changes to custom families. Do you have an idea on how to solve this issue or am I doing something completely wrong?
Thanks for your support 🙂

 

[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    [Autodesk.Revit.DB.Macros.AddInId("892B06EC-0E66-40F0-B504-21FDAB4BD192")]
    public class SD_SetParameter : IExternalEventHandler  //this is the last when one making a checklist change, EE4 must be just for when an element is new
    {
      
        public void Execute(UIApplication a)
        {
            try
            {
                //Document doc = a.ActiveUIDocument.Document;
                UIDocument uidoc = a.ActiveUIDocument;
                if (uidoc == null)
                {
                    return;
                }

                Document doc = uidoc.Document;

                ICollection<Autodesk.Revit.DB.ElementId> selection = uidoc.Selection.GetElementIds();
                
                using (Transaction t = new Transaction(doc, "Set Parameter"))
                {
                    t.Start("Set Parameters");
                    foreach (ElementId id in selection)
                    {
                        Autodesk.Revit.DB.Element e = doc.GetElement(id);

                        Parameter p = e.LookupParameter("Kommentare");
                        if (p != null)
                        {
                            p.Set("Test");
                        }

                    }
                    t.Commit();

                }
            }
            catch (Exception ex)
            {
                TaskDialog.Show("Catch", "Failed due to:" + Environment.NewLine + ex.Message);
            }
            finally
            {

            }
        }
 
        public string GetName()
        {
            return "External Event Example";
        }
    }

 

0 Likes
Accepted solutions (1)
1,313 Views
6 Replies
Replies (6)
Message 2 of 7

aclarke
Advocate
Advocate

Hi,

I believe you might have the parameter 'Kommentar' in your family and in your mode as a shared parameter.

Remove the parameter from either the model or the family so there is only one parameter to find.

LookUpParamter is finding the first of the two and the first one seems to be not the correct one.

 

This happened to me, I removed the parameter from the family and it fixed my issue.

 

 

0 Likes
Message 3 of 7

bixilix
Contributor
Contributor

Hi aclarke,

 

thanks for your suggestion. Unfortunately, that does not solve my problem. It doesn´t depend on the parameter. The problem is, that the Main Revit Thread blocks any Events when a generic model is selected and queues up the execution of my ExternalEvent.

 

When I select a system family (walls, door, window) etc.. the thread is not blocked and changes coming from my IExternalEventHandler get executed immediately.  

 

0 Likes
Message 4 of 7

aclarke
Advocate
Advocate

yeah, I had that same issue firing an IExternal from an IUpdater, sometimes it would fire then on the third time it wouldn't; then random...  drove me crazy.  I still don't think I fixed it, fixed it.

 

Have you tried this???  maybe?

 

https://thebuildingcoder.typepad.com/blog/2013/12/triggering-immediate-external-event-execute.html 

0 Likes
Message 5 of 7

bixilix
Contributor
Contributor

Hey aclarke, 

thanks for getting back at me 🙂 Unfortunately I seem to have the worst case scenario. I use a form with a modeless dialog which fires the Events via Button click.

This problem was not solved here: https://thebuildingcoder.typepad.com/blog/2013/12/triggering-immediate-external-event-execute.html

I tried a different approach by attaching to the idle command. Unfortunately, Revit is not idling when my custom family is selected which makes it impossible to intereact here...

 

I´m a little helpless now... Currently, I have to click the button, then deselect the object, so my actions are performed.. I´m thinking about performing a simple mouseclick inside the Revit-Window via Script, but this does not seem to be a clean solution at all 😞

https://stackoverflow.com/questions/8272681/how-can-i-simulate-a-mouse-click-at-a-certain-position-o...

0 Likes
Message 6 of 7

bixilix
Contributor
Contributor
Accepted solution

Ok, I´ve got "my solution now". Thanks for leading me in the right direction @Anonymous 🙂

 

... I just click the ESC-Key programmatically, after my Form Button click. This is my code:

 

 

[DllImport("user32.dll")]
        public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);

        // List Virtual Keycodes: http://www.kbdedit.com/manual/low_level_vk_list.html
        void ButtnClick(object sender, EventArgs e)
        {

            myEvent.Raise();
            // Press ESC-Key
            const int ESC = 0x1B;
            const uint KEYEVENTF_EXTENDEDKEY = 0x0001;
            keybd_event((byte)ESC, 0, KEYEVENTF_EXTENDEDKEY | 0, 0);

        }

 

0 Likes
Message 7 of 7

aclarke
Advocate
Advocate

Ha, I think we came up with the same solution....

 

the external event updater I mentioned, I just removed the 'red x' and used an escape feature for my form as well.  Seemed to work for me as well.  This is why I said I don't think I fixed it fixed it.  Just made it work with fingers crossed...

 

 

0 Likes