Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Interacting with Revit through a UserForm

  1. #1
    Member
    Join Date
    2011-04
    Posts
    8
    0 Did you find this post helpful? Yes

    Default Interacting with Revit through a UserForm

    Hi everyone,

    I have a Problem with the ActiveView in Revit. I want to hide seleted elements and here is the code:

    UIDocument uiDoc = commandData.Application.ActiveUIDocument;
    ElementSet elems = uiDoc.Selection.Elements;

    uiDoc.Document.ActiveView.Hide(elems);
    uiDoc.RefreshActiveView();


    It works when this code is written directly in the Excute function (in the class which inherited from IEXternalCommand).

    However, when I use a form to interact with Revit, it doesn't work. It is so, I have one button on the form. I want that once I click the button, the selected elements will be hidden. But when I click the button, nothing occurs.

    I tried to use ShowElements(element) to activate the ActiveView fo Revit before using Hide(elems), but what I get is that the view window is changed so that I can see the element clearly but selected elements are still visible.

    do you have any idea to deal with this problem?

    Thanks.

    Trang

  2. #2
    Member
    Join Date
    2010-09
    Posts
    4
    0 Did you find this post helpful? Yes

    Default Re: Interacting with Revit through a UserForm

    What I typically do is just pass the commandData to the form and setup the UIDocument and ElementSet variables from there. If you make the form modeless you can leave it up and subscribe to the idling event to keep refreshing your selection and you'll be able hit the hide button whenever you want.

    -Tim

  3. #3
    Member
    Join Date
    2011-04
    Posts
    8
    0 Did you find this post helpful? Yes

    Default Re: Interacting with Revit through a UserForm

    Hi Tim,

    yes, that is what I have done. Passing commandData to the form. Using the form like a Modeless. And after choosing elements in Revit, click the button. But everything is still visible. Every functions of the (revit..)document object such as ShowElements(element) work but not with ActiveView object such as Hide(selementset).

    Do you add something else in your project. I don't understand much when you say leaving the form up and adding an idling event. That is what you program or simply do it manually?

    Thanks.

    Trang.
    Last edited by dangtrang37325793; 2011-05-02 at 02:27 PM.

  4. #4
    Member
    Join Date
    2010-09
    Posts
    4
    0 Did you find this post helpful? Yes

    Default Re: Interacting with Revit through a UserForm

    Hey Trang,

    I don't think i did anything special, but I'm still learning how to use the Revit API myself so I don't always understand everything that I do. Here are the files for something I put together after my first post. It opens a modeless dialog that will tell you how many elements are selected at any given moment and includes a button to hide the selected.
    Attached Files Attached Files

  5. #5
    Member
    Join Date
    2011-04
    Posts
    8
    0 Did you find this post helpful? Yes

    Default Re: Interacting with Revit through a UserForm

    Thank Tim,

    I will look at your file

    Trang

  6. #6
    Member
    Join Date
    2011-04
    Posts
    8
    0 Did you find this post helpful? Yes

    Default Re: Interacting with Revit through a UserForm

    Hi Tim,

    It is so great. It works. The reason is that, I didn't use Transaction.

    Thank you so much.

    Trang

  7. #7
    All AUGI, all the time Ning Zhou's Avatar
    Join Date
    2006-08
    Posts
    612
    0 Did you find this post helpful? Yes

    Default Re: Interacting with Revit through a UserForm

    very old thread but still useful.

    just noticed that it's still working in 2013 but not anymore in 2014, transaction cannot be started at all and revit silently exit w/o any warning, bug or intentional?

  8. #8
    Member
    Join Date
    2012-09
    Posts
    31
    0 Did you find this post helpful? Yes

    Default Re: Interacting with Revit through a UserForm

    Quote Originally Posted by Ning Zhou View Post
    very old thread but still useful.

    just noticed that it's still working in 2013 but not anymore in 2014, transaction cannot be started at all and revit silently exit w/o any warning, bug or intentional?
    This is intentional. Calling a transaction from outside the main thread has never been supported, but before 2014, there was no verification taking place to ensure that transactions were only being called from the main thread.

    You'll need to use the Idling event if you want to drive Revit from a modeless dialog.

    From the "What's New" for Revit 2014:

    API validation

    No transactions from outside threads

    Calling into the Revit API from outside threads and outside modeless dialogs has never been supported, but it was not strictly prohibited, meaning there would be no immediate exceptions when someone tries to modify model from outside of the supported API workflows. That has been changed. It is no longer possible to start a transaction unless the caller is inside a legitimate API call, such as an external command, event, updater, call-back, etc. An exception will be thrown if such attempt is made.
    More info: http://thebuildingcoder.typepad.com/...ble-panel.html

  9. #9
    All AUGI, all the time Ning Zhou's Avatar
    Join Date
    2006-08
    Posts
    612
    0 Did you find this post helpful? Yes

    Default Re: Interacting with Revit through a UserForm

    thanks cshha, looks like it's already using idling event, maybe not using correctly?!

  10. #10
    Member
    Join Date
    2012-09
    Posts
    31
    0 Did you find this post helpful? Yes

    Default Re: Interacting with Revit through a UserForm

    You're right, it's not being used correctly.

    The Transaction needs to take place inside the Idling event.

    The button click handler and Idling event handler should look something like this in Revit 2014:

    Code:
            void revitApp_Idling(object sender, Autodesk.Revit.UI.Events.IdlingEventArgs e)
            {
                UIApplication uiapp = sender as UIApplication;
    
                UIDocument uidoc = uiapp.ActiveUIDocument;
    
                ElementSet elems = uidoc.Selection.Elements;
    
                if (elems != null)
                {
                    label1.Text = elems.Size.ToString() + " items selected.";
                }
                else
                {
                    label1.Text = "No elements selected.";
                }
    
                if (shouldRun)
                {
                    using (Transaction trans = new Transaction(uidoc.Document, "Hide elements"))
                    {
                        trans.Start();
                        uidoc.Document.ActiveView.HideElements((from Element el in elems select el.Id).ToList());
                        uidoc.RefreshActiveView();
                        trans.Commit();
                    }
    
                    shouldRun = false;
                }
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                shouldRun = true;
            }

Page 1 of 2 12 LastLast

Similar Threads

  1. interacting with excel
    By mdsalman2003 in forum AutoLISP
    Replies: 16
    Last Post: 2010-11-10, 04:00 PM
  2. All I want is a Userform to show..
    By Coolmo in forum Dot Net API
    Replies: 5
    Last Post: 2009-07-23, 02:03 PM
  3. VBA userform in excel
    By Streng in forum VBA/COM Interop
    Replies: 3
    Last Post: 2009-02-17, 07:02 PM
  4. in-place truss interacting with stick symbols
    By ck.107547 in forum Revit Structure - General
    Replies: 5
    Last Post: 2006-07-06, 12:02 PM
  5. Userform sizing
    By Coolmo in forum VBA/COM Interop
    Replies: 7
    Last Post: 2006-05-30, 11:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •