Revit Dynamo View Form Python Output Form Resizable, Aggressively on top
import clr
import sys
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")
from System.Drawing import Size, Font, FontStyle, GraphicsUnit
from System.Windows.Forms import (
Form, TextBox, FormBorderStyle, FormStartPosition,
ScrollBars, DockStyle
)
# join text list
text = "\r\n".join(IN[0])
# new defaults
DEF_W = 450
DEF_H = 800
MIN_W = 100
MIN_H = 100
DEF_TITLE = "Dynamo Viewer"
def get_int_input(idx, default_val, min_val):
try:
v = IN[idx]
except:
return default_val
if v is None:
return default_val
try:
v = int(v)
except:
return default_val
if v < min_val:
return default_val
return v
# width and height with validation/fallback
fWide = get_int_input(1, DEF_W, MIN_W)
fHigh = get_int_input(2, DEF_H, MIN_H)
# title fallback
try:
raw_title = IN[3]
except:
raw_title = None
if raw_title is None or str(raw_title).strip() == "":
title = DEF_TITLE
else:
title = str(raw_title)
# keep a global list to avoid GC
try:
_forms
except NameError:
_forms = []
class popup(Form):
def __init__(self, text):
self.InitializeComponent(text)
def InitializeComponent(self, text):
self.ClientSize = Size(fWide, fHigh)
self.Text = title
self.FormBorderStyle = FormBorderStyle.Sizable
self.MaximizeBox = True
self.MinimizeBox = True
self.StartPosition = FormStartPosition.CenterScreen
self.TopMost = True
self.textbox = TextBox()
self.textbox.Parent = self
self.textbox.Multiline = True
self.textbox.ReadOnly = True
self.textbox.ScrollBars = ScrollBars.Vertical
self.textbox.WordWrap = True
self.textbox.Dock = DockStyle.Fill
self.textbox.Font = Font("Tahoma", 8, FontStyle.Regular, GraphicsUnit.Point)
self.textbox.Text = text
def OnShown(self, e):
Form.OnShown(self, e)
self.TopMost = True
self.Activate()
self.BringToFront()
# create modeless always-on-top form
form = popup(text)
_forms.append(form)
form.Show()
form.Activate()
form.BringToFront()
# return original text
OUT = IN[0]
Comments
Post a Comment