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: 

Sorting Parameters in the Family Editor

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
stever66
3416 Views, 5 Replies

Sorting Parameters in the Family Editor

I'm trying to sort some parameters in the family editor.  I have a Macro that adds all the parameters in the  shared parameter file to the family.  But the order they appear seems to be alphabetical.  I'd like to specify the order, using the SortParameters.  Possibly by using a list that has the parameter names in the right order. 

 

I've got everything working up until the sorting.  I can't figure out how to get the argument that SortParameters from a list of the parameter names.

 

Here is my code so far.

 

//UIDocument uiDoc = this.ActiveUIDocument;
            Document doc = this.ActiveUIDocument.Document;
            
            // insert the shared parameters from the file into the family document
            Category cat = doc.Settings.Categories.get_Item(BuiltInCategory.OST_LightingFixtures);
            DefinitionFile spFile = this.Application.OpenSharedParameterFile();
            FamilyManager famMan = doc.FamilyManager;
                foreach (DefinitionGroup dG in spFile.Groups)
                    {
                        var v = (from ExternalDefinition d in dG.Definitions select d);
                            
                            using (Transaction t = new Transaction(doc))
                            {
                            t.Start("Add Space Shared Parameters");
                            TaskDialog.Show("Revit""Start");
                            foreach (ExternalDefinition eD in v)
                                {
                                FamilyParameter fp = famMan.AddParameter (eD,BuiltInParameterGroup.PG_TEXT,false);
                                }//end foreach
                            t.Commit();
                            }//end using transaction
                                            
                    }// end foreach
                
                // go through the parameters to display their names and order
                IList<FamilyParameter> familyPar = famMan.GetParameters();
                String strNames = null;
                Dictionary<string, FamilyParameter> sortedPar = new Dictionary<string, FamilyParameter>();
                foreach (FamilyParameter fp in familyPar)
                    {
                    strNames = strNames + fp.Definition.Name.ToString() + "\n";
                    sortedPar.Add(fp.Definition.Name.ToString(), fp);
                    
                    }
                    TaskDialog.Show("Revit", strNames);
                                        
                //start sorting here
                var strings = new List<String> {"TYPE""STYLE""MOUNTING""LENS""LAMP WATTS""LAMP COUNT""LAMP MODEL""TOTAL VA'S"};
                famMan.SortParameters(strings); //this line gives an error, since it wants a "Revit.DB.ParametersOrder"

 

Any help would be appreciated.
                 

5 REPLIES 5
Message 2 of 6
jeremytammik
in reply to: stever66

Just as you noticed, the SortParameters method takes a ParametersOrder enumeration argument:

 

http://www.revitapidocs.com/2018.1/329ceb60-b9b5-d603-a23c-e9fcfc9d2f62.htm

 

The ParametersOrder enumeration only includes two values:

 

http://www.revitapidocs.com/2018.1/771bd717-9d4d-d36d-0948-94e2e73f392c.htm

 

Ascending Sort parameters are in ascending(A-Z) order.
Descending

Sort parameters are in descending(Z-A) order.

 

Those are the only options you have.

 

Specifying your own arbitrary custom sort order is not supported.

 

Please note that the Revit user interface does not allow this either.

 

The Revit API will almost never enable you to do anything that is not previously supported by the user interface.

 

Please take note of this; it applies to almost everything.

 

Thank you!

 

Cheers,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 3 of 6
stever66
in reply to: jeremytammik

Jeremy:  

thanks for the response.  Sorry, but The code I posted had sortParameters when I intended to change it to ReorderParameters.

 

The parameters can be reorederd any way in Revit, and I think the ReorderParameters method allows the same using the API.

 

ReorderParamaters requires an IList of parameters.   So I think I just need a way to get the parameter itself by using the parameters string name.

 

 

Message 4 of 6
jeremytammik
in reply to: stever66

http://www.revitapidocs.com/2018.1/f3e5375b-28d7-d6c6-ea49-bf6f6289fd9a.htm

 

ReorderParameters takes an IList of parameters, just as you say, specifying the new parameters order for the family. The contents of this collection should consist of exactly the same parameters returned by the GetParameters() method. 

 

So call GetParameters, re-order the list, and pass it back in to ReorderParameters.



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 5 of 6
stever66
in reply to: jeremytammik

That sounded simple enough, but it took me a while to implement. 

 

I don't think I ever found an example of using GetParameters to get a single parameter while in the Family Editor. 

 

I had lots of false starts, and at one point I even had every parameter and its name in a dictionary, and that actually worked.  Eventually, I realized I didn't need the dictionary, and I finally realized this was pretty easy.

 

Anyhow, I learned a lot.  And here is the result: 

 

A sharp macro that runs from within the family manager, and adds and sorts parameters from the shared parameters file.

(By the way, I realize there is probably a better way to sort, but this is the best I could come up with.)

 


         public void AddSharedParameters()
        {
            
            //UIDocument uiDoc = this.ActiveUIDocument;   //dont need this
            Document doc = this.ActiveUIDocument.Document;
            
            // insert the shared parameters from the file into the family document
            Category cat = doc.Settings.Categories.get_Item(BuiltInCategory.OST_LightingFixtures);  //change the category here
                    // if you are using something besides light fixtures
            DefinitionFile spFile = this.Application.OpenSharedParameterFile();
            FamilyManager famMan = doc.FamilyManager;
                foreach (DefinitionGroup dG in spFile.Groups)  //get each group in the shared parameter file 
                    {
                        var v = (from ExternalDefinition d in dG.Definitions select d);
                            
                            using (Transaction t = new Transaction(doc))
                            {
                            t.Start("Add Shared Parameters");
                            foreach (ExternalDefinition eD in v)  //get each parameter in the current group
                                {
                                FamilyParameter fp = famMan.AddParameter (eD,BuiltInParameterGroup.PG_GENERAL,false);  //change the
                                            //group here if you want the parameters in a different group
                    
                                }//end foreach
                            t.Commit();
                            }//end using transaction
                                            
                    }// end foreach
                
                // add the parameters to an Ilist
                IList<FamilyParameter> familyPar = famMan.GetParameters(); 
                
                //list all the parameters in order to show the orignal order
                String strNames = null;
                foreach (FamilyParameter fp in familyPar)
                    {
                    strNames = strNames + fp.Definition.Name.ToString() + "\n";
                    }
                    TaskDialog.Show("Original Order", strNames);
                                        
                //
                //    
                //start creating a list of sorted parameters here------------------------------------------------------------
                                
                IList<FamilyParameter> sortedfamilyPar = new List<FamilyParameter>();  // create an ILIST for the sorted parameters
                    
                foreach (FamilyParameter fp in familyPar)  //go through each entry and find "TYPE"
                                                           //and add it to the empty ILIST
                {
                    if(fp.Definition.Name.ToString () =="TYPE")
                    {
                        sortedfamilyPar.Insert(sortedfamilyPar.Count,fp);
                        break;
                    }
                }
                
                foreach (FamilyParameter fp in familyPar)
                {
                    if(fp.Definition.Name.ToString () =="STYLE")  //find STYLE and add it second
                    {
                        sortedfamilyPar.Insert(sortedfamilyPar.Count,fp);
                        break;
                    }
                }
                
                foreach (FamilyParameter fp in familyPar)  //...
                {
                    if(fp.Definition.Name.ToString () =="MOUNTING")
                    {
                        sortedfamilyPar.Insert(sortedfamilyPar.Count,fp);
                        break;
                    }
                }
                
                foreach (FamilyParameter fp in familyPar)
                {
                    if(fp.Definition.Name.ToString () =="LENS")
                    {
                        sortedfamilyPar.Insert(sortedfamilyPar.Count,fp);
                        break;
                    }
                }                        
                 foreach (FamilyParameter fp in familyPar) //the list must contain all parameters,
                                                          //even if they are not visible to the user
                                                          //add all other parameters in familyPar to sortedfamilyPar
                {
                    if(!sortedfamilyPar.Contains(fp))
                    {
                        sortedfamilyPar.Insert(sortedfamilyPar.Count,fp);
                    }
                }
                
                // show a list of all the parameters in order to show they are sorted
                String sortedstrNames = null;
                foreach (FamilyParameter fp in sortedfamilyPar)
                    {
                    sortedstrNames = sortedstrNames + fp.Definition.Name.ToString() + "\n";
                    }
                    TaskDialog.Show("Sorted Order", sortedstrNames);
                    
                // Finally, apply the sorted parameter list to the family document
                
                using (Transaction t = new Transaction(doc))
                {
                t.Start("Sort Parameters");
                famMan.ReorderParameters(sortedfamilyPar);  
                 t.Commit();                
                }
     
         }//end addshared parameters
    }//this application
}// end namespace

Message 6 of 6
jeremytammik
in reply to: stever66

Congratulations on solving this!

 

Looks good.

 

Sure, could be improved...

 

Here are some samples on using GetParameters:

 

 

Cheers,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

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

Post to forums  

Forma Design Contest


Rail Community