Skip to content

Commit

Permalink
support snoop multi elements
Browse files Browse the repository at this point in the history
  • Loading branch information
chuongmep committed Sep 3, 2023
1 parent b213d56 commit fceb781
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 93 deletions.
49 changes: 20 additions & 29 deletions RevitElementBipChecker/Model/BaseElement.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using Autodesk.Revit.DB;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using RevitElementBipChecker.Viewmodel;

namespace RevitElementBipChecker.Model
{
public class BaseElement : ViewmodeBase
{
private Element _element;
private List<Element> _elements;
private UIDocument _uiDoc;
public BipCheckerViewmodel Viewmodel { get; set; }
public UIDocument UIDoc
Expand All @@ -15,35 +16,25 @@ public UIDocument UIDoc
set => _uiDoc = value;
}

public Element Element
public List<Element> Elements
{
get => _element;
set => _element = value;
}

private Element elementType;
public Element ElementType
{
get
{
if(Element.CanHaveTypeAssigned()) return UIDoc.Document.GetElement(Element.GetTypeId());
return elementType;
}
set => elementType = value;
}

public string category;
public string Category
{
get => Element.Category.Name;
set => category = value;
}

public string Name
{
get => Element.Name;
set => Element.Name = value;
get => _elements;
set => _elements = value;
}

//
// public string category;
// public string Category
// {
// get => Element.Category.Name;
// set => category = value;
// }
//
// public string Name
// {
// get => Element.Name;
// set => Element.Name = value;
// }
public string State { get; set; }

}
Expand Down
9 changes: 5 additions & 4 deletions RevitElementBipChecker/Model/DialogUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ public static class DialogUtils
/// </summary>
/// <param name="Caption">Title Question</param>
/// <returns></returns>
public static bool QuestionMsg(string Caption)
public static TaskDialogResult QuestionMsg(string Caption)
{

var dialog = new TaskDialog(Caption);

dialog.MainIcon = TaskDialogIcon.TaskDialogIconNone;
dialog.MainInstruction = Caption;

dialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink1, "Element Current");
dialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink2, "Element In RvtLinked");
dialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink1, "Single Element Current");
dialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink2, "Multiples Element Current");
dialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink3, "Element In RvtLinked");

return dialog.Show() == TaskDialogResult.CommandLink1;
return dialog.Show();
}
}
}
11 changes: 10 additions & 1 deletion RevitElementBipChecker/Model/ParameterData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class ParameterData
public ParameterData(Parameter parameter,Document doc,bool isinstance=true)
{
this.Parameter = parameter;
this.BuiltInParameter = (parameter.Definition as InternalDefinition).BuiltInParameter.ToString();
this.BuiltInParameter = (parameter.Definition as InternalDefinition)?.BuiltInParameter.ToString();
this.ParameterName = parameter.Definition.Name;
this.Id = parameter.Id.ToString();
this.ParameterGroup = parameter.Definition.ParameterGroup.ToString();
Expand All @@ -27,6 +27,15 @@ public ParameterData(Parameter parameter,Document doc,bool isinstance=true)
this.AssGlobalParaValue = parameter.GetAssGlobalParameterValue(doc);
}

public ParameterData(Autodesk.Revit.DB.Element element, Parameter parameter, Document doc,
bool isinstance = true) : this(parameter, doc, isinstance)
{
this.ElementId = element.Id.ToString();
this.Category = element.Category?.Name;
}

public string ElementId { get; set; }
public string Category { get; set; }
public Autodesk.Revit.DB.Parameter Parameter { get; set; }
public string ParameterName { get; set; }
public string Id { get; set; }
Expand Down
26 changes: 5 additions & 21 deletions RevitElementBipChecker/View/FrmBipChecker.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,9 @@
Margin="5,5,5,5"
ToolTip="Save Data json format to path document" />
<Button
Command="{Binding SnoopElement}"
Command="{Binding SnoopCommand}"
Content="Snoop"
Margin="5,5,5,5" />
<TextBox
HorizontalContentAlignment="Center"
Margin="5,5,5,5"
Text="{Binding Element.Name}"
ToolTip="Element Id"
VerticalContentAlignment="Center" />
<TextBox
HorizontalContentAlignment="Center"
Margin="5,5,5,5"
Text="{Binding Category}"
ToolTip="Category Of Element"
VerticalContentAlignment="Center" />
<TextBox
HorizontalContentAlignment="Center"
Margin="5,5,5,5"
Text="{Binding Name}"
ToolTip="Element Name"
VerticalContentAlignment="Center" />
<GroupBox Header="State Check">
<Label
Content="{Binding State}"
Expand Down Expand Up @@ -135,7 +117,9 @@
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding ParameterName}" Header="Name" />
<DataGridTextColumn Binding="{Binding ElementId}" Header="ElementId" />
<DataGridTextColumn Binding="{Binding Category}" Header="Category" />
<DataGridTextColumn Binding="{Binding ParameterName}" Header="Parameter Name" />
<DataGridTextColumn Binding="{Binding Id}" Header="Id" />
<DataGridTextColumn Binding="{Binding TypeOrInstance}" Header="TypeOrInstance" />
<DataGridTextColumn Binding="{Binding BuiltInParameter}" Header="BuiltInParameter" />
Expand Down Expand Up @@ -188,7 +172,7 @@
<TextBox
Grid.Column="1"
Grid.Row="0"
Margin="10,0,10,0"
Margin="10,0,0,5"
Text="{Binding SearchText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
VerticalAlignment="Center"
x:Name="tbxSearch">
Expand Down
39 changes: 24 additions & 15 deletions RevitElementBipChecker/Viewmodel/BipCheckerViewmodel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using Autodesk.Revit.DB;
Expand Down Expand Up @@ -82,7 +84,7 @@ public string SearchText
public ICommand OpenCsv => new ExportCsvCommand(this);
public ExportJsonCommand OpenJson => new ExportJsonCommand(this);
private PickFirstCommand PickFirstCommand { get; set; }
public ICommand SnoopElement => new RelayCommand(()=>revitEvent.Run(PickFirstCommand.Toggle, true, null, null, false));
public ICommand SnoopCommand => new RelayCommand(()=>revitEvent.Run(PickFirstCommand.Toggle, true, null, null, false));
public ICommand CheckTypeInstance => new RelayCommand(PickFirstCommand.ToggleTypeInstance);
public BipCheckerViewmodel(UIDocument uidoc)
{
Expand All @@ -96,32 +98,39 @@ public void GetDatabase()
{

Data = new ObservableCollection<ParameterData>();
if(Element==null) return;
if(Elements==null || Elements.Count==0) return;
Elements.ForEach(x => GetParametersData(x));
ObservableCollection<ParameterData> list = Data.GroupBy(x => x.Parameter.Id).Select(x => x.First()).ToObservableCollection();
Data = list;
//Sort
CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(data);
view.SortDescriptions.Add(new SortDescription("ElementId", ListSortDirection.Ascending));
view.SortDescriptions.Add(new SortDescription("TypeOrInstance", ListSortDirection.Ascending));
view.SortDescriptions.Add(new SortDescription("ParameterName", ListSortDirection.Ascending));
}

ObservableCollection<ParameterData> GetParametersData(Autodesk.Revit.DB.Element element)
{
if (IsInstance)
{
foreach (Parameter parameter in Element.Parameters)
foreach (Parameter parameter in element.Parameters)
{

var parameterData = new ParameterData(parameter, Element.Document);
var parameterData = new ParameterData(element, parameter, element.Document);
Data.Add(parameterData);
}
}

if (IsType && ElementType != null)
if (IsType && element.CanHaveTypeAssigned())
{
foreach (Parameter parameter in ElementType.Parameters)
Element elementType = UIDoc.Document.GetElement(element.GetTypeId());
foreach (Parameter parameter in elementType.Parameters)
{
var parameterData = new ParameterData(parameter, Element.Document, false);
var parameterData = new ParameterData(element,parameter, UIDoc.Document, false);
Data.Add(parameterData);
}
}

ObservableCollection<ParameterData> list = Data.GroupBy(x => x.Parameter.Id).Select(x => x.First()).ToObservableCollection();
Data = list;
//Sort
CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(data);
view.SortDescriptions.Add(new SortDescription("TypeOrInstance", ListSortDirection.Ascending));
view.SortDescriptions.Add(new SortDescription("ParameterName", ListSortDirection.Ascending));
return Data;
}
}
}
2 changes: 1 addition & 1 deletion RevitElementBipChecker/Viewmodel/ExportCsvCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void Execute(object parameter)
{
List<ParameterData> parameterDatas = Viewmodel.frmmain.lsBipChecker.Items.Cast<ParameterData>().ToList();
DataTable dataTable = parameterDatas.ToDataTable();
dataTable.Columns.RemoveAt(0);
dataTable.Columns.Remove("Parameter");
string path = dataTable.ExportCsv();
Process.Start(path);
}
Expand Down
77 changes: 58 additions & 19 deletions RevitElementBipChecker/Viewmodel/PickFirstCommand.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Windows;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using RevitElementBipChecker.Model;

Expand All @@ -16,20 +18,26 @@ public PickFirstCommand(BipCheckerViewmodel viewmodel)
}
void PickFirst()
{
if (Viewmodel.Element == null)
if (Viewmodel.Elements == null)
{
bool isintance = DialogUtils.QuestionMsg("Select Option Snoop Element Or LinkElement");
if (isintance == false)
TaskDialogResult result = DialogUtils.QuestionMsg("Select Option Snoop Element Or LinkElement");
switch (result)
{
PickLink_Element();
}
else
{
PickElement();
case TaskDialogResult.CommandLink1:
PickElement();
break;
case TaskDialogResult.CommandLink2:
PickElements();
break;
case TaskDialogResult.CommandLink3:
PickLinkElement();
break;
case TaskDialogResult.CommandLink4:
break;
}
}
}
private void PickLink_Element()
private void PickLinkElement()
{
try
{
Expand All @@ -39,8 +47,9 @@ private void PickLink_Element()
{
RevitLinkInstance linkInstance = e as RevitLinkInstance;
Document linkDocument = linkInstance.GetLinkDocument();
Element = linkDocument.GetElement(pickObject.LinkedElementId);
Viewmodel.Element = Element;
UIDoc = new UIDocument(linkDocument);
Elements = new List<Element>() {linkDocument.GetElement(pickObject.LinkedElementId)};
Viewmodel.Elements = Elements;
Viewmodel.State = "Link Element";
}

Expand All @@ -59,14 +68,14 @@ void PickElement()
try
{
Reference pickObject = Viewmodel.UIDoc.Selection.PickObject(ObjectType.Element);
Element = Viewmodel.UIDoc.Document.GetElement(pickObject);
Viewmodel.Element = Element;
Elements = new List<Element>(){Viewmodel.UIDoc.Document.GetElement(pickObject)};
Viewmodel.Elements = Elements;
Viewmodel.State = "Element";
if (Element.CanHaveTypeAssigned())
{
ElementType = Viewmodel.UIDoc.Document.GetElement(Element.GetTypeId());
Viewmodel.ElementType = ElementType;
}
// if (Elements.CanHaveTypeAssigned())
// {
// ElementType = Viewmodel.UIDoc.Document.GetElement(Elements.GetTypeId());
// Viewmodel.ElementType = ElementType;
// }
}
catch (Autodesk.Revit.Exceptions.OperationCanceledException) { }
catch (Exception)
Expand All @@ -75,10 +84,40 @@ void PickElement()
}
}

private void PickElements()
{
try
{
IList<Reference> pickObject = Viewmodel.UIDoc.Selection.PickObjects(ObjectType.Element);
Elements = ToElements(pickObject, Viewmodel.UIDoc.Document);
Viewmodel.Elements = Elements;
Viewmodel.State = "Elements";
// if (Elements.CanHaveTypeAssigned())
// {
// ElementType = Viewmodel.UIDoc.Document.GetElement(Elements.GetTypeId());
// Viewmodel.ElementType = ElementType;
// }
}
catch (Autodesk.Revit.Exceptions.OperationCanceledException) { }
catch (Exception)
{
// ignored
}
}
List<Element> ToElements(IList<Reference> references, Document document)
{
List<Element> elements = new List<Element>();
foreach (Reference reference in references)
{
elements.Add(document.GetElement(reference));
}
return elements;
}

public void Toggle()
{
Viewmodel.frmmain?.Hide();
Viewmodel.Element = null;
Viewmodel.Elements = null;
PickFirst();
Viewmodel.GetDatabase();
Viewmodel.frmmain?.Show();
Expand Down
8 changes: 5 additions & 3 deletions RevitElementBipChecker/Viewmodel/SelectedCommand.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Linq;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using RevitElementBipChecker.Model;

namespace RevitElementBipChecker.Viewmodel
Expand All @@ -11,8 +13,8 @@ public SelectedCommand(BipCheckerViewmodel vm)
int count = vm.UIDoc.GetSelection().Count;
if (count>0)
{
Element =vm.UIDoc.GetSelection().First();
vm.Element = Element;
Elements =vm.UIDoc.GetSelection();
vm.Elements = Elements;
vm.State = "Element";
vm.GetDatabase();
}
Expand Down

0 comments on commit fceb781

Please sign in to comment.