Existing shared parameters can be attached programmatically to some categories as specified by the BuiltInCategory and the CategorySet, can be programmatically put into parameter groups as specified by the BuiltInParameterGroup, just as what can be done through the user interface.
Revit Parameter Organizer can organize various Revit parameters such as Shared Parameters, Family Parameters, Project Parameters and Built-in Parameter in many good ways.
As what we can do manually, shared parameters can also apply to types or instances programmatically. It is achieved through the Binding and BindingMap classes. The NewTypeBinding() method creates a Binding kind of shared parameter to element type; the NewInstanceBinding() a Binding kind of shared parameter to element instance.
In this post, let’s see how to achieve the objective with code.
The following help method can attach an existing shared parameter specified by its name to a CategorySet, put it under a parameter group (BuiltInParameterGroup), and apply it to element instance or type:
public static void RawAttachSharedParameter(RvtApplication app, string name, CategorySet cats, BuiltInParameterGroup group, bool inst)
{
DefinitionFile defFile = app.OpenSharedParameterFile();
if (defFile == null) throw new Exception("No SharedParameter File!");
var v = (from DefinitionGroup dg in defFile.Groups
from ExternalDefinition d in dg.Definitions
where d.Name == name
select d);
if (v == null || v.Count() < 1) throw new Exception("Invalid Name Input!");
ExternalDefinition def = v.First();
Autodesk.Revit.DB.Binding binding = app.Create.NewTypeBinding(cats);
if(inst) binding = app.Create.NewInstanceBinding(cats);
BindingMap map = (new UIApplication(app)).ActiveUIDocument.Document.ParameterBindings;
map.Insert(def, binding, group);
}
The following test code can exercise the above help method.
…
Category wall = CachedDoc.Settings.Categories.get_Item(BuiltInCategory.OST_Walls);
Category door = CachedDoc.Settings.Categories.get_Item(BuiltInCategory.OST_Doors);
CategorySet cats1 = CachedApp.Create.NewCategorySet();
cats1.Insert(wall);
cats1.Insert(door);
RawAttachSharedParameter(CachedApp, "Area11", cats1, BuiltInParameterGroup.PG_DATA, true);
RawAttachSharedParameter(CachedApp, "LengthTest11", cats1, BuiltInParameterGroup.PG_DATA, true);
Category win = CachedDoc.Settings.Categories.get_Item(BuiltInCategory.OST_Windows);
CategorySet cats2 = CachedApp.Create.NewCategorySet();
cats2.Insert(win);
RawAttachSharedParameter(CachedApp, "URLTest31", cats2, BuiltInParameterGroup.PG_DATA, false);
RawAttachSharedParameter(CachedApp, "MaterialTest21", cats2, BuiltInParameterGroup.PG_DATA, false);
…
SharedParameter Attacher of RevitAddinWizard can create the code automatically in no time.
when i attache the shared parameters it giving me a errer(i am only doing the wall and door)
Autodesk.Revit.Exceptions.ModificationOutsideTransactionException: Attempt to modify the model outside of transaction. at apiManagedApplicationRaiseException(ApplicationException* e) at ADocument.addElement_impl(ADocument* , Element* , ElementId ) at ADocument.addElement(ADocument* , ParamElemExternal* pElement, ElementId forcedId) at Autodesk.Revit.DB.BindingMap.Insert(Definition key, Binding item, BuiltInParameterGroup parameterGroup) at RevitAddinCS1.ExtCmd.Execute(ExternalCommandData cmdData, String& msg, ElementSet elemSet) in D:\Programing\RevitApp\RevitAddinCS1\RevitAddinCS1\ExtCmd.cs:line 92
can you please help me identify this...
Posted by: Account Deleted | 04/20/2012 at 09:24 PM
You had to start a transaction if you specified the TransactionMode as Manual for the external command.
Posted by: Spiderinnet | 04/21/2012 at 01:59 AM
TransactionMode is Manuel
Posted by: Account Deleted | 04/22/2012 at 05:38 AM
if you can give your email id i will sent you the project.
Posted by: Account Deleted | 04/22/2012 at 05:40 AM
Exactly. That was what I suspected.
Two options for you. One is to switch from the Manual TransactionMode to the Automatic one for your External Command. The Revit Addin Wizard (RevitAddinWizard) provids the option for you in its wizard pages. Maybe you did not notice it when you were creating the project using the wizard.
If you persist on using the Manual TransactionMode, it is not a problem either. Pleast 'Start' a Transaction before you adding a parameter to an element and 'Commit' the Transaction after the work is done. Though I do not have a post to address directly how to use a Transaction since it is too obvious and very basic, I do have some posts providing code snippets including starting and commiting a Transaction. Here is one:
http://spiderinnet.typepad.com/blog/2011/06/itransactionfinalizer-of-revit-api-a-sample-implementation.html
In terms of your project, I don't need it. Not worth of the effort as the matter is too clear already.
Posted by: Spiderinnet | 04/23/2012 at 12:30 AM