diff --git a/RvtVa3c/Command.cs b/RvtVa3c/Command.cs index 3b2392d..675dce8 100644 --- a/RvtVa3c/Command.cs +++ b/RvtVa3c/Command.cs @@ -1,165 +1,393 @@ #region Namespaces using System; +using System.Windows.Interop; using System.Collections.Generic; using System.Diagnostics; using System.IO; +using System.Drawing; using Autodesk.Revit.ApplicationServices; using Autodesk.Revit.Attributes; using Autodesk.Revit.DB; using Autodesk.Revit.UI; using Autodesk.Revit.UI.Selection; +using System.Windows.Forms; using SaveFileDialog = System.Windows.Forms.SaveFileDialog; using DialogResult = System.Windows.Forms.DialogResult; -#endregion // Namespaces +using System.Linq; + + + +#endregion namespace RvtVa3c { - [Transaction( TransactionMode.Manual )] - public class Command : IExternalCommand - { - /// - /// Custom assembly resolver to find our support - /// DLL without being forced to place our entire - /// application in a subfolder of the Revit.exe - /// directory. - /// - System.Reflection.Assembly - CurrentDomain_AssemblyResolve( - object sender, - ResolveEventArgs args ) + [Transaction(TransactionMode.Manual)] + public class Command : IExternalCommand { - if( args.Name.Contains( "Newtonsoft" ) ) - { - string filename = Path.GetDirectoryName( - System.Reflection.Assembly - .GetExecutingAssembly().Location ); + /// + /// Custom assembly resolver to find our support + /// DLL without being forced to place our entire + /// application in a subfolder of the Revit.exe + /// directory. + /// + System.Reflection.Assembly + CurrentDomain_AssemblyResolve( + object sender, + ResolveEventArgs args) + { + if (args.Name.Contains("Newtonsoft")) + { + string filename = Path.GetDirectoryName( + System.Reflection.Assembly + .GetExecutingAssembly().Location); + + filename = Path.Combine(filename, + "Newtonsoft.Json.dll"); - filename = Path.Combine( filename, - "Newtonsoft.Json.dll" ); + if (File.Exists(filename)) + { + return System.Reflection.Assembly + .LoadFrom(filename); + } + } + return null; + } - if( File.Exists( filename ) ) + /// + /// Export a given 3D view to JSON using + /// our custom exporter context. + /// + public void ExportView3D(View3D view3d, string filename) { - return System.Reflection.Assembly - .LoadFrom( filename ); + AppDomain.CurrentDomain.AssemblyResolve + += CurrentDomain_AssemblyResolve; + + Document doc = view3d.Document; + + Va3cExportContext context + = new Va3cExportContext(doc, filename); + + CustomExporter exporter = new CustomExporter( + doc, context); + + // Note: Excluding faces just suppresses the + // OnFaceBegin calls, not the actual processing + // of face tessellation. Meshes of the faces + // will still be received by the context. + + exporter.IncludeFaces = false; + + exporter.ShouldStopOnError = false; + + exporter.Export(view3d); } - } - return null; - } - /// - /// Export a given 3D view to JSON using - /// our custom exporter context. - /// - public void ExportView3D( View3D view3d, string filename ) - { - AppDomain.CurrentDomain.AssemblyResolve - += CurrentDomain_AssemblyResolve; - Document doc = view3d.Document; + public static ParameterFilter _filter; + public static bool _filterParameters = false; + public static TabControl _tabControl; + public static Dictionary> _parameterDictionary; + public static Dictionary> _toExportDictionary; + public static bool includeT = false; - Va3cExportContext context - = new Va3cExportContext( doc, filename ); + /// + /// Function to filter the parameters of the objects in the scene + /// + /// Revit Document + /// Include Type Parameters in the filter dialog + public void filterElementParameters(Document doc,bool includeType) + { + _parameterDictionary = new Dictionary>(); + _toExportDictionary = new Dictionary>(); - CustomExporter exporter = new CustomExporter( - doc, context ); + FilteredElementCollector collector = new FilteredElementCollector(doc, doc.ActiveView.Id); - // Note: Excluding faces just suppresses the - // OnFaceBegin calls, not the actual processing - // of face tessellation. Meshes of the faces - // will still be received by the context. + // create a dictionary with all the properties for each category + foreach (var fi in collector) + { - exporter.IncludeFaces = false; + string category = fi.Category.Name; - exporter.ShouldStopOnError = false; + if (category!= "Title Blocks" && category!="Generic Annotations" && category != "Detail Items" && category!= "Cameras") + { + IList parameters = fi.GetOrderedParameters(); + List parameterNames = new List(); - exporter.Export( view3d ); - } + foreach (Parameter p in parameters) + { + string pName = p.Definition.Name; + string tempVal = ""; - #region SelectFile - /// - /// Store the last user selected output folder - /// in the current editing session. - /// - static string _output_folder_path = null; - - /// - /// Return true is user selects and confirms - /// output file name and folder. - /// - static bool SelectFile( - ref string folder_path, - ref string filename ) - { - SaveFileDialog dlg = new SaveFileDialog(); + if (StorageType.String == p.StorageType) + { + tempVal = p.AsString(); + } + else + { + tempVal = p.AsValueString(); + } + if (!string.IsNullOrEmpty(tempVal)) + { + if (_parameterDictionary.ContainsKey(category)) + { + if (!_parameterDictionary[category].Contains(pName)) + { + _parameterDictionary[category].Add(pName); + } + } + else + { + parameterNames.Add(pName); + } + } + } + if (parameterNames.Count > 0) + { + _parameterDictionary.Add(category, parameterNames); + } + if (includeType) + { + ElementId idType = fi.GetTypeId(); - dlg.Title = "Select JSON Output File"; - dlg.Filter = "JSON files|*.js"; + if (ElementId.InvalidElementId != idType) + { + Element typ = doc.GetElement(idType); + parameters = typ.GetOrderedParameters(); + List parameterTypes = new List(); + foreach (Parameter p in parameters) + { + string pName = "Type " + p.Definition.Name; + string tempVal = ""; + if (!_parameterDictionary[category].Contains(pName)) + { + if (StorageType.String == p.StorageType) + { + tempVal = p.AsString(); + } + else + { + tempVal = p.AsValueString(); + } - if( null != folder_path - && 0 < folder_path.Length ) - { - dlg.InitialDirectory = folder_path; - } + if (!string.IsNullOrEmpty(tempVal)) + { + if (_parameterDictionary.ContainsKey(category)) + { + if (!_parameterDictionary[category].Contains(pName)) + { + _parameterDictionary[category].Add(pName); + } + } + else + { + parameterTypes.Add(pName); + } + } + } + } + if (parameterTypes.Count > 0) + { + _parameterDictionary[category].AddRange(parameterTypes); + } + } - dlg.FileName = filename; + } + } + } - bool rc = DialogResult.OK == dlg.ShowDialog(); + //CREATE FILTER UI + _filter = new ParameterFilter(); - if( rc ) - { - filename = Path.Combine( dlg.InitialDirectory, - dlg.FileName ); + _tabControl = new TabControl(); + _tabControl.Size = new System.Drawing.Size(600, 375); + _tabControl.Location = new System.Drawing.Point(0, 55); + _tabControl.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) + | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); - folder_path = Path.GetDirectoryName( - filename ); - } - return rc; - } - #endregion // SelectFile + int j = 8; - public Result Execute( - ExternalCommandData commandData, - ref string message, - ElementSet elements ) - { - UIApplication uiapp = commandData.Application; - UIDocument uidoc = uiapp.ActiveUIDocument; - Application app = uiapp.Application; - Document doc = uidoc.Document; - - if( doc.ActiveView is View3D ) - { - string filename = doc.PathName; - if( 0 == filename.Length ) - { - filename = doc.Title; + // Populate the parameters as a checkbox in each tab + foreach (string c in _parameterDictionary.Keys) + { + //Create a checklist + CheckedListBox checkList = new CheckedListBox(); + + //set the properties of the checklist + checkList.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right))); + checkList.FormattingEnabled = true; + checkList.HorizontalScrollbar = true; + checkList.Items.AddRange(_parameterDictionary[c].ToArray()); + checkList.MultiColumn = true; + checkList.Size = new System.Drawing.Size(560, 360); + checkList.ColumnWidth = 200; + checkList.CheckOnClick = true; + checkList.TabIndex = j; + j++; + + for (int i = 0; i <= (checkList.Items.Count - 1); i++) + { + checkList.SetItemCheckState(i, CheckState.Checked); + } + + //add a tab + TabPage tab = new TabPage(c); + tab.Name = c; + + //attach the checklist to the tab + tab.Controls.Add(checkList); + + // attach the tab to the tab control + _tabControl.TabPages.Add(tab); + } + + // Attach the tab control to the filter form + _filter.Controls.Add(_tabControl); + + // DISPLAY FILTER UI + _filter.ShowDialog(); + + // Loop thru each tab and get the parameters to export + foreach (TabPage tab in _tabControl.TabPages) + { + List parametersToExport = new List(); + foreach (var checkedP in ((CheckedListBox)tab.Controls[0]).CheckedItems) + { + parametersToExport.Add(checkedP.ToString()); + } + _toExportDictionary.Add(tab.Name, parametersToExport); + } } - if( null == _output_folder_path ) + + + #region SelectFile + /// + /// Store the last user selected output folder + /// in the current editing session. + /// + static string _output_folder_path = null; + + + /// + /// Return true is user selects and confirms + /// output file name and folder. + /// + static bool SelectFile( + ref string folder_path, + ref string filename) { - _output_folder_path = Path.GetDirectoryName( - filename ); + SaveFileDialog dlg = new SaveFileDialog(); + + dlg.Title = "Select JSON Output File"; + dlg.Filter = "JSON files|*.js"; + + if (null != folder_path + && 0 < folder_path.Length) + { + dlg.InitialDirectory = folder_path; + } + + dlg.FileName = filename; + + bool rc = DialogResult.OK == dlg.ShowDialog(); + + if (rc) + { + filename = Path.Combine(dlg.InitialDirectory, + dlg.FileName); + + folder_path = Path.GetDirectoryName( + filename); + } + return rc; } - filename = Path.GetFileName( filename ) + ".js"; + #endregion // SelectFile - if( SelectFile( ref _output_folder_path, - ref filename ) ) + public Result Execute( + ExternalCommandData commandData, + ref string message, + ElementSet elements) { - filename = Path.Combine( _output_folder_path, - filename ); + UIApplication uiapp = commandData.Application; + UIDocument uidoc = uiapp.ActiveUIDocument; + Autodesk.Revit.ApplicationServices.Application app = uiapp.Application; + Document doc = uidoc.Document; + + + + if (doc.ActiveView is View3D) + { + string filename = doc.PathName; + if (0 == filename.Length) + { + filename = doc.Title; + } + if (null == _output_folder_path) + { + //sometimes the command fails if the file is detached from central and not saved locally + try + { + _output_folder_path = Path.GetDirectoryName( + filename); + } + catch + { + TaskDialog.Show("Folder not found", "Please, save the file and run the command again"); + return Result.Failed; + } + } + + //Dialog to ask the user if they want to choose which parameters to export or just export them all + TaskDialog td = new TaskDialog("Ask user to filter parameters"); + td.Title = "Filter parameters"; + td.CommonButtons = TaskDialogCommonButtons.No | TaskDialogCommonButtons.Yes; + td.MainInstruction = "Do you want to filter the parameters of the objects to be exported?"; + td.MainContent = "Click Yes and you will be able to select parameters for each category in the next window"; + td.AllowCancellation = true; + td.VerificationText = "Check this to include type properties"; + TaskDialogResult tdResult = td.Show(); + + if (td.WasVerificationChecked()) includeT = true; + else includeT = false; + + if (tdResult == TaskDialogResult.Yes) + { + // Filter the properties + filterElementParameters(doc, includeT); + _filterParameters = true; + if (ParameterFilter.status == "cancelled") + { + ParameterFilter.status = ""; + return Result.Cancelled; + } + } + else _filterParameters = false; + + + ViewOrientation3D vo = ((View3D)doc.ActiveView).GetOrientation(); + + // Save file + filename = Path.GetFileName(filename) + ".js"; + + if (SelectFile(ref _output_folder_path, + ref filename)) + { + filename = Path.Combine(_output_folder_path, + filename); - ExportView3D( doc.ActiveView as View3D, - filename ); + ExportView3D(doc.ActiveView as View3D, + filename); - return Result.Succeeded; + return Result.Succeeded; + } + return Result.Cancelled; + } + else + { + Util.ErrorMsg( + "You must be in a 3D view to export."); + } + return Result.Failed; } - return Result.Cancelled; - } - else - { - Util.ErrorMsg( - "You must be in a 3D view to export." ); - } - return Result.Failed; } - } } diff --git a/RvtVa3c/ParameterFilter.Designer.cs b/RvtVa3c/ParameterFilter.Designer.cs new file mode 100644 index 0000000..8ca0f0a --- /dev/null +++ b/RvtVa3c/ParameterFilter.Designer.cs @@ -0,0 +1,129 @@ +namespace RvtVa3c +{ + partial class ParameterFilter + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.checkUncheck = new System.Windows.Forms.Button(); + this.export_button = new System.Windows.Forms.Button(); + this.cancelButton = new System.Windows.Forms.Button(); + this.Description = new System.Windows.Forms.Label(); + this.checkBox1 = new System.Windows.Forms.CheckBox(); + this.SuspendLayout(); + // + // checkUncheck + // + this.checkUncheck.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); + this.checkUncheck.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.checkUncheck.Location = new System.Drawing.Point(12, 447); + this.checkUncheck.Name = "checkUncheck"; + this.checkUncheck.Size = new System.Drawing.Size(139, 33); + this.checkUncheck.TabIndex = 4; + this.checkUncheck.Text = "Check / Uncheck All"; + this.checkUncheck.UseVisualStyleBackColor = true; + this.checkUncheck.Click += new System.EventHandler(this.checkUncheck_Click); + // + // export_button + // + this.export_button.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.export_button.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.export_button.Location = new System.Drawing.Point(395, 447); + this.export_button.Name = "export_button"; + this.export_button.Size = new System.Drawing.Size(93, 33); + this.export_button.TabIndex = 5; + this.export_button.Text = "EXPORT"; + this.export_button.UseVisualStyleBackColor = true; + this.export_button.Click += new System.EventHandler(this.export_button_Click); + // + // cancelButton + // + this.cancelButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.cancelButton.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.cancelButton.Location = new System.Drawing.Point(505, 447); + this.cancelButton.Name = "cancelButton"; + this.cancelButton.Size = new System.Drawing.Size(87, 33); + this.cancelButton.TabIndex = 6; + this.cancelButton.Text = "CANCEL"; + this.cancelButton.UseVisualStyleBackColor = true; + this.cancelButton.Click += new System.EventHandler(this.cancelButton_Click); + // + // Description + // + this.Description.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.Description.Location = new System.Drawing.Point(9, 9); + this.Description.Name = "Description"; + this.Description.Size = new System.Drawing.Size(398, 45); + this.Description.TabIndex = 7; + this.Description.Text = "Select which parameters should be included with each category"; + // + // checkBox1 + // + this.checkBox1.AutoSize = true; + this.checkBox1.Checked = true; + this.checkBox1.CheckState = System.Windows.Forms.CheckState.Checked; + this.checkBox1.Location = new System.Drawing.Point(159, 455); + this.checkBox1.Name = "checkBox1"; + this.checkBox1.Size = new System.Drawing.Size(188, 17); + this.checkBox1.TabIndex = 8; + this.checkBox1.Text = "Check / Uncheck current tab only"; + this.checkBox1.UseVisualStyleBackColor = true; + this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged); + // + // ParameterFilter + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.BackColor = System.Drawing.SystemColors.ButtonHighlight; + this.ClientSize = new System.Drawing.Size(604, 492); + this.ControlBox = false; + this.Controls.Add(this.checkBox1); + this.Controls.Add(this.cancelButton); + this.Controls.Add(this.export_button); + this.Controls.Add(this.checkUncheck); + this.Controls.Add(this.Description); + this.MaximumSize = new System.Drawing.Size(900, 530); + this.MinimumSize = new System.Drawing.Size(620, 530); + this.Name = "ParameterFilter"; + this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; + this.Text = "Parameter Filter"; + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.Button checkUncheck; + private System.Windows.Forms.Button export_button; + private System.Windows.Forms.Button cancelButton; + private System.Windows.Forms.Label Description; + private System.Windows.Forms.CheckBox checkBox1; + + + + } +} \ No newline at end of file diff --git a/RvtVa3c/ParameterFilter.cs b/RvtVa3c/ParameterFilter.cs new file mode 100644 index 0000000..ec1ddfb --- /dev/null +++ b/RvtVa3c/ParameterFilter.cs @@ -0,0 +1,99 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace RvtVa3c +{ + public partial class ParameterFilter : Form + { + public ParameterFilter() + { + InitializeComponent(); + } + + + public static string status = ""; + private bool changeAll = false; + /// + /// Function to check or uncheck all the checkboxes in a tab + /// + /// + /// + private void checkUncheck_Click(object sender, EventArgs e) + { + + int index = Command._tabControl.SelectedIndex; + CheckedListBox currentCheckList = new CheckedListBox(); + if (!changeAll) + { + currentCheckList = (CheckedListBox)((TabPage)(Command._tabControl.GetControl(index))).Controls[0]; + + bool areAllChecked = true; + if (currentCheckList.CheckedItems.Count < currentCheckList.Items.Count) areAllChecked = false; + checkUncheckBoxes(currentCheckList, areAllChecked); + } + else + { + List allCheckLists = new List(); + bool areAllChecked = true; + + foreach (TabPage tab in Command._tabControl.TabPages) + { + currentCheckList = (CheckedListBox) tab.Controls[0]; + if (currentCheckList.CheckedItems.Count < currentCheckList.Items.Count) + { + areAllChecked = false; + break; + } + } + + foreach (TabPage tab in Command._tabControl.TabPages) + { + currentCheckList = (CheckedListBox)tab.Controls[0]; + checkUncheckBoxes(currentCheckList, areAllChecked); + } + } + } + + private void export_button_Click(object sender, EventArgs e) + { + this.Close(); + } + + private void cancelButton_Click(object sender, EventArgs e) + { + this.Close(); + status = "cancelled"; + } + + private void checkBox1_CheckedChanged(object sender, EventArgs e) + { + if (checkBox1.Checked) changeAll = false; + else changeAll = true; + } + + private void checkUncheckBoxes(CheckedListBox currentCheckList, bool areAllChecked) + { + if (!areAllChecked) + { + for (int i = 0; i <= (currentCheckList.Items.Count - 1); i++) + { + currentCheckList.SetItemCheckState(i, CheckState.Checked); + } + } + else + { + for (int i = 0; i <= (currentCheckList.Items.Count - 1); i++) + { + currentCheckList.SetItemCheckState(i, CheckState.Unchecked); + } + } + } + } +} diff --git a/RvtVa3c/ParameterFilter.resx b/RvtVa3c/ParameterFilter.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/RvtVa3c/ParameterFilter.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/RvtVa3c/RvtVa3c.csproj b/RvtVa3c/RvtVa3c.csproj index a6546aa..fbd8472 100644 --- a/RvtVa3c/RvtVa3c.csproj +++ b/RvtVa3c/RvtVa3c.csproj @@ -48,6 +48,8 @@ packages\Newtonsoft.Json.dll + + $(ProgramW6432)\Autodesk\Revit 2015\RevitAPI.dll False @@ -57,6 +59,8 @@ False + + @@ -65,6 +69,12 @@ + + Form + + + ParameterFilter.cs + @@ -76,6 +86,11 @@ + + + ParameterFilter.cs + + diff --git a/RvtVa3c/Util.cs b/RvtVa3c/Util.cs index f0c48fb..5aebba0 100644 --- a/RvtVa3c/Util.cs +++ b/RvtVa3c/Util.cs @@ -1,5 +1,6 @@ #region Namespaces using System; +using Autodesk.Revit.UI; using System.Collections.Generic; using System.Diagnostics; using Autodesk.Revit.DB; @@ -8,217 +9,311 @@ namespace RvtVa3c { - class Util - { - const string _caption = "vA3C"; - - /// - /// Display an error message to the user. - /// - public static void ErrorMsg( string msg ) + class Util { - Debug.WriteLine( msg ); - WinForms.MessageBox.Show( msg, - _caption, - WinForms.MessageBoxButtons.OK, - WinForms.MessageBoxIcon.Error ); - } - - /// - /// Return a string for a real number - /// formatted to two decimal places. - /// - public static string RealString( double a ) - { - return a.ToString( "0.##" ); - } + const string _caption = "vA3C"; - /// - /// Return a string for an XYZ point - /// or vector with its coordinates - /// formatted to two decimal places. - /// - public static string PointString( XYZ p ) - { - return string.Format( "({0},{1},{2})", - RealString( p.X ), - RealString( p.Y ), - RealString( p.Z ) ); - } + /// + /// Display an error message to the user. + /// + public static void ErrorMsg(string msg) + { + Debug.WriteLine(msg); + WinForms.MessageBox.Show(msg, + _caption, + WinForms.MessageBoxButtons.OK, + WinForms.MessageBoxIcon.Error); + } - /// - /// Return an integer value for a Revit Color. - /// - public static int ColorToInt( Color color ) - { - return ( (int) color.Red ) << 16 - | ( (int) color.Green ) << 8 - | (int) color.Blue; - } + /// + /// Return a string for a real number + /// formatted to two decimal places. + /// + public static string RealString(double a) + { + return a.ToString("0.##"); + } - /// - /// Extract a true or false value from the given - /// string, accepting yes/no, Y/N, true/false, T/F - /// and 1/0. We are extremely tolerant, i.e., any - /// value starting with one of the characters y, n, - /// t or f is also accepted. Return false if no - /// valid Boolean value can be extracted. - /// - public static bool GetTrueOrFalse( - string s, - out bool val ) - { - val = false; - - if( s.Equals( Boolean.TrueString, - StringComparison.OrdinalIgnoreCase ) ) - { - val = true; - return true; - } - if( s.Equals( Boolean.FalseString, - StringComparison.OrdinalIgnoreCase ) ) - { - return true; - } - if( s.Equals( "1" ) ) - { - val = true; - return true; - } - if( s.Equals( "0" ) ) - { - return true; - } - s = s.ToLower(); - - if( 't' == s[0] || 'y' == s[0] ) - { - val = true; - return true; - } - if( 'f' == s[0] || 'n' == s[0] ) - { - return true; - } - return false; - } + /// + /// Return a string for an XYZ point + /// or vector with its coordinates + /// formatted to two decimal places. + /// + public static string PointString(XYZ p) + { + return string.Format("({0},{1},{2})", + RealString(p.X), + RealString(p.Y), + RealString(p.Z)); + } - /// - /// Return a string describing the given element: - /// .NET type name, - /// category name, - /// family and symbol name for a family instance, - /// element id and element name. - /// - public static string ElementDescription( - Element e ) - { - if( null == e ) - { - return ""; - } + /// + /// Return an integer value for a Revit Color. + /// + public static int ColorToInt(Color color) + { + return ((int)color.Red) << 16 + | ((int)color.Green) << 8 + | (int)color.Blue; + } - // For a wall, the element name equals the - // wall type name, which is equivalent to the - // family name ... + /// + /// Extract a true or false value from the given + /// string, accepting yes/no, Y/N, true/false, T/F + /// and 1/0. We are extremely tolerant, i.e., any + /// value starting with one of the characters y, n, + /// t or f is also accepted. Return false if no + /// valid Boolean value can be extracted. + /// + public static bool GetTrueOrFalse( + string s, + out bool val) + { + val = false; - FamilyInstance fi = e as FamilyInstance; + if (s.Equals(Boolean.TrueString, + StringComparison.OrdinalIgnoreCase)) + { + val = true; + return true; + } + if (s.Equals(Boolean.FalseString, + StringComparison.OrdinalIgnoreCase)) + { + return true; + } + if (s.Equals("1")) + { + val = true; + return true; + } + if (s.Equals("0")) + { + return true; + } + s = s.ToLower(); - string typeName = e.GetType().Name; + if ('t' == s[0] || 'y' == s[0]) + { + val = true; + return true; + } + if ('f' == s[0] || 'n' == s[0]) + { + return true; + } + return false; + } - string categoryName = ( null == e.Category ) - ? string.Empty - : e.Category.Name + " "; + /// + /// Return a string describing the given element: + /// .NET type name, + /// category name, + /// family and symbol name for a family instance, + /// element id and element name. + /// + public static string ElementDescription( + Element e) + { + if (null == e) + { + return ""; + } - string familyName = ( null == fi ) - ? string.Empty - : fi.Symbol.Family.Name + " "; + // For a wall, the element name equals the + // wall type name, which is equivalent to the + // family name ... - string symbolName = ( null == fi - || e.Name.Equals( fi.Symbol.Name ) ) - ? string.Empty - : fi.Symbol.Name + " "; + FamilyInstance fi = e as FamilyInstance; - return string.Format( "{0} {1}{2}{3}<{4} {5}>", - typeName, categoryName, familyName, - symbolName, e.Id.IntegerValue, e.Name ); - } + string typeName = e.GetType().Name; - /// - /// Return a dictionary of all the given - /// element parameter names and values. - /// - public static Dictionary - GetElementProperties( - Element e, - bool includeType ) - { - IList parameters - = e.GetOrderedParameters(); + string categoryName = (null == e.Category) + ? string.Empty + : e.Category.Name + " "; - Dictionary a - = new Dictionary( - parameters.Count ); + string familyName = (null == fi) + ? string.Empty + : fi.Symbol.Family.Name + " "; - string key; - string val; + string symbolName = (null == fi + || e.Name.Equals(fi.Symbol.Name)) + ? string.Empty + : fi.Symbol.Name + " "; - foreach( Parameter p in parameters ) - { - key = p.Definition.Name; + return string.Format("{0} {1}{2}{3}<{4} {5}>", + typeName, categoryName, familyName, + symbolName, e.Id.IntegerValue, e.Name); + } - if( !a.ContainsKey( key ) ) + /// + /// Return a dictionary of all the given + /// element parameter names and values. + /// + public static Dictionary + GetElementProperties( + Element e, + bool includeType) { - if( StorageType.String == p.StorageType ) - { - val = p.AsString(); - } - else - { - val = p.AsValueString(); - } - - if( !string.IsNullOrEmpty( val ) ) - { - a.Add( key, val ); - } + IList parameters + = e.GetOrderedParameters(); + + Dictionary a + = new Dictionary( + parameters.Count); + + + + string key; + string val; + + + + foreach (Parameter p in parameters) + { + key = p.Definition.Name; + + if (!a.ContainsKey(key)) + { + if (StorageType.String == p.StorageType) + { + val = p.AsString(); + } + else + { + val = p.AsValueString(); + } + + if (!string.IsNullOrEmpty(val)) + { + a.Add(key, val); + } + } + } + + if (includeType) + { + ElementId idType = e.GetTypeId(); + + if (ElementId.InvalidElementId != idType) + { + Document doc = e.Document; + Element typ = doc.GetElement(idType); + parameters = typ.GetOrderedParameters(); + foreach (Parameter p in parameters) + { + key = "Type " + p.Definition.Name; + + if (!a.ContainsKey(key)) + { + if (StorageType.String == p.StorageType) + { + val = p.AsString(); + } + else + { + val = p.AsValueString(); + } + + if (!string.IsNullOrEmpty(val)) + { + a.Add(key, val); + } + } + } + } + } + return a; } - } - if( includeType ) - { - ElementId idType = e.GetTypeId(); - if( ElementId.InvalidElementId != idType ) + /// + /// Return a dictionary of all the given + /// element parameter names and values. + /// + public static Dictionary + GetElementFilteredProperties( + Element e, + bool includeType) { - Document doc = e.Document; - Element typ = doc.GetElement( idType ); - parameters = typ.GetOrderedParameters(); - foreach( Parameter p in parameters ) - { - key = "Type " + p.Definition.Name; - - if( !a.ContainsKey( key ) ) + IList parameters + = e.GetOrderedParameters(); + + Dictionary a + = new Dictionary( + parameters.Count); + + string key; + string val; + string cat = e.Category.Name; + //making sure that the file has a tab for that category + if (Command._toExportDictionary.ContainsKey(cat)) { - if( StorageType.String == p.StorageType ) - { - val = p.AsString(); - } - else - { - val = p.AsValueString(); - } - - if( !string.IsNullOrEmpty( val ) ) - { - a.Add( key, val ); - } + foreach (Parameter p in parameters) + { + key = p.Definition.Name; + + //checking if the property has been checked + if (Command._toExportDictionary[cat].Contains(key)) + { + if (!a.ContainsKey(key)) + { + if (StorageType.String == p.StorageType) + { + val = p.AsString(); + } + else + { + val = p.AsValueString(); + } + + if (!string.IsNullOrEmpty(val)) + { + a.Add(key, val); + } + } + } + } + + if (includeType) + { + ElementId idType = e.GetTypeId(); + + if (ElementId.InvalidElementId != idType) + { + Document doc = e.Document; + Element typ = doc.GetElement(idType); + parameters = typ.GetOrderedParameters(); + + foreach (Parameter p in parameters) + { + key = "Type " + p.Definition.Name; + + //checking if the property has been checked + if (Command._toExportDictionary[cat].Contains(key)) + { + if (!a.ContainsKey(key)) + { + if (StorageType.String == p.StorageType) + { + val = p.AsString(); + } + else + { + val = p.AsValueString(); + } + + if (!string.IsNullOrEmpty(val)) + { + a.Add(key, val); + } + } + } + } + } + } } - } + return a; } - } - return a; } - } }