Create Selec Object WinForm

Hello, I would like to know how I could create the select object in wl winform

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

from System.Drawing import Point, Image, Bitmap, Size
from System.Windows.Forms import Application, Button, Form, Label, TextBox

class SimpleTextBoxForm(Form):
    def __init__(self, valueA, valueB, imgpath):
        self._imgpath = imgpath
        self._valueInitA = str(valueA)
        self._valueInitB = str(valueB) 
        self._pictureBox1 = System.Windows.Forms.PictureBox()
        self._pictureBox1.BeginInit()

        self.Width = 220
        self.Height = 300

        self.textbox = TextBox()
        self.textbox.Text = self._valueInitA
        self.textbox.Location = Point(25, 25)
        self.textbox.Width = 150

        self.textbox1 = TextBox()
        self.textbox1.Text = self._valueInitB
        self.textbox1.Location = Point(25, 55)
        self.textbox1.Width = 150

        self._imageTop = Image.FromFile(self._imgpath)
        self._imageTopResize = Bitmap(self._imageTop, Size(100, 100))
        self._pictureBox1.Location = System.Drawing.Point(45, 90)
        self._pictureBox1.Name = "pictureBox1"
        self._pictureBox1.Size = System.Drawing.Size(100, 100)
        self._pictureBox1.TabIndex = 0
        self._pictureBox1.TabStop = False
        self._pictureBox1.Image = self._imageTopResize

        self.button1 = Button()
        self.button1.Text = 'Press Me'
        self.button1.Location = Point(25, 230)
        
        self.AcceptButton = self.button1

        self.Controls.Add(self.textbox)
        self.Controls.Add(self.textbox1)
        self.Controls.Add(self.button1)
        self.Controls.Add(self._pictureBox1)

        self.values = []
        self.button1.Click += self.update
        self._pictureBox1.EndInit()

    def update(self, sender, event):
        for control in self.Controls:
            if isinstance(control, TextBox):
              self.values.append(control.Text)
        self.Close()

form = SimpleTextBoxForm(IN[0], IN[1], IN[2])
form.ShowDialog()

OUT = form.values

@Christhian ,

look at pyRevit, also…

by the way use .png instead of .jpg

KR

Andreas

hi
here an example


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

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 *
from System.Windows.Forms import Application as wApp

clr.AddReference('AcMgd')
clr.AddReference('AcCoreMgd')
clr.AddReference('AcDbMgd')
clr.AddReference('AecBaseMgd')
clr.AddReference('AecPropDataMgd')
clr.AddReference('AeccDbMgd')
clr.AddReference('AutoCADNodes') 
# Import references from AutoCAD
import Autodesk
from Autodesk.AutoCAD.Runtime import *
from Autodesk.AutoCAD.ApplicationServices import *
from Autodesk.AutoCAD.EditorInput import *
from Autodesk.AutoCAD.DatabaseServices import *
from Autodesk.AutoCAD.Geometry import *

# Import references from Civil3D
from Autodesk.Civil import FeatureLinePointType
from Autodesk.Civil.ApplicationServices import *
from Autodesk.Civil.DatabaseServices import *


class Form22(Form):
	def __init__(self):
		self.selectionIds = []
		self.InitializeComponent()
	
	def InitializeComponent(self):
		self._buttonSelect = System.Windows.Forms.Button()
		self._buttonClose = System.Windows.Forms.Button()
		self.SuspendLayout()
		# 
		# buttonSelect
		# 
		self._buttonSelect.Location = System.Drawing.Point(64, 73)
		self._buttonSelect.Name = "buttonSelect"
		self._buttonSelect.Size = System.Drawing.Size(170, 41)
		self._buttonSelect.TabIndex = 0
		self._buttonSelect.Text = "Select Object"
		self._buttonSelect.UseVisualStyleBackColor = True
		self._buttonSelect.Click += self.ButtonSelectClick
		# 
		# buttonSelect
		# 
		self._buttonClose.Location = System.Drawing.Point(64, 120)
		self._buttonClose.Name = "buttonClose"
		self._buttonClose.Size = System.Drawing.Size(170, 41)
		self._buttonClose.TabIndex = 0
		self._buttonClose.Text = "Close"
		self._buttonClose.UseVisualStyleBackColor = True
		self._buttonClose.Click += self.ButtonCloseClick
		# 
		# Form27
		# 
		self.ClientSize = System.Drawing.Size(298, 200)
		self.Controls.Add(self._buttonSelect)
		self.Controls.Add(self._buttonClose)
		self.Name = "Form27"
		self.Text = "Form27"
		self.ResumeLayout(False)

	def ButtonCloseClick(self, sender, e):
		self.Close()


	def ButtonSelectClick(self, sender, e):
		acSSPrompt = editor.GetSelection() 
		if acSSPrompt.Status == PromptStatus.OK: 
			acSSet = acSSPrompt.Value 
			for s in acSSet:
				if s: 
					self.selectionIds.append(s.ObjectId)
		self.Activate()


adoc = Application.DocumentManager.MdiActiveDocument
doc = CivilApplication.ActiveDocument
editor = adoc.Editor

wApp.EnableVisualStyles()
form = Form22()
wApp.Run(form)

OUT = form.selectionIds
4 Likes

@c.poupin Hello, I tried to adapt your code to my code but I have a small error, I don’t know where I am going wrong and I managed to get the select button to appear but when I select it gives me an error
I checked your code and it works perfectly

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

from System.Drawing import Point, Image, Bitmap, Size
from System.Windows.Forms import Application, Button, Form, Label, TextBox

class SimpleTextBoxForm(Form):
    def __init__(self, valueA, valueB, imgpath):
        self._imgpath = imgpath
        self._valueInitA = str(valueA)
        self._valueInitB = str(valueB) 
        self._pictureBox1 = System.Windows.Forms.PictureBox()
        self._pictureBox1.BeginInit()

        self.Width = 220
        self.Height = 300

        self.textbox = TextBox()
        self.textbox.Text = self._valueInitA
        self.textbox.Location = Point(25, 25)
        self.textbox.Width = 150

        self.textbox1 = TextBox()
        self.textbox1.Text = self._valueInitB
        self.textbox1.Location = Point(25, 55)
        self.textbox1.Width = 150

        self._imageTop = Image.FromFile(self._imgpath)
        self._imageTopResize = Bitmap(self._imageTop, Size(100, 100))
        self._pictureBox1.Location = System.Drawing.Point(45, 90)
        self._pictureBox1.Name = "pictureBox1"
        self._pictureBox1.Size = System.Drawing.Size(100, 100)
        self._pictureBox1.TabIndex = 0
        self._pictureBox1.TabStop = False
        self._pictureBox1.Image = self._imageTopResize

        self.button1 = Button()
        self.button1.Text = 'Press Me'
        self.button1.Location = Point(25, 230)

        self.buttonSelect = Button()
        self.buttonSelect.Text = 'Select Object'
        self.buttonSelect.Location = Point(25, 185)

        self.AcceptButton = self.button1

        self.Controls.Add(self.textbox)
        self.Controls.Add(self.textbox1)
        self.Controls.Add(self.button1)
        self.Controls.Add(self.buttonSelect)
        self.Controls.Add(self._pictureBox1)

        self.values = []
        self.button1.Click += self.update
        self.buttonSelect.Click += self.selectObject
        self._pictureBox1.EndInit()

    def update(self, sender, event):
        for control in self.Controls:
            if isinstance(control, TextBox):
                self.values.append(control.Text)
        self.Close()

    def selectObject(self, sender, event):
        # Lógica para seleccionar el objeto y registrar su valor
        acSSPrompt = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.GetSelection()
        if acSSPrompt.Status == Autodesk.AutoCAD.EditorInput.PromptStatus.OK:
            acSSet = acSSPrompt.Value
            for s in acSSet:
                if s:
                    self.values.append(str(s.ObjectId))
        self.Activate()

form = SimpleTextBoxForm(IN[0], IN[1], IN[2])
form.ShowDialog()

OUT = form.values

image

If this were to work one of the outputs would be the selected item

hi,

I add the missing import in my previous post

# Import references from AutoCAD
import Autodesk
1 Like

@c.poupin add what you told me but the error has changed to this other
image

Application services are part of the accoremgd.dll library - add this line before

clr.AddReference('AcCoreMgd')
1 Like

You can try this:

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

from System.Drawing import Point, Image, Bitmap, Size
from System.Windows.Forms import Application, Button, Form, Label, TextBox

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager

clr.AddReference("RevitAPI")
clr.AddReference("RevitAPIUI")
import Autodesk 
from Autodesk.Revit.UI import Selection

doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication 
app = uiapp.Application 
uidoc = uiapp.ActiveUIDocument

class SimpleTextBoxForm(Form):
    def __init__(self, valueA, valueB):
        self._valueInitA = str(valueA)
        self._valueInitB = str(valueB) 
        self._pictureBox1 = System.Windows.Forms.PictureBox()
        self._pictureBox1.BeginInit()
        self.TopMost = True

        self.Width = 220
        self.Height = 300

        self.textbox = TextBox()
        self.textbox.Text = self._valueInitA
        self.textbox.Location = Point(25, 25)
        self.textbox.Width = 150

        self.textbox1 = TextBox()
        self.textbox1.Text = self._valueInitB
        self.textbox1.Location = Point(25, 55)
        self.textbox1.Width = 150

        self.button1 = Button()
        self.button1.Text = 'Press Me'
        self.button1.Location = Point(25, 230)

        self.buttonSelect = Button()
        self.buttonSelect.Text = 'Select Object'
        self.buttonSelect.Location = Point(25, 185)

        self.AcceptButton = self.button1

        self.Controls.Add(self.textbox)
        self.Controls.Add(self.textbox1)
        self.Controls.Add(self.button1)
        self.Controls.Add(self.buttonSelect)
        self.Controls.Add(self._pictureBox1)

        self.values = []
        self.button1.Click += self.update
        self.buttonSelect.Click += self.selectObject
        self._pictureBox1.EndInit()

    def update(self, sender, event):
        for control in self.Controls:
            if isinstance(control, TextBox):
                self.values.append(control.Text)
        self.Close()

    def selectObject(self, sender, event):
    	sel = uidoc.Selection.PickObject(Selection.ObjectType.Element)
    	if sel:
    		element = doc.GetElement(sel.ElementId)
    		self.buttonSelect.Text = element.Name
        

form = SimpleTextBoxForm(IN[0], IN[1])
form.Show()

OUT = form.values
1 Like

thanks it works as i wish @newshunhk @c.poupin @Mike.Buttery