EaBIM一直以来积极响应国家“十二五”推进建筑业信息化的号召,对建筑领域的信息技术开展深入技术交流和探讨!致力于打造“BIM-建筑师-生态技术”三位一体综合资源交流共享平台,希望为BIM与可持续设计理念及技术的普及做出微小的贡献!!!

萧闫子 发表于 2014-1-9 11:53:07

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.

大奔KY 发表于 2014-3-13 12:22:33

路过!!!
不发表意见……

长风 发表于 2014-2-20 15:07:21

路过!!!
不发表意见……

ben7 发表于 2014-6-12 09:43:07

(*^__^*) 嘻嘻……

AK47 发表于 2014-3-10 16:41:09

谢谢老师…

OK佬 发表于 2014-3-10 17:02:29

谢谢!!!
帮顶……

codywu 发表于 2014-3-10 17:07:49

非常感谢!!

一梦千寻 发表于 2014-4-2 16:05:05

顶......
楼下跟上.....

audigy 发表于 2014-4-8 10:27:05

路过!!!
帮顶……

大奔KY 发表于 2014-4-8 10:32:25

顶!!!!!!!!!!!!!!!!!!!!!!!!!

ben7 发表于 2014-4-10 15:49:16

顶起来…………

dison 发表于 2014-4-10 15:53:20

(*^__^*) 嘻嘻……

AK47 发表于 2014-4-23 10:40:55

顶起来…………

妮可 发表于 2014-5-14 14:04:27

顶!!!!!!!!!!!!!!!!!!!!!!!!!

千里独行 发表于 2014-5-14 14:06:27

路过!!!
不发表意见……

严英华 发表于 2014-5-14 14:06:51

顶起来…………
页: [1] 2
查看完整版本: Parameter of Revit API – 48: Convert Shared Parameter to FamilyParameter