Hi, is it possible to send a transaction via winform
My current code doesn’t work, I don’t know if this is possible I think I’m doing it wrong with the definition of the function and the sending in the transaction
script:
import clr
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import View,BuiltInParameter,Element
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
import System.Drawing
import System.Windows.Forms
from System.Drawing import *
from System.Windows.Forms import *
class MainForm(Form):
def __init__(self):
self.InitializeComponent()
def InitializeComponent(self):
self._label1 = System.Windows.Forms.Label()
self._numericUpDown1 = System.Windows.Forms.NumericUpDown()
self._numericUpDown1.BeginInit()
self.SuspendLayout()
#
# label1
#
self._label1.Font = System.Drawing.Font("Microsoft Sans Serif", 12, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 0)
self._label1.Location = System.Drawing.Point(12, 30)
self._label1.Name = "label1"
self._label1.Size = System.Drawing.Size(100, 23)
self._label1.TabIndex = 0
self._label1.Text = "Phase :"
self._label1.Click += self.Label1Click
#
# numericUpDown1
#
self._numericUpDown1.Font = System.Drawing.Font("Microsoft Sans Serif", 15.75, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0)
self._numericUpDown1.Location = System.Drawing.Point(105, 24)
self._numericUpDown1.Name = "numericUpDown1"
self._numericUpDown1.Size = System.Drawing.Size(62, 31)
self._numericUpDown1.TabIndex = 1
self._numericUpDown1.Click += self.calcul
#
# MainForm
#
self.ClientSize = System.Drawing.Size(217, 97)
self.Controls.Add(self._numericUpDown1)
self.Controls.Add(self._label1)
self.Name = "MainForm"
self.Text = "visualiser avancement"
self._numericUpDown1.EndInit()
self.ResumeLayout(False)
def Label1Click(self, sender, e):
pass
def calcul(self, sender, e):
a=int(self._numericUpDown1.Value+1)
ab=MainForm()
ab.ShowDialog()
doc = DocumentManager.Instance.CurrentDBDocument
phase = UnwrapElement(IN[0])
phasimp=calcul()
vuecourante=doc.ActiveView
TransactionManager.Instance.EnsureInTransaction(doc)# Start Transaction
vuecourante.get_Parameter(BuiltInParameter.VIEW_PHASE).Set(phase[phasimp].Id)
TransactionManager.Instance.TransactionTaskDone()# End Transaction
OUT = 0
Sincerely
christian.stan
You are very lucky, most of the times you don´t even get errors for that, just strange behavior. And you also got the line, that´s special.
Go to the options and toggle show whitespace:
Or look at the code in notepad.
What do you see?
In notepad you have different options to replace all tabs with spaces, or make 4 spaces instead of tabs in general. Would be great to have that in dynamo. It´s a pain if you copy/paste a lot of code…
If you ever get indention errors and can´t find the problem a beautifier can help.
2 Likes
Merci pour la correction de l’indendation
j’ai un problème d’opérande décimal et int
cordialement
christian.stan
You were just misplacing the bracket
def calcul(self, sender, e):
a = int(self._numericUpDown1.Value) + 1
Does not work,
I’m wasting your time
I need to understand more about winform and sending events
(it’s still too unclear, I’m skipping too many steps)
Sincerely
christian.stan
Try to first convert to float or string, then take this result and convert to integer.
Int(float()) +1
I no longer have the decimal int message with:
def calcul(self, sender, e):
a=int(float(str(self._numericUpDown1.Value)))+1
ab=MainForm()
ab.ShowDialog()
doc = DocumentManager.Instance.CurrentDBDocument
phase = UnwrapElement(IN[0])
phasimp=calcul()
But no event at the end
I think I’ll stick with a slider in auto mode on dynamo, the winform too complicated for me still…
good evening thank you for the time given
Sincerely
christian.stan
here a simple winform with numericupdown:
import clr
import System.Windows.Forms as WinForms
from System.Drawing import Size
import System
clr.AddReference('RevitAPI')
clr.AddReference('RevitAPIUI')
from Autodesk.Revit.DB import *
def create_form():
form = WinForms.Form()
form.Text = "Numeric Input"
form.Size = Size(250, 150)
numeric_up_down = WinForms.NumericUpDown()
numeric_up_down.Location = System.Drawing.Point(50, 50)
numeric_up_down.Size = System.Drawing.Size(120, 20)
numeric_up_down.Minimum = System.Decimal(0)
numeric_up_down.Maximum = System.Decimal(100)
numeric_up_down.Value = System.Decimal(0)
form.Controls.Add(numeric_up_down)
form.ShowDialog()
# Convert System.Decimal to string, then to float
return float(str(numeric_up_down.Value)) + 1
numeric_value = create_form()
OUT = numeric_value
Edit:
Also works with integer
return int(str(numeric_up_down.Value)) + 1
2 Likes
hi, thank you very much for the advice and for the example, I corrected it accordingly.
I can’t get the dynamic side of things, but hey, it’s still progress, the transaction has been sent well.
code python
import clr
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import View,BuiltInParameter,Element
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
import System
import System.Drawing
import System.Windows.Forms
from System.Drawing import *
from System.Windows.Forms import *
doc = DocumentManager.Instance.CurrentDBDocument
phase = UnwrapElement(IN[0])
vuecourante=doc.ActiveView
class MainForm(Form):
def __init__(self):
self.InitializeComponent()
def InitializeComponent(self):
self._label1 = System.Windows.Forms.Label()
self._numericUpDown1 = System.Windows.Forms.NumericUpDown()
self._numericUpDown1.BeginInit()
self.SuspendLayout()
#
# label1
#
self._label1.Font = System.Drawing.Font("Microsoft Sans Serif", 12, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 0)
self._label1.Location = System.Drawing.Point(12, 30)
self._label1.Name = "label1"
self._label1.Size = System.Drawing.Size(100, 23)
self._label1.TabIndex = 0
self._label1.Text = "Phase :"
self._label1.Click += self.Label1Click
#
# numericUpDown1
#
self._numericUpDown1.Font = System.Drawing.Font("Microsoft Sans Serif", 15.75, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0)
self._numericUpDown1.Location = System.Drawing.Point(105, 24)
self._numericUpDown1.Name = "numericUpDown1"
self._numericUpDown1.Size = System.Drawing.Size(62, 31)
self._numericUpDown1.TabIndex = 1
self._numericUpDown1.Click += self.calcul
#
# MainForm
#
self.ClientSize = System.Drawing.Size(217, 97)
self.Controls.Add(self._numericUpDown1)
self.Controls.Add(self._label1)
self.Name = "MainForm"
self.Text = "visualiser avancement"
self._numericUpDown1.EndInit()
self.ResumeLayout(False)
def Label1Click(self, sender, e):
pass
def calcul(self, sender, e):
phasimp=int(float(str(self._numericUpDown1.Value)))+1
TransactionManager.Instance.EnsureInTransaction(doc)# Start Transaction
vuecourante.get_Parameter(BuiltInParameter.VIEW_PHASE).Set(phase[phasimp].Id)
TransactionManager.Instance.TransactionTaskDone()# End Transaction
abc=MainForm()
abc.ShowDialog()
OUT = 0
Sincerely
christian.stan
Hi @christian.stan
ShowDialog()
block the current thread
with Show()
method, you can try to implement an External Event Handler (to date, I recommend IronPython2 or IronPython3 in this case)
some examples here
https://pythoncvc.net/?p=247
2 Likes
hi, thank you for the feedback, I didn’t have the usual reflex to scan the blog, I’m going to try to understand how to set this up (thank you very much for the lead)
I’m going to try this over the Christmas holidays.
I would return at this time.
Sincerely
christian.stan
1 Like