-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewport.cs
476 lines (417 loc) · 18.3 KB
/
Viewport.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using Rhino;
using Rhino.DocObjects;
using Rhino.Geometry;
using Rhino.Geometry.Collections;
namespace SolidUtils
{
public enum SFLineType
{
Default,
Hidden, Dashed, DashDot, Center, Border, Dots, // Rhino default line types
myDash, myDashSmall,
}
public static class Viewport
{
public static bool DEBUG = false;
#region Redraw
[DllImport("user32")]
public static extern int SendMessage(IntPtr hWnd, Int32 wMsg, bool wParam, Int32 lParam);
private const int WM_SETREDRAW = 11;
internal static void RedrawWhenPossiblePlease()
{
Interlocked.Increment(ref Redraw_WhenRedrawSuppresed_CallsCount);
}
internal static long Redraw_WhenRedrawSuppresed_CallsCount;
private static long RedrawSuppressor_StackCount;
private static bool Redraw_IsSuppresed
{
get { return Interlocked.Read(ref RedrawSuppressor_StackCount) > 0; }
}
public class RedrawSuppressorRhinoMainWindow : IDisposable
{
private static long StackCount;
public RedrawSuppressorRhinoMainWindow()
{
Interlocked.Increment(ref StackCount);
if (Interlocked.Read(ref StackCount) == 1)
{
SendMessage(RhinoApp.MainApplicationWindow.Handle, WM_SETREDRAW, false, 0);
}
}
public void Dispose()
{
Interlocked.Decrement(ref StackCount);
if (Interlocked.Read(ref StackCount) == 0)
{
SendMessage(RhinoApp.MainApplicationWindow.Handle, WM_SETREDRAW, true, 0);
}
}
}
public class RedrawSuppressor : IDisposable
{
private readonly string operationName;
private readonly bool redrawAnyway;
private bool? saveRedrawEnabled;
public RedrawSuppressor(RhinoDoc doc, string operationName, bool clearHighlights, bool clearSelections) //, bool callRedrawAfterIfCallsWasMade = true
{
this.operationName = operationName;
this.redrawAnyway = redrawAnyway;
Interlocked.Increment(ref RedrawSuppressor_StackCount);
if (Interlocked.Read(ref RedrawSuppressor_StackCount) == 1)
{
if (Viewport.DEBUG)
{
log.temp("!!! REDRAW: RedrawSuppressor+ " + operationName);
}
Redraw_WhenRedrawSuppresed_CallsCount = 0;
if (doc == null)
{
doc = RhinoDoc.ActiveDoc;
}
if (doc != null)
{
saveRedrawEnabled = doc.Views.RedrawEnabled;
doc.Views.RedrawEnabled = false;
if (doc.Views.ActiveView != null)
{
//IDEA: show text in view port what is action in progress
}
if (clearSelections)
{
doc.Objects.UnselectAll();
}
if (clearHighlights)
{
Layers.Debug.Clear();
Layers.HighlightLayer.Clear();
}
}
}
}
public void Dispose()
{
Interlocked.Decrement(ref RedrawSuppressor_StackCount);
if (Interlocked.Read(ref RedrawSuppressor_StackCount) == 0)
{
if (Viewport.DEBUG)
{
log.temp("!!! REDRAW: RedrawSuppressor- " + operationName);
}
var doc = RhinoDoc.ActiveDoc;
if (doc != null)
{
if (saveRedrawEnabled.HasValue)
{
doc.Views.RedrawEnabled = saveRedrawEnabled.Value;
saveRedrawEnabled = null;
}
if (redrawAnyway || Redraw_WhenRedrawSuppresed_CallsCount > 0)
{
var saveRedrawViewPort_WhenRedrawSuppresed_CallsCount = Redraw_WhenRedrawSuppresed_CallsCount;
Redraw_WhenRedrawSuppresed_CallsCount = 0;
Redraw(doc, "RedrawSuppressor.Dispose: {0} (calls to redraw {1})"._Format(operationName, saveRedrawViewPort_WhenRedrawSuppresed_CallsCount));
}
}
// we have to clear flag because inside of RedrawSuppressor we select and unselect objects and add highlights for them
// so we want selection and highlight to be persist after we finish our redraw
RhinoDocSafeEvents.OnIdleClearHighlightLayer = false;
}
}
}
/// <summary>
/// Call as mutch times as you want.
/// To get maximum performance - all calls to this method should be wrapped in clause 'using(new RedrawSuppressor()){...}'
/// </summary>
/// <param name="doc"></param>
/// <param name="operationName"></param>
/// <param name="redrawEvenIfRedrawIsSuppressed"></param>
public static void Redraw(RhinoDoc doc, string operationName, bool redrawEvenIfRedrawIsSuppressed = false)
{
if (Shared.FILEGROUPOPERATIONS_CONVERTINGFILES) return;
if (doc == null) return;
//doc.Views.ActiveView.MainViewport.WorldAxesVisible = false;
if ((!Redraw_IsSuppresed) || redrawEvenIfRedrawIsSuppressed)
{
var saveRedrawEnabled = doc.Views.RedrawEnabled;
//var Watch = Stopwatch.StartNew();
//Watch.Start();
if (doc.Views.RedrawEnabled == false)
{
//log.file("RedrawViewPort - setting RedrawEnabled to true");
doc.Views.RedrawEnabled = true;
}
try
{
// here speed optimization - redraw only current view - all other views will be synchronized shomehow
// additional condition 'doc.Views.ActiveView.Maximized' doesnt required - anyway views will be synchronized
// speed increased from 250ms to 15ms - why so huge difference i dont know (probably there are some repaint issues in Rhino)
if (doc.Views.ActiveView != null) // && doc.Views.ActiveView.Maximized
{
doc.Views.ActiveView.Redraw(); // 5-10 times faster from full redraw
}
else
{
doc.Views.Redraw(); // full redraw - slow
}
if (Viewport.DEBUG)
{
log.temp("!!! REDRAW: RedrawViewPort - " + operationName);
//Watch.Stop();
//TimeSpan ts = Watch.Elapsed;
//string elapsedTime = String.Format("{0:0.000}", ts.TotalMilliseconds);
//log.temp("!!! REDRAW: time taken: " + elapsedTime);
}
//log.temp("!!! REDRAW: RedrawViewPort- " + operationName);
//log.temp("!!!!!! RedrawViewPort");
}
finally
{
if (doc.Views.RedrawEnabled != saveRedrawEnabled) doc.Views.RedrawEnabled = saveRedrawEnabled;
}
}
else if (Redraw_IsSuppresed)
{
RedrawWhenPossiblePlease();
}
}
#endregion
public static class LineTypes
{
public static int GetLineTypeIndex(RhinoDoc doc, SFLineType type)
{
Linetype line = null;
switch (type)
{
case SFLineType.Default:
return -1;
case SFLineType.Hidden:
case SFLineType.Dashed:
case SFLineType.DashDot:
case SFLineType.Center:
case SFLineType.Border:
case SFLineType.Dots:
line = doc.Linetypes.FirstOrDefault(o => o.Name == type.ToString());
break;
case SFLineType.myDash:
case SFLineType.myDashSmall:
var myTypeName = "SF_LineType_" + type.ToString();
var l = doc.Linetypes.FirstOrDefault(o => o.Name == myTypeName);
if (l == null)
{
var lineType = new Linetype();
lineType.Name = myTypeName;
switch (type)
{
case SFLineType.myDash:
lineType.AppendSegment(0.1, true);
lineType.AppendSegment(0.05, false);
break;
case SFLineType.myDashSmall:
lineType.AppendSegment(0.05, true);
lineType.AppendSegment(0.05, false);
break;
}
var index = doc.Linetypes.Add(lineType);
line = doc.Linetypes[index];
}
else
{
line = doc.Linetypes[l.LinetypeIndex];
}
break;
}
// return our line index
if (line != null)
{
return line.LinetypeIndex;
}
// default line index - continuous line
return -1;
}
}
public static void Select(RhinoDoc doc, IEnumerable<RhinoObject> objects, double fitFactor = 3, bool resetCameraPosition = false)
{
foreach (var o in objects)
{
o.Select(true);
}
Zoom(doc, objects, fitFactor, resetCameraPosition);
}
public static void Zoom(RhinoDoc doc, IEnumerable<RhinoObject> objects, double fitFactor = 3, bool resetCameraPosition = false)
{
Zoom(doc, objects.Select(o => o.Geometry), fitFactor, resetCameraPosition);
}
public static void Zoom(RhinoDoc doc, IEnumerable<GeometryBase> objects, double fitFactor = 3, bool resetCameraPosition = false)
{
Zoom(doc, objects.Select(o => o.GetBoundingBox(true)), fitFactor, resetCameraPosition);
}
public static void Zoom(RhinoDoc doc, IEnumerable<BoundingBox> boxes, double fitFactor = 3, bool resetCameraPosition = false)
{
BoundingBox box = BoundingBox.Empty;
bool isBoxEmpty = true;
foreach (var b in boxes)
{
if (isBoxEmpty)
{
box = b;
isBoxEmpty = false;
}
else
{
box.Union(b);
}
}
if (!isBoxEmpty)
{
Zoom(doc, box, fitFactor, resetCameraPosition);
}
}
public static void Zoom(RhinoDoc doc, BoundingBox box, double fitFactor = 3, bool resetCameraPosition = false)
{
if (doc.Views.ActiveView == null) return;
if (!box.IsValid) return;
bool needRedraw = false;
if (resetCameraPosition)
{
ResetCameraPosition(doc, box, false);
needRedraw = true;
}
// if (Shared.FILEBROWSER_FILEGROUPOPERATIONS_ENABLED) return;
//var view = doc.Views.ActiveView;
//if (view != null)
//{
// var xMiddle = view.ScreenRectangle.Width/2;
// var yMiddle = view.ScreenRectangle.Height/2;
// var screenMiddle = new Point3d(xMiddle, yMiddle, 0);
// //var pointMiddle = view.ScreenToClient(new Point2d(xMiddle, yMiddle));
// Transform screen_to_world = view.ActiveViewport.GetTransform(CoordinateSystem.Screen,
// CoordinateSystem.World);
// screenMiddle.Transform(screen_to_world);
// Transform world_to_camera = view.ActiveViewport.GetTransform(CoordinateSystem.World,
// CoordinateSystem.Camera);
// doc.Objects.AddBrep(box.ToBrep());
// //l.Transform(world_to_camera);
// box.GetCorners();
//}
//return;
if (!fitFactor._IsSame(0))
{
//const double pad = 0.05; // A little padding...
//double dx = (box.Max.X - box.Min.X) * pad;
//double dy = (box.Max.Y - box.Min.Y) * pad;
//double dz = (box.Max.Z - box.Min.Z) * pad;
fitFactor = fitFactor * 0.1;
double dx = Math.Abs(box.Max.X - box.Min.X) * fitFactor;
double dy = Math.Abs(box.Max.Y - box.Min.Y) * fitFactor;
double dz = Math.Abs(box.Max.Z - box.Min.Z) * fitFactor;
box.Inflate(dx, dy, dz);
doc.Views.ActiveView.ActiveViewport.ZoomBoundingBox(box);
needRedraw = true;
}
if (needRedraw)
{
Redraw(doc, "Viewport.Zoom");
}
}
public static void ResetCameraPosition(RhinoDoc doc, BoundingBox box, bool redraw = true)
{
if (doc.Views.ActiveView == null) return;
doc.Views.ActiveView.ActiveViewport.SetCameraLocation(Point3d.Origin, true);
doc.Views.ActiveView.ActiveViewport.SetCameraDirection(Point3d.Origin - box.Center, true);
if (redraw)
{
Redraw(doc, "Viewport.ResetCameraPosition");
}
}
public static void SetCameraPosition(RhinoDoc doc, Vector3d direction, bool redraw = true)
{
if (doc.Views.ActiveView == null) return;
if (doc.Views.ActiveView.ActiveViewport == null) return;
doc.Views.ActiveView.ActiveViewport.SetCameraDirection(direction, true);
if (redraw)
{
Redraw(doc, "Viewport.SetCameraPosition");
}
}
public static void SetCameraPositionIfDifferent(RhinoDoc doc, Vector3d direction, double maxDifferenceInAngle, bool redraw = true)
{
if (doc.Views.ActiveView == null) return;
if (doc.Views.ActiveView.ActiveViewport == null) return;
var v = doc.Views.ActiveView.ActiveViewport;
var angle = v.CameraDirection._AngleInRadians(direction)._RadianToDegree();
if (angle < maxDifferenceInAngle)
{
return;
}
v.SetCameraDirection(direction, true);
if (redraw)
{
Redraw(doc, "Viewport.SetCameraPosition");
}
}
public static void ZoomAll(RhinoDoc doc, double fitFactor = 3, bool resetCameraPosition = false)
{
if (doc.Views.ActiveView == null) return;
Zoom(doc, doc.Objects, fitFactor, resetCameraPosition);
}
public static void Zoom100(RhinoDoc doc)
{
if (doc.Views.ActiveView == null) return;
//foreach (var v in doc.Views)
//{
// v.ActiveViewport.ZoomExtents();
//}
doc.Views.ActiveView.ActiveViewport.ZoomExtents();
Redraw(doc, "Viewport.Zoom100");
}
public static void UnselectAll()
{
var doc = RhinoDoc.ActiveDoc;
if (doc != null)
{
doc.Objects.UnselectAll();
}
}
public static void CreateRenderMeshes(bool showProgress, List<RhinoObject> objs = null, RhinoDoc doc = null)
{
if (doc == null)
{
doc = RhinoDoc.ActiveDoc;
}
if (doc == null) return;
CreateRenderMeshes(showProgress, doc.MeshingParameterStyle, objs, doc);
}
public static void CreateRenderMeshes(bool showProgress, MeshingParameterStyle meshingStyle, List<RhinoObject> objs = null, RhinoDoc doc = null)
{
if (doc == null)
{
doc = RhinoDoc.ActiveDoc;
}
if (doc == null) return;
if (objs == null)
{
objs = doc.Objects.Where(o => o.GetMeshes(MeshType.Render).Length == 0).ToList();
}
else
{
objs = objs.Where(o => o.GetMeshes(MeshType.Render).Length == 0).ToList();
}
if (objs.Count == 0) return;
using (var meshingParametersU = doc.GetMeshingParameters(meshingStyle))
{
var meshingParameters = meshingParametersU;
var caption = String.Format("Creating meshes for {0} objects ...", objs.Count);
objs._ForeachParallel_WithOrWithoutProgressWindow(showProgress, caption, obj => obj.CreateMeshes(MeshType.Render, meshingParameters, false));
}
}
}
}