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