Adding and Removing Shared Parameters
This program adds or removes shared parameters from a family
Example of Removing shared parameters from a family (run while in the family editor with the family open)
//Delete all parameters that start with something
famMan = doc.FamilyManager;
IList<FamilyParameter> familyPar = famMan.GetParameters();
using (Transaction t = new Transaction(doc))
{
t.Start("Delete parameters");
foreach (FamilyParameter fp in familyPar)
{
if (fp.Definition.Name.StartsWith("Lighting") && !(fp.Definition.Name.Contains("EMEGENCY")))
{
famMan.RemoveParameter (fp);
} //end foreach
t.Commit();
}//end using transaction
2nd Example of Removing all parameters from a family that can be removed: (run while in the family editor with the family open)
//or delete all parameters that can be deleted--------------------------------------------------------------------
famMan = doc.FamilyManager;
IList<FamilyParameter> familyPar = famMan.GetParameters();
using (Transaction t = new Transaction(doc))
{
t.Start("Delete parameters");
int n = 0;
foreach (FamilyParameter fp in familyPar)
{
try
{
famMan.RemoveParameter (fp);
}
catch
{
n=n+1;
}
}
t.Commit();
TaskDialog.Show("Revit Remove Family Parameters", "Could not remove " + n.ToString () + " parameters.");
} // end using transaction
Example of Adding shared parameters to a family (run while in the family editor with the family open). Adds all the parameters from a group called 01- General. Change “01-General” to the group name in your shared parameter file. Or delete :
if (dG.Name.ToString() == "01 - General")
to add all shared parameters.
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using System.Collections.Generic;
using System.Linq;
//add reference to System.Windows.Forms via Project-Add Reference....
using System.Windows.Forms;
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Collections;
//standard macro revit headers not shown including transaction mode, addin id, startup and shutdown modules
public void AddNewSharedParameters()
{
Document doc = this.ActiveUIDocument.Document;
//------------------------------------Add the New Shared Parameters-----------------------------------------
//open a shared parameters filename
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = (@"C:\Temp "); // add your file path to the shared parameters file here:
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
foreach (string path in openFileDialog1.FileNames)
{
FileInfo filePath = new FileInfo(path);
//MessageBox.Show("filePath.FullName.ToString() = " + filePath.FullName.ToString());
this.Application.SharedParametersFilename = filePath.FullName.ToString();
}
}
this.Application.OpenSharedParameterFile ();
// 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;
IList<FamilyParameter> existfamilyPar = famMan.GetParameters();
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 Light Fixture Shared Parameters");
foreach (ExternalDefinition eD in v) //get each parameter in the current group
{
try
{
//TaskDialog.Show("Revit", dG.Name.ToString());
if (dG.Name.ToString() == "01 - General")
{
FamilyParameter fp = famMan.AddParameter (eD,BuiltInParameterGroup.PG_ELECTRICAL_LIGHTING,false);
}
}
//FamilyParameter fp = famMan.AddParameter (eD,BuiltInParameterGroup.PG_GENERAL,false); //change the
//group here if you want the parameters in a different group
}//end try
catch
{
}
}//end foreach
t.Commit();
}//end using transaction
}// end foreach