Prompting the User to select the feature line and output the feature line string name

Hi

I am creating dynamo scripts using FeatureLine.ByName node to do a variety of tasks.

Currently I have a string node which I manually type the name of the feature line. Then then passes to the FeatureLine.ByName. I would like to make this more interactive for the intended user. When running the script I would like to prompt the user to select the required feature line and the output is the feature line name as a string.

I used co-pilot to assist with the python scripting. However Dynamo returns the error message with the python script: typeerror : instance property must be accessed through class instance [‘File “”, line 25, in \n’]

import clr
import sys

# Add references to Civil 3D and AutoCAD libraries
clr.AddReference('AcMgd')
clr.AddReference('AcDbMgd')
clr.AddReference('AeccDbMgd')

from Autodesk.AutoCAD.ApplicationServices import Application
from Autodesk.AutoCAD.EditorInput import PromptEntityOptions, PromptEntityResult
from Autodesk.Civil.ApplicationServices import CivilApplication
from Autodesk.Civil.DatabaseServices import FeatureLine

# Get the active document
doc = Application.DocumentManager.MdiActiveDocument
ed = doc.Editor

# Prompt the user to select a feature line
opts = PromptEntityOptions("\nSelect a feature line: ")
opts.SetRejectMessage("\nObject must be a feature line.")
opts.AddAllowedClass(FeatureLine, True)
res = ed.GetEntity(opts)

# Check if the user made a valid selection
if res.Status == PromptEntityResult.Status.OK:
    with doc.LockDocument():
        tr = doc.TransactionManager.StartTransaction()
        try:
            # Get the selected feature line
            feature_line = tr.GetObject(res.ObjectId, OpenMode.ForRead)
            # Get the name of the feature line
            feature_line_name = feature_line.Name
            tr.Commit()
        except Exception as e:
            tr.Abort()
            feature_line_name = str(e)
else:
    feature_line_name = "Selection was not successful."

# Output the feature line name
OUT = feature_line_name

Any help would be great.

Is there a particular reason to use python for this ?

As a disclaimer, I didn’t tried running this one with the player yet so maybe it is because of this you’re doing it with python ?

Well, I made a few modifications to find the error.
Turns out you did not reference the AcCoreMgd.dll and you used wrong classes at some places.
Also, you used PromptEntityResult.Status which does not exist, instead I used the PromptStatus Enum res.Status == Editorinput.PromptStatus.OK
image

import clr
import sys

# Add references to Civil 3D and AutoCAD libraries
clr.AddReference('AcMgd')
clr.AddReference('AcCoreMgd')
clr.AddReference('AcDbMgd')
clr.AddReference('AecBaseMgd')
clr.AddReference('AecPropDataMgd')
clr.AddReference('AeccDbMgd')

from Autodesk.AutoCAD.ApplicationServices import *
import Autodesk.AutoCAD.EditorInput as Editorinput
from Autodesk.Civil.ApplicationServices import *
import Autodesk.Civil.DatabaseServices as Civil
from Autodesk.AutoCAD.DatabaseServices import *

# Get the active document
doc = Application.DocumentManager.MdiActiveDocument
ed = doc.Editor

# Prompt the user to select a feature line
opts = Editorinput.PromptEntityOptions("\nSelect a feature line: ")
opts.SetRejectMessage("\nObject must be a feature line.")
opts.AddAllowedClass(Civil.FeatureLine, True)
res = ed.GetEntity(opts)

# Check if the user made a valid selection
if res.Status == Editorinput.PromptStatus.OK:
    with doc.LockDocument():
        with doc.Database as db:
            with db.TransactionManager.StartTransaction() as tr:
                try:
                    # Get the selected feature line
                    feature_line = tr.GetObject(res.ObjectId, OpenMode.ForRead)
                    # Get the name of the feature line
                    feature_line_name = feature_line.Name
                    tr.Commit()
                except Exception as e:
                    tr.Abort()
                    feature_line_name = str(e)
else:
    feature_line_name = "Selection was not successful."

# Output the feature line name
OUT = feature_line_name

This code worked for me.

1 Like

Hi David, I tried the nodes you suggested. The Select Object is only for AutoCAD objects not civil 3D objects. So I have to use the python node.

Kovacsv thank you also for providing finding the syntax error in the python script.

Did you try with the data-shapes nodes?

Hi Daniel, from my limited understanding of the data-shapes node. It is built for Revit. Yes I can use the some of the nodes. But Revit built packages unfortunately don’t work for Civil 3D. Happy to test it out and prove my theory wrong.

Last I checked it works for Revit, Civil3D, Alias, FormIt, Advance Steel… The Revit selection nodes won’t work but the rest will, and I believe there are some Civil 3D nodes too.

What makes you think that it only works on AutoCAD objects? It works great with Civil 3D objects too:

1 Like

In the screenshot I provided, I had selected a feature line (C3D object) so should work but glad you have the python code solution by @kovacsv !

Apologies everyone. Yes when you run the Civil Object plus the CivilObject.Name nodes it does return the string name of the civil 3D object.