- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Thanks for your time viewing this. My first foray into API development (not even sure if the terms I'm using are correct) so please forgive any ignorance of Forum etiquette I might not be aware of (first post here too I think). Cheers.
I'm trying to build a macro to take a bunch of families in a folder, open each file and remove any line pattern definitions that match provided criteria (I'll add further clean-up duties to the script later hopefully). I ended up using Dynamo to generate the list of files and search criteria as that seemed easy to do, but decided it was best to then pass that info to a Python script to process the files (open, make changes, close, repeat). I can get different parts of the macro to work on their own, but having trouble putting it all together. I don't have the experience to understand what I'm doing wrong and am hoping it will be obvious to someone here.
# Load the Python Standard and DesignScript Libraries
import sys
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
app = doc.Application
# The inputs to this node will be stored as a list in the IN variables.
input_paths = IN[0]
input_patterns = IN[1]
open_options = OpenOptions() #Creating a new OpenOptions object
#Creating an empty list to contain output
removed = []
for path in input_paths: #Iterating through each filepath string
filepath = FilePath(path) #Creating a Revit FilePath object from this
family_doc = app.OpenDocumentFile(filepath, open_options) #Telling the Revit application to open the file we specify)
line_patterns = FilteredElementCollector(family_doc).OfClass(LinePatternElement).ToElements()
#create list of line patterns to delete
nameIdDict = {}
#keylist = [IN[1]] #keylist is a list of pattern names to be deleted if existing
#removed = []
removed.append("file {}".format(path))
for i in line_patterns:
if i.Name in input_patterns:
nameIdDict[i.Name] = i.Id
removed.append("Found {} line patterns to delete".format(len(nameIdDict)))
#remove line patterns from document
TransactionManager.Instance.EnsureInTransaction(family_doc)
for name, id in nameIdDict.items():
removed.append("Will delete line pattern {}, ".format(name))
removed.append("with ID {}".format(id))
family_doc.Delete(id)
TransactionManager.Instance.TransactionTaskDone()
family_doc.Close(False)
#Finally, closing the document (False = without saving)
OUT = removed #Outputting the list of patterns deleted:
Solved! Go to Solution.
Developer Advocacy and Support +