Preview of geometry in UI

Hello guys,

how can you create a preview image a geometry in UI? There is a code here in C# but I can’t import System.Drawing into Python.

Any ideas?

My goal is: Selecting type of geometry → Rectengular or circle and then after the user enters dimensions, the geometry should be updated. The part of UI I can do if I would know how to create a preview of geometry.

Looks like @Mark.Ackerley was able to reference it here:

1 Like

Hello Sean,

thank you very much for reply. Creating UI is not the problem. The problem is how to create a preview image of geometry. I would use WPF to make it dynamic. So if user enter a dimension the geometry should be updated.

Hi
an simple example with winform

dynamicPicture

import clr
clr.AddReference('System.Windows.Forms')
clr.AddReference('System.Drawing')
import System.Drawing
import System.Windows.Forms

from System.Drawing import *
from System.Windows.Forms import *

class MainForm(Form):
	def __init__(self):
		self._Width = 100
		self._Height = 100
		self._Diameter = 50
		self.InitializeComponent()
		self.Paint += self.OnPaint
	
	def InitializeComponent(self):
		self._numericUpDownHeight = System.Windows.Forms.NumericUpDown()
		self._numericUpDownWidth = System.Windows.Forms.NumericUpDown()
		self._numericUpDownArc = System.Windows.Forms.NumericUpDown()
		self._numericUpDownHeight.BeginInit()
		self._numericUpDownWidth.BeginInit()
		self._numericUpDownArc.BeginInit()
		self.SuspendLayout()
		# 
		# numericUpDownHeight
		# 
		self._numericUpDownHeight.Increment = System.Decimal(System.Array[System.Int32]([10, 0, 0, 0]))
		self._numericUpDownHeight.Location = System.Drawing.Point(48, 199)
		self._numericUpDownHeight.Maximum = System.Decimal(System.Array[System.Int32]([200, 0, 0, 0]))
		self._numericUpDownHeight.Name = "numericUpDownHeight"
		self._numericUpDownHeight.Size = System.Drawing.Size(120, 20)
		self._numericUpDownHeight.TabIndex = 1
		self._numericUpDownHeight.Value = System.Decimal(System.Array[System.Int32]([self._Height, 0, 0, 0]))
		self._numericUpDownHeight.ValueChanged += self.NumericUpDownHeightValueChanged
		# 
		# numericUpDownWidth
		# 
		self._numericUpDownWidth.Increment = System.Decimal(System.Array[System.Int32]([10, 0, 0, 0]))
		self._numericUpDownWidth.Location = System.Drawing.Point(48, 151)
		self._numericUpDownWidth.Maximum = System.Decimal(System.Array[System.Int32]([200, 0, 0, 0]))
		self._numericUpDownWidth.Name = "numericUpDownWidth"
		self._numericUpDownWidth.Size = System.Drawing.Size(120, 20)
		self._numericUpDownWidth.TabIndex = 2
		self._numericUpDownWidth.Value = System.Decimal(System.Array[System.Int32]([self._Width, 0, 0, 0]))
		self._numericUpDownWidth.ValueChanged += self.NumericUpDownWidthValueChanged
		# 
		# numericUpDownArc
		# 
		self._numericUpDownArc.Increment = System.Decimal(System.Array[System.Int32]([10, 0, 0, 0]))
		self._numericUpDownArc.Location = System.Drawing.Point(48, 48)
		self._numericUpDownArc.Maximum = System.Decimal(System.Array[System.Int32]([200, 0, 0, 0]))
		self._numericUpDownArc.Name = "numericUpDownArc"
		self._numericUpDownArc.Size = System.Drawing.Size(120, 20)
		self._numericUpDownArc.TabIndex = 2
		self._numericUpDownArc.Value = System.Decimal(System.Array[System.Int32]([self._Diameter, 0, 0, 0]))
		self._numericUpDownArc.ValueChanged += self.NumericUpDownArcValueChanged
		# 
		# MainForm
		# 
		self.ClientSize = System.Drawing.Size(466, 300)
		self.Controls.Add(self._numericUpDownArc)
		self.Controls.Add(self._numericUpDownWidth)
		self.Controls.Add(self._numericUpDownHeight)
		self.Name = "MainForm"
		self.Text = "dynamicDraw"
		self._numericUpDownHeight.EndInit()
		self._numericUpDownWidth.EndInit()
		self._numericUpDownArc.EndInit()
		self.ResumeLayout(False)
		
		
	def OnPaint(self, event):
		g = event.Graphics
		pen = Pen(Color.Gray, 1)
		g.FillRectangle(Brushes.Gray, 200, 140, self._Width , self._Height)
		g.DrawArc(pen, 200, 40, self._Diameter , self._Diameter, 0, 360)

		

	def NumericUpDownWidthValueChanged(self, sender, e):
		self._Width = sender.Value
		self.Refresh()


	def NumericUpDownHeightValueChanged(self, sender, e):
		self._Height = sender.Value
		self.Refresh()
		
	def NumericUpDownArcValueChanged(self, sender, e):
		self._Diameter = sender.Value
		self.Refresh()
		
objform = MainForm()
objform.ShowDialog()
4 Likes

Thank you very much Cyril! Now time to combine it with things that I planed :slight_smile:

1 Like