Skip to content

Commit

Permalink
add e.g open cloud model
Browse files Browse the repository at this point in the history
  • Loading branch information
chuongmep committed Nov 18, 2024
1 parent 7ed820f commit 7caedcf
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 3 deletions.
3 changes: 2 additions & 1 deletion AddInManager.sln
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ Global
{5016ED6D-5A9A-4F59-AE49-CAA9615798F7}.Release R23|Any CPU.ActiveCfg = Release|Any CPU
{5016ED6D-5A9A-4F59-AE49-CAA9615798F7}.Release R24|Any CPU.ActiveCfg = Release|Any CPU
{5016ED6D-5A9A-4F59-AE49-CAA9615798F7}.Release R25|Any CPU.ActiveCfg = Release|Any CPU
{1661572C-EF3A-4DD6-83BD-CB4239CE8CDD}.Debug R22|Any CPU.ActiveCfg = Debug R22|Any CPU
{1661572C-EF3A-4DD6-83BD-CB4239CE8CDD}.Debug R23|Any CPU.ActiveCfg = Debug R23|Any CPU
{1661572C-EF3A-4DD6-83BD-CB4239CE8CDD}.Debug R24|Any CPU.ActiveCfg = Debug R24|Any CPU
{1661572C-EF3A-4DD6-83BD-CB4239CE8CDD}.Installer|Any CPU.ActiveCfg = Debug R21|Any CPU
Expand All @@ -97,6 +96,8 @@ Global
{1661572C-EF3A-4DD6-83BD-CB4239CE8CDD}.Debug R25|Any CPU.ActiveCfg = Release R25|Any CPU
{1661572C-EF3A-4DD6-83BD-CB4239CE8CDD}.Debug R25|Any CPU.Build.0 = Release R25|Any CPU
{1661572C-EF3A-4DD6-83BD-CB4239CE8CDD}.Debug R23|Any CPU.Build.0 = Debug R23|Any CPU
{1661572C-EF3A-4DD6-83BD-CB4239CE8CDD}.Release R22|Any CPU.Build.0 = Release R22|Any CPU
{1661572C-EF3A-4DD6-83BD-CB4239CE8CDD}.Debug R22|Any CPU.ActiveCfg = Debug R22|Any CPU
{1661572C-EF3A-4DD6-83BD-CB4239CE8CDD}.Debug R22|Any CPU.Build.0 = Debug R22|Any CPU
{21460D85-C4AD-49D5-963F-CF13C4AE99EB}.Debug R22|Any CPU.ActiveCfg = Debug R22|Any CPU
{21460D85-C4AD-49D5-963F-CF13C4AE99EB}.Debug R22|Any CPU.Build.0 = Debug R22|Any CPU
Expand Down
101 changes: 101 additions & 0 deletions Test/Sample/OpenModelFromCloud.cs
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; }
}
}
106 changes: 106 additions & 0 deletions Test/Sample/UpdateUFCodeBaseFamilyNameType.cs
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; }
}
8 changes: 6 additions & 2 deletions Test/Test.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net48</TargetFramework>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
<LangVersion>default</LangVersion>
<IsPackable>false</IsPackable>
<UseWindowsForms>true</UseWindowsForms>
<PlatformTarget>x64</PlatformTarget>
Expand Down Expand Up @@ -53,6 +52,11 @@
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug R22' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<DocumentationFile>bin\Debug R22\Test.xml</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CsvHelper" Version="33.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
Expand Down

0 comments on commit 7caedcf

Please sign in to comment.