Hello,
I tried to make a script that will allow user to make rebars and fabric reinforcement elements visible or invisible. I get stuck to the part where i can’t set correctly in the cod the part witch make elements visible or invisible. Here is the code:
from pyrevit import revit, DB, forms
# Function to allow the user to select between Rebar and Fabric Reinforcement and toggle visibility
def select_reinforcement_type():
# Create a list of options to be displayed in the dialog
options = ['Rebar', 'Fabric Reinforcement']
# Show the selection dialog to the user
selected_options = forms.SelectFromList.show(options, "Select Reinforcement Type", multiselect=True)
# If user didn't select any option, return early
if not selected_options:
forms.alert("No option selected. Operation canceled.")
return
# Ask the user if they want to make the selected elements visible or invisible
visibility_option = forms.ask_for_one_item(
["Visible", "Invisible"],
"Select Visibility Option"
)
if visibility_option is None:
forms.alert("No visibility option selected. Operation canceled.")
return
# Get the current Revit document and active view
doc = revit.doc
view = doc.ActiveView
# List to store selected elements
elements = []
# Collect elements based on the user's selection
if 'Rebar' in selected_options:
# Get all rebar elements in the model from the correct namespace
elements.extend(DB.FilteredElementCollector(doc).OfClass(DB.Structure.Rebar).ToElements())
if 'Fabric Reinforcement' in selected_options:
# Get all fabric reinforcement elements in the model from the correct namespace
elements.extend(DB.FilteredElementCollector(doc).OfClass(DB.Structure.FabricReinforcement).ToElements())
# If no elements found, notify the user
if not elements:
forms.alert("No elements of selected type found in the model.")
return
# Start a transaction to modify element visibility
with revit.Transaction("Toggle Element Visibility"):
for element in elements:
# Create an element override in the current view
if visibility_option == "Visible":
# Override visibility of element to be visible in this view
view.SetElementOverrides(element.Id, DB.OverridesVisibility(True))
elif visibility_option == "Invisible":
# Override visibility of element to be invisible in this view
view.SetElementOverrides(element.Id, DB.OverridesVisibility(False))
# Notify the user of the operation
forms.alert("Visibility of selected elements has been updated.")
# Run the function
select_reinforcement_type()
Hi,
You are setting your visibility_option variable to the text ‹visible› or …
What you want is to set it for a parameter, right ?
So you need to get that parameter by its name and then set its value
Hello,
Your metod helped me to achieve what i wanted. Thank you verry much.
Here is the complete code, feel free to used it
from pyrevit import revit, DB, forms
# Function to allow the user to select between Rebar and Fabric Reinforcement and toggle visibility
def select_reinforcement_type():
# Create a list of options to be displayed in the dialog
options = ['Structural Rebar', 'Structural Fabric Reinforcement']
# Show the selection dialog to the user
selected_options = forms.SelectFromList.show(options, "Select Reinforcement Type", multiselect=True)
# If user didn't select any option, return early
if not selected_options:
forms.alert("No option selected. Operation canceled.")
return
# Ask the user to select visibility option, defaulting to "Visible"
visibility_option = forms.ask_for_one_item(
["Visible", "Invisible"], # List of options (only one can be selected)
title="Set Visibility", # Title of the dialog
default="Visible"
)
# If no option is selected (None), default to "Visible"
if visibility_option is None:
visibility_option = "Visible"
# Get the current Revit document and active view
doc = revit.doc
view = doc.ActiveView
# Initialize the element collector
elements = []
# Collect Rebar elements if selected
if 'Structural Rebar' in selected_options:
rebar_elements = DB.FilteredElementCollector(doc).OfCategory(DB.BuiltInCategory.OST_Rebar).WhereElementIsNotElementType().ToElements()
elements.extend(rebar_elements)
# Collect Fabric Reinforcement elements if selected
if 'Structural Fabric Reinforcement' in selected_options:
fabric_reinforcement_elements = DB.FilteredElementCollector(doc).OfCategory(DB.BuiltInCategory.OST_FabricReinforcement).WhereElementIsNotElementType().ToElements()
elements.extend(fabric_reinforcement_elements)
# If no elements are found in the selected categories
if not elements:
forms.alert("No elements found in the selected categories.")
return
# Start a transaction to modify element visibility
with revit.Transaction("Toggle Element Visibility"):
for element in elements:
if visibility_option == "Visible":
# Set element to visible in the view
element.SetUnobscuredInView(view, True)
elif visibility_option == "Invisible":
# Set element to invisible in the view
element.SetUnobscuredInView(view, False)
# Notify the user of the operation
forms.alert("Visibility of selected elements has been updated.")
# Run the function
select_reinforcement_type()