Skip to content

Commit 99466e2

Browse files
committed
async transactions
1 parent 2893909 commit 99466e2

File tree

3 files changed

+56
-72
lines changed

3 files changed

+56
-72
lines changed

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [0.26.4] - 2025-05-16
11+
### Changed
12+
- Enable running transactions from async Thread
13+
1014
## [0.26.3] - 2025-05-14
1115
### Changed
1216
- Update to Fesh 0.26.3
@@ -52,7 +56,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
5256
## [0.14.0] - 2024-11-04
5357
### Changed
5458
- First public release
55-
[Unreleased]: https://github.com/goswinr/Fesh.Revit/compare/0.26.3...HEAD
59+
60+
61+
[Unreleased]: https://github.com/goswinr/Fesh.Revit/compare/0.26.4...HEAD
62+
[0.26.4]: https://github.com/goswinr/Fesh.Revit/compare/0.26.3...0.26.4
5663
[0.26.3]: https://github.com/goswinr/Fesh.Revit/compare/0.25.0...0.26.3
5764
[0.25.0]: https://github.com/goswinr/Fesh.Revit/compare/0.24.1...0.25.0
5865
[0.24.1]: https://github.com/goswinr/Fesh.Revit/compare/0.24.0...0.24.1

Fesh.Revit/Src/Addin.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace Fesh.Revit
1+
namespace Fesh.Revit
22

33
open Autodesk.Revit.UI
44
open Autodesk.Revit.DB
@@ -142,7 +142,7 @@ type FeshAddin()= // : IExternalApplication = // don't rename ! This is referenc
142142
|ExternalEventRequest.TimedOut -> DebugUtils.alert "exEvent.Raise() returned ExternalEventRequest.TimedOut"
143143
|x -> DebugUtils.alert $"exEvent.Raise() returned unknown ExternalEventRequest: {x}"
144144

145-
/// runs a F# function via the IExternalEventHandler pattern for mode-less dialogs
145+
/// Runs a F# function via the IExternalEventHandler pattern for mode-less dialogs
146146
/// this is the only way to run code from mode-less dialogs such as Fesh editor
147147
member this.RunOnDoc (transaction:Document->unit) =
148148
this.RunOnApp (fun app -> transaction app.ActiveUIDocument.Document)

Fesh.Revit/Src/ScriptingSyntax.fs

Lines changed: 46 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -9,81 +9,58 @@ open Fesh
99

1010
module ScriptingSyntax =
1111

12-
/// Runs a function in a transaction
13-
/// Will log errors to Fesh Log if transaction has problems
14-
let run (transaction: Document-> unit) =
15-
FeshAddin.Instance.RunOnDoc (fun (doc:Document) ->
16-
use t = new Transaction(doc, "Fesh F# script")
17-
let s = t.Start()
18-
match s with
19-
|TransactionStatus.Started -> () //transaction has begun (until committed or rolled back)
2012

21-
|TransactionStatus.Committed //simply committed, ended an empty transaction, flushed all, or undo is disabled
22-
|TransactionStatus.Uninitialized //initial value, the transaction has not been started yet in this status
23-
|TransactionStatus.RolledBack //rolled back (aborted)
24-
|TransactionStatus.Pending //returned from error handling that took over managing the transaction
25-
|TransactionStatus.Error //error while committing or rolling back
26-
|TransactionStatus.Proceed -> //while still in error handling (internal status)
27-
eprintfn "Transaction.Start returned: %A" s
28-
|_ -> eprintfn "Transaction.Start returned unknown state: %A" s
13+
let private transact (doc:Document) (action:unit->unit)=
14+
use t = new Transaction(doc, "Fesh F# script")
15+
let s = t.Start()
16+
match s with
17+
|TransactionStatus.Started -> () //transaction has begun (until committed or rolled back)
2918

30-
try
31-
transaction doc
32-
with ex ->
33-
match DebugUtils.Fesh with
34-
|None -> ()
35-
|Some fesh -> fesh.Log.PrintfnColor 240 0 0 "Function in transaction failed with:\r\n%A" ex
19+
|TransactionStatus.Committed //simply committed, ended an empty transaction, flushed all, or undo is disabled
20+
|TransactionStatus.Uninitialized //initial value, the transaction has not been started yet in this status
21+
|TransactionStatus.RolledBack //rolled back (aborted)
22+
|TransactionStatus.Pending //returned from error handling that took over managing the transaction
23+
|TransactionStatus.Error //error while committing or rolling back
24+
|TransactionStatus.Proceed -> //while still in error handling (internal status)
25+
eprintfn "Transaction.Start returned: %A" s
26+
|_ -> eprintfn "Transaction.Start returned unknown state: %A" s
3627

37-
let r = t.Commit()
38-
match r with
39-
|TransactionStatus.Committed -> () //simply committed, ended an empty transaction, flushed all, or undo is disabled
28+
try
29+
action()
30+
with ex ->
31+
match DebugUtils.Fesh with
32+
|None -> ()
33+
|Some fesh -> fesh.Log.PrintfnColor 240 0 0 "Function in transaction failed with:\r\n%A" ex
4034

41-
|TransactionStatus.Uninitialized //initial value, the transaction has not been started yet in this status
42-
|TransactionStatus.Started //transaction has begun (until committed or rolled back)
43-
|TransactionStatus.RolledBack //rolled back (aborted)
44-
|TransactionStatus.Pending //returned from error handling that took over managing the transaction
45-
|TransactionStatus.Error //error while committing or rolling back
46-
|TransactionStatus.Proceed -> //while still in error handling (internal status)
47-
eprintfn "Transaction.Commit returned: %A" r
48-
|_ -> eprintfn "Transaction.Commit returned unknown state: %A" r
49-
)
35+
let r = t.Commit()
36+
match r with
37+
|TransactionStatus.Committed -> () //simply committed, ended an empty transaction, flushed all, or undo is disabled
5038

51-
/// Runs a function in a transaction
52-
/// Will log errors to Fesh Log if transaction has problems
53-
let runApp (transaction: UIApplication-> unit) =
54-
FeshAddin.Instance.RunOnApp (fun (app:UIApplication) ->
55-
let doc = app.ActiveUIDocument.Document
56-
use t = new Transaction(doc, "Fesh F# script")
57-
let s = t.Start()
58-
match s with
59-
|TransactionStatus.Started -> () //transaction has begun (until committed or rolled back)
39+
|TransactionStatus.Uninitialized //initial value, the transaction has not been started yet in this status
40+
|TransactionStatus.Started //transaction has begun (until committed or rolled back)
41+
|TransactionStatus.RolledBack //rolled back (aborted)
42+
|TransactionStatus.Pending //returned from error handling that took over managing the transaction
43+
|TransactionStatus.Error //error while committing or rolling back
44+
|TransactionStatus.Proceed -> //while still in error handling (internal status)
45+
eprintfn "Transaction.Commit returned: %A" r
46+
|_ -> eprintfn "Transaction.Commit returned unknown state: %A" r
6047

61-
|TransactionStatus.Committed //simply committed, ended an empty transaction, flushed all, or undo is disabled
62-
|TransactionStatus.Uninitialized //initial value, the transaction has not been started yet in this status
63-
|TransactionStatus.RolledBack //rolled back (aborted)
64-
|TransactionStatus.Pending //returned from error handling that took over managing the transaction
65-
|TransactionStatus.Error //error while committing or rolling back
66-
|TransactionStatus.Proceed -> //while still in error handling (internal status)
67-
eprintfn "Transaction.Start returned: %A" s
68-
|_ -> eprintfn "Transaction.Start returned unknown state: %A" s
6948

70-
try
71-
transaction app
72-
with ex ->
73-
match DebugUtils.Fesh with
74-
|None -> ()
75-
|Some fesh -> fesh.Log.PrintfnColor 240 0 0 "Function in transaction failed with:\r\n%A" ex
7649

77-
let r = t.Commit()
78-
match r with
79-
|TransactionStatus.Committed -> () //simply committed, ended an empty transaction, flushed all, or undo is disabled
50+
/// Runs a function in a transaction using a Document
51+
/// Will log errors to Fesh Log if transaction has problems
52+
/// This function ca be invoked from any thread, it will switch to the Revit UI thread
53+
let run (transaction: Document -> unit) =
54+
let action () =
55+
FeshAddin.Instance.RunOnDoc (fun (doc:Document) -> transact doc (fun () -> transaction doc))
56+
Fittings.SyncWpf.doSync action
8057

81-
|TransactionStatus.Uninitialized //initial value, the transaction has not been started yet in this status
82-
|TransactionStatus.Started //transaction has begun (until committed or rolled back)
83-
|TransactionStatus.RolledBack //rolled back (aborted)
84-
|TransactionStatus.Pending //returned from error handling that took over managing the transaction
85-
|TransactionStatus.Error //error while committing or rolling back
86-
|TransactionStatus.Proceed -> //while still in error handling (internal status)
87-
eprintfn "Transaction.Commit returned: %A" r
88-
|_ -> eprintfn "Transaction.Commit returned unknown state: %A" r
89-
)
58+
59+
/// Runs a function in a transaction using an UIApplication
60+
/// (app.ActiveUIDocument.Document gets the active document)
61+
/// Will log errors to Fesh Log if transaction has problems
62+
/// This function ca be invoked from any thread, it will switch to the Revit UI thread
63+
let runApp (transaction: UIApplication-> unit) =
64+
let action () =
65+
FeshAddin.Instance.RunOnApp (fun (app:UIApplication) -> transact app.ActiveUIDocument.Document (fun () -> transaction app))
66+
Fittings.SyncWpf.doSync action

0 commit comments

Comments
 (0)