Parameter of Revit API – 48: Convert Shared Parameter to FamilyParameter
We talked about creating new FamilyParameter instances before. Now let’s see how to create a shared FamilyParameter, which is converted from an existing Shared Parameter.Before creating the specific code for the SharedParameter to FamilyParameter Converter, we need to put some Coders together first that we introduced and demonstrated before.They will save us a lot of precious time and avoid duplicate effort.Here they are:#region From FamilyParameter Creator
public static List<T> RawConvertSetToList<T>(IEnumerable set)
{
List<T> list = (from T p in set select p).ToList<T>();
return list;
}public static FamilyParameter RawFindFamilyParameter(FamilyManager fm, string parameterName)
{
FamilyParameter fp = RawConvertSetToList<FamilyParameter>(fm.Parameters).
FirstOrDefault(e => e.Definition.Name.Equals(parameterName, StringComparison.CurrentCultureIgnoreCase));
if (fp == null) throw new Exception("Invalid ParameterName Input!"); return fp;
}public static FamilyType RawFindFamilyType(FamilyManager fm, string familyTypeName)
{
FamilyType famType = RawConvertSetToList<FamilyType>(fm.Types).
FirstOrDefault(e => e.Name.Equals(familyTypeName, StringComparison.CurrentCultureIgnoreCase));
if (famType == null) throw new Exception("Invalid FamilyTypeName Input!"); return famType;
}public static void RawSetFamilyParameterValue(FamilyManager fm, string familyTypeName, string parameterName, object value)
{
RawSetFamilyParameterValue(fm, RawFindFamilyType(fm, familyTypeName), RawFindFamilyParameter(fm, parameterName), value);
}public static void RawSetFamilyParameterValue(FamilyManager fm, FamilyType ft, string parameterName, object value)
{
RawSetFamilyParameterValue(fm, ft, RawFindFamilyParameter(fm, parameterName), value);
}public static void RawSetFamilyParameterValue(FamilyManager fm, string familyTypeName, FamilyParameter fp, object value)
{
RawSetFamilyParameterValue(fm, RawFindFamilyType(fm, familyTypeName), fp, value);
}public static void RawSetFamilyParameterValue(FamilyManager fm, FamilyType ft, FamilyParameter fp, object value)
{
FamilyType curFamType = fm.CurrentType;
fm.CurrentType = ft; try
{
switch (fp.StorageType)
{
case StorageType.None:
break;
case StorageType.Double:
if (value.GetType().Equals(typeof(string)))
{
fm.Set(fp, double.Parse(value as string));
}
else
{
fm.Set(fp, Convert.ToDouble(value));
}
break;
case StorageType.Integer:
if (value.GetType().Equals(typeof(string)))
{
fm.Set(fp, int.Parse(value as string));
}
else
{
fm.Set(fp, Convert.ToInt32(value));
}
break;
case StorageType.ElementId:
if (value.GetType().Equals(typeof(ElementId)))
{
fm.Set(fp, value as ElementId);
}
else if (value.GetType().Equals(typeof(string)))
{
fm.Set(fp, new ElementId(int.Parse(value as string)));
}
else
{
fm.Set(fp, new ElementId(Convert.ToInt32(value)));
}
break;
case StorageType.String:
fm.Set(fp, value.ToString());
break;
}
}
catch
{
throw new Exception("Invalid Value Input!");
}
finally
{
fm.CurrentType = curFamType;
}
}
#endregion
With all the above code handy, it becomes a pretty easy task to create some code to convert a Shared Parameter definition in an existing external DefinitionFile to a FamilyParameter in a particular Revit Family Document (Manager):public static ExternalDefinition RawFindExternalDefinition(DefinitionFile defFile, string name)
{
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)
return null;
else
return v.First();
}public static void ConvertSharedParameterToFamilyParameter(ExternalDefinition extDef, FamilyManager famMan, bool instance, string type, object value )
{
FamilyParameter fp = famMan.AddParameter(extDef, extDef.ParameterGroup, instance);
if (value != null) RawSetFamilyParameterValue(famMan, type, fp, value);
}A few points may be worth of mentioning here:
[*]The ParameterGroup of the new FamilyParameter will use the same one in the Shared Parameter ExternalDefinition. If it needs to be overridden, it’s very easy to extend the converters.
[*]The newly created FamilyParameter can be instance applicable or type applicable.
[*]The newly created FamilyParameter can have a default value for a specific FamilyType. If it is not necessary, the arguments and the value assignment code can be removed without any negative impact.
The following test code can exercise the methods:
ExternalDefinition extdef = RawFindExternalDefinition(CachedApp.OpenSharedParameterFile(), "Area41");
ConvertSharedParameterToFamilyParameter(extdef, CachedDoc.FamilyManager, false, "Type 1", "0.00");In terms of details of the various coders referenced in this post please refer to previous posts for ideas and code examples.
路过!!!
不发表意见……
路过!!!
不发表意见……
(*^__^*) 嘻嘻……
谢谢老师…
谢谢!!!
帮顶……
非常感谢!!
顶......
楼下跟上.....
路过!!!
帮顶……
顶!!!!!!!!!!!!!!!!!!!!!!!!!
顶起来………… (*^__^*) 嘻嘻……
顶起来………… 顶!!!!!!!!!!!!!!!!!!!!!!!!! 路过!!!
不发表意见…… 顶起来…………
页:
[1]
2