Skip to content

Commit

Permalink
add a new feature: Favourite members
Browse files Browse the repository at this point in the history
  • Loading branch information
NeVeSpl committed May 21, 2024
1 parent 3e00f64 commit 616ad5a
Show file tree
Hide file tree
Showing 16 changed files with 231 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using Autodesk.Revit.DB;
using RevitDBExplorer.Domain.DataModel.Accessors;
Expand Down Expand Up @@ -39,5 +40,25 @@ public static IMemberOverride ByFunc<TReturnType>(Expression<Func<Document, TFor
}
};
}

public static IMemberOverride ByFunc<TParam0Type, TReturnType>(Expression<Func<Document, TForType, TParam0Type, TReturnType>> getter, IEnumerable<TParam0Type> param_0_arguments)
{
var compiledGetter = getter.Compile();
string syntax = getter.ToCeSharp();
var uniqueId = getter.GetUniqueId();

var param_0_name = getter.Parameters[2].Name;

return new MemberOverride<TForType>()
{
UniqueId = uniqueId,
MemberAccessorFactory = () =>
{
var accessor = new MemberAccessorByFuncUltra<TForType, TParam0Type, TReturnType>(compiledGetter, param_0_arguments, param_0_name);
accessor.DefaultInvocation.Syntax = syntax;
return accessor;
}
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal class Element_Overrides : IHaveMembersOverrides
{
public IEnumerable<IMemberOverride> GetOverrides() =>
[
MemberOverride<Element>.ByFunc((document, element) => element.GetDependentElements(null)),
MemberOverride<Element>.ByFunc((document, element) => element.GetDependentElements(null)),
];
}
}
22 changes: 19 additions & 3 deletions sources/RevitDBExplorer/Domain/DataModel/SnoopableItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using RevitDBExplorer.Domain.DataModel.ValueViewModels.Base;
using RevitDBExplorer.Domain.RevitDatabaseScripting;
using RevitDBExplorer.WPF;
using RevitExplorer.Visualizations.DrawingVisuals;

// (c) Revit Database Explorer https://github.com/NeVeSpl/RevitDBExplorer/blob/main/license.md

Expand All @@ -19,7 +20,7 @@ internal abstract class SnoopableItem : BaseViewModel, IAmSourceOfEverything, IC
private readonly IValueViewModel itemValueViewModel;

public event Action ParentObjectChanged;
public string AccessorName { get; private set; }

public virtual string Name { get; }
public IValueViewModel ValueViewModel { get; private set; } = EmptyPresenter.Instance;
public bool CanBeSnooped
Expand All @@ -33,6 +34,7 @@ public bool CanBeSnooped
return false;
}
}
public bool CanBeVisualized => (ValueViewModel as DefaultPresenter)?.CanBeVisualized == true;
public virtual bool CanGenerateCode { get; }


Expand Down Expand Up @@ -69,9 +71,9 @@ private void Read(SnoopableContext context, object @object)
{
ValueViewModel = new ErrorPresenter(Labeler.GetLabelForException(valueAccessException));
}
OnPropertyChanged(nameof(ValueViewModel));
OnPropertyChanged(nameof(AccessorName));
OnPropertyChanged(nameof(ValueViewModel));
OnPropertyChanged(nameof(CanBeSnooped));
OnPropertyChanged(nameof(CanBeVisualized));
}
private void RaiseParentObjectChanged()
{
Expand All @@ -92,6 +94,16 @@ IEnumerable<SnoopableObject> IAmSourceOfEverything.Snoop(UIApplication app)
}


public IEnumerable<DrawingVisual> GetVisualization()
{
if (ValueViewModel is DefaultPresenter defaultPresenter)
{
return defaultPresenter.GetVisualization(parent.Context, parent.Object);
}
return [];
}



private bool isFrozen = false;
private IList<SnoopableObject> frozenSnooopResult;
Expand Down Expand Up @@ -120,5 +132,9 @@ public string GenerateScript(TemplateInputsKind inputsKind)

return new MemberInvocationTemplateSelector().Evaluate(parent.Object.GetType(), invocation, TemplateCmdKind.Select, inputsKind);
}
public string GetUniqueId()
{
return accessor?.UniqueId;
}
}
}
3 changes: 1 addition & 2 deletions sources/RevitDBExplorer/Domain/DataModel/SnoopableMember.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Collections.Generic;
using System.Windows.Documents;
using RevitDBExplorer.Domain.DataModel.Members.Base;
using RevitDBExplorer.Domain.DataModel.ValueViewModels;

// (c) Revit Database Explorer https://github.com/NeVeSpl/RevitDBExplorer/blob/main/license.md

Expand All @@ -17,7 +16,7 @@ internal sealed class SnoopableMember : SnoopableItem
public override string Name => memberDescriptor.Name;
public DocXml Documentation => memberDescriptor.Documentation;
public override bool CanGenerateCode => memberDescriptor.Kind != MemberKind.None;
public bool CanBeVisualized => (ValueViewModel as DefaultPresenter)?.ValueContainer?.CanBeVisualized == true;



public SnoopableMember(SnoopableObject parent, MemberDescriptor memberDescriptor) : base(parent, memberDescriptor.MemberAccessor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using RevitDBExplorer.Domain.DataModel.ValueContainers.Base;
using RevitDBExplorer.Domain.DataModel.ValueViewModels.Base;
using RevitDBExplorer.WPF;
using RevitExplorer.Visualizations.DrawingVisuals;

// (c) Revit Database Explorer https://github.com/NeVeSpl/RevitDBExplorer/blob/main/license.md

Expand All @@ -22,7 +23,7 @@ public IValueContainer ValueContainer
{
return valueContainer;
}
set
private set
{
valueContainer = value;
OnPropertyChanged();
Expand All @@ -41,28 +42,33 @@ public string Label
}
}
public bool CanBeSnooped { get; private set; }
public bool CanBeVisualized { get; private set; }
public string ToolTip => ValueContainer?.ToolTip;


public DefaultPresenter(IAccessorForDefaultPresenter accessor)
{
this.accessor = accessor;
}


public void Read(SnoopableContext context, object @object)
{
var result = accessor.Read(context, @object);
ValueContainer = result.State;
Label = result.Label;
CanBeSnooped = result.CanBeSnooped;
CanBeVisualized = result.CanBeVisualized;
}

public IEnumerable<SnoopableObject> Snoop(SnoopableContext context, object @object)
{
if (accessor is IAccessorForDefaultPresenter snooper)
{
return snooper.Snoop(context, @object, valueContainer);
}
return Enumerable.Empty<SnoopableObject>();
{
return accessor.Snoop(context, @object, valueContainer) ?? [];
}

public IEnumerable<DrawingVisual> GetVisualization(SnoopableContext context, object @object)
{
return accessor.GetVisualization(context, @object, valueContainer) ?? [];
}
}
}
78 changes: 78 additions & 0 deletions sources/RevitDBExplorer/Domain/FavoriteMembersManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;

// (c) Revit Database Explorer https://github.com/NeVeSpl/RevitDBExplorer/blob/main/license.md

namespace RevitDBExplorer.Domain
{
internal class FavoriteMembersManager
{
private static readonly string FilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "_RDBE_\\FavoriteMembers.json");
private static List<FavoriteMemberDTO> favorites = new List<FavoriteMemberDTO>();
private static HashSet<string> favoriteIds = new HashSet<string>()
{
"Element.get_Geometry(Options)",
"Element.get_Parameters()",
"Rebar.GetCenterlineCurves(Boolean,Boolean,Boolean,MultiplanarOption,Int32)",
};

static FavoriteMembersManager()
{
Load();
}

private static void Load()
{
if (File.Exists(FilePath))
{
string jsonString = File.ReadAllText(FilePath);
favorites = JsonSerializer.Deserialize<List<FavoriteMemberDTO>>(jsonString);
favoriteIds = new HashSet<string>(favorites.Select(x => x.Id));
}
}
private static void Save()
{
Directory.CreateDirectory(Path.GetDirectoryName(FilePath));
string jsonString = JsonSerializer.Serialize(favorites);
File.WriteAllText(FilePath, jsonString);
}


public static bool IsFavorite(string id)
{
if (string.IsNullOrEmpty(id)) return false;
return favoriteIds.Contains(id);
}
public static void AddFavorite(string id)
{
favorites.Add(new FavoriteMemberDTO { Id = id });
favoriteIds.Add(id);
Save();
}
public static void RemoveFavorite(string id)
{
favorites.RemoveAll(x => x.Id == id);
favoriteIds.Remove(id);
Save();
}
}


internal class FavoriteMemberDTO : IEquatable<FavoriteMemberDTO>
{
public string Id { get; set; }


public bool Equals(FavoriteMemberDTO other)
{
return Id == other.Id;
}
public override int GetHashCode()
{
return Id.GetHashCode();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace RevitDBExplorer.Domain.RevitDatabaseQuery.Autocompletion
{
internal class FavoritesManager
{
private static readonly string FilePath;
private static readonly string FilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "_RDBE_\\FavoriteQueries.json");
private static List<FavoriteQueryDTO> favoriteQueries = new List<FavoriteQueryDTO>()
{
new FavoriteQueryDTO() { Query = "t: ParameterElement", Description = "User defined parameters" }
Expand All @@ -33,9 +33,7 @@ public static List<FavoriteQueryDTO> FavoriteQueries


static FavoritesManager()
{
var lad = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
FilePath = Path.Combine(lad, "_RDBE_\\FavoriteQueries.json");
{
Load();
}

Expand Down
12 changes: 3 additions & 9 deletions sources/RevitDBExplorer/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,8 @@ private void UpdateRDV()
drawingVisuals.AddRange(snoopableObjectTreeItem.Object.GetVisualization());
}
if (selectedItem is ListItemForMember listItemForMember)
{
var leftOne = listItemForMember[0];
var righttOne = listItemForMember[1];

if (leftOne?.ValueViewModel is DefaultPresenter { ValueContainer: { } } presenter)
{
drawingVisuals.AddRange(presenter.ValueContainer.GetVisualization());
}
{
drawingVisuals.AddRange(listItemForMember.GetVisualization());
}
}

Expand Down Expand Up @@ -364,7 +358,7 @@ private void GlobalKeyboardHook_KeyDown(object sender, System.Windows.Forms.KeyE
var snoopableObjectTreeItem = selectedItems.OfType<SnoopableObjectTreeItem>().FirstOrDefault();
if (listItemForMember != null)
{
CHMService.OpenCHM(listItemForMember[0] ?? listItemForMember[1]);
listItemForMember.OpenCHM();
return;
}
if (snoopableObjectTreeItem != null)
Expand Down
19 changes: 3 additions & 16 deletions sources/RevitDBExplorer/Resources/Templates/ListViewItem.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,12 @@
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<Border x:Name="Bd" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
<GridViewRowPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
<GridViewRowPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
<GridViewRowPresenter.Resources>
<Style TargetType="ContentPresenter">
<Setter Property="Margin" Value="0"/>
<Setter Property="VerticalAlignment" Value="Stretch"/>
<Style.Triggers>
<EventTrigger RoutedEvent="Loaded">
<BeginStoryboard>
<Storyboard >
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="VerticalAlignment">
<DiscreteObjectKeyFrame KeyTime="0:0:0">
<DiscreteObjectKeyFrame.Value>
<VerticalAlignment>Center</VerticalAlignment>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Style.Triggers>

</Style>
</GridViewRowPresenter.Resources>
</GridViewRowPresenter>
Expand Down
1 change: 1 addition & 0 deletions sources/RevitDBExplorer/Resources/Themes/Dark.Colors.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
<SolidColorBrush x:Key="Query.BorderBrush" Color="{StaticResource Primary}"/>

<SolidColorBrush x:Key="List.Highlighted" Color="{StaticResource Error}"/>
<SolidColorBrush x:Key="List.Favourite" Color="#B97C2F"/>

<SolidColorBrush x:Key="TabItem.Selected" Color="{StaticResource PrimaryLight}"/>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
<SolidColorBrush x:Key="Query.BorderBrush" Color="{StaticResource Primary}"/>

<SolidColorBrush x:Key="List.Highlighted" Color="#FF476C"/>
<SolidColorBrush x:Key="List.Favourite" Color="#222933"/>

<SolidColorBrush x:Key="TabItem.Selected" Color="{StaticResource Primary}"/>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
<SolidColorBrush x:Key="Query.BorderBrush" Color="#ABADB3"/>

<SolidColorBrush x:Key="List.Highlighted" Color="#FFABBC"/>
<SolidColorBrush x:Key="List.Favourite" Color="#6dedff"/>

<SolidColorBrush x:Key="TabItem.Selected" Color="white"/>

Expand Down
Loading

0 comments on commit 616ad5a

Please sign in to comment.