Skip to content

Commit

Permalink
Update context support for FamilyManager
Browse files Browse the repository at this point in the history
  • Loading branch information
Nice3point committed Dec 30, 2024
1 parent 99adcf8 commit 3202041
Showing 1 changed file with 34 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,42 +24,17 @@

namespace RevitLookup.Core.Summary.Descriptors;

public sealed class FamilyManagerDescriptor(FamilyManager familyManager) : Descriptor, IDescriptorResolver
public sealed class FamilyManagerDescriptor(FamilyManager familyManager) : Descriptor, IDescriptorResolver, IDescriptorResolver<Document>
{
public Func<IVariant>? Resolve(string target, ParameterInfo[] parameters)
{
return target switch
{
nameof(FamilyManager.GetAssociatedFamilyParameter) => ResolveGetAssociatedFamilyParameter,
nameof(FamilyManager.IsParameterLockable) => ResolveIsParameterLockable,
nameof(FamilyManager.IsParameterLocked) => ResolveIsParameterLocked,
_ => null
};

IVariant ResolveGetAssociatedFamilyParameter()
{
var elementTypes = Context.ActiveDocument.GetElements().WhereElementIsElementType();
var elementInstances = Context.ActiveDocument.GetElements().WhereElementIsNotElementType();
var elements = elementTypes
.UnionWith(elementInstances)
.ToElements();

var variants = Variants.Values<KeyValuePair<Parameter, FamilyParameter>>(elements.Count);
foreach (var element in elements)
{
foreach (Parameter parameter in element.Parameters)
{
var familyParameter = familyManager.GetAssociatedFamilyParameter(parameter);
if (familyParameter is not null)
{
variants.Add(new KeyValuePair<Parameter, FamilyParameter>(parameter, familyParameter));
}
}
}

return variants.Consume();
}

IVariant ResolveIsParameterLockable()
{
var familyParameters = familyManager.Parameters;
Expand All @@ -86,4 +61,37 @@ IVariant ResolveIsParameterLocked()
return variants.Consume();
}
}

Func<Document, IVariant>? IDescriptorResolver<Document>.Resolve(string target, ParameterInfo[] parameters)
{
return target switch
{
nameof(FamilyManager.GetAssociatedFamilyParameter) => ResolveGetAssociatedFamilyParameter,
_ => null
};

IVariant ResolveGetAssociatedFamilyParameter(Document context)
{
var elementTypes = context.GetElements().WhereElementIsElementType();
var elementInstances = context.GetElements().WhereElementIsNotElementType();
var elements = elementTypes
.UnionWith(elementInstances)
.ToElements();

var variants = Variants.Values<KeyValuePair<Parameter, FamilyParameter>>(elements.Count);
foreach (var element in elements)
{
foreach (Parameter parameter in element.Parameters)
{
var familyParameter = familyManager.GetAssociatedFamilyParameter(parameter);
if (familyParameter is not null)
{
variants.Add(new KeyValuePair<Parameter, FamilyParameter>(parameter, familyParameter));
}
}
}

return variants.Consume();
}
}
}

0 comments on commit 3202041

Please sign in to comment.