-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSample-eto-viewcapture.py
95 lines (76 loc) · 3.2 KB
/
Sample-eto-viewcapture.py
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
################################################################################
# SampleEtoViewCaptureDialog.py
# MIT License - Copyright (c) 2017 Robert McNeel & Associates.
# See License.md in the root of this repository for details.
################################################################################
import clr
clr.AddReference("Eto")
clr.AddReference("Rhino.UI")
from Rhino.UI import *
from Eto.Forms import *
from Eto.Drawing import *
import Rhino
import scriptcontext
import System
# Display a "view capture" in an Eto dialog box
def SampleEtoViewCaptureDialog():
# Capture button click handler
def OnCaptureButtonClick(sender, e):
# Capture the active view to a System.Drawing.Bitmap
view = scriptcontext.doc.Views.ActiveView
bitmap = view.CaptureToBitmap()
# Convert the System.Drawing.Bitmap to an Eto.Drawing.Bitmap
# which is required for an Eto image view control
stream = System.IO.MemoryStream()
format = System.Drawing.Imaging.ImageFormat.Png
System.Drawing.Bitmap.Save(bitmap, stream, format)
if stream.Length != 0:
image_view.Image = Bitmap(stream)
stream.Dispose()
# Update the text label
label.Text = 'Captured view: {}'.format(view.ActiveViewport.Name)
# Disable the default button
dialog.DefaultButton.Enabled = False
# Close button click handler
def OnCloseButtonClick(sender, e):
dialog.Close(True)
# Create an Eto dialog
dialog = Dialog[bool]()
dialog.Title = 'SampleEtoViewCaptureDialog'
dialog.Padding = Padding(10)
# Create a text label
label = Label(Text = 'Click the "Capture" button...')
# Create an image view
image_view = ImageView()
image_view.Size = Size(300, 200)
image_view.Image = None
# Create the default button
dialog.DefaultButton = Button(Text = 'Capture')
dialog.DefaultButton.Click += OnCaptureButtonClick
# Create the abort button
dialog.AbortButton = Button(Text = 'Close')
dialog.AbortButton.Click += OnCloseButtonClick
# Create a table layout for the buttons
button_layout = TableLayout()
button_layout.Spacing = Size(5, 5)
if Rhino.Runtime.HostUtils.RunningOnWindows:
button_layout.Rows.Add(TableRow(None, dialog.DefaultButton, dialog.AbortButton))
else:
button_layout.Rows.Add(TableRow(None, dialog.AbortButton, dialog.DefaultButton))
# Create a table layout and add all the controls
content_layout = TableLayout()
content_layout.Padding = Padding(10)
content_layout.Spacing = Size(5, 5)
content_layout.Rows.Add(label)
content_layout.Rows.Add(image_view)
content_layout.Rows.Add(None) # spacer
content_layout.Rows.Add(button_layout)
# Set the dialog content
dialog.Content = content_layout
# Display the dialog box in a modal fashion
dialog.ShowModal(RhinoEtoApp.MainWindow)
# Check to see if this file is being executed as the "main" python
# script instead of being used as a module by some other python script
# This allows us to use the module which ever way we want.
if __name__ == "__main__":
SampleEtoViewCaptureDialog()