Define the 'Select Object' command as a function using Civil3D Dynamo Python script

I want to define the ‘Select Object’ command as a function using Civil3D Dynamo Python script.

Because I want to create a User Interface using the WindowsForm module in my Dynamo script, I plan to utilize various types of input parameters like buttons, browser buttons, etc. Particularly, I want to enhance the script so that when a polyline (object) is selected from the current drawing through the User Interface, that object is outputted. This part is quite challenging for me. I would be very grateful if you could suggest a method related to this.

# Load the Python Standard and DesignScript Libraries
import sys
import clr

# Add Assemblies for AutoCAD and Civil3D
clr.AddReference('AcMgd')
clr.AddReference('AcCoreMgd')
clr.AddReference('AcDbMgd')
clr.AddReference('AecBaseMgd')
clr.AddReference('AecPropDataMgd')
clr.AddReference('AeccDbMgd')
clr.AddReference('System.Windows.Forms')
clr.AddReference('System.Drawing')

# Import references from AutoCAD
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.ApplicationServices import *
from Autodesk.Civil.DatabaseServices import *

from System.Windows.Forms import Application, Form, Label, Button, DialogResult
from System.Drawing import Size, Point
from System.Windows.Forms import TextBox
from System.Windows.Forms import OpenFileDialog
from Autodesk.AutoCAD.EditorInput import PromptEntityOptions, PromptEntityResult


# The inputs to this node will be stored as a list in the IN variables.

global userInput, fileInput
userInput = None
fileInput = None


class MyForm(Form):
    def __init__(self):
        self.Text = 'DHBIM'
        self.Size = Size(700, 400)
        
        self.outputLabel = Label()
        self.outputLabel.Location = Point(10, 70)
        self.outputLabel.Size = Size(280, 20)
        self.Controls.Add(self.outputLabel)
        
        self.label = Label()
        self.label.Text = '설계유량(m3/day):'
        self.label.Location = Point(10, 15)
        self.label.Size = Size(180, 30)
        self.Controls.Add(self.label)
        
        self.textBox = TextBox()
        self.textBox.Location = Point(200, 10)
        self.textBox.Size = Size(150, 20)
        self.Controls.Add(self.textBox)
        
        self.label = Label()
        self.label.Text = '관경별 공사비:'
        self.label.Location = Point(10, 100)
        self.label.Size = Size(180, 30)
        self.Controls.Add(self.label)
        
        self.browseButton = Button()
        self.browseButton.Text = '찾아보기'
        self.browseButton.Location = Point(200,100)
        self.browseButton.Click += self.on_browse_click
        self.browseButton.Size = Size(100, 40)  # '찾아보기' 버튼 크기 설정
        self.Controls.Add(self.browseButton)
        
           
        self.button = Button()
        self.button.Text = '확인'
        self.button.Location = Point(200, 300)
        self.button.Click += self.on_button_click
        self.button.Size = Size(100, 30)  # '확인' 버튼 크기 설정
        self.Controls.Add(self.button)


    def on_browse_click(self, sender, args):
        fileDialog = OpenFileDialog()
        if fileDialog.ShowDialog() == DialogResult.OK:
            global fileInput
            fileInput = fileDialog.FileName
        
    
    def on_button_click(self, sender, args):
        global userInput
        userInput = self.textBox.Text
        self.Close()  # 폼을 닫습니다
        

Application.EnableVisualStyles()
form = MyForm()
Application.Run(form)


OUT = [userInput, fileInput] if userInput is not None and fileInput is not None else "No Input"```

see this

I appreciate your helpful response, and I’m grateful for your efforts. However, I have no programming background, so I’m finding it difficult to update my user interface solely with the Dynamo script you’ve shared. I’m really sorry to ask, but could you share the complete script that produces results like the ones shown in the second image?

TRY

# Load the Python Standard and DesignScript Libraries
import sys
import clr

# Add Assemblies for AutoCAD and Civil3D
clr.AddReference('AcMgd')
clr.AddReference('AcCoreMgd')
clr.AddReference('AcDbMgd')
clr.AddReference('AecBaseMgd')
clr.AddReference('AecPropDataMgd')
clr.AddReference('AeccDbMgd')
clr.AddReference('System.Windows.Forms')
clr.AddReference('System.Drawing')
clr.AddReference('AutoCADNodes')

import Autodesk
# Import references from AutoCAD
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.ApplicationServices import *
from Autodesk.Civil.DatabaseServices import *

from System.Windows.Forms import Application, Form, Label, Button, DialogResult
from System.Drawing import Size, Point
from System.Windows.Forms import TextBox
from System.Windows.Forms import OpenFileDialog
from Autodesk.AutoCAD.EditorInput import PromptEntityOptions, PromptEntityResult


# Import references from Dynamo
from Autodesk.AutoCAD.DynamoNodes import SelectionByQuery
# The inputs to this node will be stored as a list in the IN variables.

global userInput, fileInput , selectobjects
userInput = None
fileInput = None
selectobjects = None

adoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
ed = adoc.Editor

class MyForm(Form):
    def __init__(self):
        self.Text = 'DHBIM'
        self.Size = Size(700, 400)
        
        self.outputLabel = Label()
        self.outputLabel.Location = Point(10, 70)
        self.outputLabel.Size = Size(280, 20)
        self.Controls.Add(self.outputLabel)
        
        self.label = Label()
        self.label.Text = 'Design flow rate(m3/day):'
        self.label.Location = Point(10, 15)
        self.label.Size = Size(180, 30)
        self.Controls.Add(self.label)
        
        self.textBox = TextBox()
        self.textBox.Location = Point(200, 10)
        self.textBox.Size = Size(150, 20)
        self.Controls.Add(self.textBox)
        
        self.label = Label()
        self.label.Text = 'Construction cost by pipe size:'
        self.label.Location = Point(10, 100)
        self.label.Size = Size(180, 30)
        self.Controls.Add(self.label)
        
        self.browseButton = Button()
        self.browseButton.Text = 'Browse'
        self.browseButton.Location = Point(200,100)
        self.browseButton.Click += self.on_browse_click
        self.browseButton.Size = Size(100, 40)  # '찾아보기' 버튼 크기 설정
        self.Controls.Add(self.browseButton)

        self.label = Label()
        self.label.Text = 'select object:'
        self.label.Location = Point(10, 200)
        self.label.Size = Size(180, 30)
        self.Controls.Add(self.label)


        self.sButton = Button()
        self.sButton.Text = 'select'
        self.sButton.Location = Point(200,200)
        self.sButton.Click += self.cl_click
        self.sButton.Size = Size(100, 40)  # '찾아보기' 버튼 크기 설정
        self.Controls.Add(self.sButton)
 
 
           
        self.button = Button()
        self.button.Text = 'check'
        self.button.Location = Point(200, 300)
        self.button.Click += self.on_button_click
        self.button.Size = Size(100, 30)  # '확인' 버튼 크기 설정
        self.Controls.Add(self.button)


    def on_browse_click(self, sender, args):
        fileDialog = OpenFileDialog()
        #selectobjects = select_objects()[0]
        
        if fileDialog.ShowDialog() == DialogResult.OK:
            global fileInput
            fileInput = fileDialog.FileName
        
    
    def on_button_click(self, sender, args):
        global userInput
        userInput = self.textBox.Text
        self.Close()  # 폼을 닫습니다
        
        
    def cl_click(self, sender, args):
        global selectobjects
        selectobjects = select_objects()
 
def select_objects():
	
	global adoc
	global ed
	
	output = []
	
	with adoc.LockDocument():
		with adoc.Database as db:
			with db.TransactionManager.StartTransaction() as t:

				acSSPrompt = ed.GetSelection()
	
				if acSSPrompt.Status == PromptStatus.OK:
					acSSet = acSSPrompt.Value
					hndl = []
					for s in acSSet:
						if s:
							obj = t.GetObject(s.ObjectId, OpenMode.ForRead)
							hndl.append(str(obj.Handle))
							
				t.Commit()
	for h in hndl:
		output.append(SelectionByQuery.GetObjectByObjectHandle(h))
	return output	

"""
def select_objects():
	
	global adoc
	global ed
	
	output = []
	
	with adoc.LockDocument():
		with adoc.Database as db:
			with db.TransactionManager.StartTransaction() as t:

				acSSPrompt = ed.GetSelection()
	
				if acSSPrompt.Status == PromptStatus.OK:
					acSSet = acSSPrompt.Value
					hndl = []
					for s in acSSet:
						if s:
							obj = t.GetObject(s.ObjectId, OpenMode.ForRead)
							hndl.append(str(obj.Handle))
							
				t.Commit()
	for h in hndl:
		output.append(SelectionByQuery.GetObjectByObjectHandle(h))
	return output	

"""
Application.EnableVisualStyles()
form = MyForm()
Application.Run(form)


OUT = [userInput, fileInput , selectobjects] #if userInput is not None and fileInput is not None else "No Input"```

7

1 Like

I will study based on the script you shared and try to apply it in various ways. I don’t know how to repay your kindness…I’m so grateful. I will study hard and test to see if other types of input are possible as well. Can I ask for your help if I have more questions? ^^