0

I've created a shared parameter via Revit 2024 API and bind the parameter with Sheets, Views and Schedules. And later when I tried to set value for the instances with the shared parameters, the Schedules instances are fine. However, instances of Sheets or Views threw an exception because the shared parameter is read only. I've done some research saying there are some limitations when setting value for some type's shared parameters, especially for built in type. But since it works for Schedules, I'm wondering if there is a walk around for Views and Sheets. Below is how I create the shared parameters and set its values.

    private void CreateSharedParameter(UIApplication uiApp, Document doc, string groupName, string paramName)
{

    string sharedParamFile = @"C:\Program Files\Autodesk\OrganizationSharedParams.txt"; 

    if (!File.Exists(sharedParamFile))
    {
        File.Create(sharedParamFile).Dispose();
    }

    // Set the shared parameter file
    uiApp.Application.SharedParametersFilename = sharedParamFile;

    // Open the shared parameter file
    DefinitionFile defFile = uiApp.Application.OpenSharedParameterFile();
    if (defFile == null)
    {
        TaskDialog.Show("Error", "Shared parameter file could not be opened.");
        return;
    }

    DefinitionGroup defGroup = defFile.Groups.get_Item(groupName) ?? defFile.Groups.Create(groupName);

    // Create the definition
    ExternalDefinitionCreationOptions options = new ExternalDefinitionCreationOptions(paramName, SpecTypeId.String.Text)
    {
        Visible = true,
        UserModifiable = true
    };

    Definition definition = defGroup.Definitions.get_Item(paramName) ?? defGroup.Definitions.Create(options);

    // Bind to ViewSheet category
    Category sheetCat = doc.Settings.Categories.get_Item(BuiltInCategory.OST_Sheets);
    Category viewCat = doc.Settings.Categories.get_Item(BuiltInCategory.OST_Views);
    Category scheduleCat = doc.Settings.Categories.get_Item(BuiltInCategory.OST_Schedules);

    CategorySet catSet = uiApp.Application.Create.NewCategorySet();
    catSet.Insert(sheetCat);
    catSet.Insert(viewCat);
    catSet.Insert(scheduleCat);

    // Instance binding (vs. Type binding)
    InstanceBinding binding = uiApp.Application.Create.NewInstanceBinding(catSet);

    // Add parameter to the document
    BindingMap bindingMap = doc.ParameterBindings;

    using (Transaction tx = new Transaction(doc, "Add Shared Parameter"))
    {
        tx.Start();
        if (!bindingMap.Contains(definition))
        {
            bindingMap.Insert(definition, binding, GroupTypeId.IdentityData);
        }
        tx.Commit();
    }
}

    public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{

    Document doc = commandData.Application.ActiveUIDocument.Document;

    CreateSharedParameter(commandData.Application, doc, "ViewOrganization", "ProjectBrowser.Group");

    // Get all View instances in the document
    FilteredElementCollector viewCollector = new FilteredElementCollector(doc)
        .OfClass(typeof(View))
        .WhereElementIsNotElementType();

    // Get all ViewSchedule instances in the document
    FilteredElementCollector scheduleCollector = new FilteredElementCollector(doc)
        .OfClass(typeof(ViewSchedule))
        .WhereElementIsNotElementType();

    // Get all ViewSheet instances in the document
    FilteredElementCollector sheetCollector = new FilteredElementCollector(doc)
        .OfClass(typeof(ViewSheet))
        .WhereElementIsNotElementType();


    using (Transaction acTrans = new Transaction(doc, "SetSharedParamValue"))
    {
        acTrans.Start();


        foreach (Element element in scheduleCollector)
        {
            Parameter group = element.LookupParameter("Organization.ProjectBrowser.Group");
            if (group != null)
            {
                string disValue = group.HasValue ? group.AsValueString() : "";
                Parameter sharedGroup = element.LookupParameter("Organization.ProjectBrowser.Group1");
                if (sharedGroup != null)
                {
                    sharedGroup.Set(disValue); // will throw an exception if it's ViewSheet or View
                }
            }

            Parameter subGroup = element.LookupParameter("Organization.ProjectBrowser.Subgroup");
            if (subGroup != null)
            {
                string disValue = subGroup.HasValue ? subGroup.AsValueString() : "";
                //Definition d = 
                Parameter sharedSubGroup = element.LookupParameter("Organization.ProjectBrowser.Subgroup1");
                if (sharedSubGroup != null)
                {
                    sharedSubGroup.Set(disValue);
                }
            }
        }


        acTrans.Commit();
    }

    return Result.Succeeded;
}

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.