Python code missing somthing?

Hi, I have this python code i was meant to insert into a python script node. I cleaned all the white space and replaced with tabs but i am getting no values when i insert all categories of doors. Can anyone possibly tell me why? Im using dynamo 1.2 and revit 2017.2. The Python Script is meant to pull and list information from the doors, mainly to and from room info.

Copyright© 2017, Konrad K Sobon

@arch_laboratory, http://archi-lab.net

import clr
# Import RevitAPI
clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *

# Import DocumentManager and TransactionManager
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

# Import ToDSType(bool) extension method
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
 
# Start Transaction
doc = DocumentManager.Instance.CurrentDBDocument

fam_inst = IN[0]

elements = []
for i in IN[0]:
	elements.append(UnwrapElement(i))

def TryGetToRoom(room, phase):
	try:
		toRoom = room.get_ToRoom(phase)
	except:
		toRoom = None
		pass
	return toRoom

def TryGetFromRoom(room, phase):
	try:
		fromRoom = room.get_FromRoom(phase)
	except:
		fromRoom = None
		pass
	return fromRoom

room_number, room_name, doors, room = [], [], [], []
for i in elements:
	for phase in doc.Phases:
		if i.CreatedPhaseId == phase.Id:
			for phase2 in doc.Phases:
				if TryGetToRoom(i, phase2) != None:
					to_room = TryGetToRoom(i, phase2)
					break
				else:
					to_room = None
			for phase3 in doc.Phases:
				if TryGetFromRoom(i, phase3) != None:
					from_room = TryGetFromRoom(i, phase3)
					break
				else:
					from_room = None

			if from_room == None and to_room == None:
				room_number.append("No to or from room")
				room_name.append("No to or from room")
				doors.append("No to or from room")
			elif to_room != None:
				Numbers = to_room.GetParameters("Number")
				Names = to_room.GetParameters("Name")
				for n in range(len(Numbers)):
					room_number.append(Numbers[n].AsString())
					room_name.append(Names[n].AsString())
				doors.append(i)
				room.append(to_room)
		  		
#Assign your output to the OUT variable
OUT = room_number, room_name, doors, room

Side note, how do i post code properly on here?

some code here
    indented code

Awesome, thanks Gui

Did it work before you played with it?

FYI, Python coding is pretty strict in relation to spaces or tabs.
You can use programs like Notepad++ or Atom to Strip & Replace whitespace etc and format it correctly.

There’s no OUT statement in the code, that’s probably why it’s not returning anything in Dynamo

You are actually missing this from first line:

# Copyright(c) 2017, Konrad K Sobon
# @arch_laboratory, http://archi-lab.net

You are welcome.

6 Likes

I got the code off here, never had anything like that in it.

Got it in there now for ya.

Thanks T, i added the out statement but now I have this error.

Warning: IronPythonEvaluator.EvaluateIronPythonScript operation failed.
unexpected token ‘’

The code was off youtube comments. Made sure i replaced spaces with tabs carefully. Double checked the code was the same space off the video i was watching too

It’s all right, I am not usually concerned too much if you were to take it and make changes or improvements and then post back, but this seems to be largely unchanged from the original. Anyways, this is the source: http://archi-lab.net/door-numbering-v2-0-for-dynamo-0-8-0/

This is the latest, last time I worked on this code:

# Copyright(c) 2017, Konrad Sobon
# @arch_laboratory, http://archi-lab.net

# Import RevitAPI
import clr
clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *

# Import DocumentManager and TransactionManager
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument

# Import ToDSType(bool) extension method
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)

filter = IN[1]
filter2 = IN[2]
fam_inst = IN[0]
phase = UnwrapElement(IN[3])

def TryGetToRoom(room, phase):
	try:
		toRoom = room.get_ToRoom(phase)
	except:
		toRoom = None
		pass
	return toRoom

def TryGetFromRoom(room, phase):
	try:
		fromRoom = room.get_FromRoom(phase)
	except:
		fromRoom = None
		pass
	return fromRoom

def GetFromToNames(door, phase, f = filter, f2 = filter2):
	fromName = ""
	fromRoom = TryGetFromRoom(door, phase)
	if fromRoom != None:
		fromName = fromRoom.get_Parameter(BuiltInParameter.ROOM_NAME).AsString()
	
	toName = ""
	toRoom = TryGetToRoom(door, phase)
	if toRoom != None:
		toName = toRoom.get_Parameter(BuiltInParameter.ROOM_NAME).AsString()
	
	# Filter2 - where doors are outswinging from electrical closets etc.
	if fromName != "" and any(fromName == x for x in filter2):
		return [door, fromRoom]
	
	if toName == "" or any(toName == x for x in filter):
		if fromName == "" or any(fromName == x for x in filter):
			return [door, toRoom]
		else:
			return [door, fromRoom]
	elif any(fromName == x for x in filter):
		return [door, toRoom]
	elif any(toName == x for x in filter):
		#return [door, fromRoom]
		return ["", ""]
	else:
		return [door, toRoom]
output = []
for door in IN[0]:
	output.append(GetFromToNames(UnwrapElement(door), phase))
					
#Assign your output to the OUT variable
OUT = output

I think I removed bunch of stuff. I am not sure why, but maybe it was not really needed down the line.

Good luck!

1 Like

Vanman,

How did you resolve the “unexpected token “”” error?
I’m having the same issue and this thread is the only place I can find it mentioned…

I have 1800 doors to renumber in the next couple days, so automation is my only hope!
Thanks,

W

I’m sorry I cant remember what happened with using this. I think I just reverted back to my door renumber with model line spline Door-Renumber-With-Prefix.dyn (16.4 KB)

I think it’s because you have a " somewhere that’s not sopposed to be there :slight_smile: