Update a modeless dialog by external event

Update a modeless dialog by external event

Anonymous
Not applicable
1,814 Views
6 Replies
Message 1 of 7

Update a modeless dialog by external event

Anonymous
Not applicable

Dear

I created a modeless form because I need to select elements from Revit window and update the form repeatedly.  but unfortunately, the dialog is not updated after the external event handler is called. It should be noted that I do no change the model in Revit, in fact, I just want to select elements and assign them into a node of a tree view. but the tree view is crashed after I click buttons in the form.

 

Best Regards

 

My Application.cs is as follows

public class Application : IExternalApplication
{
// class instance
internal static Application thisApp = null;
//ModelessForm instance
public static ModelessForm m_MyForm;

/// <summary>
/// Implements the OnShutdown event
/// </summary>
/// <param name="application"></param>
/// <returns></returns>
///
public Result OnShutdown(UIControlledApplication application)
{
if (m_MyForm != null && m_MyForm.Visible)
{
m_MyForm.Close();
}

return Result.Succeeded;
}

/// <summary>
/// Implements the OnStartup event
/// </summary>
/// <param name="application"></param>
/// <returns></returns>
///

public Result OnStartup(UIControlledApplication application)
{
m_MyForm = null; // no dialog needed yet;the command will bring it
thisApp = this; // static access to this application instance

return Result.Succeeded;
}

/// <summary>
/// This method creates and shows a modeless dialog, unless it already exists.
/// </summary>
/// <remarks>
/// The external command invokes this on the end-user's request
/// </remarks>
///

public void ShowForm(UIApplication uiapp)
{
// If we do not have a dialog yet, create and show it
if (m_MyForm == null || m_MyForm.IsDisposed)
{
// A new handler to handle request posting by dialog
RequestHandler handler = new RequestHandler();

// External Event for the dialog to use (to post request)
ExternalEvent exEvent = ExternalEvent.Create(handler);

// We give the objects to the new dialog;
// The dialog becomes the owner responsible fore disposing them, eventually.
m_MyForm = new ModelessForm(exEvent, handler, uiapp);

m_MyForm.ExEvent = exEvent;
handler.Item = m_MyForm;

m_MyForm.Show();
}
}

And the execute of the external event is as follows:

public void Execute(UIApplication uiapp)
{

if ((Request.Take()) == RequestId.None)
{

UIDocument revitDoc = uiapp.ActiveUIDocument;
Document dbdoc = revitDoc.Document;
Autodesk.Revit.DB.View view = dbdoc.ActiveView;



ICollection<ElementId> ids = uiapp.ActiveUIDocument.Selection.GetElementIds();

if (0 == ids.Count)
{
FilteredElementCollector collector =
new FilteredElementCollector(revitDoc.Document, view.Id)
.WhereElementIsNotElementType();
ids = collector.ToElementIds();
}


ICollection<Element> elemSet = new
List<Element>(ids.Select<ElementId, Element>(id => dbdoc.GetElement(id)));

using (Transaction trans = new Transaction(uiapp.ActiveUIDocument.Document))
{
trans.Start("R2019 External Event Sample");

Item.ViewModelNonStatic(uiapp);

trans.Commit();
}

}
}

 ViewModelNonStatic is a method of ModelessForm.

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

jeremytammik
Autodesk
Autodesk

Why do you start and commit a transaction if you are not modifying the document?

 

If all you are doing is selecting elements, there is no need whatsoever for any transaction at all.

 

In fact, in that case, I recommend that you specify read-only transaction mode for your entire application.

 

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 3 of 7

Anonymous
Not applicable

Dear 

Thanks for your answer.

In fact, I changed the transaction into read-only. But my problem exists yet. I have a tree view in ModelessForm and my goal is that when I select elements, they automatically add to the tree view. In this way, I created a public method namely "ViewModelNonStatic" in the ModelessForm which adds elements to the tree view. I call "ViewModelNonStatic" in the "Execute" method of "RequestHandler" where just elements are selected. But my tree view never updates. Another problem is that when I press a button in the form which raises the "Execute", all the contents of the tree view disappear.

Would you mind helping me?

 

Some snippets of my code are as follows:

 

public ModelessForm Item { get; set; }

The Execute of my request handler:

public void Execute(UIApplication uiapp)
{
if ((Request.Take()) == RequestId.None)
{
UIDocument revitDoc = uiapp.ActiveUIDocument;
Document dbdoc = revitDoc.Document;

ICollection<ElementId> ids = uiapp.ActiveUIDocument.Selection.GetElementIds();

ICollection<Element> elemSet = new
List<Element>(ids.Select<ElementId, Element>(id => dbdoc.GetElement(id)));

Item.ViewModelNonStatic(uiapp);
}
}

The method of ModelessForm which raise Execute:

private void TreeNodeSelected(object sender, TreeNodeMouseClickEventArgs e)
        {
            m_tvObjs1.SelectedNode = null;
            mySelectedNode = m_tvObjs1.GetNodeAt(e.X, e.Y);
            m_tvObjs1.SelectedNode = mySelectedNode;
            m_ExEvent.Raise();
        }

 

0 Likes
Message 4 of 7

Kennan.Chen
Advocate
Advocate

It's better to post more code to make things clear.

Message 5 of 7

Anonymous
Not applicable

Thank you very much. My issue was solved.

0 Likes
Message 6 of 7

Anonymous
Not applicable
Accepted solution

The following line can be added to the RequestHandler:

Application.thisApp.m_MyForm.AddObjectsToTreebyClick(Application.thisApp.m_MyForm.m_AddedNodes);

In which,  AddObjectsToTreebyClick and m_AddedNodes are the method and the field of the ModelessForm.

0 Likes
Message 7 of 7

tf
Contributor
Contributor

Hi,

Would you mind telling me how you solved this problem.  I have a similar problem.  Thanks in advance.