-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
215 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
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; | ||
using CsvHelper; | ||
using Microsoft.Win32; | ||
|
||
namespace Test; | ||
|
||
[Transaction(TransactionMode.Manual)] | ||
public class OpenModelFromCloud : IExternalCommand | ||
{ | ||
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) | ||
{ | ||
// open model from acc | ||
var doc = commandData.Application.ActiveUIDocument.Document; | ||
string region = "US"; | ||
List<DataInput> dataInputs = new List<DataInput>(); | ||
// browser to item path | ||
using (var reader = new StreamReader(BrowsePath())) | ||
{ | ||
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture)) | ||
{ | ||
var records = csv.GetRecords<DataInput>().ToList(); | ||
dataInputs.AddRange(records); | ||
} | ||
|
||
} | ||
|
||
string csvFamily = BrowsePath(); | ||
|
||
foreach (DataInput dataInput in dataInputs) | ||
{ | ||
// Guid projectGuid = new Guid("ca790fb5-141d-4ad5-b411-0461af2e9748"); | ||
// Guid modelGuid = new Guid("53d475e0-67b2-4971-b91a-b7f32a8ca5c2"); | ||
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()); | ||
OpenLogFileAndWrite($"Open model {modelPath}"); | ||
UpdateUFCodeBaseFamily updateUfCodeBaseFamily = new UpdateUFCodeBaseFamily(); | ||
updateUfCodeBaseFamily.Execute(document, csvFamily); | ||
// sync model | ||
doc.SynchronizeWithCentral(new TransactWithCentralOptions(), new SynchronizeWithCentralOptions()); | ||
OpenLogFileAndWrite("Sync model to central is done"); | ||
// close model | ||
doc.Close(false); | ||
} | ||
catch (Exception e) | ||
{ | ||
Trace.Write(e.Message); | ||
} | ||
} | ||
|
||
return Result.Succeeded; | ||
|
||
} | ||
public void OpenLogFileAndWrite(string message) | ||
{ | ||
string fileName = "log.txt"; | ||
string logFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), 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 string BrowsePath() | ||
{ | ||
var dialog = new OpenFileDialog(); | ||
dialog.Filter = "Revit Files (*.csv)|*.csv"; | ||
dialog.Title = "Select a items file"; | ||
dialog.ShowDialog(); | ||
return dialog.FileName; | ||
} | ||
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Windows; | ||
using Autodesk.Revit.Attributes; | ||
using Autodesk.Revit.DB; | ||
using Autodesk.Revit.UI; | ||
using CsvHelper; | ||
|
||
namespace Test; | ||
|
||
[Transaction(TransactionMode.Manual)] | ||
public class UpdateUFCodeBaseFamily : IExternalCommand | ||
{ | ||
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) | ||
{ | ||
var doc = commandData.Application.ActiveUIDocument.Document; | ||
// load csv path | ||
|
||
string? browseFile = BrowseFileCsv(); | ||
if (browseFile == null) | ||
{ | ||
return Result.Cancelled; | ||
} | ||
|
||
// browse fil | ||
|
||
// KeyBasedTreeEntriesLoadContent loadContent = new KeyBasedTreeEntriesLoadContent(); | ||
// // load configuration assembly code : | ||
// ClassificationEntries.LoadClassificationEntriesFromFile(doc.PathName, classificationEntry); | ||
// load assembly code setting | ||
|
||
// read csv file | ||
Execute(doc, browseFile); | ||
|
||
MessageBox.Show("Done"); | ||
|
||
return Result.Succeeded; | ||
} | ||
|
||
public void Execute(Autodesk.Revit.DB.Document doc, string browseFile) | ||
{ | ||
List<InputAssembly> inputAssemblies = new List<InputAssembly>(); | ||
using (var reader = new StreamReader(browseFile)) | ||
{ | ||
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture)) | ||
{ | ||
var records = csv.GetRecords<InputAssembly>().ToList(); | ||
inputAssemblies.AddRange(records); | ||
} | ||
} | ||
|
||
// update assembly code base on family name and type name | ||
using (var tran = new Transaction(doc, "Update Assembly Code")) | ||
{ | ||
tran.Start(); | ||
// get all family types | ||
var familyTypes = new FilteredElementCollector(doc).OfClass(typeof(FamilySymbol)).Cast<FamilySymbol>() | ||
.ToList(); | ||
foreach (FamilySymbol familySymbol in familyTypes) | ||
{ | ||
var inputAssembly = inputAssemblies.FirstOrDefault(x => | ||
x.Family == familySymbol.FamilyName && x.FamilyType == familySymbol.Name); | ||
if (inputAssembly != null) | ||
{ | ||
Parameter parameter = familySymbol.get_Parameter(BuiltInParameter.UNIFORMAT_CODE); | ||
if (parameter != null && !parameter.IsReadOnly) | ||
{ | ||
parameter.Set(inputAssembly.UF2Code); | ||
} | ||
|
||
var descriptionParameter = familySymbol.get_Parameter(BuiltInParameter.ALL_MODEL_DESCRIPTION); | ||
if (descriptionParameter != null && !descriptionParameter.IsReadOnly) | ||
{ | ||
descriptionParameter.Set(inputAssembly.UF2Description); | ||
} | ||
} | ||
} | ||
tran.Commit(); | ||
} | ||
} | ||
|
||
string? BrowseFileCsv() | ||
{ | ||
Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog(); | ||
openFileDialog.Filter = "CSV files (*.csv)|*.csv"; | ||
if (openFileDialog.ShowDialog() == true) | ||
{ | ||
return openFileDialog.FileName; | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
|
||
public class InputAssembly | ||
{ | ||
public string ModelName { get; set; } | ||
public string Category { get; set; } | ||
public string Family { get; set; } | ||
public string FamilyType { get; set; } | ||
public string UF2Code { get; set; } | ||
public string UF2Description { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters