using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using HStation.RevitDev.Model.ModelEnum;
using HStation.RevitDev.RevitDataExport.Common;
using HStation.RevitDev.RevitDataExport.Entity;
using HStation.RevitDev.RevitDataExport.Service;
using HStation.RevitDev.RevitDataExport.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using DataGrid = System.Windows.Controls.DataGrid;
namespace HStation.RevitDev.RevitDataExport.Forms
{
///
/// WPF_FamilyPanel.xaml 的交互逻辑
///
public partial class Wpf_InstancePanel : UserControl, IDockablePaneProvider
{
public Wpf_InstancePanel()
{
InitializeComponent();
InitializeControls();
}
private void InitializeControls()
{
var values = Enum.GetValues(typeof(RevitType));
this.tabControl.Items.Clear();
foreach (var value in values)
{
var revitType = (RevitType)value;
if (revitType.IsRequired())
{
TabItem tabItem = CreateTabItem(revitType);
this.tabControl.Items.Add(tabItem);
}
}
}
private TabItem CreateTabItem(RevitType revitType)
{
var tabItem = new TabItem();
tabItem.Tag = revitType;
tabItem.Height = 30;
tabItem.Header = revitType.GetDescription();
DataGrid dataGrid = CreateDataGrid(revitType);
tabItem.Content = dataGrid;
return tabItem;
}
private DataGrid CreateDataGrid(RevitType revitType)
{
var dataGrid = new DataGrid();
dataGrid.IsReadOnly = true;
dataGrid.LoadingRow += DataGrid_LoadingRow;
dataGrid.SelectionUnit = DataGridSelectionUnit.Cell;
dataGrid.MouseDoubleClick += DataGrid_MouseDoubleClick;
dataGrid.CanUserAddRows = false;
CreateRows(dataGrid, revitType);
return dataGrid;
}
private static void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
e.Row.Header = e.Row.GetIndex() + 1;
}
private void DataGrid_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
try
{
DataGrid dataGrid = sender as DataGrid;
var selectItem = dataGrid.SelectedCells;
var selectCell = selectItem[0];
var columnIndex = selectCell.Column.DisplayIndex;
if (columnIndex == 0)
{
ShowElement(selectCell);
}
else if (columnIndex == 2)
{
ShowLinkedElements(selectCell);
}
}
catch (Exception ex)
{
TaskDialog.Show("错误", ex.Message);
}
}
private void ShowLinkedElements(DataGridCellInfo selectCell)
{
string ids = (selectCell.Item as ElementModel).LinkIds;
if (string.IsNullOrEmpty(ids))
{
return;
}
var idArray = ids.Split(',');
var idList = idArray.Select(x => new ElementId(int.Parse(x)))?.ToList();
GlobalResource.CurrentUIDocument.Selection.SetElementIds(idList);
GlobalResource.CurrentUIDocument.ShowElements(idList);
}
private void ShowElement(DataGridCellInfo selectCell)
{
string id = (selectCell.Item as ElementModel).Id;
if (string.IsNullOrEmpty(id))
{
return;
}
var elemId = new ElementId(int.Parse(id));
GlobalResource.CurrentUIDocument.Selection.SetElementIds(
new List { elemId });
GlobalResource.CurrentUIDocument.ShowElements(elemId);
}
private void DataGrid_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
try
{
DataGrid dataGrid = sender as DataGrid;
var selectCells = dataGrid.SelectedCells;
string ids = (selectCells[0].Item as ElementModel).LinkIds;
var idArray = ids.Split(',');
var idList = idArray.Select(x => new ElementId(int.Parse(x)))?.ToList();
GlobalResource.CurrentUIDocument.Selection.SetElementIds(idList);
}
catch (Exception ex)
{
TaskDialog.Show("错误", ex.Message);
}
}
private void CreateRows(DataGrid dataGrid, RevitType revitType)
{
var models = RevitMepElementService.GetElementModels(revitType);
dataGrid.ItemsSource = models;
}
public void SetupDockablePane(DockablePaneProviderData data)
{
data.FrameworkElement = this;
DockablePaneState state = new DockablePaneState();
state.DockPosition = DockPosition.Bottom;
state.MinimumWidth = GlobalResource.ThumbnailSize;
state.MinimumHeight = GlobalResource.ThumbnailSize;
data.InitialState = state;
}
private void reflesh_Click(object sender, RoutedEventArgs e)
{
DocumentUtil.AddUnhandledElements();
UpdateForm();
}
public void UpdateForm()
{
InitializeControls();
}
}
}