Skip to content

Commit 71aa490

Browse files
committed
nicer Error Messages when remembering objects
1 parent d753e2e commit 71aa490

File tree

2 files changed

+35
-20
lines changed

2 files changed

+35
-20
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
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.10.2] - 2025-03-25
11+
### Fixed
12+
- nicer Error Messages when remembering objects
13+
1014
## [0.10.1] - 2025-03-15
1115
### Changed
1216
- Referencing Rhino.Scripting 0.10.1

Src/Rhino.Scripting/Selection.fs

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -103,20 +103,26 @@ module AutoOpenSelection =
103103
[<OPT;DEF(0)>]maximumCount:int,
104104
[<OPT;DEF(true)>]printCount:bool,
105105
[<OPT;DEF(null:Input.Custom.GetObjectGeometryFilter)>]customFilter:Input.Custom.GetObjectGeometryFilter) : Guid ResizeArray =
106-
try
107-
let objectIds = rememberedObjects.[message]
108-
if printCount then // this print statement also raises an exception if object does not exist to trigger reselection
109-
// RhinoScriptSyntax.PrintfnBlue "GetObjectsAndRemember for '%s': %s" message ( RhinoScriptSyntax.ObjectDescription(objectIds)) // TODO
110-
RhinoScriptSyntax.Print $"GetObjectsAndRemember for '%s{message}': %d{objectIds.Count} objects"
111-
elif objectIds.Exists (System.Predicate ( fun g -> let o = RhinoScriptSyntax.Doc.Objects.FindId(g) in isNull o || o.IsDeleted )) then
112-
failwith "GetObjectsAndRemember" // to trigger reselection if object does not exist anymore
113-
objectIds
114-
with e ->
115-
//Printf.lightGray "%A" e
106+
let get() =
116107
let ids = RhinoScriptSyntax.GetObjects(message, filter, group, preselect, select, objects, minimumCount, maximumCount, printCount, customFilter)
117108
rememberedObjects.[message] <- ids
118109
ids
119110

111+
match rememberedObjects.TryGetValue message with
112+
| false, _ ->
113+
get()
114+
| true , objectIds ->
115+
if objectIds.Count = 0 then
116+
get()
117+
elif objectIds.Exists (System.Predicate ( fun g -> let o = RhinoScriptSyntax.Doc.Objects.FindId g in isNull o || o.IsDeleted )) then
118+
get()
119+
else
120+
if printCount then
121+
RhinoScriptSyntax.Print $"GetObjectsAndRemember for '%s{message}': {RhinoScriptSyntax.ObjectDescription(objectIds)}"
122+
objectIds
123+
124+
125+
120126

121127
///<summary>Returns the same object as in the last user interaction with the same prompt message
122128
/// If none found, Prompts user to pick one object and remembers it.
@@ -139,20 +145,25 @@ module AutoOpenSelection =
139145
[<OPT;DEF(false)>]select:bool,
140146
[<OPT;DEF(false)>]printDescr:bool,
141147
[<OPT;DEF(null:Input.Custom.GetObjectGeometryFilter)>]customFilter:Input.Custom.GetObjectGeometryFilter) : Guid =
142-
try
143-
let objectId = rememberedObjects.[message].[0] // this may raises an exception if the key does not exist, to trigger reselection
144-
if printDescr then
145-
// RhinoScriptSyntax.PrintfnBlue "GetObjectAndRemember for '%s': one %s" message ( RhinoScriptSyntax.ObjectDescription(objectId)) // this print statement also raises an exception if guid object does not exist, to trigger reselection
146-
RhinoScriptSyntax.Print $"GetObjectAndRemember for '%s{message}': one %s{ RhinoScriptSyntax.ObjectDescription(objectId)}"
147-
elif (let o = RhinoScriptSyntax.Doc.Objects.FindId(objectId) in isNull o || o.IsDeleted) then
148-
failwith "GetObjectAndRemember" // to trigger reselection if object does not exist anymore
149-
objectId
150-
with e ->
151-
// Printf.lightGray "%A" e
148+
let get() =
152149
let id = RhinoScriptSyntax.GetObject(message, filter, preselect, select, customFilter, subObjects=false)
153150
rememberedObjects.[message] <- ResizeArray [id]
154151
id
155152

153+
match rememberedObjects.TryGetValue message with
154+
| false, _ ->
155+
get()
156+
| true , objectIds ->
157+
if objectIds.Count <> 1 then
158+
get()
159+
elif (let o = RhinoScriptSyntax.Doc.Objects.FindId objectIds.[0] in isNull o || o.IsDeleted ) then
160+
get()
161+
else
162+
if printDescr then
163+
RhinoScriptSyntax.Print $"GetObjectAndRemember for '%s{message}': {RhinoScriptSyntax.ObjectDescription(objectIds.[0])}"
164+
objectIds.[0]
165+
166+
156167
///<summary>Clears all remembered objects form internal Dictionary that where added via rs.GetObjectAndRemember() or rs.GetObjectsAndRemember()</summary>
157168
static member ClearRememberedObjects() : unit =
158169
// RhinoScriptSyntax.PrintfGray "Cleared %d remembered Selection Sets" rememberedObjects.Count

0 commit comments

Comments
 (0)