ListView Data Winform

Hi all,

I try to write a script that give me a UI with a checkbox.
But the input is a list, how can I do it inside a class / def?

import clr #.NET Laden
import sys #sys is de fundamentele Python bibliotheek
#de standaard IronPython-bibliotheken
sys.path.append(r'H:\BIM-REVIT\00 GEBRUIKERSGROEP BIM\00 BRONBESTANDEN\Python\defs') #Imports the
#standaard IronPython-bibliotheken, die alles dekken, van servers en
#encryptie tot reguliere expressies.
import functies as fc
import System #The System namespace in de hoofdmap .NET
from System import Array #.NET class voor het verwerken van array-informatie
import System.Collections.Generic as MGen #Module kan nu benaderd worden met MGen.xxxx
#from System.Collections.Generic import * #Hiermee kunt u generieke afhandelen. Revit's API
#soms wil hard-getypte 'generieke' lijsten, genaamd ILists. Als je niet nodig hebt
#deze kunt u deze regel verwijderen.
clr.AddReference('ProtoGeometry')  #Een Dynamo-bibliotheek voor zijn proxygeometrie
#classes. Je hebt dit alleen nodig als je interactie hebt met geometrie.
import Autodesk.DesignScript.Geometry as AGeo #Module kan worden opgeroepen a=met AGeo.xxxx
#from Autodesk.DesignScript.Geometry import * #Laadt alles in Dynamo's
#geometriebibliotheek
clr.AddReference("RevitNodes") #Dynamo's nodes voor Revit
import Revit #Laad in de Revit-namespaces in RevitNodes
clr.ImportExtensions(Revit.Elements) #Meer laden van Dynamo's Revit-bibliotheken
clr.ImportExtensions(Revit.GeometryConversion) #Meer laden van Dynamo's
#Revit-bibliotheken. Je hebt dit alleen nodig als je interactie hebt met geometrie.
clr.AddReference("RevitServices") #Dynamo's classes voor het omgaan met Revit-documenten
import RevitServices 
from RevitServices.Persistence import DocumentManager #Een interne Dynamo class
#dat het document bijhoudt waaraan Dynamo momenteel is gekoppeld
from RevitServices.Transactions import TransactionManager #Een Dynamo class voor
#transacties openen en sluiten om de database van het Revit-document te wijzigen

clr.AddReference("RevitAPI") #Verwijzing naar Revit's API DLL's toevoegen
clr.AddReference("RevitAPIUI") #Verwijzing naar Revit's APIUI DLL's toevoegen

import Autodesk #Loads the Autodesk namespace
import Autodesk.Revit.DB as RDB #Loading Revit's API UI classes module kan nu worden aangeroepen met RDB.xxxx
#from Autodesk.Revit.DB import * #Loading Revit's API UI classes
import Autodesk.Revit.UI as RUI # Loading Revit's API UI classes als RUI.xxxx
#from Autodesk.Revit.UI import * #Loading Revit's API UI classes

doc = DocumentManager.Instance.CurrentDBDocument #Dit is het actieve Revit document
uiapp = DocumentManager.Instance.CurrentUIApplication #Een handle instellen voor het actieve Revit UI-document
app = uiapp.Application  #Een handle instellen op de momenteel geopende instantie van de Revit-toepassing
uidoc = uiapp.ActiveUIDocument #Een handle instellen op de momenteel geopende instantie van de Revit UI-toepassing
revit_version = int(doc.Application.VersionNumber)
# code omrekenen revit feet naar huidig ingestele document units
if revit_version >= 2022:
    ForgeLength = "autodesk.spec.aec:length-1.0.0"  # zie voor lijst ForgeTypeIds.xlsx in de map handleidingen
    ForgeTypeLength = RDB.ForgeTypeId(ForgeLength)
    getDocUnits = doc.GetUnits()
    getDisplayUnitsLength = getDocUnits.GetFormatOptions(ForgeTypeLength).GetUnitTypeId()
else:
    getDocUnits = doc.GetUnits()
    getDisplayUnits = getDocUnits.GetFormatOptions(RDB.UnitType.UT_Length).DisplayUnits
    
def uwlist(input):
    result = input if isinstance(input, list) else [input]
    return UnwrapElement(result)

# einde code omrekenen revit feet naar huidig ingestelde document units
# Hieronder kan je dan gaan programmeren
# Gebruik boiler template


panelsch = IN[0]

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 Form1(Form):
	def __init__(self):
		self.out = []
		self.InitializeComponent()
	
	def InitializeComponent(self):
		self._Voltooien = System.Windows.Forms.Button()
		self._checkBox1 = System.Windows.Forms.CheckBox()
		self.SuspendLayout()
		# 
		# Voltooien
		# 
		self._Voltooien.Location = System.Drawing.Point(173, 569)
		self._Voltooien.Name = "Voltooien"
		self._Voltooien.Size = System.Drawing.Size(154, 23)
		self._Voltooien.TabIndex = 0
		self._Voltooien.Text = "Voltooien"
		self._Voltooien.UseVisualStyleBackColor = True
		self._Voltooien.UseWaitCursor = True
		self._Voltooien.Click += self.Button1Click
		# 
		# checkBox1
		#
		s = 0
		sc = 4
		for p in panelsch:		
			self._checkBox1.Location = System.Drawing.Point(100, sc)
			self._checkBox1.Text = p[s]
			sc += 25
			s += 1
			
		self._checkBox1.Name = "checkBox1"
		self._checkBox1.Size = System.Drawing.Size(227, 559)
		self._checkBox1.TabIndex = 1
		self._checkBox1.UseVisualStyleBackColor = True
		# 
		# Form1
		# 
		self.AcceptButton = self._Voltooien
		self.ClientSize = System.Drawing.Size(489, 604)
		self.Controls.Add(self._checkBox1)
		self.Controls.Add(self._Voltooien)
		self.Name = "Form1"
		self.ShowIcon = False
		self.Text = "Form1"
		self.TopMost = True
		self.ResumeLayout(False)


	def Button1Click(self, sender, e):
		self.out.append(self._checkBox1.Text)
		self.Close()

#Start Transaction
#TransactionManager.Instance.EnsureInTransaction(doc)
#Eind Transactie
#TransactionManager.Instance.TransactionTaskDone()

form = Form1()
Application.Run(form)

OUT = form.out
1 Like

I tried to check the datashape package, but it is to difficult for me :sweat_smile:

You need to construct a list of CheckBoxes then add them to the form.
When returning results for what has been checked, you need to iterate through the created list.

4 Likes

Thanks @Ewan_Opie , do you know how to create the select all / select none option?

Hi,
an example with a CheckListBox

example select all UI

import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

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

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

class Form24(Form):
	def __init__(self, lstSchedule):
		self._lstSchedule = lstSchedule
		self.out = []
		self.InitializeComponent()
	
	def InitializeComponent(self):
		self._checkedListBox1 = System.Windows.Forms.CheckedListBox()
		self._checkBoxSelectAll = System.Windows.Forms.CheckBox()
		self._button1 = System.Windows.Forms.Button()
		self.SuspendLayout()
		# 
		# checkedListBox1
		# 
		self._checkedListBox1.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right
		self._checkedListBox1.FormattingEnabled = True
		self._checkedListBox1.CheckOnClick = True
		self._checkedListBox1.Location = System.Drawing.Point(12, 30)
		self._checkedListBox1.Name = "checkedListBox1"
		self._checkedListBox1.DataSource = self._lstSchedule 
		#self._checkedListBox1.Items.AddRange(System.Array[System.Object](self._lstSchedule))
		self._checkedListBox1.DisplayMember = "Name"
		self._checkedListBox1.Size = System.Drawing.Size(260, 244)
		self._checkedListBox1.TabIndex = 0
		# 
		# checkBoxSelectAll
		# 
		self._checkBoxSelectAll.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left
		self._checkBoxSelectAll.Location = System.Drawing.Point(12, 291)
		self._checkBoxSelectAll.Name = "checkBoxSelectAll"
		self._checkBoxSelectAll.Size = System.Drawing.Size(104, 24)
		self._checkBoxSelectAll.TabIndex = 1
		self._checkBoxSelectAll.Text = "Select All"
		self._checkBoxSelectAll.UseVisualStyleBackColor = True
		self._checkBoxSelectAll.CheckedChanged += self.CheckBoxSelectAllCheckedChanged
		# 
		# button1
		# 
		self._button1.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right
		self._button1.Location = System.Drawing.Point(191, 289)
		self._button1.Name = "button1"
		self._button1.Size = System.Drawing.Size(75, 27)
		self._button1.TabIndex = 2
		self._button1.Text = "OK"
		self._button1.UseVisualStyleBackColor = True
		self._button1.Click += self.Button1Click
		# 
		# Form24
		# 
		self.ClientSize = System.Drawing.Size(284, 334)
		self.Controls.Add(self._button1)
		self.Controls.Add(self._checkBoxSelectAll)
		self.Controls.Add(self._checkedListBox1)
		self.Name = "Form24"
		self.Text = "Form24"
		self.ResumeLayout(False)


	def CheckBoxSelectAllCheckedChanged(self, sender, e):
		for i in range(self._checkedListBox1.Items.Count):
			self._checkedListBox1.SetItemChecked(i, sender.Checked) 

	def Button1Click(self, sender, e):
		self.out = self._checkedListBox1.CheckedItems
		self.Close()
	
toList = lambda x : x if hasattr(x, '__iter__') else [x]

#Preparing input from dynamo to revit
lstSchedules = toList(UnwrapElement(IN[0]))
objForm = Form24(lstSchedules)
objForm.ShowDialog()
OUT = objForm.out
5 Likes

@c.poupin ,

I try to build a template for my inputs.
When I try to change the SelectionMode SharpDevelop and Python give me an error:


image

Any idea how I can use the checkboxes and how to activate this methods?

I want the same as the DataShape ListView Data node.

For multi selection, you need to use a ListView instead of a CheckedListBox

1 Like

@c.poupin , thanks, that’s hard haha.
I will give it a try :slight_smile:

I’m starting to get somewhere, how can I get the inputs?
I added self._input = input.

import clr #.NET Laden
import sys #sys is de fundamentele Python bibliotheek
#de standaard IronPython-bibliotheken
sys.path.append(r'H:\BIM-REVIT\00 GEBRUIKERSGROEP BIM\00 BRONBESTANDEN\Python\defs') #Imports the
#standaard IronPython-bibliotheken, die alles dekken, van servers en
#encryptie tot reguliere expressies.
import functies as fc
import System #The System namespace in de hoofdmap .NET
from System import Array #.NET class voor het verwerken van array-informatie
import System.Collections.Generic as MGen #Module kan nu benaderd worden met MGen.xxxx
#from System.Collections.Generic import * #Hiermee kunt u generieke afhandelen. Revit's API
#soms wil hard-getypte 'generieke' lijsten, genaamd ILists. Als je niet nodig hebt
#deze kunt u deze regel verwijderen.
clr.AddReference('ProtoGeometry')  #Een Dynamo-bibliotheek voor zijn proxygeometrie
#classes. Je hebt dit alleen nodig als je interactie hebt met geometrie.
import Autodesk.DesignScript.Geometry as AGeo #Module kan worden opgeroepen a=met AGeo.xxxx
#from Autodesk.DesignScript.Geometry import * #Laadt alles in Dynamo's
#geometriebibliotheek
clr.AddReference("RevitNodes") #Dynamo's nodes voor Revit
import Revit #Laad in de Revit-namespaces in RevitNodes
clr.ImportExtensions(Revit.Elements) #Meer laden van Dynamo's Revit-bibliotheken
clr.ImportExtensions(Revit.GeometryConversion) #Meer laden van Dynamo's
#Revit-bibliotheken. Je hebt dit alleen nodig als je interactie hebt met geometrie.
clr.AddReference("RevitServices") #Dynamo's classes voor het omgaan met Revit-documenten
import RevitServices 
from RevitServices.Persistence import DocumentManager #Een interne Dynamo class
#dat het document bijhoudt waaraan Dynamo momenteel is gekoppeld
from RevitServices.Transactions import TransactionManager #Een Dynamo class voor
#transacties openen en sluiten om de database van het Revit-document te wijzigen

clr.AddReference("RevitAPI") #Verwijzing naar Revit's API DLL's toevoegen
clr.AddReference("RevitAPIUI") #Verwijzing naar Revit's APIUI DLL's toevoegen

import Autodesk #Loads the Autodesk namespace
import Autodesk.Revit.DB as RDB #Loading Revit's API UI classes module kan nu worden aangeroepen met RDB.xxxx
#from Autodesk.Revit.DB import * #Loading Revit's API UI classes
import Autodesk.Revit.UI as RUI # Loading Revit's API UI classes als RUI.xxxx
#from Autodesk.Revit.UI import * #Loading Revit's API UI classes

doc = DocumentManager.Instance.CurrentDBDocument #Dit is het actieve Revit document
uiapp = DocumentManager.Instance.CurrentUIApplication #Een handle instellen voor het actieve Revit UI-document
app = uiapp.Application  #Een handle instellen op de momenteel geopende instantie van de Revit-toepassing
uidoc = uiapp.ActiveUIDocument #Een handle instellen op de momenteel geopende instantie van de Revit UI-toepassing
revit_version = int(doc.Application.VersionNumber)
# code omrekenen revit feet naar huidig ingestele document units
if revit_version >= 2022:
    ForgeLength = "autodesk.spec.aec:length-1.0.0"  # zie voor lijst ForgeTypeIds.xlsx in de map handleidingen
    ForgeTypeLength = RDB.ForgeTypeId(ForgeLength)
    getDocUnits = doc.GetUnits()
    getDisplayUnitsLength = getDocUnits.GetFormatOptions(ForgeTypeLength).GetUnitTypeId()
else:
    getDocUnits = doc.GetUnits()
    getDisplayUnits = getDocUnits.GetFormatOptions(RDB.UnitType.UT_Length).DisplayUnits
    
def uwlist(input):
    result = input if isinstance(input, list) else [input]
    return UnwrapElement(result)

# einde code omrekenen revit feet naar huidig ingestelde document units
# Hieronder kan je dan gaan programmeren
# Gebruik boiler template
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 Form2(Form):
	def __init__(self):
		self.InitializeComponent()
		self._input = input
	
	def InitializeComponent(self):
		for i in input:
			listViewItem11 = System.Windows.Forms.ListViewItem(str(i))
		self._listView1 = System.Windows.Forms.ListView()
		self.SuspendLayout()
		# 
		# listView1
		# 
		#self._listView1.Items.Checked = True
		self._listView1.Items.AddRange(System.Array[System.Windows.Forms.ListViewItem]([listViewItem11]))
		self._listView1.Location = System.Drawing.Point(43, 30)
		self._listView1.Name = "listView1"
		self._listView1.Size = System.Drawing.Size(410, 97)
		self._listView1.TabIndex = 0
		self._listView1.UseCompatibleStateImageBehavior = False
		# 
		# Form2
		# 
		self.ClientSize = System.Drawing.Size(484, 461)
		self.Controls.Add(self._listView1)
		self.Name = "Form2"
		self.Text = "Form2"
		self.ResumeLayout(False)


#Start Transaction
#TransactionManager.Instance.EnsureInTransaction(doc)
#Eind Transactie
#TransactionManager.Instance.TransactionTaskDone()
input = IN[0]

objForm = Form2()
objForm.ShowDialog()

Got it! :slight_smile:

class Form2(Form):
	def __init__(self):
		self.InitializeComponent()
		self._input = input
		
	def InitializeComponent(self):
		self._listView1 = System.Windows.Forms.ListView()
		self.items = []
		for i in input:
			self.items.append(System.Windows.Forms.ListViewItem(str(i)))

		self.SuspendLayout()
		# 
		# listView1
		# 
		self._listView1.CheckBoxes = True
		self._listView1.View = System.Windows.Forms.View.List
		self._listView1.Items.AddRange(System.Array[System.Windows.Forms.ListViewItem](self.items))
		self._listView1.Location = System.Drawing.Point(43, 30)
		self._listView1.Name = "listView1"
		self._listView1.Size = System.Drawing.Size(410, 97)
		self.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D
		self._listView1.TabIndex = 0
	
		# 
		# Form2
		# 
		self.ClientSize = System.Drawing.Size(484, 461)
		self.Controls.Add(self._listView1)
		self.Name = "Form2"
		self.Text = "Form2"
		self.ResumeLayout(False)


2 Likes