Skip to content

Commit

Permalink
support read revit data svf local
Browse files Browse the repository at this point in the history
  • Loading branch information
chuongmep committed Sep 25, 2024
1 parent a10a946 commit 93a75e3
Show file tree
Hide file tree
Showing 12 changed files with 120 additions and 28 deletions.
10 changes: 5 additions & 5 deletions APSToolkit/Auth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class Auth
private string? ClientId { get; set; }
private string? ClientSecret { get; set; }

private Token Token { get; set; }
private Token? Token { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="Auth"/> class.
Expand Down Expand Up @@ -112,7 +112,7 @@ public static string GetCallbackUrl()
/// </summary>
/// <returns>The 2-legged access token.</returns>
/// <exception cref="Exception">Thrown when APS_CLIENT_ID or APS_CLIENT_SECRET environment variables are missing.</exception>
public async Task<Token> Get2LeggedToken()
public async Task<Token?> Get2LeggedToken()
{
Autodesk.Forge.TwoLeggedApi twoLeggedApi = new Autodesk.Forge.TwoLeggedApi();
if (string.IsNullOrEmpty(ClientId) || string.IsNullOrEmpty(ClientSecret))
Expand All @@ -137,7 +137,7 @@ public async Task<Token> Get2LeggedToken()
}
return Token;
}
public async Task<Token> Get3LeggedToken(string? callbackUrl = null, string? scopes = null)
public async Task<Token?> Get3LeggedToken(string? callbackUrl = null, string? scopes = null)
{
if (string.IsNullOrEmpty(scopes))
{
Expand Down Expand Up @@ -230,7 +230,7 @@ private void OpenDefaultBrowser(string url)
Console.WriteLine($"Error opening default browser: {ex.Message}");
}
}
public async Task<Token> Get3LeggedTokenPkce(string? callbackUrl = null, string? scopes = null)
public async Task<Token?> Get3LeggedTokenPkce(string? callbackUrl = null, string? scopes = null)
{
if (string.IsNullOrEmpty(scopes))
{
Expand Down Expand Up @@ -351,7 +351,7 @@ private string GenerateCodeChallenge(string codeVerifier)
/// <param name="scope">The array of scopes specifying the access permissions for the refreshed token.</param>
/// <returns>The refreshed 3-legged access token.</returns>
/// <exception cref="Exception">Thrown when clientId, clientSecret, or refreshToken is null or empty.</exception>
public async Task<Token> Refresh3LeggedToken(string refreshToken,
public async Task<Token?> Refresh3LeggedToken(string refreshToken,
Scope[] scope)
{
Autodesk.Forge.ThreeLeggedApi threeLeggedApi = new Autodesk.Forge.ThreeLeggedApi();
Expand Down
4 changes: 2 additions & 2 deletions APSToolkit/BIM360/BIM360.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ namespace APSToolkit.BIM360;
public class BIM360
{
private static string Host = "https://developer.api.autodesk.com";
private Token Token { get; set; }
private Token? Token { get; set; }

public BIM360()
{
var auth = new Auth();
this.Token = auth.Get2LeggedToken().Result;
}
public BIM360(Token token)
public BIM360(Token? token)
{
Token = token;
}
Expand Down
4 changes: 2 additions & 2 deletions APSToolkit/Database/BucketStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ namespace APSToolkit.Database;

public class BucketStorage
{
private Token Token { get; set; }
public BucketStorage(Token token)
private Token? Token { get; set; }
public BucketStorage(Token? token)
{
Token = token;
}
Expand Down
75 changes: 63 additions & 12 deletions APSToolkit/Database/PropDbReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text;
using System.Text.RegularExpressions;
using APSToolkit.Utils;
using Autodesk.Forge.DesignAutomation.Model;
using ICSharpCode.SharpZipLib.GZip;
using Newtonsoft.Json;
using OfficeOpenXml;
Expand Down Expand Up @@ -58,7 +59,7 @@ private static byte[] Unzip(byte[] input)
}
}

private Token Token { get; set; }
private Token? Token { get; set; }

/// <summary>
/// Reads a GZip-compressed file from the specified path, decompresses its content, and returns the decompressed byte array.
Expand All @@ -71,28 +72,32 @@ private static byte[] Unzip(string filePath)
return Unzip(input);
}

public PropDbReader(string urn)
{
this.Urn = urn;
var auth = new Auth();
Token = auth.Get2LeggedToken().Result;
}
public PropDbReader()
{
var auth = new Auth();
Token = auth.Get2LeggedToken().Result;
}

/// <summary>
/// Read All Information properties from urn model
/// </summary>
/// <param name="urn"></param>
/// <param name="token"></param>
/// <param name="token">default authentication 2legend</param>
/// <returns></returns>
public PropDbReader(string urn, Token token)
public PropDbReader(string urn, Token? token = null)
{
this.Urn = urn;
Token = token;
DownloadStreamAsync(urn, token.AccessToken).Wait();
if (token == null)
{
var auth = new Auth();
token = auth.Get2LeggedToken().Result;
}
else
{
Token = token;
}

DownloadStreamAsync(urn, token!.AccessToken).Wait();
}

/// <summary>
Expand All @@ -102,7 +107,7 @@ public PropDbReader(string urn, Token token)
/// <param name="urn">The unique identifier for the model in the Autodesk Forge derivative service.</param>
/// <param name="accessToken">The access token for authenticating requests to the Autodesk Forge service.</param>
/// <returns>An asynchronous task representing the PropDbReader instance with the downloaded and extracted data.</returns>
private async Task<PropDbReader> DownloadStreamAsync(string? urn, string accessToken)
private async Task<PropDbReader> DownloadStreamAsync(string? urn, string? accessToken)
{
List<Derivatives.Resource> resourcesToDownload =
await Derivatives.ExtractProbDbAsync(urn, accessToken).ConfigureAwait(false);
Expand Down Expand Up @@ -174,6 +179,49 @@ await Derivatives.ExecuteRequestData(request, resource)
return this;
}

public static PropDbReader ReadFromSvf(string svfPath)
{
/*
* Initialize PropReader from svf file
* svfPath: path to svf file, e.g. "path/to/3D.svf"
* Remark: see tutorial at https://chuongmep.com/posts/2024-09-25-revit-extractor.html
* return: Instance
*/
string parentDir = Path.GetFullPath(Path.Combine(svfPath, @"..\..\.."));
string idsPath = Path.Combine(parentDir, "objects_ids.json.gz");
if (!File.Exists(idsPath))
{
throw new FileNotFoundException($"File {idsPath} not found");
}

string offsetsPath = Path.Combine(parentDir, "objects_offs.json.gz");
if (!File.Exists(offsetsPath))
{
throw new FileNotFoundException($"File {offsetsPath} not found");
}

string avsPath = Path.Combine(parentDir, "objects_avs.json.gz");
if (!File.Exists(avsPath))
{
throw new FileNotFoundException($"File {avsPath} not found");
}

string attrsPath = Path.Combine(parentDir, "objects_attrs.json.gz");
if (!File.Exists(attrsPath))
{
throw new FileNotFoundException($"File {attrsPath} not found");
}

string valsPath = Path.Combine(parentDir, "objects_vals.json.gz");
if (!File.Exists(valsPath))
{
throw new FileNotFoundException($"File {valsPath} not found");
}
var reader = new PropDbReader(idsPath, offsetsPath, avsPath, attrsPath, valsPath);
return reader;
}


/// <summary>
/// Initialize PropDbReader with byte[]
/// </summary>
Expand Down Expand Up @@ -265,10 +313,12 @@ public Property[] EnumerateProperties(int id)
{
item.DisplayPrecision = int.Parse(attr[7]);
}

if (attr.Length >= 9)
{
item.ForgeParameterId = attr[8];
}

item.Value = value;
properties.Add(item);
}
Expand Down Expand Up @@ -571,6 +621,7 @@ private void GetChildrenRecursion(ref DataTable dataTable, int[] child)
{
dataTable.Columns.Add(columnName);
}

dataRow[columnName] = property.Value;
}

Expand Down
18 changes: 17 additions & 1 deletion APSToolkit/Database/PropDbReaderRevit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -644,9 +644,25 @@ public Property[] GetAllPublicProperties()
return properties;
}

public PropDbReaderRevit(string urn, Token token) : base(urn, token)
public PropDbReaderRevit(string urn, Token? token) : base(urn, token)
{
Configuration = new RevitDataConfiguration(urn, token);
}
public static PropDbReaderRevit ReadFromSvf(string path)

Check warning on line 651 in APSToolkit/Database/PropDbReaderRevit.cs

View workflow job for this annotation

GitHub Actions / build

'PropDbReaderRevit.ReadFromSvf(string)' hides inherited member 'PropDbReader.ReadFromSvf(string)'. Use the new keyword if hiding was intended.
{
PropDbReader propDbReader = PropDbReader.ReadFromSvf(path);
PropDbReaderRevit propDbReaderRevit = new PropDbReaderRevit();
propDbReaderRevit.ids = propDbReader.ids;
propDbReaderRevit.offsets = propDbReader.offsets;
propDbReaderRevit.avs = propDbReader.avs;
propDbReaderRevit.attrs = propDbReader.attrs;
propDbReaderRevit.vals = propDbReader.vals;
return propDbReaderRevit;
}

public PropDbReaderRevit()
{

}
public PropDbReaderRevit(byte[] _ids, byte[] _offsets, byte[] _avs, byte[] _attrs, byte[] _vals) : base(_ids,
_offsets, _avs, _attrs, _vals)
Expand Down
4 changes: 2 additions & 2 deletions APSToolkit/Database/RevitDataConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class RevitDataConfiguration

public string Urn { get; set; }

public Token Token { get; set; }
public Token? Token { get; set; }

public Dictionary<string, string> Units = new Dictionary<string, string>();

Expand All @@ -37,7 +37,7 @@ public RevitDataConfiguration()
{

}
public RevitDataConfiguration(string urn,Token token) : this()
public RevitDataConfiguration(string urn,Token? token) : this()
{
this.Urn = urn;
this.Token = token;
Expand Down
2 changes: 1 addition & 1 deletion APSToolkit/Derivatives.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ public static async Task<List<Resource>> ExtractSvfAsync(string? urn, string acc
return resources;
}

public static async Task<List<Resource>> ExtractProbDbAsync(string? urn, string accessToken)
public static async Task<List<Resource>> ExtractProbDbAsync(string? urn, string? accessToken)
{
DerivativesApi derivativeApi = new DerivativesApi();
derivativeApi.Configuration.AccessToken = accessToken;
Expand Down
2 changes: 1 addition & 1 deletion APSToolkit/DesignAutomation/RevitDesignAutomate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public RevitDesignAutomate(DesignAutomateConfiguration configuration) : this()
public async Task<Status> ExecuteJob(string projectId, string versionId, string callBackUrl)
{
// access Token
Token token = new Auth(_configuration.ClientId, _configuration.ClientSecret)
Token? token = new Auth(_configuration.ClientId, _configuration.ClientSecret)
.Get2LeggedToken().Result;
string userAccessToken = token.AccessToken;
bool isCompositeDesign = DAUtils.IsCompositeDesign(userAccessToken, projectId, versionId).Result;
Expand Down
2 changes: 1 addition & 1 deletion APSToolkit/Token.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public Token()
public Token Refresh2LegToken()
{
var auth = new Auth();
Token token = auth.Get2LeggedToken().Result;
Token? token = auth.Get2LeggedToken().Result;
this.AccessToken = token.AccessToken;
this.TokenType = token.TokenType;
this.ExpiresIn = token.ExpiresIn;
Expand Down
12 changes: 12 additions & 0 deletions APSToolkitUnit/ProbDbReaderRevitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ public void Setup()
Console.WriteLine($"Time elapsed seconds: {elapsedMs / 1000}");
}

[Test]
public void InitFromSvf()
{
string path = @"C:\Users\vho2\3D Objects\output\output\Resource\3D View\{3D} 960621\{3D}.svf";
var prop = PropDbReaderRevit.ReadFromSvf(path);
Dictionary<int, string> allCategories = prop.GetAllCategories();
foreach (KeyValuePair<int, string> publicProperty in allCategories)
{
Console.WriteLine(publicProperty.Key + " : " + publicProperty.Value);
}
}

[Test]
[TestCaseSource(typeof(Settings), nameof(Settings.RevitTestUrn))]
public void GetAllCategoriesTest(string urn)
Expand Down
13 changes: 13 additions & 0 deletions APSToolkitUnit/ProbDbReaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ public void Setup()
Console.WriteLine($"Time elapsed seconds: {elapsedMs / 1000}");
}

[Test]
public void ReadFromSvfFileTest()
{
string filePAth = @"C:\Users\vho2\3D Objects\output\output\Resource\3D View\{3D} 960621\{3D}.svf";
PropDbReader propDbReader = PropDbReader.ReadFromSvf(filePAth);
Console.WriteLine(propDbReader.ids.Length);
Dictionary<string,string?> publicProperties = propDbReader.GetPublicProperties(1);
foreach (KeyValuePair<string,string> publicProperty in publicProperties)
{
Console.WriteLine(publicProperty.Key + " : " + publicProperty.Value);
}
}

[Test]
public void PropDbReaderSQLTest()
{
Expand Down
2 changes: 1 addition & 1 deletion APSToolkitUnit/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static class Settings
"dXJuOmFkc2sud2lwcHJvZDpmcy5maWxlOnZmLmZqSkFRUUx6U3BxLTR3eWRPdG9DMGc_dmVyc2lvbj0x";

public const string _RevitTestUrn =
"dXJuOmFkc2sud2lwcHJvZDpmcy5maWxlOnZmLndzZURxSkdRVFF5SEZIZ2ZBbmZ5NWc_dmVyc2lvbj0x";
"dXJuOmFkc2sud2lwcHJvZDpmcy5maWxlOnZmLkotQ2laSHpGVEd5LUEwLVJmaEVVTVE_dmVyc2lvbj04";

public const string _IfcUrn =
"dXJuOmFkc2sud2lwcHJvZDpmcy5maWxlOnZmLjEzLVdVN2NBU2kyQThVdUNqQVFmUkE_dmVyc2lvbj0x";
Expand Down

0 comments on commit 93a75e3

Please sign in to comment.