Skip to content

Commit

Permalink
add dummy example
Browse files Browse the repository at this point in the history
  • Loading branch information
chuongmep committed Nov 20, 2024
1 parent 3ebe92e commit ef0739d
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 83 deletions.
23 changes: 23 additions & 0 deletions Test/Sample/BenchmarkCommand/BenchmarkCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Windows.Input;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace Test.BenchmarkCommand;

public class BenchmarkCommand : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
throw new System.NotImplementedException();
}
}

public class ItemReport
{
public string FolderId { get; set; }
public string ItemId { get; set; }
public string ProjectId { get; set; }
public string ProjectGuid { get; set; }
public string ModelGuid { get; set; }
public string ModelName { get; set; }
}
196 changes: 196 additions & 0 deletions Test/Sample/CountExportJsonBatchCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Security.Permissions;
using System.Text;
using System.Windows;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
using CsvHelper;
using Microsoft.Win32;
using Newtonsoft.Json;

namespace Test;

[Transaction(TransactionMode.Manual)]
public class CountExportJsonBatchCommand : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
var doc = commandData.Application.ActiveUIDocument.Document;
string region = "US";
// browser to item path
string folder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
// create new folder data
string dataFolder = Path.Combine(folder, "data");
if (!Directory.Exists(dataFolder))
{
Directory.CreateDirectory(dataFolder);
}
using (var reader = new StreamReader(BrowsePathItems()))
{
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
var records = csv.GetRecords<DataInput>().ToList();
foreach (DataInput dataInput in records)
{
try
{
var projectGuid = new Guid(dataInput.project_guid);
var modelGuid = new Guid(dataInput.model_guid);
var modelPath = ModelPathUtils.ConvertCloudGUIDsToCloudPath(region, projectGuid, modelGuid);
Document document = doc.Application.OpenDocumentFile(modelPath, new OpenOptions());
var eles = new FilteredElementCollector(document).WhereElementIsNotElementType().ToElements();
// save json with length of ducts unit meter, pipes length unit meter, all count by category
Output output = new Output();
output.modelName = document.Title;
output.PipeLenght = 0;
output.DuctLenght = 0;
output.CableTrayLenght = 0;
output.ConduitsLenght = 0;
output.ConduitRunsLength = 0;
output.CableTrayRunsLength = 0;
output.CountByCategory = new Dictionary<string?, string>();
foreach (var ele in eles)
{
if (ele is Pipe pipe)
{
output.PipeLenght +=
ConvertToRealLength(
pipe.get_Parameter(BuiltInParameter.CURVE_ELEM_LENGTH)?.AsDouble() ?? 0);
}
else if (ele is Duct duct)
{
output.DuctLenght +=
ConvertToRealLength(
duct.get_Parameter(BuiltInParameter.CURVE_ELEM_LENGTH)?.AsDouble() ?? 0);
}
else if (ele is CableTray cableTray)
{
output.CableTrayLenght +=
ConvertToRealLength(cableTray.get_Parameter(BuiltInParameter.CURVE_ELEM_LENGTH)
?.AsDouble() ?? 0);
}
else if (ele is Conduit conduit)
{
output.ConduitsLenght +=
ConvertToRealLength(conduit.get_Parameter(BuiltInParameter.CURVE_ELEM_LENGTH)
?.AsDouble() ?? 0);
}
else if (ele is ConduitRun conduitRun)
{
output.ConduitRunsLength += ConvertToRealLength(
conduitRun.get_Parameter(BuiltInParameter.CURVE_ELEM_LENGTH)?.AsDouble() ?? 0);
}
else if (ele is CableTrayRun cableTrayRun)
{
output.CableTrayRunsLength += ConvertToRealLength(
cableTrayRun.get_Parameter(BuiltInParameter.CURVE_ELEM_LENGTH)?.AsDouble() ?? 0);
}
}

// group by category and count
var groupByCategory = eles.GroupBy(x => x.Category?.Name);
foreach (var group in groupByCategory)
{
if (group.Key == null)
{
continue;
}

output.CountByCategory.Add(group.Key!, group.Count().ToString());
}

// sort by key
output.CountByCategory =
output.CountByCategory.OrderBy(x => x.Key).ToDictionary(x => x.Key, x => x.Value);
// save to json
string json =
JsonConvert.SerializeObject(output, Formatting.Indented);
string filePath = Path.Combine(dataFolder, $"{document.Title}.json");
File.WriteAllText(filePath, json, Encoding.UTF8);
OpenLogFileAndWrite($"Save to {filePath}", folder);
}
catch (Exception e)
{
OpenLogFileAndWrite(e.Message, folder);
}
}
}
}

MessageBox.Show("Done");

// Process.Start("output.json");
// close model
//doc.Close(false);
return Result.Succeeded;
}
public void OpenLogFileAndWrite(string message,string folder)
{
string fileName = "log.txt";
string logFile = Path.Combine(folder, fileName);
if (!File.Exists(logFile))
{
using (StreamWriter sw = File.CreateText(logFile))
{
sw.WriteLine(message);
}
}
else
{
using (StreamWriter sw = File.AppendText(logFile))
{
sw.WriteLine(message);
}
}
}

public class DataInput
{
public string item_id { get; set; }
public string item_name { get; set; }
public string project_guid { get; set; }
public string model_guid { get; set; }
}

public string BrowsePathItems()
{
var dialog = new OpenFileDialog();
dialog.Filter = "Revit Files (*.csv)|*.csv";
dialog.Title = "Select a items file";
dialog.ShowDialog();
return dialog.FileName;
}

public double ConvertToRealLength(double length)
{
// convert to meter
if (length == null)
{
return 0;
}

double convert = UnitUtils.Convert(length, UnitTypeId.Feet, UnitTypeId.Meters);
return convert;
}

public class Output
{
public string modelName { get; set; }
public double? PipeLenght { get; set; }
public double? DuctLenght { get; set; }
public double? CableTrayLenght { get; set; }
public double? ConduitsLenght { get; set; }
public double? ConduitRunsLength { get; set; }
public double? CableTrayRunsLength { get; set; }
public Dictionary<string?, string> CountByCategory { get; set; }
}
}
16 changes: 10 additions & 6 deletions Test/Sample/CountExportJsonCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,27 @@ public Result Execute(ExternalCommandData commandData, ref string message, Eleme
{
if (ele is Pipe pipe)
{
output.PipeLenght +=ConvertToRealLength( pipe.get_Parameter(BuiltInParameter.CURVE_ELEM_LENGTH).AsDouble());
output.PipeLenght +=ConvertToRealLength( pipe.get_Parameter(BuiltInParameter.CURVE_ELEM_LENGTH)?.AsDouble()??0);
}
else if (ele is Duct duct)
{
output.DuctLenght += ConvertToRealLength(duct.get_Parameter(BuiltInParameter.CURVE_ELEM_LENGTH).AsDouble());
output.DuctLenght += ConvertToRealLength(duct.get_Parameter(BuiltInParameter.CURVE_ELEM_LENGTH)?.AsDouble()??0);
}
else if (ele is CableTray cableTray)
{
output.CableTrayLenght += ConvertToRealLength(cableTray.get_Parameter(BuiltInParameter.CURVE_ELEM_LENGTH).AsDouble());
output.CableTrayLenght += ConvertToRealLength(cableTray.get_Parameter(BuiltInParameter.CURVE_ELEM_LENGTH)?.AsDouble()??0);
}
else if (ele is Conduit conduit)
{
output.ConduitsLenght += ConvertToRealLength(conduit.get_Parameter(BuiltInParameter.CURVE_ELEM_LENGTH).AsDouble());
output.ConduitsLenght += ConvertToRealLength(conduit.get_Parameter(BuiltInParameter.CURVE_ELEM_LENGTH)?.AsDouble()??0);
}
else if (ele is ConduitRun conduitRun)
{
output.ConduitRunsLength += ConvertToRealLength(conduitRun.get_Parameter(BuiltInParameter.CURVE_ELEM_LENGTH).AsDouble());
output.ConduitRunsLength += ConvertToRealLength(conduitRun.get_Parameter(BuiltInParameter.CURVE_ELEM_LENGTH)?.AsDouble() ?? 0);
}
else if (ele is CableTrayRun cableTrayRun)
{
output.CableTrayRunsLength += ConvertToRealLength(cableTrayRun.get_Parameter(BuiltInParameter.CURVE_ELEM_LENGTH).AsDouble());
output.CableTrayRunsLength += ConvertToRealLength(cableTrayRun.get_Parameter(BuiltInParameter.CURVE_ELEM_LENGTH)?.AsDouble()??0);
}

}
Expand Down Expand Up @@ -102,6 +102,10 @@ public Result Execute(ExternalCommandData commandData, ref string message, Eleme
public double ConvertToRealLength(double length)
{
// convert to meter
if (length == null)
{
return 0;
}
double convert = UnitUtils.Convert(length, UnitTypeId.Feet, UnitTypeId.Meters);
return convert;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Windows;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
Expand All @@ -15,7 +13,7 @@
namespace Test;

[Transaction(TransactionMode.Manual)]
public class OpenModelFromCloud : IExternalCommand
public class OpenModelFromCloudUpdateAssemblyCode : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
Expand All @@ -24,7 +22,7 @@ public Result Execute(ExternalCommandData commandData, ref string message, Eleme
string region = "US";
List<DataInput> dataInputs = new List<DataInput>();
// browser to item path
using (var reader = new StreamReader(BrowsePath()))
using (var reader = new StreamReader(BrowsePathItems()))
{
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
Expand All @@ -33,7 +31,7 @@ public Result Execute(ExternalCommandData commandData, ref string message, Eleme
}
}

string csvFamily = BrowsePath();
string csvFamily = BrowsePathFamily();

foreach (DataInput dataInput in dataInputs)
{
Expand All @@ -45,7 +43,7 @@ public Result Execute(ExternalCommandData commandData, ref string message, Eleme
var modelGuid = new Guid(dataInput.model_guid);
var modelPath = ModelPathUtils.ConvertCloudGUIDsToCloudPath(region, projectGuid, modelGuid);
Document document = doc.Application.OpenDocumentFile(modelPath, new OpenOptions());
OpenLogFileAndWrite($"Open model {modelPath}");
OpenLogFileAndWrite($"Open model {modelPath.CentralServerPath}");
UpdateUFCodeBaseFamily updateUfCodeBaseFamily = new UpdateUFCodeBaseFamily();
updateUfCodeBaseFamily.Execute(document, csvFamily);
// sync model
Expand All @@ -54,15 +52,16 @@ public Result Execute(ExternalCommandData commandData, ref string message, Eleme
RelinquishOptions rOptions = new RelinquishOptions(true);
rOptions.UserWorksets = true;
syncopt.SetRelinquishOptions(rOptions);
syncopt.SaveLocalBefore = false;
syncopt.SaveLocalAfter = false;
// syncopt.SaveLocalBefore = false;
// syncopt.SaveLocalAfter = false;
doc.SynchronizeWithCentral(twcOpts, syncopt);
OpenLogFileAndWrite("Sync model to central is done");
// close model
doc.Close(false);
}
catch (Exception e)
{
OpenLogFileAndWrite($"Error: {e.Message}");
Trace.Write(e.Message);
}
}
Expand Down Expand Up @@ -90,7 +89,15 @@ public void OpenLogFileAndWrite(string message)
}
}

public string BrowsePath()
public string BrowsePathFamily()
{
var dialog = new OpenFileDialog();
dialog.Filter = "Revit Files (*.csv)|*.csv";
dialog.Title = "Select a items file";
dialog.ShowDialog();
return dialog.FileName;
}
public string BrowsePathItems()
{
var dialog = new OpenFileDialog();
dialog.Filter = "Revit Files (*.csv)|*.csv";
Expand Down
Loading

0 comments on commit ef0739d

Please sign in to comment.