Skip to content

Commit 0207fec

Browse files
committed
create layer on UI thread
1 parent 779f0ff commit 0207fec

File tree

7 files changed

+611
-642
lines changed

7 files changed

+611
-642
lines changed

CHANGELOG.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
## [Unreleased]
1010

11-
## [0.11.0] - 2025-03-15
11+
## [0.11.0] - 2025-05-24
1212
### Added
1313
- build for net7.0 too
14+
### Fixed
15+
- many typos in docstrings via copilot
16+
- create layers only on UI thread
1417

1518
## [0.10.1] - 2025-03-15
1619
### Changed
@@ -78,7 +81,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7881
- First public release
7982

8083
[Unreleased]: https://github.com/goswinr/Rhino.Scripting/compare/0.11.0-net7...HEAD
81-
[0.11.0-net7]: https://github.com/goswinr/Rhino.Scripting/compare/0.10.1...0.11.0-net7
84+
[0.11.0]: https://github.com/goswinr/Rhino.Scripting/compare/0.10.1...0.11.0-net7
8285
[0.10.1]: https://github.com/goswinr/Rhino.Scripting/compare/0.10.0...0.10.1
8386
[0.10.0]: https://github.com/goswinr/Rhino.Scripting/compare/0.9.0...0.10.0
8487
[0.9.0]: https://github.com/goswinr/Rhino.Scripting/compare/0.8.2...0.9.0

Src/AutoGeneratedCode/AllScriptingFilesCombinedIntoOne_DontEditThisFile.fs

Lines changed: 533 additions & 569 deletions
Large diffs are not rendered by default.

Src/RhinoSync.fs

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ type RhinoSync private () =
3333
static let mutable prettyFormatters: ResizeArray<obj -> option<string>> = null
3434

3535
/// Red green blue text
36-
static let mutable printColor : int-> int -> int -> string -> unit = //changed via reflection below from Fesh.Rhino
37-
fun _ _ _ s -> Console.Write s
36+
static let mutable printColor : int * int * int * string -> unit= //changed via reflection below from Fesh.Rhino
37+
fun (_,_,_, s) -> Console.Write s
3838

3939
/// Red green blue text
40-
static let mutable printNewLineColor : int-> int -> int -> string -> unit = //changed via reflection below from Fesh.Rhino
41-
fun _ _ _ s -> Console.WriteLine s
40+
static let mutable printNewLineColor : int * int * int * string -> unit = //changed via reflection below from Fesh.Rhino
41+
fun (_,_,_, s) -> Console.WriteLine s
4242

4343
static let mutable clear : unit -> unit = // changed via reflection below from Fesh
4444
fun () -> ()
@@ -48,36 +48,43 @@ type RhinoSync private () =
4848
match allAss |> Array.tryFind (fun a -> a.GetName().Name = "Fesh") with
4949
| Some feshAssembly ->
5050
try
51-
let printModule = feshAssembly.GetType("Fesh.Model.IFeshLogModule")
51+
let printModule = feshAssembly.GetType "Fesh.Model.IFeshLogModule"
5252
if notNull printModule then
53-
let pc = printModule.GetProperty("printColor" ).GetValue(feshAssembly)
54-
if notNull pc then
55-
let pct = pc :?> int-> int -> int -> string -> unit
56-
printColor <- pct
57-
let pnc = printModule.GetProperty("printnColor").GetValue(feshAssembly)
58-
if notNull pnc then
59-
let pct = pnc :?> int-> int -> int -> string -> unit
60-
printNewLineColor <- pct
6153
let cl = printModule.GetProperty("clear").GetValue(feshAssembly)
6254
if notNull cl then
6355
let clt = cl :?> unit -> unit
6456
clear <- clt
57+
58+
try
59+
let pc = printModule.GetProperty( "printColorTupled" ).GetValue(feshAssembly)
60+
if notNull pc then
61+
let pct = pc :?> int * int * int * string -> unit
62+
printColor <- pct
63+
64+
let pnc = printModule.GetProperty("printnColorTupled").GetValue(feshAssembly)
65+
if notNull pnc then
66+
let pct = pnc :?> int * int * int * string -> unit
67+
printNewLineColor <- pct
68+
69+
with ex ->
70+
eprintfn "The Fesh.Model.IFeshLogModule.clear was found but printColorTupled failed. The Error was: %A" ex
71+
6572
with ex ->
6673
eprintfn "The Fesh was found but setting up color printing failed. The Error was: %A" ex
6774
|None -> ()
6875

6976
match allAss |> Array.tryFind (fun a -> a.GetName().Name = "Pretty") with
7077
| Some prettyAssembly ->
7178
try
72-
let prettySettings = prettyAssembly.GetType("Pretty.PrettySettings")
79+
let prettySettings = prettyAssembly.GetType "Pretty.PrettySettings"
7380
if notNull prettySettings then
7481
let formatters = prettySettings.GetProperty("Formatters").GetValue(prettyAssembly) :?> ResizeArray<obj -> option<string>>
7582
if notNull formatters then
7683
prettyFormatters <- formatters
7784

7885

7986
with ex ->
80-
eprintfn "The Pretty was found but setting up color printing failed. The Error was: %A" ex
87+
eprintfn "An Assembly 'Pretty' was found but setting up color printing failed. The Error was: %A" ex
8188
|None -> ()
8289

8390

@@ -211,14 +218,14 @@ type RhinoSync private () =
211218
/// Prints in both RhinoApp.WriteLine and Console.WriteLine, or Fesh Editor with color if running.
212219
/// Red green blue text , NO new line
213220
static member PrintColor r g b s =
214-
printColor r g b s
221+
printColor (r, g, b, s)
215222
RhinoApp.Write s
216223
RhinoApp.Wait()
217224

218225
/// Prints in both RhinoApp.WriteLine and Console.WriteLine, or Fesh Editor with color if running.
219226
/// Red green blue text , with new line
220227
static member PrintnColor r g b s =
221-
printNewLineColor r g b s
228+
printNewLineColor (r, g, b, s)
222229
RhinoApp.WriteLine s
223230
RhinoApp.Wait()
224231

Src/Scripting_Layer.fs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ open Rhino.Scripting.RhinoScriptingUtils
55
open System
66

77

8-
9-
10-
11-
128
[<AutoOpen>]
139
module AutoOpenLayer =
1410

Src/Scripting_Light.fs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
namespace Rhino.Scripting
22

33
open Rhino
4-
54
open System
6-
75
open Rhino.Geometry
86

9-
10-
11-
127
[<AutoOpen>]
138
module AutoOpenLight =
149
type RhinoScriptSyntax with

Src/Scripting_Line.fs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
namespace Rhino.Scripting
22

33
open Rhino
4-
54
open System
6-
75
open Rhino.Geometry
86

9-
107
[<AutoOpen>]
118
module AutoOpenLine =
129
type RhinoScriptSyntax with

Src/UtilLayer.fs

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ module internal UtilLayer =
9696
|LayerFound i -> i
9797

9898

99+
99100
/// Creates all parent layers too if they are missing, uses same locked state and colors for all new layers.
100101
/// The collapseParents parameter only has an effect if layer is created, not if it exists already
101102
let internal getOrCreateLayer(name, colorForNewLayers, visible:LayerState, locked:LayerState, allowAllUnicode:bool, collapseParents:bool) : FoundOrCreatedIndex =
@@ -109,51 +110,57 @@ module internal UtilLayer =
109110
match name.Split( [|"::"|], StringSplitOptions.None) with // TODO or use StringSplitOptions.RemoveEmptyEntries ??
110111
| [| |] -> RhinoScriptingException.Raise "RhinoScriptSyntax.UtilLayer.getOrCreateLayer: Cannot get or create layer for name: '%s'" name
111112
| ns ->
112-
let rec createLayer(nameList, prevId, prevIdx, root) : int =
113-
match nameList with
114-
| [] -> prevIdx // exit recursion
115-
| branch :: rest ->
116-
if String.IsNullOrWhiteSpace branch then // to cover for StringSplitOptions.None
117-
RhinoScriptingException.Raise "RhinoScriptSyntax.UtilLayer.getOrCreateLayer: A segment falls into String.IsNullOrWhiteSpace. Cannot get or create layer for name: '%s'" name
118-
let fullPath = if root="" then branch else root + "::" + branch
119-
match State.Doc.Layers.FindByFullPath(fullPath, RhinoMath.UnsetIntIndex) with
120-
| RhinoMath.UnsetIntIndex -> // actually create layer:
121-
failOnBadShortLayerName (branch, name, allowAllUnicode) // only check non existing sub layer names
122-
let layer = DocObjects.Layer.GetDefaultLayerProperties()
123-
if prevId <> Guid.Empty then
124-
layer.ParentLayerId <- prevId
125-
if collapseParents then
126-
State.Doc.Layers.[prevIdx].IsExpanded <- false
127-
128-
match visible with
129-
|ByParent -> ()
130-
|On -> visibleSetTrue(layer, true)
131-
|Off -> visibleSetFalse(layer, true)
132-
133-
match locked with
134-
|ByParent -> ()
135-
|On -> lockedSetTrue(layer, true)
136-
|Off -> lockedSetFalse(layer, true)
137-
138-
layer.Name <- branch
139-
layer.Color <- colorForNewLayers() // delay creation of (random) color till actually needed ( so random colors are not created, in most cases layer exists)
140-
let i = State.Doc.Layers.Add(layer)
141-
let id = State.Doc.Layers.[i].Id // just using layer.Id would be empty guid
142-
createLayer(rest , id , i, fullPath)
143-
144-
| i ->
145-
createLayer(rest , State.Doc.Layers.[i].Id , i ,fullPath)
146-
147-
LayerCreated (createLayer( ns |> List.ofArray, Guid.Empty, 0, ""))
113+
RhinoSync.DoSync(fun () -> // if not done sync the layer ui becomes unresponsive and might crash
114+
let rec createLayer(nameList, prevId, prevIdx, root) : int =
115+
match nameList with
116+
| [] -> prevIdx // exit recursion
117+
| branch :: rest ->
118+
if String.IsNullOrWhiteSpace branch then // to cover for StringSplitOptions.None
119+
RhinoScriptingException.Raise "RhinoScriptSyntax.UtilLayer.getOrCreateLayer: A segment falls into String.IsNullOrWhiteSpace. Cannot get or create layer for name: '%s'" name
120+
let fullPath = if root="" then branch else root + "::" + branch
121+
match State.Doc.Layers.FindByFullPath(fullPath, RhinoMath.UnsetIntIndex) with
122+
| RhinoMath.UnsetIntIndex -> // actually create layer:
123+
failOnBadShortLayerName (branch, name, allowAllUnicode) // only check non existing sub layer names
124+
let layer = DocObjects.Layer.GetDefaultLayerProperties()
125+
if prevId <> Guid.Empty then
126+
layer.ParentLayerId <- prevId
127+
if collapseParents then
128+
State.Doc.Layers.[prevIdx].IsExpanded <- false
129+
130+
match visible with
131+
|ByParent -> ()
132+
|On -> visibleSetTrue(layer, true)
133+
|Off -> visibleSetFalse(layer, true)
134+
135+
match locked with
136+
|ByParent -> ()
137+
|On -> lockedSetTrue(layer, true)
138+
|Off -> lockedSetFalse(layer, true)
139+
140+
layer.Name <- branch
141+
layer.Color <- colorForNewLayers() // delay creation of (random) color till actually needed ( so random colors are not created, in most cases layer exists)
142+
let i = State.Doc.Layers.Add(layer)
143+
let id = State.Doc.Layers.[i].Id // just using layer.Id would be empty guid
144+
createLayer(rest , id , i, fullPath)
145+
146+
| i ->
147+
createLayer(rest , State.Doc.Layers.[i].Id , i ,fullPath)
148+
149+
LayerCreated (createLayer( ns |> List.ofArray, Guid.Empty, 0, ""))
150+
)
148151
| i -> LayerFound i
149152

150153

151154
/// creates layer with default name
152155
let internal createDefaultLayer(color, visible, locked) =
153-
let layer = DocObjects.Layer.GetDefaultLayerProperties()
154-
layer.Color <- color() // delay creation of (random) color till actually needed ( so random colors are not created, in most cases layer exists)
155-
if layer.ParentLayerId <> Guid.Empty then RhinoScriptingException.Raise "RhinoScriptSyntax.createDefaultLayer how can a new default layer have a parent ? %A" layer
156-
layer.IsVisible <- visible
157-
layer.IsLocked <- locked
158-
State.Doc.Layers.Add(layer)
156+
RhinoSync.DoSync(fun () ->
157+
let layer = DocObjects.Layer.GetDefaultLayerProperties()
158+
layer.Color <- color() // delay creation of (random) color till actually needed ( so random colors are not created, in most cases layer exists)
159+
if layer.ParentLayerId <> Guid.Empty then RhinoScriptingException.Raise "RhinoScriptSyntax.createDefaultLayer how can a new default layer have a parent ? %A" layer
160+
layer.IsVisible <- visible
161+
layer.IsLocked <- locked
162+
State.Doc.Layers.Add(layer)
163+
)
164+
165+
159166

0 commit comments

Comments
 (0)