Off the back of @SeanP 's great post here. I am trying to not only export subcategories but change the name of any it encounters called “Escape Lines” and rename them to “Escape”.
#Loop each line to get the Subcategory or Object Style of the Line
for line in lines:
sub = line.LookupParameter("Subcategory").AsElementId()
#Get the Name of the Object Style
subname = famDoc.GetElement(sub).Name
if subname.lower() == "escape lines":
TransactionManager.Instance.EnsureInTransaction(famDoc)
famDoc.GetElement(sub).Name = "Escape"
TransactionManager.Instance.TransactionTaskDone()
TransactionManager.Instance.ForceCloseTransaction()
#Add the List of Object Syles for that Family to the Overall List
ObjectStyles.append(subs)
#Close the Family Document without saving
famDoc.Close(False)
But I’m getting the error:
Is this the computer basically saying no to setting a subcategories name? If so, I don’t accept it if so
here a workaround, by creating a new one and delete the old
import clr
import sys
import System
#
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
import Autodesk.DesignScript.Geometry as DS
#import Revit API
clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *
import Autodesk.Revit.DB as DB
#import net library
from System import Array
from System.Collections.Generic import List, IList, Dictionary
#import transactionManager and DocumentManager (RevitServices is specific to Dynamo)
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument
def change_line_style_name(old_style_name, new_style_name):
"""
This function changes the line style name from old_style_name to new_style_name.
Parameters:
old_style_name (str): The name of the old line style.
new_style_name (str): The name of the new line style.
Returns:
list: A list of lines with the updated line style.
"""
old_lineSubCat = next((subcat for subcat in doc.OwnerFamily.FamilyCategory.SubCategories if subcat.Name == old_style_name), None)
if old_lineSubCat is None:
return "Category '{}' not found".format(old_style_name)
else:
TransactionManager.Instance.EnsureInTransaction(doc)
new_lineSubCat = next((subcat for subcat in doc.OwnerFamily.FamilyCategory.SubCategories if subcat.Name == new_style_name), None)
if new_lineSubCat is None:
new_lineSubCat = doc.Settings.Categories.NewSubcategory(doc.OwnerFamily.FamilyCategory, new_style_name)
# copy properties to the the new category
new_lineSubCat.LineColor = old_lineSubCat.LineColor
for i in System.Enum.GetValues(GraphicsStyleType):
try:
new_lineSubCat.SetLinePatternId(old_lineSubCat.GetLinePatternId(i), i)
new_lineSubCat.SetLineWeight(old_lineSubCat.GetLineWeight(i), i)
except Exception as ex:
print(ex)
lines = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Lines).WhereElementIsNotElementType().ToElements()
#
for line in lines:
para = line.get_Parameter(BuiltInParameter.FAMILY_CURVE_GSTYLE_PLUS_INVISIBLE)
graphicStyle = line.LineStyle
line.LineStyle = new_lineSubCat.GetGraphicsStyle(graphicStyle.GraphicsStyleType)
# delete the old line style
doc.Delete(old_lineSubCat.Id)
TransactionManager.Instance.TransactionTaskDone()
return lines
old_style_name = IN[0]
new_style_name = IN[1]
if doc.IsFamilyDocument :
OUT = change_line_style_name(old_style_name, new_style_name)