Hi all,
I have created a script to duplicate views, add prefix and apply a view template. It work for the first time, if I want to duplicate the view again it will just override the first duplicated view. If I want to duplicate more views I have to close dynamo and run the script again.
Is there a way to make it work without having to close and open dynamo again?
For repeated production use I recommend you utilize Dynamo Player instead; there are no element bindings in that environment so things will run quicker.
You can ensure content avoids cross-session element binding via function passing.
You can ensure content avoids in-session element binding by this bit of Python:
import clr #add the common language runtime (clr) so we can access .net libraries in the Python environment
clr.AddReference("RevitNodes") #add the Revit Nodes dynamo tools to the CLR
import Revit #import the Revit namespace
clr.ImportExtensions(Revit.Elements) #add the RevitElements extention to the CLR
clr.AddReference("RevitServices") #add the RevitServices namespace to the CLR
import RevitServices #import the Revit services namespace to the Python environment
from RevitServices.Persistence import DocumentManager #import the document manager class to the Python environment
from RevitServices.Transactions import TransactionManager #import the transaction manager class to the Python environment
clr.AddReference("RevitAPI") #add the Revit API to the CLR
import Autodesk #import the autodesk namespace to the Python environment
from Autodesk.Revit.DB import Document, ElementId, ElementTransformUtils, XYZ #import the necessary classes of the Revit API
from System.Collections.Generic import List as cList #import the .net List class as cList
doc = DocumentManager.Instance.CurrentDBDocument #get the active document
repressBindings = IN[1] #an input from the Dynamo environment to repress bindings or not
net = []
elems = IN[0]
if elems == None: #if the content if IN[0] from the Dynamo namespace is null
OUT = None #return nothing
else: #otherwise
if repressBindings: #if repress bindings is true
elems = UnwrapElement(elems) #convert the elements from Dynamo elements to native Revit elements
elemIds = [e.Id for e in elems] #get the element ids
elemIdList = cList[ElementId](elemIds) #convert the element ids to a .net list
TransactionManager.Instance.EnsureInTransaction(doc) #start a transaction
copies = ElementTransformUtils.CopyElements(doc,elemIdList,XYZ(0,0,0)) #copy the existing elements by a 0 length vetor
deletion = [doc.Delete(i) for i in elemIdList] #delete the elements provided by Dynamo
newElems = [doc.GetElement(i) for i in copies] #get the newly copied elements from their element ID and repress in-session binding
results = [i.ToDSType(True) for i in newElems]
TransactionManager.Instance.TransactionTaskDone() #end the transaction
OUT = results #set the output to the list of elements with no bindings
else: #otherwise
OUT = elems #return the lsit of elements