Cant get text data by import instance

import clr
import sys

sys.path.append('C:\\Program Files (x86)\\IronPython 2.7\\Lib')

clr.AddReference("System.Core")
clr.AddReference('ProtoGeometry')
clr.AddReference("RevitAPI")
clr.AddReference("RevitAPIUI")
clr.AddReference("RevitNodes")
clr.AddReference("RevitServices")

import System
from System.Linq import Enumerable
from Autodesk.DesignScript.Geometry import *
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI import *
from Autodesk.Revit.Creation import *
from Revit import Elements
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument
app = DocumentManager.Instance.CurrentUIApplication.Application

def toList(input):
    if isinstance(input, list) == False:
        return [input]
    else:
        return input

# Unwrap the input element
importInstance = UnwrapElement(IN[0])

# Initialize an empty list to store the layer names
layers = []

# Initialize empty lists to store the information
layer_names = []
line_types = []
curves = []
text_values = []

# Check if the input element is a CAD import instance
if isinstance(importInstance, ImportInstance):
    # Collect all layers from the CAD import instance
    for subcategory in importInstance.Category.SubCategories:
        layers.append(subcategory.Name)

    # Get the geometry of the import instance
    import_instance_geometry = importInstance.get_Geometry(Options())

    # Iterate over the geometry and extract information
    for geometry_object in import_instance_geometry:
        if isinstance(geometry_object, GeometryInstance):
            symbol_geometry = geometry_object.GetSymbolGeometry()
            for curve in symbol_geometry:
                curves.append(curve)

        elif isinstance(geometry_object, Line):
            line_types.append(geometry_object.LineStyle.Name)

        elif isinstance(geometry_object, TextElement):
            text_values.append(geometry_object.Text)

else:
    # Handle the case when the input element is not a CAD import instance
    OUT = "Input is not a CAD import instance."

OUT = layers, line_types, curves, text_values

1686716300889


1686716329237

1 Like

@ericke1 ,

it works …

import clr
import sys

clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Structure import *

clr.AddReference('RevitAPIUI')
from Autodesk.Revit.UI import *

clr.AddReference('System')
from System.Collections.Generic import List

clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
clr.ImportExtensions(Revit.Elements)

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

def toList(input):
	if isinstance(input, list) == False:
		return [input]
	else:
		return input
#Unwrap the input element

importInstance = UnwrapElement(IN[0])
#Initialize an empty list to store the layer names

layers = []
#Initialize empty lists to store the information

layer_names = []
line_types = []
curves = []
text_values = []
#Check if the input element is a CAD import instance

if isinstance(importInstance, ImportInstance):
# Collect all layers from the CAD import instance
	for subcategory in importInstance.Category.SubCategories:
		layers.append(subcategory.Name)
# Get the geometry of the import instance
	import_instance_geometry = importInstance.get_Geometry(Options())
# Iterate over the geometry and extract information
	for geometry_object in import_instance_geometry:
		if isinstance(geometry_object, GeometryInstance):
			symbol_geometry = geometry_object.GetSymbolGeometry()
			for curve in symbol_geometry:
				curves.append(curve)
		elif isinstance(geometry_object, Line):
			line_types.append(geometry_object.LineStyle.Name)
		elif isinstance(geometry_object, TextElement):
			text_values.append(geometry_object.Text)
else:
# Handle the case when the input element is not a CAD import instance
				OUT = "Input is not a CAD import instance."

OUT = layers, line_types, curves, text_values

KR

Andreas

but actually no work Text_value still show empty list

Hi,
it is not possible to get the text value from a ImportInstance (dwg link) by analyzing its geometry

you can use the BimorphNodes package (that uses another process)

image

Hey,

LinkDWG also has a few nodes that you can dig around in to see the code… It’s using COM objects and you need to have AutoCAD open to get them.

Hope that is of interest…

1 Like

I don’t want to load that node to every pc so I decide to writing code by my self but thank for this suggestion

Thomas Mahon describes possible methods here… Can anyone pls change this code and add insert IN[2] = LinkDWG - #2 by Thomas_Mahon

1 Like

or this :wink:

1 Like

nothing is easy :wink:

1 Like

Revit has the AcDbMgd.dll library available.

You could side load the DWG file and scrape for text objects using the Autocad API

clr.AddReference("AcDbMgd")
from Autodesk.AutoCAD.Runtime import *
from Autodesk.AutoCAD.DatabaseServices import *
from Autodesk.AutoCAD.Geometry import *

# get DWG path example
dwgpath = IN[0]

with Database(False, True) as db:
    db.ReadDwgFile(dwgpath, FileShare.ReadWrite, False, "")
        with db.TransactionManager.StartTransaction() as t:
            # Do AutoCAD API stuff here
            # Commit changes
            t.Commit()
1 Like

Thank you very much for your comment, I have ideas that you could use, but I still don’t understand very well how the code could interact with Revit using the code in PYTHON SCRIPT. Could you share a little more about it, since the “AcDbMgd” library to begin with I wouldn’t know how to obtain said library

The AcDbMgd.dll file is installed with Revit - although not officially documented I believe it is used to import/export DWGs

I have used it in the past to make changes to exported DWGs to comply with client requirements by sideloading and making changes using Revit data

Please note that when using it with Dynamo and python it can be incredibly temperamental

That DLL is a portion of the AutoCAD API, so documentation there of would be in the AutoCAD API documentation. However as only a portion of the necessary DLLs are provided you are somewhat limited in their access and capability before we take Python’s limitations into accountz

In hindsight there is a VERY good reason Python based custom nodes never caught on in Dynamo for Civil 3D - Python access to the API is too limited by the great many ties into NET methods which don’t have a proper wrapper in Python. Doesn’t mean you can’t accomplish it, but that it is much harder and more apt to break to pieces on any given update to the house of cards that you’re relying on. BetweenNET, Revit, Revit API, AutoCAD API, Dynamo, Python, Dynamo’s Python engine, and any/all customizations even not utilized to any of the above (even if not used by your script) can cause things to break. Moving to C# would be better if you’re up for it, but that’s a bigger lift and there is usually a better path forward for scaled automations (i.e. batch process the file using regular AutoCAD, or move to APS or other tools).

1 Like

Totally agree here
The AutoCAD API inherently feels unstable when using Dynamo and python - some days it just doesn’t like the way the wind is blowing and decides to throw exceptions (and this is on Civil3d not just Revit)

1 Like

And the C# equivalent is somehow insanely stable and performant… It’s almost like code likes to be run in it’s intended environment or something. :joy: