Select Object cad text by System.Linq

HI ALL
HI @c.poupin
Please Help
How to choose TEXTS from Autocad drawing by System.Linq
selcet Object cad text by System.Linq does not work

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')

# 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 *

adoc = Application.DocumentManager.MdiActiveDocument
editor = adoc.Editor

 # Add DesignScript references
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *


clr.AddReference("System.Core")
import System
clr.ImportExtensions(System.Linq)
from System.Linq import Enumerable

# Enable Python support and load DesignScript library
from System.Collections.Generic import Dictionary

def get_text():
	
	textProps = []
	obj=[]
	handles = []
	errorReport = None
	global adoc
	
	with adoc.LockDocument():
		with adoc.Database as db:
			with db.TransactionManager.StartTransaction() as t:
				bt = t.GetObject(db.BlockTableId, OpenMode.ForWrite)
				btr = t.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite)
				btrlq = btr.Cast[ObjectId ]().ToArray()
				btraa = btr.Cast[ObjectId ]().Select(id >= t.GetObject(id, OpenMode.ForWrite))
				t.Commit()			
	if errorReport == None:
		return btrlq
	else:
		return btrlq
	
OUT = get_text()

I can choose it with this code

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')

# 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 *

adoc = Application.DocumentManager.MdiActiveDocument
editor = adoc.Editor

 # Add DesignScript references
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *


clr.AddReference("System.Core")
import System
clr.ImportExtensions(System.Linq)
from System.Linq import Enumerable

# Enable Python support and load DesignScript library
from System.Collections.Generic import Dictionary

def get_text():
	
	textProps = []
	obj=[]
	handles = []
	errorReport = None
	global adoc
	
	with adoc.LockDocument():
		with adoc.Database as db:
			with db.TransactionManager.StartTransaction() as t:
				bt = t.GetObject(db.BlockTableId, OpenMode.ForWrite)
				btr = t.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite)
				for oid in btr:
				    bl = t.GetObject(oid, OpenMode.ForRead)
				    if isinstance(bl,DBText):
				       handles.append(str(bl.Handle))
				t.Commit()			
	if errorReport == None:
		return handles
	else:
		return handles
	
OUT = get_text()

There is an example with c#

 var modelSpace = (BlockTableRecord)tr.GetObject(
                    SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite);

                // get all curve entities in model space
                var curveClass = RXObject.GetClass(typeof(DBText));
                var curves = modelSpace 
                    .Cast<ObjectId>()
                    .Where(id => id.ObjectClass.IsDerivedFrom(curveClass))
                    .Select(id => (Curve)tr.GetObject(id, OpenMode.ForRead))
                    .ToArray();

Thanks in advance for any help

Uploading: CCCC.dwg…
linq t.dyn (9.6 KB)

Hello @hosneyalaa

an example with Autocad PythonShell (I do not have Civil3D)

import clr
import sys
import System
clr.AddReference("System.Core")
import System
clr.ImportExtensions(System.Linq)
from System.Linq import *

pf_path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFilesX86)
sys.path.append(pf_path + '\\IronPython 2.7\\Lib')
import os
import math
clr.AddReference('acmgd')
clr.AddReference('acdbmgd')
clr.AddReference('accoremgd')
# 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 *
doc = Application.DocumentManager.MdiActiveDocument
ed = doc.Editor
db = doc.Database
# Write Code Below
all_blkName = []
with doc.LockDocument():
	with doc.Database as db:
		with db.TransactionManager.StartTransaction() as t:
			bt = t.GetObject(db.BlockTableId,OpenMode.ForRead)
			btr  = t.GetObject(bt[BlockTableRecord.ModelSpace],OpenMode.ForRead)
			print(btr.GetType())
			btrlq = btr.Cast[ObjectId]().ToList()
			print(btrlq.GetType())
			#print(btrlq.Count)
			btraa = btrlq.Select(lambda id : t.GetObject(id, OpenMode.ForWrite))
			print(btraa)
			for obj in btraa:
				if isinstance(obj,MText):
					print(str(obj.Handle))
			t.Commit()
3 Likes

Hi @hosneyalaa

You can just change your line to:

btraa = btrlq.Where(lambda id : id.ObjectClass.IsDerivedFrom(RXClass.GetClass(DBText)))

2 Likes

@Kulkul @c.poupin thank you

Is it possible to get the string of each text

Thanks :pray:

Yep!

2 Likes

Thanks @Kulkul

I’m sorry I’m away from the computer
Is this code correct to get the string depending on what you shared

btrstr = btraa.Where(lambda id : id.t.GetObject(id, OpenMode.ForRead).textstring)