Modelles form and external event with trasaction

Modelles form and external event with trasaction

sonicer
Collaborator Collaborator
757 Views
7 Replies
Message 1 of 8

Modelles form and external event with trasaction

sonicer
Collaborator
Collaborator

Hello,

Is it possible with modelles form and external events command with Trasaction group??

Open trasaction group when modelles is show. Then make some other modification in model with several buttons and then close form with transaction group close..

 

thx.

0 Likes
758 Views
7 Replies
Replies (7)
Message 2 of 8

jeremytammik
Autodesk
Autodesk

You should really use a spell checker, especially in the description of your question.

  

The spelling errors you introduce make it harder to understand your question and almost impossible to find it again in subsequent searches.

  

I spell check almost everything I publish as a courtesy to my fellow human beings and also to the inhuman search engines, even though the latter can cope better with typos.

  

Now, to address your question:

  

You can use a transaction group exactly and absolutely everywhere that a normal transaction can be used.

 

In other words, it requires a valid Revit API context, nothing more and nothing less.

 

Here is more information about thew valid Revit API context:

 

https://thebuildingcoder.typepad.com/blog/about-the-author.html#5.28

 

Here is everything you need to know about transaction groups:

 

https://thebuildingcoder.typepad.com/blog/about-the-author.html#5.53

 



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

0 Likes
Message 3 of 8

sonicer
Collaborator
Collaborator

I'm sorry, I wrote it in a hurry.


I am preparing some add-in for Revit.

I have modeless dialogue with many buttons.

The buttons call external events with transaction.

Is it possible all this transaction assimilate after closing this dialogue?


Why I am using modeless dialogue?
I need to be able use pan/zoom function on the floor plan to see the preview result.

 

thx.

 

 

 

 

0 Likes
Message 4 of 8

jeremytammik
Autodesk
Autodesk

Hey, that sounds interesting.

 

Am I right in assuming that you wish to change various different things in your modeless dialogue and see the intermediate results before confirming the commit? 

 

That might require regeneration of the model, and even committing the transaction. 

 

And you wish to pan and zoom before committing the transaction?

 

This may be difficult or impossible.

 

The external event handler implements an execute method. In that method, you can open a transaction, close and commit it. You can do the same with a transaction group.

 

However, I hardly believe that you can open a transaction or transaction group and leave it open after returning from the event handler. and then returning to a different event handler by raising separate subsequent external event to close it.

  

Tricky and interesting.

 

Please clarify further what you are after.

  

Please take time and care to formulate your question.

  

If you are in a hurry, do something else by yourself.

  

Please do not ask questions to other people in a hurry, or you will be wasting everybodys time.

 

Thank you.

 

Cheers,

 

Jeremy

 



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

0 Likes
Message 5 of 8

Kennan.Chen
Advocate
Advocate

One possible solution:

public class MyWindow : Window, IExternalEventHandler
{
  private IList<Action<UIApplication>> _actions;
  private IList<Action<UIApplication>> Actions => _actions ?? (_actions = new List<Action<UIApplication>>());

  public MyWindow()
  {
    var commit = ExternalEvent.Create(this);
    this.Closed += (sender,e) => {
      commit.Raise();
    }
  }
  
  public void Execute(UIApplication application)
  {
    using (var transaction = new Transaction(application.ActionUIDocument.Document,"doing something"))
    {
        transaction.Start();
        Actions.ForEach(action => {
          action(app);
        })
        transaction.Commit();
    }
  }

  private void OnOneButtonClick(object sender, EventArgs e)
  {
    Actions.Add(app => 
    {
      // some business code here
    })
  }

  private void OnAnotherButtonClick(object sender, EventArgs e)
  {
    Actions.Add(app => 
    {
      // some business code here
    })
  }
}
0 Likes
Message 6 of 8

Kennan.Chen
Advocate
Advocate

Sorry for my carelessness.

 

The code should be :

Actions.ForEach(action => {
  action(application);
})

 

0 Likes
Message 7 of 8

Kennan.Chen
Advocate
Advocate

My carelessness again. The window will be disposed on closed so the IExternalEventHandler instance will not be available if the window itself implements IExternalEventHandler. We'd better separate them.

public class MyWindow : Window
{
  private AccumulatedExternalEventHandler EventHandler { get; }
  public MyWindow()
  {
    EventHandler = new AccumulatedExternalEventHandler();
    var commit = ExternalEvent.Create(EventHandler);
    this.Closed += (sender,e) => {
      commit.Raise();
    };
  }

  private void OnOneButtonClick(object sender, EventArgs e)
  {
    EventHandler.Add(app =>
    {
      // revit api code here
    });
  }

  private void OnAnotherButtonClick(object sender, EventArgs e)
  {
    EventHandler.Add(app =>
    {
      // revit api code here
    });
  }
}

public class AccumulatedExternalEventHandler : IExternalEventHandler
{
  private IList<Action<UIApplication>> _actions;
  private IList<Action<UIApplication>> Actions => _actions ?? (_actions = new List<Action<UIApplication>>());

  public void Add(Action<UIApplication> action)
  {
    Actions.Add(action);
  }

  public void Execute(UIApplication application)
  {
    using (var transaction = new Transaction(application.ActionUIDocument.Document,"doing something"))
    {
        transaction.Start();
        Actions.ForEach(action => {
          action(application);
        })
        transaction.Commit();
    }
  }
}
0 Likes
Message 8 of 8

sonicer
Collaborator
Collaborator

thanks

thats look like very interesting.

I found some blog about external events and trasaction group.

https://thebuildingcoder.typepad.com/blog/2018/11/more-on-transaction-groups-and-assimilation.html

 

But I change my concept from modelles dialog to modal dialog and used for preview  PreviewControl from Revit api.

 

thx all.

0 Likes