static bool AddProjectParameterBinding(
Document doc,
ProjectParameterData projectParameterData,
Category category)
{
// Following good SOA practices, first validate incoming parameters
if( doc == null )
{
throw new ArgumentNullException( "doc" );
}
if( doc.IsFamilyDocument )
{
throw new Exception(
"doc can not be a family document." );
}
if( projectParameterData == null )
{
throw new ArgumentNullException(
"projectParameterData" );
}
if( category == null )
{
throw new ArgumentNullException( "category" );
}
bool result = false;
CategorySet cats = projectParameterData.Binding
.Categories;
if( cats.Contains( category ) )
{
// It's already bound to the desired category.
// Nothing to do.
string errorMessage = string.Format(
"The project parameter '{0}' is already bound to the '{1}' category.",
projectParameterData.Definition.Name,
category.Name );
throw new Exception( errorMessage );
}
cats.Insert( category );
// See if the parameter is an instance or type parameter.
InstanceBinding instanceBinding
= projectParameterData.Binding as InstanceBinding;
if( instanceBinding != null )
{
// Is an Instance parameter
InstanceBinding newInstanceBinding
= doc.Application.Create
.NewInstanceBinding( cats );
if( doc.ParameterBindings.ReInsert(
projectParameterData.Definition,
newInstanceBinding ) )
{
result = true;
}
}
else
{
// Is a type parameter
TypeBinding typeBinding
= doc.Application.Create
.NewTypeBinding( cats );
if( doc.ParameterBindings.ReInsert(
projectParameterData.Definition, typeBinding ) )
{
result = true;
}
}
return result;
}