Set Detail Number - Avoid Error

Hello Dynamo Friends :slight_smile:

I want to set the Detail Number of a view and if it is already in use i don´t want to get this error:

image

I want the code to don´t set the number if it is not possible, but i dont want this error to show up.
Is this possible somehow? My attempt with Try/Except did not work, i get the error again.

TransactionManager.Instance.EnsureInTransaction(doc)

for viewport in viewports:
	detailnumber = viewport.get_Parameter(BuiltInParameter.VIEWPORT_DETAIL_NUMBER)
	try:
		NewNumber = detailnumber.Set("5")
		OUT = outlist.append(NewNumber)
	except:
		message = "fail"
		OUT = outlist.append(message)
		
TransactionManager.Instance.TransactionTaskDone()

If it is not possible i have to check the numbers before and build some logic for that. Just avoiding this error would be easier.

2 Likes

@gerhard.p ,

i think you have to do something like increment your numbers

liker here:

grafik

KR

Andreas

I don’t think ‘try, except increase and try again’ is the right process here.

Get all the details, find the last one, add one to that, and start the sequence there. A single quantifiable action vs many potentially failed actions.

2 Likes

I had the problem that a failed attempt for setting the detail number will kill the whole dynamo script, even undoing tasks that already worked successfull.
Yes, I will check a list of the current numbers before setting new ones. Avoiding the error sould be an additional safety net, but if it’s not possible i have to live without it.

Thanks for your replies :slight_smile:

I now just use an if x not in y statement to make sure the detail numbers don´t already exist.

Final_DetailNumbers = []
for i in numbers:
	if str(i) not in Filtered_DetailNumbers:
		Final_DetailNumbers.append(str(i))

Full code:

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

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

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

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

if isinstance(IN[0],list):
	New_Views = UnwrapElement(IN[0])
else:
	New_Views = [UnwrapElement(IN[0])]

New_View_DetailNumbers = []

for New_View in New_Views:
	ID = New_View.SheetId
	New_View_DetailNumber = New_View.get_Parameter(BuiltInParameter.VIEWPORT_DETAIL_NUMBER).AsString()
	try:
		integer=int(New_View_DetailNumber)
		New_View_DetailNumbers.append(integer)
	except:
		pass
		
Sheet = doc.GetElement(ID)
viewIDs = Sheet.GetAllPlacedViews()
all_viewports = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Viewports).WhereElementIsNotElementType().ToElements()

Current_DetailNumbers = []

for viewport in all_viewports:
	viewportId = viewport.ViewId
	for viewID in viewIDs:
		if viewID == viewportId:
			Current_DetailNumber = viewport.get_Parameter(BuiltInParameter.VIEWPORT_DETAIL_NUMBER).AsString()
			try:
				integer=int(Current_DetailNumber)
				Current_DetailNumbers.append(integer)
			except:
				pass
		
Filtered_DetailNumbers = []		

for Current_DetailNumber in Current_DetailNumbers:
	if Current_DetailNumber not in New_View_DetailNumbers:
		Filtered_DetailNumbers.append(Current_DetailNumber)
if Filtered_DetailNumbers:
	max=int(max(Filtered_DetailNumbers))
else:
	max=0

n = 100
numbers = []
for i in range(max+1, n+1):
    numbers.append(i)
    
Final_DetailNumbers = []
for i in numbers:
	if str(i) not in Filtered_DetailNumbers:
		Final_DetailNumbers.append(str(i))

for V, f in zip(New_Views, Final_DetailNumbers):
	V.get_Parameter(BuiltInParameter.VIEWPORT_DETAIL_NUMBER).Set(f)
	
OUT = New_View_DetailNumbers,Current_DetailNumbers, Filtered_DetailNumbers, Final_DetailNumbers, New_Views