Skip to content

Commit

Permalink
introduce a new trait IHaveVisualization.CanBeVisualized for type han…
Browse files Browse the repository at this point in the history
…dlers
  • Loading branch information
NeVeSpl committed May 9, 2024
1 parent d3c03f7 commit 41f7b01
Show file tree
Hide file tree
Showing 14 changed files with 84 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public override ReadResult Read(SnoopableContext context, TSnoopedObjectType @ob
var value = new ValueContainer<TReturnType>();
var result = get(context.Document, @object);
value.SetValueTyped(context, result);
return new ReadResult(value.ValueAsString, "[ByFunc] " + value.TypeName, value.CanBeSnooped, value);
return new ReadResult(value.ValueAsString, "[ByFunc] " + value.TypeHandlerName, value.CanBeSnooped, value);
}
public override IEnumerable<SnoopableObject> Snoop(SnoopableContext context, TSnoopedObjectType @object, IValueContainer state)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public override ReadResult Read(SnoopableContext context, object @object)
var resolvedArgs = ResolveArguments(paramsDef, context.Document, @object);
var result = getMethod.Invoke(@object, resolvedArgs);
value.SetValue(context, result);
return new ReadResult(value.ValueAsString, "[ByRef] " + value.TypeName, value.CanBeSnooped, value);
return new ReadResult(value.ValueAsString, "[ByRef] " + value.TypeHandlerName, value.CanBeSnooped, value);
}
public override IEnumerable<SnoopableObject> Snoop(SnoopableContext context, object @object, IValueContainer state)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public override ReadResult Read(SnoopableContext context, TSnoopedObjectType typ
var value = new ValueContainer<TReturnType>();
var result = func(typedObject);
value.SetValueTyped(context, result);
return new ReadResult(value.ValueAsString, "[ByRefComp] " + value.TypeName, value.CanBeSnooped, value);
return new ReadResult(value.ValueAsString, "[ByRefComp] " + value.TypeHandlerName, value.CanBeSnooped, value);
}

public override IEnumerable<SnoopableObject> Snoop(SnoopableContext context, TSnoopedObjectType typedObject, IValueContainer state)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public MemberAccessorForConstValue(Type type, SnoopableContext context, object v

public override ReadResult Read(SnoopableContext context, object @object)
{
return new ReadResult(value.ValueAsString, value.TypeName, value.CanBeSnooped, value);
return new ReadResult(value.ValueAsString, value.TypeHandlerName, value.CanBeSnooped, value);
}
public override IEnumerable<SnoopableObject> Snoop(SnoopableContext context, object @object, IValueContainer state)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public ReadResult Read(SnoopableContext context, object @object)
case StorageType.None:
break;
}
return new ReadResult(value.ValueAsString, "[ByParam] " + value.TypeName, value.CanBeSnooped, value);
return new ReadResult(value.ValueAsString, "[ByParam] " + value.TypeHandlerName, value.CanBeSnooped, value);

}
public IEnumerable<SnoopableObject> Snoop(SnoopableContext context, object @object, IValueContainer state)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace RevitDBExplorer.Domain.DataModel.ValueContainers.Base
{
internal interface ITypeHandler : IToLabel, ICanBeSnooped, ISnoop, IHaveVisualization
internal interface ITypeHandler : IHaveLabel, ISnoop, IHaveVisualization
{
Type Type { get; }
}
internal interface ITypeHandler<in T> : IToLabel<T>, ICanBeSnooped<T>, ISnoop<T>, IHaveVisualization<T>
internal interface ITypeHandler<in T> : IHaveLabel<T>, ISnoop<T>, IHaveVisualization<T>
{
Type Type { get; }
string GetTypeHandlerName(T value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ internal interface IValueContainer
{
Type Type { get; }
Type TypeHandlerType { get; }
string TypeName { get; }
string TypeHandlerName { get; }
IValueContainer SetValue(SnoopableContext context, object value);


string ValueAsString { get; }
bool CanBeSnooped { get; }
bool CanBeVisualized { get; }
string ToolTip { get; }
IEnumerable<SnoopableObject> Snoop();
IEnumerable<DrawingVisual> GetVisualization();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,42 @@

namespace RevitDBExplorer.Domain.DataModel.ValueContainers.Base
{
internal interface ICanBeSnooped
internal interface ISnoop
{
bool CanBeSnooped(SnoopableContext context, object value);
IEnumerable<SnoopableObject> Snoop(SnoopableContext context, object value);
}
internal interface ICanBeSnooped<in T>
internal interface ISnoop<in T>
{
bool CanBeSnooped(SnoopableContext context, T value);
IEnumerable<SnoopableObject> Snoop(SnoopableContext context, T value);
}

internal interface IToLabel

internal interface IHaveLabel
{
string ToLabel(SnoopableContext context, object value);
}
internal interface IToLabel<in T>
internal interface IHaveLabel<in T>
{
string ToLabel(SnoopableContext context, T value);
}

internal interface ISnoop
{
IEnumerable<SnoopableObject> Snoop(SnoopableContext context, object value);
}
internal interface ISnoop<in T>
{
IEnumerable<SnoopableObject> Snoop(SnoopableContext context, T value);
}
}


internal interface IHaveToolTip<in T>
{
string GetToolTip(SnoopableContext context, T value);
}


internal interface IHaveVisualization
{
bool CanBeVisualized(SnoopableContext context, object value);
IEnumerable<DrawingVisual> GetVisualization(SnoopableContext context, object value);
}
internal interface IHaveVisualization<in T>
{
bool CanBeVisualized(SnoopableContext context, T value);
IEnumerable<DrawingVisual> GetVisualization(SnoopableContext context, T value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public string GetTypeHandlerName(T value)



bool ICanBeSnooped.CanBeSnooped(SnoopableContext context, object value)
bool ISnoop.CanBeSnooped(SnoopableContext context, object value)
{
T typedValue = value.CastValue<T>(type);
return (this as ICanBeSnooped<T>).CanBeSnooped(context, typedValue);
return (this as ISnoop<T>).CanBeSnooped(context, typedValue);
}
bool ICanBeSnooped<T>.CanBeSnooped(SnoopableContext context, T value)
bool ISnoop<T>.CanBeSnooped(SnoopableContext context, T value)
{
if (value is null) return false;
return CanBeSnoooped(context, value);
Expand All @@ -51,12 +51,12 @@ IEnumerable<SnoopableObject> ISnoop<T>.Snoop(SnoopableContext context, T value)



string IToLabel.ToLabel(SnoopableContext context, object value)
string IHaveLabel.ToLabel(SnoopableContext context, object value)
{
T typedValue = value.CastValue<T>(type);
return (this as IToLabel<T>).ToLabel(context, typedValue);
return (this as IHaveLabel<T>).ToLabel(context, typedValue);
}
string IToLabel<T>.ToLabel(SnoopableContext context, T value)
string IHaveLabel<T>.ToLabel(SnoopableContext context, T value)
{
if (value is null) return "<null>";
var label = ToLabel(context, value);
Expand All @@ -67,6 +67,20 @@ string IToLabel<T>.ToLabel(SnoopableContext context, T value)



bool IHaveVisualization.CanBeVisualized(SnoopableContext context, object value)
{
T typedValue = value.CastValue<T>(type);
return (this as IHaveVisualization<T>).CanBeVisualized(context, typedValue);
}
bool IHaveVisualization<T>.CanBeVisualized(SnoopableContext context, T value)
{
if (value is null) return false;
return CanBeVisualized(context, value);
}
protected virtual bool CanBeVisualized(SnoopableContext context, T value) => false;



IEnumerable<DrawingVisual> IHaveVisualization.GetVisualization(SnoopableContext context, object value)
{
T typedValue = value.CastValue<T>(type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal sealed class ValueContainer<T> : IValueContainer
public Type TypeHandlerType => typeHandler.GetType();
public Type Type => typeHandler.Type;
public T Value => value;
public string TypeName
public string TypeHandlerName
{
get
{
Expand All @@ -38,10 +38,9 @@ public ValueContainer()


public IValueContainer SetValue(SnoopableContext context, object value)
{
this.context = context;
this.value = value.CastValue<T>(Type);

{
SetValueTyped(context, value.CastValue<T>(Type));

return this;
}
public ValueContainer<T> SetValueTyped(SnoopableContext context, T value)
Expand All @@ -55,6 +54,7 @@ public ValueContainer<T> SetValueTyped(SnoopableContext context, T value)

public string ValueAsString => typeHandler?.ToLabel(context, value) ?? "RDBE Error";
public bool CanBeSnooped => typeHandler.CanBeSnooped(context, value);
public bool CanBeVisualized => typeHandler.CanBeVisualized(context, value);

public string ToolTip
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using Autodesk.Revit.DB;
using NSourceGenerators;
using RevitDBExplorer.Domain.DataModel.ValueContainers.Base;

// (c) Revit Database Explorer https://github.com/NeVeSpl/RevitDBExplorer/blob/main/license.md
Expand All @@ -8,8 +9,10 @@ namespace RevitDBExplorer.Domain.DataModel.ValueContainers
{
internal class BindingMapHandler : TypeHandler<BindingMap>
{
protected override bool CanBeSnoooped(SnoopableContext context, BindingMap map) => map is not null && !map.IsEmpty;
protected override string ToLabel(SnoopableContext context, BindingMap map) => $"Bindings : {map.Size}";
protected override bool CanBeSnoooped(SnoopableContext context, BindingMap map) => !map.IsEmpty;
protected override string ToLabel(SnoopableContext context, BindingMap map) => Labeler.GetLabelForCollection("Binding", map.Size);

[CodeToString]
protected override IEnumerable<SnoopableObject> Snooop(SnoopableContext context, BindingMap map)
{
var iterator = map.ForwardIterator();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Autodesk.Revit.DB;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using NSourceGenerators;
using RevitDBExplorer.Domain.DataModel.ValueContainers.Base;

// (c) Revit Database Explorer https://github.com/NeVeSpl/RevitDBExplorer/blob/main/license.md
Expand All @@ -7,11 +9,16 @@ namespace RevitDBExplorer.Domain.DataModel.ValueContainers
{
internal class BoundarySegmentHandler : TypeHandler<BoundarySegment>
{
protected override bool CanBeSnoooped(SnoopableContext context, BoundarySegment boundarySegment) => boundarySegment is not null;

protected override bool CanBeSnoooped(SnoopableContext context, BoundarySegment boundarySegment) => true;
protected override string ToLabel(SnoopableContext context, BoundarySegment boundarySegment)
{
return $"ID: {boundarySegment.ElementId}, {boundarySegment.GetCurve()?.Length} ft"; ;
}

[CodeToString]
protected override IEnumerable<SnoopableObject> Snooop(SnoopableContext context, BoundarySegment boundarySegment)
{
yield return new SnoopableObject(context.Document, boundarySegment);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected override string ToLabel(SnoopableContext context, ElementId id)
var element = context.Document?.GetElement(id);
if (element != null)
{
return (new ElementHandler() as IToLabel<Element>).ToLabel(context, element);
return (new ElementHandler() as IHaveLabel<Element>).ToLabel(context, element);
}
return $"{id}";
}
Expand Down
29 changes: 23 additions & 6 deletions tests/ValueContainersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace RevitDBExplorer.Tests
public class ValueContainersTests
{
[RevitTestMethod]
public void SelectTypeHandlerFor_BuiltInParameter(RevitContext revitContext)
public void TypeHandlerFor_BuiltInParameter(RevitContext revitContext)
{
var type = typeof(BuiltInParameter);
var typeHandler = ValueContainerFactory.SelectTypeHandlerFor(type);
Expand All @@ -23,7 +23,7 @@ public void SelectTypeHandlerFor_BuiltInParameter(RevitContext revitContext)
}

[RevitTestMethod]
public void SelectTypeHandlerFor_Long(RevitContext revitContext)
public void TypeHandlerFor_Long(RevitContext revitContext)
{
var type = typeof(long);
var typeHandler = ValueContainerFactory.SelectTypeHandlerFor(type);
Expand All @@ -34,7 +34,7 @@ public void SelectTypeHandlerFor_Long(RevitContext revitContext)


[RevitTestMethod]
public void CreateValueContainerFor_BuiltInParameter(RevitContext revitContext)
public void ValueContainerFor_BuiltInParameter(RevitContext revitContext)
{
var document = OpenRevitFile(revitContext);

Expand All @@ -46,10 +46,27 @@ public void CreateValueContainerFor_BuiltInParameter(RevitContext revitContext)
Assert.AreEqual(typeof(EnumHandler<System.Enum>), valueContainer.TypeHandlerType);
Assert.AreEqual(typeof(System.Enum), valueContainer.Type);

Assert.AreEqual("Enum : BuiltInParameter", valueContainer.TypeName);
Assert.AreEqual("BuiltInParameter.FUNCTION_PARAM", valueContainer.ValueAsString);
Assert.AreEqual("Enum : BuiltInParameter", valueContainer.TypeHandlerName);
Assert.AreEqual("BuiltInParameter.FUNCTION_PARAM", valueContainer.ValueAsString);


document.Close(false);
}

[RevitTestMethod]
public void ValueContainerFor_Wall(RevitContext revitContext)
{
var document = OpenRevitFile(revitContext);

var wall = new FilteredElementCollector(document).WhereElementIsNotElementType().OfClass(typeof(Wall)).FirstElement();
var valueContainer = ValueContainerFactory.Create(typeof(Wall));
valueContainer.SetValue(new SnoopableContext() { Document = document }, wall);

Assert.AreEqual(typeof(ValueContainer<Element>), valueContainer.GetType());
Assert.AreEqual(typeof(ElementHandler), valueContainer.TypeHandlerType);
Assert.AreEqual(typeof(Element), valueContainer.Type);

Assert.AreEqual("Element : Wall", valueContainer.TypeHandlerName);
Assert.AreEqual("Basic Wall: Generic - 200mm (420680)", valueContainer.ValueAsString);

document.Close(false);
}
Expand Down

0 comments on commit 41f7b01

Please sign in to comment.