Hey, so i have a small script where i want to assign a material parameter to geometry in my revit family, and afterwards reload it into my document.
however I seem to be unable to save the document as it tells me:
Can anyone help me? the code is below:
import clr
import sys
import os
# import Revit API
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
# import Revit API UI
clr.AddReference('RevitAPIUI')
# import Revit Services
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
# import ProtoGeometry
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
dmi = DocumentManager.Instance #setting up document manager instance
doc = dmi.CurrentDBDocument #Gets current document
uiapp = dmi.CurrentUIApplication #gets current Revit app
uidoc = uiapp.ActiveUIDocument.Document
app = uiapp.Application #gets current UI app
#The inputs to this node will be stored as a list in the IN variables.
# Use Dynamo to get the family types you want to edit
# For example, let's say you have a list of FamilyTypes called `familyTypes`
familyTypes = IN[0]
# Define the parameter name you want to use to drive the material
parameterName = "Local_Material_Parameter"
# Loop through each FamilyType
for familyType in familyTypes:
# Get the FamilySymbol for the FamilyType
familySymbol = doc.GetElement(ElementId(familyType.Family.Id))
# Close any open transactions
TransactionManager.Instance.TransactionTaskDone()
TransactionManager.Instance.ForceCloseTransaction()
# Open the family document
familyDoc = uidoc.EditFamily(familySymbol)
TransactionManager.Instance.EnsureInTransaction(familyDoc)
# Get the FamilyManager and create the parameter
familyManager = familyDoc.FamilyManager
try:
parameter = familyManager.AddParameter(parameterName, BuiltInParameterGroup.INVALID, ParameterType.Material, False)
except:
try:
parameter = familyManager.Parameter(parameterName)
except:
pass
# Find all the geometry elements in the family document
solids = FilteredElementCollector(familyDoc).OfCategory(BuiltInCategory.OST_GenericModel).WhereElementIsNotElementType().ToElements()
# Loop through each Solid and assign the parameter to its geometry elements
for solid in solids:
try:
# Assign the parameter to the Solid object
solid.SetMaterialId(parameter.Id)
except:
pass
# Commit the transaction and close the family document
TransactionManager.Instance.TransactionTaskDone()
# Define the save as options
saveAsOptions = SaveAsOptions()
saveAsOptions.OverwriteExistingFile = True
# Get docname of familydoc
familyDocPath= familyDoc.PathName
tempDir = os.environ['LOCALAPPDATA'] + '\Temp'
if not os.path.exists(tempDir):
os.makedirs(tempDir)
tempFilePath = os.path.join(tempDir, os.path.basename(familyDocPath))
familyDoc.SaveAs(tempFilePath,saveAsOptions)
familyDoc.Close(False) #!!!!!!!!!!!!!!!!!LINE 84!!!!!!!!!!!!!!!!!!!!!!!!!!!!LINE 84!
TransactionManager.Instance.EnsureInTransaction(doc)
# Load the edited family into the current document
uidoc.LoadFamily(tempFilePath)
# Close any open transactions
TransactionManager.Instance.TransactionTaskDone()