- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi,
i am trying to create a addin that takes all parameters in a file and saves them to a CSV.
I build the project and it goes well but navisworks does not start after.
This is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Autodesk.Navisworks.Api;
using Autodesk.Navisworks.Api.Plugins;
namespace NavisworksDB
{
[PluginAttribute("NavisworksDB", "One", DisplayName = "NavisworksDB", ToolTip = "BIM Model Validator")]
public class MainClass : AddInPlugin
{
// Step 1 : implement execute method
public override int Execute(params string[] parameters)
{
// Step 2: current document
Document doc = Autodesk.Navisworks.Api.Application.ActiveDocument;
// Step 3: display message
StringBuilder message = new StringBuilder();
// Step 4: select all elements in the project
ModelItemCollection items = doc.CurrentSelection.SelectedItems;
// Step 5: Create a DataTable that has all the possible parameters as columns
DataTable dataTable = new DataTable("ElementsData");
foreach (ModelItem item in items)
{
PropertyCategoryCollection elementCategories = item.PropertyCategories;
foreach (PropertyCategory elementCategory in elementCategories)
{
string categoryName = elementCategory.DisplayName;
foreach (DataProperty dataProperty in elementCategory.Properties)
{
string columnName = categoryName + "." + dataProperty.DisplayName;
if (!dataTable.Columns.Contains(columnName))
{
dataTable.Columns.Add(columnName, typeof(string));
}
}
}
}
// Step 6: Iterate through each element in the project and append the values to the DataTable
foreach (ModelItem item in items)
{
DataRow row = dataTable.NewRow();
PropertyCategoryCollection elementCategories = item.PropertyCategories;
foreach (PropertyCategory elementCategory in elementCategories)
{
string categoryName = elementCategory.DisplayName;
foreach (DataProperty dataProperty in elementCategory.Properties)
{
string columnName = categoryName + "." + dataProperty.DisplayName;
row[columnName] = dataProperty.Value.ToString();
}
}
dataTable.Rows.Add(row);
}
// Step 7: Save DataTable to CSV
StringBuilder csvContent = new StringBuilder();
IEnumerable<string> columnNames = dataTable.Columns.Cast<DataColumn>().Select(column => column.ColumnName);
csvContent.AppendLine(string.Join(",", columnNames));
foreach (DataRow row in dataTable.Rows)
{
IEnumerable<string> fields = row.ItemArray.Select(field => field.ToString());
csvContent.AppendLine(string.Join(",", fields));
}
System.IO.File.WriteAllText("ElementsData.csv", csvContent.ToString());
return 0;
}
}
}
Solved! Go to Solution.