@@ -9,81 +9,58 @@ open Fesh
9
9
10
10
module ScriptingSyntax =
11
11
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)
20
12
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)
29
18
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
36
27
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
40
34
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
50
38
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
60
47
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
69
48
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
76
49
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
80
57
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