Skip to content

Commit 8cfc5e2

Browse files
authored
Merge pull request #313 from FriendsOfCADability/claude/amazing-shannon-1nem7t
Improve clipboard data handling with better error handling
2 parents 7bd8f92 + 643115a commit 8cfc5e2

1 file changed

Lines changed: 32 additions & 12 deletions

File tree

CADability.Forms/CadFrame.cs

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,18 @@ GeoObjectList IUIService.GetDataPresent(object data)
135135
{
136136
if (data is IDataObject idata)
137137
{
138-
if (idata.GetDataPresent(System.Windows.Forms.DataFormats.Serializable))
138+
byte[] bytes = idata.GetData(typeof(byte[])) as byte[];
139+
if (bytes != null)
139140
{
140-
return idata.GetData(System.Windows.Forms.DataFormats.Serializable) as GeoObjectList;
141+
try
142+
{
143+
using (MemoryStream ms = new MemoryStream(bytes))
144+
{
145+
JsonSerialize js = new JsonSerialize();
146+
return js.FromStream(ms) as GeoObjectList;
147+
}
148+
}
149+
catch { }
141150
}
142151
}
143152
return null;
@@ -307,21 +316,32 @@ void IUIService.SetClipboardData(GeoObjectList objects, bool copy)
307316
}
308317
GeoObjectList IUIService.GetClipboardData()
309318
{
310-
GeoObjectList objects = null;
311-
IDataObject data = Clipboard.GetDataObject();
312-
byte[] bytes = data.GetData(typeof(byte[])) as byte[];
313-
if (bytes != null)
319+
try
314320
{
315-
MemoryStream ms = new MemoryStream(bytes);
316-
JsonSerialize js = new JsonSerialize();
317-
objects = js.FromStream(ms) as GeoObjectList;
318-
ms.Dispose();
321+
IDataObject data = Clipboard.GetDataObject();
322+
if (data == null) return null;
323+
byte[] bytes = data.GetData(typeof(byte[])) as byte[];
324+
if (bytes != null)
325+
{
326+
using (MemoryStream ms = new MemoryStream(bytes))
327+
{
328+
JsonSerialize js = new JsonSerialize();
329+
return js.FromStream(ms) as GeoObjectList;
330+
}
331+
}
319332
}
320-
return objects;
333+
catch { }
334+
return null;
321335
}
322336
bool IUIService.HasClipboardData()
323337
{
324-
return ((IUIService)this).GetClipboardData() != null;
338+
try
339+
{
340+
IDataObject data = Clipboard.GetDataObject();
341+
return data != null && data.GetDataPresent(typeof(byte[]));
342+
}
343+
catch { }
344+
return false;
325345
}
326346
event EventHandler IUIService.ApplicationIdle
327347
{

0 commit comments

Comments
 (0)