Py Revit Json checkmark

Py Revit Json checkmark

makan.sabeti
Observer Observer
348 Views
1 Reply
Message 1 of 2

Py Revit Json checkmark

makan.sabeti
Observer
Observer

I am creating a pop up where a user can save category, instance parameter, and type parameter. I save the selections with the values in a json. I then prompt the user to name their selection so that next time they can see what they saved and load it in if they like. The issue is that when a user selects the previous saved selections it does not check mark off the selections made, even though it reads in the json. The following is my code and I believe the issue is from function select_manual_check: 

# Function to load selection from JSON
def load_selection_from_json(selection_name)

 

    json_path = os.path.join(parameter_sets_dir"{}.json".format(selection_name))
    with open(json_path"r"as json_file:
        return json.load(json_file)

 

# Function to save selection to JSON
def save_selection_to_json(selection_namecategoriesinstance_parameterstype_parameters)

 

    json_path = os.path.join(parameter_sets_dir"{}.json".format(selection_name))
    with open(json_path"w"as json_file:
        json.dump({
            "categories"categories,
            "instance_parameters"instance_parameters,
            "type_parameters"type_parameters
        }, json_file)

 

# Function to collect model categories
def get_model_categories():
    return [cat.Name for cat in doc.Settings.Categories if cat.CategoryType == CategoryType.Model]

 

# Function to collect elements from selected categories
def collect_elements(categories)

 

    elements = []
    for category_name in categories:
        category = next((cat for cat in doc.Settings.Categories if cat.Name == category_name), None)
        if category:
            bic = System.Enum.Parse(BuiltInCategory, category.Id.IntegerValue.ToString())
            elements.extend(FilteredElementCollector(doc).OfCategory(bic).WhereElementIsNotElementType().ToElements())
    return elements

 

# Function to display selection dialog with manual check mark processing
def select_with_manual_check(optionschecked_optionstitle)

 

    options_with_checks = [(optopt in checked_optionsfor opt in sorted(options)]
    checked = forms.SelectFromList.show(
        [opt[0for opt in options_with_checks],
        #checked_options,
        title=title,
        multiselect=True,
        button_name="Select"
    )
    #checked = forms.BaseCheckBoxItem(checked_options)
    return checked

 

# Main function to run the selection
def main():
    # List saved selection sets
    saved_selections = [f.replace(".json"""for f in os.listdir(parameter_sets_dirif f.endswith(".json")]
   
    if saved_selections:
        selection_names = saved_selections + ["Create New Selection"]
        selected_name = forms.SelectFromList.show(
            sorted(selection_names),
            title="Select a Saved Selection or Create New",
            multiselect=False,
            button_name="Select"
        )
    else:
        forms.alert("No saved selections found. Creating a new selection..."exitscript=False)
        selected_name = "Create New Selection"

 

    # Initialize empty lists for selection if creating a new one
    model_categories_selected = []
    instance_parameter_to_select = []
    type_parameter_to_select = []

 

    # Load existing selection set if one is chosen
    if selected_name != "Create New Selection":
        loaded_selection = load_selection_from_json(selected_name)
        model_categories_selected = loaded_selection.get("categories", [])
        instance_parameter_to_select = loaded_selection.get("instance_parameters", [])
        type_parameter_to_select = loaded_selection.get("type_parameters", [])

 

    # Model Category Selection
    model_category_names = get_model_categories()
    selected_categories = select_with_manual_check(model_category_namesmodel_categories_selected"Select Model Categories")
   
    # Collect elements from the selected categories
    elements = collect_elements(selected_categories)

 

    # Instance Parameter Selection with Manual Check
    unique_instance_parameter_names = {p.Definition.Name for e in elements for p in e.Parameters}
    selected_instance_parameters = select_with_manual_check(unique_instance_parameter_namesinstance_parameter_to_select"Select Instance Parameters")

 

    # Type Parameter Selection with Manual Check
    unique_type_parameter_names = set()
    for element in elements:
        element_type = doc.GetElement(element.GetTypeId())
        if element_type:
            unique_type_parameter_names.update(p.Definition.Name for p in element_type.Parameters)
   
    selected_type_parameters = select_with_manual_check(unique_type_parameter_namestype_parameter_to_select"Select Type Parameters")

 

    # Save the selection if needed
    selection_name = forms.ask_for_string(prompt="Enter a name for this selection:"title="Save Selection")
    if selection_name:
        save_selection_to_json(selection_nameselected_categoriesselected_instance_parametersselected_type_parameters)

 

# Run main
main()
0 Likes
349 Views
1 Reply
Reply (1)
Message 2 of 2

jeremy_tammik
Alumni
Alumni

So, your question has nothing to do with the Revit API, does it? Maybe it would be better to raise this in a forum discussing Python, or the UI functionality that you are using?

  

Also, I do not see any function in your code named select_manual_check...

  

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