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
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
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.
1 Attachment(s)
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.
Re: Interacting with Revit through a UserForm
Thank Tim,
I will look at your file :)
Trang
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
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?
Re: Interacting with Revit through a UserForm
Quote:
Originally Posted by
Ning Zhou
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:
Quote:
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
Re: Interacting with Revit through a UserForm
thanks cshha, looks like it's already using idling event, maybe not using correctly?!
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;
}