We have addressed converting Parameter to Shared Parameter previously. Now let’s see how to convert FamilyParameter to Shared Parameter.
Revit Parameter Organizer can organize various Revit parameters such as Shared Parameters, Family Parameters, Project Parameters and Built-in Parameter in many good ways.
Before creating the specific code for the 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 Infoer
public class RawFamilyParameterInfo
{
public string Name { get; set; }
public string ElementID { get; set; }
public string GUID { get; set; }
public string BuiltinID { get; set; }
public BuiltInParameterGroup Group { get; set; }
public ParameterType Type { get; set; }
public StorageType Storage { get; set; }
public string Unit { get; set; }
public bool Shared { get; set; }
public bool ReadOnly { get; set; }
public bool Instance { get; set; }
public bool Reporting { get; set; }
public bool FormulaDetermined { get; set; }
public bool CanAssignFormula { get; set; }
}
public static string RawGetDUTString(FamilyParameter p)
{
string unitType = string.Empty;
try { unitType = p.DisplayUnitType.ToString(); }
catch { }
return unitType;
}
public static List<RawFamilyParameterInfo> RawGetFamilyParametersInfo(Document doc)
{
List<RawFamilyParameterInfo> paramList =
(from FamilyParameter p in doc.FamilyManager.Parameters
select new RawFamilyParameterInfo
{
Name = p.Definition.Name,
ElementID = p.Id.IntegerValue.ToString(),
GUID = (p.Definition as ExternalDefinition) != null ? (p.Definition as ExternalDefinition).GUID.ToString() : string.Empty,
BuiltinID = (p.Definition as InternalDefinition) != null ? (p.Definition as InternalDefinition).BuiltInParameter.ToString() : string.Empty,
Group = p.Definition.ParameterGroup,
Type = p.Definition.ParameterType,
Storage = p.StorageType,
Unit = RawGetDUTString(p),
Shared = (p.Definition as ExternalDefinition) != null,
ReadOnly = p.IsReadOnly,
Instance = p.IsInstance,
Reporting = p.IsReporting,
FormulaDetermined = p.IsDeterminedByFormula,
CanAssignFormula = p.CanAssignFormula,
}).ToList();
return paramList;
}
#endregion
#region From SharedParameter 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 Definition RawCreateSharedParameter(RvtApplication app, string name, string group, ParameterType type, bool visible)
{
DefinitionFile defFile = app.OpenSharedParameterFile();
if (defFile == null) throw new Exception("No SharedParameter File!");
DefinitionGroup dg = RawConvertSetToList<DefinitionGroup>(defFile.Groups).FirstOrDefault(g => g.Name == group);
if (dg == null) dg = defFile.Groups.Create(group);
Definition def = RawConvertSetToList<Definition>(dg.Definitions).FirstOrDefault(d => d.Name == name);
if (def != null) return def; //dg.Definitions.Erase(def); //ReadOnly Exception!!
def = dg.Definitions.Create(name, type, visible);
return def;
}
#endregion
With all the above code handy, it becomes a pretty easy task to create some code to convert a particular FamilyParameter or all FamilyParameter instances of a Revit Family Document to shared parameters:
public static void ConvertFamilyParameterInfoToSharedParameter(Document familyDoc, RawFamilyParameterInfo info, bool convertSharedOnly, bool visibilityOverride)
{
if (info.Type == ParameterType.Invalid)
{
info.Type = ParameterType.Text;
}
if (convertSharedOnly)
{
if (info.Shared)
{
RawCreateSharedParameter(familyDoc.Application, info.Name, "For_" + familyDoc.Title, info.Type, visibilityOverride);
}
}
else
{
RawCreateSharedParameter(familyDoc.Application, info.Name, "For_" + familyDoc.Title, info.Type, visibilityOverride);
}
}
public static void ConvertFamilyParameterToSharedParameter(Document familyDoc, string name, bool convertSharedOnly, bool visibilityOverride)
{
List<RawFamilyParameterInfo> pList = RawGetFamilyParametersInfo(familyDoc);
List<RawFamilyParameterInfo> pListOfInterest = pList.FindAll(p => p.Name == name);
foreach (RawFamilyParameterInfo info in pListOfInterest)
{
ConvertFamilyParameterInfoToSharedParameter(familyDoc, info, convertSharedOnly, visibilityOverride);
}
}
public static void ConvertAllFamilyParametersToSharedParameters(Document familyDoc, bool convertSharedOnly, bool visibilityOverride)
{
List<RawFamilyParameterInfo> pList = RawGetFamilyParametersInfo(familyDoc);
foreach (RawFamilyParameterInfo info in pList)
{
ConvertFamilyParameterInfoToSharedParameter(familyDoc, info, convertSharedOnly, visibilityOverride);
}
}
A few points may be worth of mentioning here:
- The ParameterType of some valid FamilyParameter instances may be Invalid! We assign the most popular Text ParameterType to the FamilyParameter instances in this case to resolve the creation problem of the affected shared parameters.
- The name of the DefinitionGroup of all the shared parameters will be defaulted as For_<Family Document Name>.
- Since the visibility information of the original shared parameter definitions if any is not easy to find back, we give a chance in these methods to override the visibility for the shared parameters.
The following test code can exercise the methods:
ConvertFamilyParameterToSharedParameter(CachedDoc, "URL", false, true);
ConvertAllFamilyParametersToSharedParameters(CachedDoc, false, true);
In terms of details of the various coders referenced in this post please refer to previous posts for ideas and code examples.
FamilyParameter Converter of RevitAddinWizard can create all these in a second.
Recent Comments