michael.montagne
2008-03-05, 10:06 PM
I'm getting inconsistent results with this code. It is ripped right out of the SDK and should add a shared parameter to the project file. If I open a new project it works great. But on subsequent tries, it fails to add the parameter at all.
public bool SetNewParameterToRooms (string sNewParam)
{
//Open the shared parameters file
// via the private method AccessOrCreateExternalSharedParameterFile
DefinitionFile informationFile = AccessOrCreateExternalSharedParameterFile();
System.Diagnostics.Debug.WriteLine("Setting Parameter " + sNewParam);
if (null == informationFile)
{
return false;
}
// Access an existing or create a new group in the shared parameters file
DefinitionGroups informationCollections = informationFile.Groups;
DefinitionGroup informationCollection = null;
informationCollection = informationCollections.get_Item("Rooms");
if (null == informationCollection)
{
System.Diagnostics.Debug.WriteLine("Rooms group not found in sharedparams.txt");
return false;
}
// Access an existing or create a new external parameter definition
// belongs to a specific group
Definition information = informationCollection.Definitions.get_Item(sNewParam);
if (null == information)
{
informationCollection.Definitions.Create(sNewParam, Autodesk.Revit.Parameters.ParameterType.Text);
information = informationCollection.Definitions.get_Item(sNewParam);
}
// Create a new Binding object with the categories to which the parameter will be bound
CategorySet categories = m_revit.Create.NewCategorySet();
Category roomsClassification = null;
// use category in instead of the string name to get category
roomsClassification = m_revit.ActiveDocument.Settings.Categories.get_Item(BuiltInCategory.OST_Rooms);
categories.Insert(roomsClassification);
InstanceBinding caseTying = m_revit.Create.NewInstanceBinding(categories);
// Add the binding and definition to the document
bool boundResult = m_revit.ActiveDocument.ParameterBindings.Insert(information, caseTying);
return boundResult;
}
public bool SetNewParameterToRooms (string sNewParam)
{
//Open the shared parameters file
// via the private method AccessOrCreateExternalSharedParameterFile
DefinitionFile informationFile = AccessOrCreateExternalSharedParameterFile();
System.Diagnostics.Debug.WriteLine("Setting Parameter " + sNewParam);
if (null == informationFile)
{
return false;
}
// Access an existing or create a new group in the shared parameters file
DefinitionGroups informationCollections = informationFile.Groups;
DefinitionGroup informationCollection = null;
informationCollection = informationCollections.get_Item("Rooms");
if (null == informationCollection)
{
System.Diagnostics.Debug.WriteLine("Rooms group not found in sharedparams.txt");
return false;
}
// Access an existing or create a new external parameter definition
// belongs to a specific group
Definition information = informationCollection.Definitions.get_Item(sNewParam);
if (null == information)
{
informationCollection.Definitions.Create(sNewParam, Autodesk.Revit.Parameters.ParameterType.Text);
information = informationCollection.Definitions.get_Item(sNewParam);
}
// Create a new Binding object with the categories to which the parameter will be bound
CategorySet categories = m_revit.Create.NewCategorySet();
Category roomsClassification = null;
// use category in instead of the string name to get category
roomsClassification = m_revit.ActiveDocument.Settings.Categories.get_Item(BuiltInCategory.OST_Rooms);
categories.Insert(roomsClassification);
InstanceBinding caseTying = m_revit.Create.NewInstanceBinding(categories);
// Add the binding and definition to the document
bool boundResult = m_revit.ActiveDocument.ParameterBindings.Insert(information, caseTying);
return boundResult;
}