Problem using Python script in Dynamo to change multiple family files

Problem using Python script in Dynamo to change multiple family files

SDL-chris
Explorer Explorer
660 Views
2 Replies
Message 1 of 3

Problem using Python script in Dynamo to change multiple family files

SDL-chris
Explorer
Explorer

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

:

 

SDLchris_1-1729051718645.png

 

0 Likes
Accepted solutions (1)
661 Views
2 Replies
Replies (2)
Message 2 of 3

jeremy_tammik
Alumni
Alumni
Accepted solution

Dear Chris,

  

Welcome to the Revit API and congratulations on getting all your bits and pieces to work individually.

  

Your script looks fine to me.

  

However, unfortunately, it is hard for me to say anything about why it might not work, since I am not conversant in Python or Dynamo. This forum is dedicated to the pure .NET Revit API. Dynamo provides a wrapper around that. To discuss Dynamo-specific questions, you will probably be better of raising this in the Dynamo forum:

  

   

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 3 of 3

SDL-chris
Explorer
Explorer

Thanks Jeremy, the redirection was a big help and I got it resolved on that site

 

0 Likes