diff --git a/Test/Sample/BenchmarkCommand/BenchmarkCommand.cs b/Test/Sample/BenchmarkCommand/BenchmarkCommand.cs deleted file mode 100644 index 875b9a4..0000000 --- a/Test/Sample/BenchmarkCommand/BenchmarkCommand.cs +++ /dev/null @@ -1,24 +0,0 @@ -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; } - public string Version { get; set; } -} \ No newline at end of file diff --git a/Test/Sample/BenchmarkCommand/CategoriesCommand.cs b/Test/Sample/BenchmarkCommand/CategoriesCommand.cs new file mode 100644 index 0000000..3136ff4 --- /dev/null +++ b/Test/Sample/BenchmarkCommand/CategoriesCommand.cs @@ -0,0 +1,122 @@ +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Windows; +using Autodesk.Revit.Attributes; +using Autodesk.Revit.DB; +using Autodesk.Revit.UI; +using Test.BenchmarkCommand; + +namespace Test.Sample.BenchmarkCommand; + +[Transaction(TransactionMode.Manual)] +public class CategoriesCommand : IExternalCommand +{ + public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) + { + var doc = commandData.Application.ActiveUIDocument.Document; + List categoryBenchmarks = new List(); + var categories = new FilteredElementCollector(doc) + .WhereElementIsNotElementType() + .Where(x=>x.Category!=null) + .GroupBy(x => x.Category.Name); + foreach (IGrouping? category in categories) + { + var categoryBenchmark = new CategoryBenchmark(); + categoryBenchmark.ModelName = doc.Title + ".rvt"; + categoryBenchmark.Count = category.Count(); + categoryBenchmark.Id = category.First().Category.Id.ToString(); + categoryBenchmark.Name = category.Key; + categoryBenchmark.Workset = CountWorksetName(doc,category.ToList()); + categoryBenchmark.Family = CountFamilyName(category.ToList()); + categoryBenchmark.Type = CountFamilyTypeName(category.ToList()); + categoryBenchmark.Nestest = CountNestestElement(category.ToList()); + categoryBenchmark.Length = category.Sum(x=> UnitFeetToMeter(x.LookupParameter("Length")?.AsDouble() ?? 0)); + categoryBenchmark.Width = category.Sum(x=> UnitFeetToMeter(x.LookupParameter("Width")?.AsDouble() ?? 0)); + categoryBenchmark.Height = category.Sum(x=> UnitFeetToMeter(x.LookupParameter("Height")?.AsDouble() ?? 0)); + categoryBenchmarks.Add(categoryBenchmark); + } + // save csv + string folderPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments); + string filePath = System.IO.Path.Combine(folderPath, "categories.csv"); + CsvUtils.WriteCsvCategories(categoryBenchmarks, filePath); + Process.Start(filePath); + return Result.Succeeded; + } + public double CountWorksetName(Document doc,List elements) + { + List names = new List(); + WorksetTable worksetTable = doc.GetWorksetTable(); + foreach (Element element in elements) + { + WorksetId worksetId = element.WorksetId; + if (worksetId == WorksetId.InvalidWorksetId) continue; + Workset workset = worksetTable.GetWorkset(worksetId); + names.Add(workset.Name); + } + return names.Distinct().Count(); + } + public double CountFamilyTypeName(List elements) + { + List names = new List(); + foreach (Element element in elements) + { + string? familyName = element.get_Parameter(BuiltInParameter.ELEM_FAMILY_PARAM)?.AsValueString(); + string? typeName = element.get_Parameter(BuiltInParameter.ELEM_TYPE_PARAM)?.AsValueString(); + if (familyName != null && typeName != null) names.Add(familyName + typeName); + } + return names.Distinct().Count(); + } + public double CountFamilyName(List elements) + { + List names = new List(); + foreach (Element element in elements) + { + string? familyName = element.get_Parameter(BuiltInParameter.ELEM_FAMILY_PARAM)?.AsValueString(); + if (familyName != null) names.Add(familyName); + + } + return names.Distinct().Count(); + } + + public double CountNestestElement(List elements) + { + List names = new List(); + foreach (Element element in elements) + { + if(element is FamilyInstance familyInstance) + { + ICollection subComponentIds = familyInstance.GetSubComponentIds(); + foreach (ElementId subComponentId in subComponentIds) + { + names.Add(element.Document.GetElement(subComponentId).Name); + } + } + } + return names.Distinct().Count(); + } + + + public double UnitFeetToMeter(double value) + { + return UnitUtils.Convert(value,UnitTypeId.Feet,UnitTypeId.Meters); + } + public class CategoryBenchmark + { + public string ModelName { get; set; } + public string Id { get; set; } + public string Name { get; set; } + public double Count { get; set; } = 0; + public double Workset { get; set; } + public double Family { get; set; } = 0; + public double Type { get; set; } = 0; + public double Nestest { get; set; } = 0; + // set feild name csv + [CsvHelper.Configuration.Attributes.Name("Length(m)")] + public double Length { get; set; } + [CsvHelper.Configuration.Attributes.Name("Width(m)")] + public double Width { get; set; } + [CsvHelper.Configuration.Attributes.Name("Height(m)")] + public double Height { get; set; } + } +} \ No newline at end of file diff --git a/Test/Sample/BenchmarkCommand/CsvUtils.cs b/Test/Sample/BenchmarkCommand/CsvUtils.cs new file mode 100644 index 0000000..ea95478 --- /dev/null +++ b/Test/Sample/BenchmarkCommand/CsvUtils.cs @@ -0,0 +1,115 @@ +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using CsvHelper; +using Test.Sample.BenchmarkCommand; + +namespace Test.BenchmarkCommand; + +public static class CsvUtils +{ + public static void WriteOverallBenmark(List items, string fileName) + { + // get from csv, if rows not exist, overwrite, else append + var csvFile = new FileInfo(fileName); + if (csvFile.Exists && csvFile.Length > 0) + { + // delete data have same model name + var records = new List(); + using var reader = new StreamReader(fileName); + using var csv = new CsvReader(reader, CultureInfo.InvariantCulture); + records.AddRange(csv.GetRecords()); + records.RemoveAll(x => x.ModelName == items[0].ModelName); + records.AddRange(items); + reader.Close(); + using var writer = new StreamWriter(fileName); + using var csvWriter = new CsvWriter(writer, CultureInfo.InvariantCulture); + csvWriter.WriteRecords(records); + + } + else + { + using var writer = new StreamWriter(fileName,true); + using var csvWriter = new CsvWriter(writer, CultureInfo.InvariantCulture); + csvWriter.WriteRecords(items); + } + } + public static void WriteCsvWarnings(List items, string fileName) + { + // get from csv, if rows not exist, overwrite, else append + var csvFile = new FileInfo(fileName); + if (csvFile.Exists && csvFile.Length > 0) + { + // delete data have same model name + var records = new List(); + using var reader = new StreamReader(fileName); + using var csv = new CsvReader(reader, CultureInfo.InvariantCulture); + records.AddRange(csv.GetRecords()); + records.RemoveAll(x => x.ModelName == items[0].ModelName); + records.AddRange(items); + reader.Close(); + using var writer = new StreamWriter(fileName); + using var csvWriter = new CsvWriter(writer, CultureInfo.InvariantCulture); + csvWriter.WriteRecords(records); + + } + else + { + using var writer = new StreamWriter(fileName,true); + using var csvWriter = new CsvWriter(writer, CultureInfo.InvariantCulture); + csvWriter.WriteRecords(items); + } + } + public static void WriteCsvWorksets(List items, string fileName) + { + // get from csv, if rows not exist, overwrite, else append + var csvFile = new FileInfo(fileName); + if (csvFile.Exists && csvFile.Length > 0) + { + // delete data have same model name + var records = new List(); + using var reader = new StreamReader(fileName); + using var csv = new CsvReader(reader, CultureInfo.InvariantCulture); + records.AddRange(csv.GetRecords()); + records.RemoveAll(x => x.ModelName == items[0].ModelName); + records.AddRange(items); + reader.Close(); + using var writer = new StreamWriter(fileName); + using var csvWriter = new CsvWriter(writer, CultureInfo.InvariantCulture); + csvWriter.WriteRecords(records); + + } + else + { + using var writer = new StreamWriter(fileName,true); + using var csvWriter = new CsvWriter(writer, CultureInfo.InvariantCulture); + csvWriter.WriteRecords(items); + } + } + public static void WriteCsvCategories(List items, string fileName) + { + // get from csv, if rows not exist, overwrite, else append + var csvFile = new FileInfo(fileName); + if (csvFile.Exists && csvFile.Length > 0) + { + // delete data have same model name + var records = new List(); + using var reader = new StreamReader(fileName); + using var csv = new CsvReader(reader, CultureInfo.InvariantCulture); + records.AddRange(csv.GetRecords()); + records.RemoveAll(x => x.ModelName == items[0].ModelName); + records.AddRange(items); + reader.Close(); + using var writer = new StreamWriter(fileName); + using var csvWriter = new CsvWriter(writer, CultureInfo.InvariantCulture); + csvWriter.WriteRecords(records); + + } + else + { + using var writer = new StreamWriter(fileName,true); + using var csvWriter = new CsvWriter(writer, CultureInfo.InvariantCulture); + csvWriter.WriteRecords(items); + } + } +} \ No newline at end of file diff --git a/Test/Sample/BenchmarkCommand/OverallCommand.cs b/Test/Sample/BenchmarkCommand/OverallCommand.cs new file mode 100644 index 0000000..1f30455 --- /dev/null +++ b/Test/Sample/BenchmarkCommand/OverallCommand.cs @@ -0,0 +1,250 @@ +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using Autodesk.Revit.Attributes; +using Autodesk.Revit.DB; +using Autodesk.Revit.DB.DirectContext3D; +using Autodesk.Revit.UI; +using Test.BenchmarkCommand; + +namespace Test.Sample.BenchmarkCommand; + +[Transaction(TransactionMode.Manual)] +public class OverallCommand : IExternalCommand +{ + public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) + { + var doc = commandData.Application.ActiveUIDocument.Document; + var allElements = new FilteredElementCollector(doc).WhereElementIsNotElementType().ToElements(); + BenchmarkReport benchmarkReport = new BenchmarkReport(); + benchmarkReport.FolderId = GetFolderId(doc); + benchmarkReport.ItemId = GetItemId(doc); + benchmarkReport.ProjectId = GetProjectId(doc); + benchmarkReport.ProjectGuid = GetProjectGuid(doc); + benchmarkReport.ModelGuid = GetModelGuid(doc); + benchmarkReport.ModelName = doc.Title + ".rvt"; + benchmarkReport.Version = doc.Application.VersionName; + benchmarkReport.Path = GetPath(doc); + benchmarkReport.Unit = GetUnit(doc); + benchmarkReport.Categories = GetCountCategories(doc).ToString(); + benchmarkReport.Worksets = GetCountWorksets(doc).ToString(); + benchmarkReport.Levels = GetCountLevels(doc).ToString(); + benchmarkReport.Warnings = GetCountWarnings(doc).ToString(); + benchmarkReport.NestedElements = GetCountNestedElements(doc).ToString(); + benchmarkReport.Families = GetCountFamilies(doc).ToString(); + benchmarkReport.FamilyTypes = GetCountFamilyTypes(doc).ToString(); + benchmarkReport.RevitLinks = GetCountRevitLinks(doc).ToString(); + benchmarkReport.CadLinked = GetCountCadLinked(doc).ToString(); + benchmarkReport.Groups = GetCountGroups(doc).ToString(); + benchmarkReport.CoordinationModel = GetCountCoordinationModel(doc).ToString(); + benchmarkReport.Schedules = GetCountSchedules(doc).ToString(); + benchmarkReport.Views = GetCountViews(doc).ToString(); + benchmarkReport.Sheets = GetCountSheets(doc).ToString(); + benchmarkReport.ViewFilters = GetCountViewFilters(doc).ToString(); + // save json + string fileName = "OverallBenchmark.csv"; + string filePath = + System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments), + fileName); + CsvUtils.WriteOverallBenmark(new List(){benchmarkReport}, filePath); + Process.Start(filePath); + return Result.Succeeded; + } + + public string? GetFolderId(Document doc) + { + return doc.GetCloudFolderId(true); + } + + public string GetItemId(Document doc) + { + return doc.GetCloudModelUrn(); + } + + public string GetModelGuid(Document doc) + { + ModelPath modelPath = doc.GetCloudModelPath(); + return modelPath.GetModelGUID().ToString(); + } + + public string GetProjectGuid(Document doc) + { + ModelPath modelPath = doc.GetCloudModelPath(); + return modelPath.GetProjectGUID().ToString(); + } + + public string GetProjectId(Document doc) + { + return doc.GetProjectId(); + } + + public string GetPath(Document doc) + { + if (doc.IsModelInCloud) + { + return doc.GetCloudModelPath().ToString(); + } + + return doc.PathName; + } + + public string GetUnit(Document doc) + { + return doc.DisplayUnitSystem.ToString(); + } + + public double GetCountCategories(Document doc) + { + var categories = new FilteredElementCollector(doc) + .WhereElementIsNotElementType() + .Where(x=>x.Category!=null) + .Select(x => x.Category.Name) + .Distinct(); + return categories.Count(); + } + public double GetCountWorksets(Document doc) + { + var worksets = new FilteredWorksetCollector(doc).OfKind(WorksetKind.UserWorkset).ToWorksets(); + return worksets.Count; + } + public double GetCountLevels(Document doc) + { + var levels = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Levels).ToElements(); + return levels.Count; + } + public double GetCountWarnings(Document doc) + { + var warnings = doc.GetWarnings(); + return warnings.Count; + } + public double GetCountNestedElements(Document doc) + { + // filter family instance + var familyInstances = new FilteredElementCollector(doc) + .OfClass(typeof(FamilyInstance)) + .Cast(); + List names = new List(); + var nestedElements = familyInstances.Where(x => x.GetSubComponentIds().Count > 0); + foreach (var nestedElement in nestedElements) + { + var subComponentIds = nestedElement.GetSubComponentIds(); + foreach (var subComponentId in subComponentIds) + { + var subComponent = doc.GetElement(subComponentId); + names.Add(subComponent.Name); + } + } + // count unique nested elements + return names.Distinct().Count(); + } + public double GetCountFamilies(Document doc) + { + var families = new FilteredElementCollector(doc) + .OfClass(typeof(Family)) + .ToElements(); + return families.Count; + } + public double GetCountFamilyTypes(Document doc) + { + var familySymbols = new FilteredElementCollector(doc) + .OfClass(typeof(FamilySymbol)) + .ToElements(); + return familySymbols.Count; + } + public double GetCountRevitLinks(Document doc) + { + var revitLinks = new FilteredElementCollector(doc) + .OfClass(typeof(RevitLinkInstance)) + .ToElements(); + return revitLinks.Count; + } + public double GetCountCadLinked(Document doc) + { + var cadLinks = new FilteredElementCollector(doc) + .OfClass(typeof(CADLinkType)) + .ToElements(); + return cadLinks.Count; + } + public double GetCountGroups(Document doc) + { + var groups = new FilteredElementCollector(doc) + .OfClass(typeof(Group)) + .ToElements(); + return groups.Count; + } + public double GetCountCoordinationModel(Document doc) + { + ICollection instanceIds = DirectContext3DDocumentUtils.GetDirectContext3DHandleInstances(doc, new ElementId(BuiltInCategory.OST_Coordination_Model)); + foreach (var id in instanceIds) + { + Element elem = doc.GetElement(id); + if (null != elem) + { + Element typeElem = doc.GetElement(elem.GetTypeId()); + if (null != typeElem) + { + Parameter param = typeElem.LookupParameter("Path"); + string path = param.AsValueString(); + // Check if this is the CM you're looking for by evaluating 'path' + } + } + } + return instanceIds.Count; + } + public double GetCountSchedules(Document doc) + { + var schedules = new FilteredElementCollector(doc) + .OfClass(typeof(ViewSchedule)) + .ToElements(); + return schedules.Count; + } + public double GetCountViews(Document doc) + { + var views = new FilteredElementCollector(doc) + .OfClass(typeof(View)) + .ToElements(); + return views.Count; + } + public double GetCountSheets(Document doc) + { + var sheets = new FilteredElementCollector(doc) + .OfClass(typeof(ViewSheet)) + .ToElements(); + return sheets.Count; + } + public double GetCountViewFilters(Document doc) + { + var viewFilters = new FilteredElementCollector(doc) + .OfClass(typeof(ParameterElement)) + .ToElements(); + return viewFilters.Count; + } +} + +public class BenchmarkReport +{ + 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; } + public string? Version { get; set; } + public string? Path { get; set; } + public string? Unit { get; set; } + public string? Categories { get; set; } + public string? Worksets { get; set; } + public string? Levels { get; set; } + public string? Warnings { get; set; } + public string? NestedElements { get; set; } + public string? Families { get; set; } + public string? FamilyTypes { get; set; } + public string? RevitLinks { get; set; } + public string? CadLinked { get; set; } + public string? Groups { get; set; } + public string? CoordinationModel { get; set; } + public string? Schedules { get; set; } + public string? Views { get; set; } + public string? Sheets { get; set; } + public string? ViewFilters { get; set; } +} \ No newline at end of file diff --git a/Test/Sample/BenchmarkCommand/WarningCommand.cs b/Test/Sample/BenchmarkCommand/WarningCommand.cs new file mode 100644 index 0000000..0a940aa --- /dev/null +++ b/Test/Sample/BenchmarkCommand/WarningCommand.cs @@ -0,0 +1,46 @@ +using System.Collections.Generic; +using System.Diagnostics; +using Autodesk.Revit.Attributes; +using Autodesk.Revit.DB; +using Autodesk.Revit.UI; +using Test.BenchmarkCommand; + +namespace Test.Sample.BenchmarkCommand; + +[Transaction(TransactionMode.Manual)] +public class WarningCommand : IExternalCommand +{ + public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) + { + var doc = commandData.Application.ActiveUIDocument.Document; + var warnings = doc.GetWarnings(); + List warningBenchmarks = new List(); + foreach (FailureMessage failureMessage in warnings) + { + var warningBenchmark = new WarningBenchmark(); + warningBenchmark.ModelName = doc.Title+".rvt"; + warningBenchmark.Severity = failureMessage.GetSeverity().ToString(); + warningBenchmark.WarningDescription = failureMessage.GetDescriptionText(); + foreach (ElementId? elementId in failureMessage.GetFailingElements()) + { + warningBenchmark.ElementId = elementId.ToString(); + warningBenchmark.Category = doc.GetElement(elementId)?.Category?.Name ?? "Unknown"; + warningBenchmarks.Add(warningBenchmark); + } + } + // save csv download + string folderPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments); + string filePath = System.IO.Path.Combine(folderPath, "warning.csv"); + CsvUtils.WriteCsvWarnings(warningBenchmarks, filePath); + Process.Start(filePath); + return Result.Succeeded; + } + public class WarningBenchmark + { + public string ModelName { get; set; } + public string Category { get; set; } + public string Severity { get; set; } + public string ElementId { get; set; } + public string WarningDescription { get; set; } + } +} \ No newline at end of file diff --git a/Test/Sample/BenchmarkCommand/WorksetsCommand.cs b/Test/Sample/BenchmarkCommand/WorksetsCommand.cs new file mode 100644 index 0000000..a575435 --- /dev/null +++ b/Test/Sample/BenchmarkCommand/WorksetsCommand.cs @@ -0,0 +1,41 @@ +using System.Collections.Generic; +using System.Diagnostics; +using Autodesk.Revit.Attributes; +using Autodesk.Revit.DB; +using Autodesk.Revit.UI; +using Test.BenchmarkCommand; + +namespace Test.Sample.BenchmarkCommand; + +[Transaction(TransactionMode.Manual)] +public class WorksetsCommand : IExternalCommand +{ + public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) + { + var doc = commandData.Application.ActiveUIDocument.Document; + var worksets = new FilteredWorksetCollector(doc).OfKind(WorksetKind.UserWorkset).ToWorksets(); + List worksetsBenchmarks = new List(); + foreach (Workset workset in worksets) + { + var worksetBenchmark = new WorksetsBenchmark(); + worksetBenchmark.ModelName = doc.Title + ".rvt"; + worksetBenchmark.Id = workset.Id.ToString(); + worksetBenchmark.Name = workset.Name; + worksetBenchmark.Owner = workset.Owner; + worksetsBenchmarks.Add(worksetBenchmark); + } + // save csv download + string folderPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments); + string filePath = System.IO.Path.Combine(folderPath, "worksets.csv"); + CsvUtils.WriteCsvWorksets(worksetsBenchmarks, filePath); + Process.Start(filePath); + return Result.Succeeded; + } + public class WorksetsBenchmark + { + public string ModelName { get; set; } + public string Id { get; set; } + public string Name { get; set; } + public string Owner { get; set; } + } +} \ No newline at end of file diff --git a/Test/Sample/UnloadAndSaveModelAsCloudCommand.cs b/Test/Sample/UnloadAndSaveModelAsCloudCommand.cs index ff8fd8f..6a60c57 100644 --- a/Test/Sample/UnloadAndSaveModelAsCloudCommand.cs +++ b/Test/Sample/UnloadAndSaveModelAsCloudCommand.cs @@ -6,7 +6,9 @@ using Autodesk.Revit.DB; using Autodesk.Revit.UI; using Microsoft.Win32; +using Ookii.Dialogs.Wpf; using Application = Autodesk.Revit.ApplicationServices.Application; +using TaskDialog = Autodesk.Revit.UI.TaskDialog; namespace Test { @@ -15,16 +17,52 @@ public class UnloadAndSaveModelAsCloudCommand : IExternalCommand { public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { - //UIApplication uiApp = commandData.Application; - //uiApp.Idling += ApplicationIdling; - Guid accountId = new Guid("1715cf2b-cc12-46fd-9279-11bbc47e72f6"); - Guid projectId = new Guid("ca790fb5-141d-4ad5-b411-0461af2e9748"); - string folderIdMech = "urn:adsk.wipprod:fs.folder:co.kHlWc1ajSHSxey-_bGjKwg"; - string dir = OpenDirectoryDialog(); - List revitPaths = GetAllRevitPaths(dir); + // read file .env to get account id and project id + VistaOpenFileDialog openFileDialog = new VistaOpenFileDialog(); + openFileDialog.Filter = "Text files (*.env)|*.env"; + openFileDialog.Title = "Select a text file with FolderId"; + string envPath = string.Empty; + string folderIdStr = string.Empty; + Guid accountId = Guid.Empty; + Guid projectId = Guid.Empty; + string? dirPath = string.Empty; + if (openFileDialog.ShowDialog() == true) + { + envPath = openFileDialog.FileName; + string[] envLines = File.ReadAllLines(openFileDialog.FileName); + string accountIdStr = envLines[0].Split('=')[1]; + string projectIdStr = envLines[1].Split('=')[1]; + folderIdStr = envLines[2].Split('=')[1]; + accountId = new Guid(accountIdStr); + projectId = new Guid(projectIdStr); + dirPath = Path.GetDirectoryName(openFileDialog.FileName); + } + else + { + return Result.Cancelled; + } + if (string.IsNullOrEmpty(envPath)) + { + return Result.Cancelled; + } + // string[] envLines = File.ReadAllLines(envPath); + // string accountIdStr = envLines[0].Split('=')[1]; + // string projectIdStr = envLines[1].Split('=')[1]; + // string folderIdStr = envLines[2].Split('=')[1]; + // Guid accountId = new Guid(accountIdStr); + // Guid projectId = new Guid(projectIdStr); + //string folderIdMech = "urn:adsk.wipprod:fs.folder:co.kHlWc1ajSHSxey-_bGjKwg"; + // create a new temp folder if it does not exist + List revitPaths = GetAllRevitPaths(dirPath); + string logPath = Path.Combine(dirPath, "log.txt"); + dirPath = Path.Combine(dirPath, "Temp"); + if (!Directory.Exists(dirPath)) + { + Directory.CreateDirectory(dirPath); + } + List report = new List(); // start write a .txt log file - string logPath = Path.Combine(dir, "log.txt"); // clear log file if (File.Exists(logPath)) { @@ -34,10 +72,20 @@ public Result Execute(ExternalCommandData commandData, ref string message, Eleme { foreach (string revitPath in revitPaths) { + // create temp folder in current directory, tempfodler name is guid + string tempFolder = Path.Combine(dirPath, Guid.NewGuid().ToString()); + if (!Directory.Exists(tempFolder)) + { + Directory.CreateDirectory(tempFolder); + } + // copy revit file to temp folder and set the path to the copied file + string copiedRevitPath = Path.Combine(tempFolder, Path.GetFileName(revitPath)); + File.Copy(revitPath, copiedRevitPath); + var newRevitPath = copiedRevitPath; UnloadRevitLinks(revitPath); - string fileName = Path.GetFileNameWithoutExtension(revitPath); + string fileName = Path.GetFileNameWithoutExtension(newRevitPath); - Document? doc = OpenDocument(commandData.Application.Application, revitPath, report); + Document? doc = OpenDocument(commandData.Application.Application, newRevitPath, report); if (doc == null) { continue; @@ -47,7 +95,7 @@ public Result Execute(ExternalCommandData commandData, ref string message, Eleme // sync to central //doc.SynchronizeWithCentral(new TransactWithCentralOptions(), new SynchronizeWithCentralOptions()); // publish model by command - doc.SaveAsCloudModel(accountId, projectId, folderIdMech, fileName); + doc.SaveAsCloudModel(accountId, projectId, folderIdStr, fileName); // write to log format DAteTime.Now - ModelName - Status writer.WriteLine($"{DateTime.Now} - {fileName} - Success"); // sleep for 5 seconds to allow the cloud model to be created @@ -66,17 +114,6 @@ public Result Execute(ExternalCommandData commandData, ref string message, Eleme Process.Start(logPath); return Result.Succeeded; } - - public string OpenDialogGetPath() - { - OpenFileDialog openFileDialog = new OpenFileDialog(); - openFileDialog.Filter = "CSV files (*.csv)|*.csv"; - if (openFileDialog.ShowDialog() == true) - { - return openFileDialog.FileName; - } - return string.Empty; - } public string OpenDirectoryDialog() { System.Windows.Forms.FolderBrowserDialog folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog();