From 2a076499a6b51ab3c10bd5d1969c2e5444bbb1f3 Mon Sep 17 00:00:00 2001 From: SergeyNefyodov Date: Mon, 6 May 2024 22:23:05 +0200 Subject: [PATCH 1/2] Add TableView support --- .../Core/ComponentModel/DescriptorMap.cs | 1 + .../Descriptors/TableViewDescriptor.cs | 180 ++++++++++++++++++ 2 files changed, 181 insertions(+) create mode 100644 source/RevitLookup/Core/ComponentModel/Descriptors/TableViewDescriptor.cs diff --git a/source/RevitLookup/Core/ComponentModel/DescriptorMap.cs b/source/RevitLookup/Core/ComponentModel/DescriptorMap.cs index f2894e4b..8175fdf5 100644 --- a/source/RevitLookup/Core/ComponentModel/DescriptorMap.cs +++ b/source/RevitLookup/Core/ComponentModel/DescriptorMap.cs @@ -121,6 +121,7 @@ public static Descriptor FindDescriptor(object obj, Type type) Wire value when type == typeof(Wire) => new WireDescriptor(value), ViewSchedule value when type == typeof(ViewSchedule) => new ViewScheduleDescriptor(value), ScheduleDefinition value when type == typeof(ScheduleDefinition) => new ScheduleDefinitionDescriptor(value), + TableView value when type == typeof(TableView) => new TableViewDescriptor(value), TableData value when type == typeof(TableData) => new TableDataDescriptor(value), TableSectionData value when type == typeof(TableSectionData) => new TableSectionDataDescriptor(value), #if REVIT2024_OR_GREATER diff --git a/source/RevitLookup/Core/ComponentModel/Descriptors/TableViewDescriptor.cs b/source/RevitLookup/Core/ComponentModel/Descriptors/TableViewDescriptor.cs new file mode 100644 index 00000000..c77bae44 --- /dev/null +++ b/source/RevitLookup/Core/ComponentModel/Descriptors/TableViewDescriptor.cs @@ -0,0 +1,180 @@ +// Copyright 2003-2024 by Autodesk, Inc. +// +// Permission to use, copy, modify, and distribute this software in +// object code form for any purpose and without fee is hereby granted, +// provided that the above copyright notice appears in all copies and +// that both that copyright notice and the limited warranty and +// restricted rights notice below appear in all supporting +// documentation. +// +// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS. +// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF +// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC. +// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE +// UNINTERRUPTED OR ERROR FREE. +// +// Use, duplication, or disclosure by the U.S. Government is subject to +// restrictions set forth in FAR 52.227-19 (Commercial Computer +// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii) +// (Rights in Technical Data and Computer Software), as applicable. + +using System.Reflection; +using Autodesk.Revit.DB.Electrical; +using RevitLookup.Core.Contracts; +using RevitLookup.Core.Objects; + +namespace RevitLookup.Core.ComponentModel.Descriptors; + +public class TableViewDescriptor(TableView tableView) : Descriptor, IDescriptorResolver +{ + public ResolveSet Resolve(Document context, string target, ParameterInfo[] parameters) + { + return target switch + { + nameof(TableView.GetAvailableParameterCategories) => ResolveAvailableParameterCategories(), + nameof(TableView.GetAvailableParameters) => ResolveAvailableParameters(), + nameof(TableView.GetCalculatedValueName) => ResolveCalculatedValueName(), + nameof(TableView.GetCalculatedValueText) => ResolveCalculatedValueText(), + nameof(TableView.IsValidSectionType) => ResolveIsValidSectionType(), + nameof(TableView.GetCellText) => ResolveCellText(), + _ => null + }; + + ResolveSet ResolveAvailableParameterCategories() + { + if (tableView is not ViewSchedule && tableView is not PanelScheduleView) + return ResolveSet.Append("This view is not a schedule view"); + + TableData tableData = null; + if (tableView is ViewSchedule viewSchedule) + tableData = viewSchedule.GetTableData(); + else if (tableView is PanelScheduleView panelScheduleView) + tableData = panelScheduleView.GetTableData(); + + var sectionTypes = Enum.GetValues(typeof(SectionType)); + var resolveSummary = new ResolveSet(); + foreach (SectionType sectionType in sectionTypes) + { + var tableSectionData = tableData!.GetSectionData(sectionType); + if (tableSectionData is null) continue; + for (var i = tableSectionData.FirstRowNumber; i < tableSectionData.LastRowNumber; i++) + { + resolveSummary.AppendVariant(tableView.GetAvailableParameterCategories(sectionType, i), + $"{sectionType}: Row {i}"); + } + } + + return resolveSummary; + } + + ResolveSet ResolveAvailableParameters() + { + var categories = context.Settings.Categories; + var resolveSummary = new ResolveSet(categories.Size); + foreach (Category category in categories) + { + var result = TableView.GetAvailableParameters(context, category.Id); + resolveSummary.AppendVariant(result, $"{category.Name}"); + } + + return resolveSummary; + } + + ResolveSet ResolveCalculatedValueName() + { + if (tableView is not ViewSchedule && tableView is not PanelScheduleView) + return ResolveSet.Append("This view is not a schedule view"); + + TableData tableData = null; + if (tableView is ViewSchedule viewSchedule) + tableData = viewSchedule.GetTableData(); + else if (tableView is PanelScheduleView panelScheduleView) + tableData = panelScheduleView.GetTableData(); + + var sectionTypes = Enum.GetValues(typeof(SectionType)); + var resolveSummary = new ResolveSet(); + foreach (SectionType sectionType in sectionTypes) + { + var tableSectionData = tableData!.GetSectionData(sectionType); + if (tableSectionData is null) continue; + for (var i = tableSectionData.FirstRowNumber; i < tableSectionData.LastRowNumber; i++) + for (var j = tableSectionData.FirstColumnNumber; j < tableSectionData.LastColumnNumber; j++) + { + var result = tableView.GetCalculatedValueName(sectionType, i, j); + resolveSummary.AppendVariant(result, $"{sectionType}, Row {i}, Column {j}: {result}"); + } + } + + return resolveSummary; + } + + ResolveSet ResolveCalculatedValueText() + { + if (tableView is not ViewSchedule && tableView is not PanelScheduleView) + return ResolveSet.Append("This view is not a schedule view"); + + TableData tableData = null; + if (tableView is ViewSchedule viewSchedule) + tableData = viewSchedule.GetTableData(); + else if (tableView is PanelScheduleView panelScheduleView) + tableData = panelScheduleView.GetTableData(); + + var sectionTypes = Enum.GetValues(typeof(SectionType)); + var resolveSummary = new ResolveSet(); + foreach (SectionType sectionType in sectionTypes) + { + var tableSectionData = tableData!.GetSectionData(sectionType); + if (tableSectionData is null) continue; + for (var i = tableSectionData.FirstRowNumber; i < tableSectionData.LastRowNumber; i++) + for (var j = tableSectionData.FirstColumnNumber; j < tableSectionData.LastColumnNumber; j++) + { + var result = tableView.GetCalculatedValueText(sectionType, i, j); + resolveSummary.AppendVariant(result, $"{sectionType}, Row {i}, Column {j}: {result}"); + } + } + + return resolveSummary; + } + + ResolveSet ResolveCellText() + { + if (tableView is not ViewSchedule && tableView is not PanelScheduleView) + return ResolveSet.Append("This view is not a schedule view"); + + TableData tableData = null; + if (tableView is ViewSchedule viewSchedule) + tableData = viewSchedule.GetTableData(); + else if (tableView is PanelScheduleView panelScheduleView) + tableData = panelScheduleView.GetTableData(); + + var sectionTypes = Enum.GetValues(typeof(SectionType)); + var resolveSummary = new ResolveSet(); + foreach (SectionType sectionType in sectionTypes) + { + var tableSectionData = tableData!.GetSectionData(sectionType); + if (tableSectionData is null) continue; + for (var i = tableSectionData.FirstRowNumber; i < tableSectionData.LastRowNumber; i++) + for (var j = tableSectionData.FirstColumnNumber; j < tableSectionData.LastColumnNumber; j++) + { + var result = tableView.GetCellText(sectionType, i, j); + resolveSummary.AppendVariant(result, $"{sectionType}, Row {i}, Column {j}: {result}"); + } + } + + return resolveSummary; + } + + ResolveSet ResolveIsValidSectionType() + { + var sectionTypes = Enum.GetValues(typeof(SectionType)); + var resolveSummary = new ResolveSet(); + foreach (SectionType sectionType in sectionTypes) + { + var result = tableView.IsValidSectionType(sectionType); + resolveSummary.AppendVariant(result, $"{sectionType}: {result}"); + } + + return resolveSummary; + } + } +} \ No newline at end of file From ac82da29669513b20538e1d5cf1e202e689b1035 Mon Sep 17 00:00:00 2001 From: Nice3point Date: Tue, 7 May 2024 20:22:14 +0300 Subject: [PATCH 2/2] Cleanup --- .../Descriptors/TableViewDescriptor.cs | 79 ++++++------------- 1 file changed, 24 insertions(+), 55 deletions(-) diff --git a/source/RevitLookup/Core/ComponentModel/Descriptors/TableViewDescriptor.cs b/source/RevitLookup/Core/ComponentModel/Descriptors/TableViewDescriptor.cs index c77bae44..d1f3ab1a 100644 --- a/source/RevitLookup/Core/ComponentModel/Descriptors/TableViewDescriptor.cs +++ b/source/RevitLookup/Core/ComponentModel/Descriptors/TableViewDescriptor.cs @@ -31,7 +31,7 @@ public ResolveSet Resolve(Document context, string target, ParameterInfo[] param { return target switch { - nameof(TableView.GetAvailableParameterCategories) => ResolveAvailableParameterCategories(), + // nameof(TableView.GetAvailableParameterCategories) => ResolveAvailableParameterCategories(), disabled, long computation time nameof(TableView.GetAvailableParameters) => ResolveAvailableParameters(), nameof(TableView.GetCalculatedValueName) => ResolveCalculatedValueName(), nameof(TableView.GetCalculatedValueText) => ResolveCalculatedValueText(), @@ -40,33 +40,6 @@ public ResolveSet Resolve(Document context, string target, ParameterInfo[] param _ => null }; - ResolveSet ResolveAvailableParameterCategories() - { - if (tableView is not ViewSchedule && tableView is not PanelScheduleView) - return ResolveSet.Append("This view is not a schedule view"); - - TableData tableData = null; - if (tableView is ViewSchedule viewSchedule) - tableData = viewSchedule.GetTableData(); - else if (tableView is PanelScheduleView panelScheduleView) - tableData = panelScheduleView.GetTableData(); - - var sectionTypes = Enum.GetValues(typeof(SectionType)); - var resolveSummary = new ResolveSet(); - foreach (SectionType sectionType in sectionTypes) - { - var tableSectionData = tableData!.GetSectionData(sectionType); - if (tableSectionData is null) continue; - for (var i = tableSectionData.FirstRowNumber; i < tableSectionData.LastRowNumber; i++) - { - resolveSummary.AppendVariant(tableView.GetAvailableParameterCategories(sectionType, i), - $"{sectionType}: Row {i}"); - } - } - - return resolveSummary; - } - ResolveSet ResolveAvailableParameters() { var categories = context.Settings.Categories; @@ -82,14 +55,12 @@ ResolveSet ResolveAvailableParameters() ResolveSet ResolveCalculatedValueName() { - if (tableView is not ViewSchedule && tableView is not PanelScheduleView) - return ResolveSet.Append("This view is not a schedule view"); - - TableData tableData = null; - if (tableView is ViewSchedule viewSchedule) - tableData = viewSchedule.GetTableData(); - else if (tableView is PanelScheduleView panelScheduleView) - tableData = panelScheduleView.GetTableData(); + var tableData = tableView switch + { + ViewSchedule viewSchedule => viewSchedule.GetTableData(), + PanelScheduleView panelScheduleView => panelScheduleView.GetTableData(), + _ => throw new NotSupportedException($"{tableView.GetType().FullName} is not supported in the current API version") + }; var sectionTypes = Enum.GetValues(typeof(SectionType)); var resolveSummary = new ResolveSet(); @@ -97,11 +68,12 @@ ResolveSet ResolveCalculatedValueName() { var tableSectionData = tableData!.GetSectionData(sectionType); if (tableSectionData is null) continue; + for (var i = tableSectionData.FirstRowNumber; i < tableSectionData.LastRowNumber; i++) for (var j = tableSectionData.FirstColumnNumber; j < tableSectionData.LastColumnNumber; j++) { var result = tableView.GetCalculatedValueName(sectionType, i, j); - resolveSummary.AppendVariant(result, $"{sectionType}, Row {i}, Column {j}: {result}"); + resolveSummary.AppendVariant(result, $"{sectionType}, row {i}, column {j}: {result}"); } } @@ -110,14 +82,12 @@ ResolveSet ResolveCalculatedValueName() ResolveSet ResolveCalculatedValueText() { - if (tableView is not ViewSchedule && tableView is not PanelScheduleView) - return ResolveSet.Append("This view is not a schedule view"); - - TableData tableData = null; - if (tableView is ViewSchedule viewSchedule) - tableData = viewSchedule.GetTableData(); - else if (tableView is PanelScheduleView panelScheduleView) - tableData = panelScheduleView.GetTableData(); + var tableData = tableView switch + { + ViewSchedule viewSchedule => viewSchedule.GetTableData(), + PanelScheduleView panelScheduleView => panelScheduleView.GetTableData(), + _ => throw new NotSupportedException($"{tableView.GetType().FullName} is not supported in the current API version") + }; var sectionTypes = Enum.GetValues(typeof(SectionType)); var resolveSummary = new ResolveSet(); @@ -125,11 +95,12 @@ ResolveSet ResolveCalculatedValueText() { var tableSectionData = tableData!.GetSectionData(sectionType); if (tableSectionData is null) continue; + for (var i = tableSectionData.FirstRowNumber; i < tableSectionData.LastRowNumber; i++) for (var j = tableSectionData.FirstColumnNumber; j < tableSectionData.LastColumnNumber; j++) { var result = tableView.GetCalculatedValueText(sectionType, i, j); - resolveSummary.AppendVariant(result, $"{sectionType}, Row {i}, Column {j}: {result}"); + resolveSummary.AppendVariant(result, $"{sectionType}, row {i}, column {j}: {result}"); } } @@ -138,14 +109,12 @@ ResolveSet ResolveCalculatedValueText() ResolveSet ResolveCellText() { - if (tableView is not ViewSchedule && tableView is not PanelScheduleView) - return ResolveSet.Append("This view is not a schedule view"); - - TableData tableData = null; - if (tableView is ViewSchedule viewSchedule) - tableData = viewSchedule.GetTableData(); - else if (tableView is PanelScheduleView panelScheduleView) - tableData = panelScheduleView.GetTableData(); + var tableData = tableView switch + { + ViewSchedule viewSchedule => viewSchedule.GetTableData(), + PanelScheduleView panelScheduleView => panelScheduleView.GetTableData(), + _ => throw new NotSupportedException($"{tableView.GetType().FullName} is not supported in the current API version") + }; var sectionTypes = Enum.GetValues(typeof(SectionType)); var resolveSummary = new ResolveSet(); @@ -157,7 +126,7 @@ ResolveSet ResolveCellText() for (var j = tableSectionData.FirstColumnNumber; j < tableSectionData.LastColumnNumber; j++) { var result = tableView.GetCellText(sectionType, i, j); - resolveSummary.AppendVariant(result, $"{sectionType}, Row {i}, Column {j}: {result}"); + resolveSummary.AppendVariant(result, $"{sectionType}, row {i}, column {j}: {result}"); } }