Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How can I extract categories of project parameters?

2 REPLIES 2
Reply
Message 1 of 3
gminx2018
180 Views, 2 Replies

How can I extract categories of project parameters?

Hello, All! I am developing an addin feature that runs through a routine to get a base template converted over to any one of a handful of unique client configurations. For the step where standards are transferred, I've managed to copy everything except the project parameters. My initial approach was to iterate through the binding map of the recipient model then the donor model, filtering out duplicates. Then going the traditional route of setting the category set, creating the instance binding then inserting into the document binding map. However, one thing I can't quite work out is how to extract the categories from the donor parameters. I thought I was being clever and referenced a SharedParameterElement list to grab the category of the parameter when the name matched, but it turns out the category property of each SharedParameterElement in the list is null. This seems wrong because when I open the model and look in project parameters, they clearly have categories assigned. This prevents the creation of the parameter in the recipient model since doc.ParameterBindings.Insert() requires the Instance Binding item to have non-null categories in its set. I feel like there is a very simple solution that I'm just not seeing here. Any help would be appreciated!

 

nullcat.png

 

 

            var sharedParams = new FilteredElementCollector(masterDoc).OfClass(typeof(SharedParameterElement)).Select(x => x as SharedParameterElement).ToList();

            SubTransaction projParamCopy = new SubTransaction(doc);
            try
            {
                BindingMap bm = doc.ParameterBindings;
                DefinitionBindingMapIterator itor = bm.ForwardIterator();
                itor.Reset();
                while (itor.MoveNext())
                {
                    Definition d = itor.Key;
                    paramNames.Add(d.Name);

                }
                ;//breakpoint
                projParamCopy.Start();
                BindingMap masterBM = masterDoc.ParameterBindings;
                DefinitionBindingMapIterator masterItor = masterBM.ForwardIterator();
                masterItor.Reset();
                while (masterItor.MoveNext())
                {
                    Definition d = masterItor.Key;
                    if (!paramNames.Any(x => x == d.Name))
                    {
                        var param = sharedParams.FirstOrDefault(x => x.Name == d.Name);
                        Category cat = param.Category;
                        CategorySet catSet = uiapp.Application.Create.NewCategorySet();
                        catSet.Insert(cat);
                        InstanceBinding newIB = uiapp.Application.Create.NewInstanceBinding(catSet);
                        doc.ParameterBindings.Insert(d, newIB, d.ParameterGroup);

                    }
                }
                projParamCopy.Commit();
                ;//Breakpoint
            } catch (Exception ex)
            {
                TaskDialog.Show("Oops...", $"There was an issue creating the project parameters.\n\r\n\r{ex.Message}");
                projParamCopy.RollBack();
                return false;
            }

 

2 REPLIES 2
Message 2 of 3
jeremy_tammik
in reply to: gminx2018

Yes, the category property of each SharedParameterElement is null, because that is inherited from the Element class and makes no sense in this context.  You would need a way to extract the list of categories associated with the master document bindings.  Off-hand, I don't see a way to achieve that.

   

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
Message 3 of 3
mhannonQ65N2
in reply to: gminx2018

The category bindings are stored in the document's ParameterBindings property. To get the categories for a parameter, you would do something like this:

var binding = (ElementBinding)document.ParameterBindings[myParameter.GetDefinition()];
var categories = binding.Categories;

 

To convert the categories variable into a more usable form, you could do this: 

var categoriesArray = categories.Cast<Category>().ToArray();

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Forma Design Contest


Rail Community