python - get family type name from value in comments parameter

python - get family type name from value in comments parameter

ellliaz
Participant Participant
2,151 Views
5 Replies
Message 1 of 6

python - get family type name from value in comments parameter

ellliaz
Participant
Participant
# Load the Python Standard and DesignScript Libraries
import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

import clr
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 *
from Autodesk.Revit.UI.Selection import *
clr.AddReference("RevitAPIUI")
from Autodesk.Revit.UI import TaskDialog

import random
import string

doc = DocumentManager.Instance.CurrentDBDocument
uidoc = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument
selection = uidoc.Selection
comments_structural_framing = IN[0]
structural_framing_family = IN[1]

# Get the selected detail item family
ref = selection.PickObject(ObjectType.Element, "Select a detail item family")
detail_item_family = doc.GetElement(ref)

# Start a transaction
TransactionManager.Instance.EnsureInTransaction(doc)

text_parameters = [p for p in detail_item_family.Parameters if p.StorageType == StorageType.String]

# Get the parameter with the specified name
parameter = None
for p in text_parameters:
    if p.Definition.Name == comments_structural_framing:
        parameter = p
        break
      
if parameter is not None and parameter.StorageType == StorageType.ElementId:
    family_id = parameter.AsElementId()
    family = doc.GetElement(family_id)
    if family.Category.Id.IntegerValue == (int) (BuiltInCategory.OST_StructuralFraming):
        OUT = family.Name
    else:
        OUT = "The value of the specified parameter does not correspond to a structural framing family."
else:
    OUT = "Parameter not found or has invalid type"

# Commit the transaction
TransactionManager.Instance.TransactionTaskDone()

# Display a message
TaskDialog.Show("Success", "The name of the structural framing family is: {}".format(OUT))# 

Hello everyone,

 

in this script i'm trying to pick a detail item family.

if the text_parameter name of the detail item is p.Definition.Name == comments_structural_framing i need to find the type name of the corresponding structural framing family. i then need to write this type name to the value of the text parameter in the detail item family. 

 

apparently is not finding any p.Definition.Name == comments_structural_framing so it skip to "Parameter not found or has invalid type". 

 

 

I know it's a little crazy but i hope you can help! 

 

thanks 

Accepted solutions (1)
2,152 Views
5 Replies
Replies (5)
Message 2 of 6

Yien_Chao
Advisor
Advisor

"i need to find the type name of the corresponding structural framing family"

 

if i understand well, you want to find the type in the class OST_StructuralFraming with the corresponding name of a detail family?

Message 3 of 6

ellliaz
Participant
Participant

Yes. Where the parameters name of the detail family are equal to the comments parameters of the structural framing family. 

 

Message 4 of 6

Yien_Chao
Advisor
Advisor
Accepted solution

i suggest to create a dictionary.

 

get your detail element names.

then create a dictionary of your OST_StructuralFraming, comments : name of type

 

then create  loop on your detail item's name and search in the dictionary.

 

hope that might help.

Message 5 of 6

ellliaz
Participant
Participant

smart! i'll try that, thanks 😉 

Message 6 of 6

ellliaz
Participant
Participant

and it worked! thanks 😁

 

 

# 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
import clr # import Common Language Runtime

from Autodesk.Revit.DB import *
from Autodesk.Revit.UI.Selection import *

doc = DocumentManager.Instance.CurrentDBDocument

# Get all Structural Framing elements
structural_framing_collector = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_StructuralFraming).WhereElementIsNotElementType()

# Create an empty dictionary to store the comments and names of the Structural Framing elements
structural_framing_dict = {}

# Loop through each Structural Framing element and add its comments and name to the dictionary
for element in structural_framing_collector:
    comments = element.get_Parameter(BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS).AsString()
    name = element.Name
    if comments and comments != "":
        structural_framing_dict[comments] = name

# Get all Detail Item elements
detail_item_collector = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_DetailComponents).WhereElementIsNotElementType()

# Start a transaction
TransactionManager.Instance.EnsureInTransaction(doc)

# Loop through each Detail Item element and compare its text parameters with the names in the dictionary
for element in detail_item_collector:
    for key in structural_framing_dict.keys():
        param = element.LookupParameter(key)
        if param and param.StorageType == StorageType.String:
            param.Set(structural_framing_dict[key])

# Commit the transaction
TransactionManager.Instance.TransactionTaskDone()