Get BlockReference Rotation Value of list

Following on from the previous thread;
https://forum.dynamobim.com/t/get-blockreference-rotation-value/47139

Is there a way to change the input of this script to work with a list? I’ve tried changing the line of the script input but it’s only resulted in errors.

@Kulkul code is below from the previous thread,

for the line “if isinstance(bl, BlockReference) and bl.Name.startswith(IN[0]):” does there need to be another loop?

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

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

# The inputs to this node will be stored as a list in the IN variables.
	
adoc = Application.DocumentManager.MdiActiveDocument
editor = adoc.Editor

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)
			blh = []
			blr = []
			errorReport = None
			for oid in btr:
				bl = t.GetObject(oid, OpenMode.ForRead)
				try:
					# Filter BlockReference starts with.				
					if isinstance(bl, BlockReference) and bl.Name.startswith(IN[0]):
					# Get values of handle & Rotation.
						blh.append(bl.Handle)
						blr.append(bl.Rotation)
				# Catch and report Exception...	
				except:
					import traceback
					errorReport = traceback.format_exc()
					
					
			t.Commit()
           				

# Assign your output to the OUT variable.

if errorReport == None:
	OUT = blh,[x*57.2958 for x in blr]# Convert from radians to degrees
else:
	OUT = errorReport

Cheers

Hi @RDM

Try removing " and bl.Name.startswith(IN[0])".

@RDM if you’re interested in skipping the Python, you could use the Civil 3D Toolkit node ObjectExtensions.GetParameterByName and not have to worry about input list structure.

Hey,

I’ve tried that, but the script seems to collect every block in the document (60) while I’m trying to input a specific list of 20 blocks.

@RDM Ah got it. Give me sometime I’m not near my pc.

Hey,

This is basically what I’m looking for, although I would like to try stick with Python so it doesn’t rely on other users installing the Civil Toolkit package.

But this is a good solution for now to make sure the rest of the script works.

Thanks

@RDM Check this out:

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

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


# Declare global variables
a_doc = Application.DocumentManager.MdiActiveDocument
editor = a_doc.Editor
# Create Function
def Task_Get_BlockRotation(items):
	if not isinstance(items,list):
		items = [items]
	global a_doc
	error_report = None
	block_rot = []
	try:
		# Get the active document in the AutoCAD session:
		with a_doc.LockDocument():
			with a_doc.Database as db:
				# Start a transaction
				with db.TransactionManager.StartTransaction() as t:
				# Iterate through the list of handles
					for i in items:
						handle=i.Handle
						# Convert the handle hexadecimal string to 64-bit integer
						# Get the object id using handle
						oid = db.GetObjectId(False, Handle(int(handle, 16)), 0) 
						# Get the object from the database
						obj = t.GetObject(oid, OpenMode.ForRead)
						# Make sure obj is BlockReference
						if isinstance(obj, BlockReference):
							block_rot.append(obj.Rotation*57.2958)# Convert from radians to degrees
					# Commit changes to the database before ending transaction
					t.Commit()
	# if error occurs anywhere in the process catch it
	except:
		import traceback
		error_report = traceback.format_exc()
	# Report if this was successful...	
	if error_report is None:
		return block_rot
	else:
		return error_report
		
# Assign your output to the OUT variable
OUT = Task_Get_BlockRotation(IN[0])
1 Like

Works perfectly, had to subtract 180 from the rotation to match the output from the civil custom node.

Thanks