Skip to content

Commit a14de5b

Browse files
authored
File import service now creates branch if one doesn't already exist (#670)
1 parent a89407a commit a14de5b

File tree

9 files changed

+184
-163
lines changed

9 files changed

+184
-163
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using Ara3D.Utils;
2+
using Speckle.Sdk.Api;
3+
using Speckle.Sdk.Credentials;
4+
5+
namespace Speckle.Importers.Ifc.Tester2;
6+
7+
/// <summary>
8+
/// This is a test file for testing the IFC importer locally
9+
/// Except for the logic in the preview service, this is pretty much exactly the same as what is running when
10+
/// you upload an ifc file.
11+
/// </summary>
12+
/// <param name="clientFactory"></param>
13+
/// <param name="importer"></param>
14+
/// <param name="accountManager"></param>
15+
public sealed class IfcTester(IClientFactory clientFactory, Importer importer, IAccountManager accountManager)
16+
{
17+
// Settings, Change these to suit!
18+
private readonly FilePath _filePath =
19+
//new(@"C:\Users\Jedd\Desktop\GRAPHISOFT_Archicad_Sample_Project-S-Office_v1.0_AC25.ifc");
20+
new(@"C:\Users\Jedd\Desktop\EST-BRE-AF-3D-BT1-30-SD-00001-A-P.ifc");
21+
private readonly Uri _serverUrl = new("https://app.speckle.systems");
22+
private const string PROJECT_ID = "f3a42bdf24";
23+
24+
public async Task Run()
25+
{
26+
var account = accountManager.GetAccounts(_serverUrl).First();
27+
using var speckleClient = clientFactory.Create(account);
28+
string modelName = _filePath.GetFileName();
29+
var existing = await speckleClient.Project.GetWithModels(PROJECT_ID, 1, modelsFilter: new(search: modelName));
30+
string? existingModel = existing.models.items.Count >= 1 ? existing.models.items.First().id : null;
31+
32+
// Convert IFC to Speckle Objects
33+
34+
ImporterArgs args =
35+
new()
36+
{
37+
ServerUrl = _serverUrl,
38+
FilePath = _filePath.ToString(),
39+
ProjectId = PROJECT_ID,
40+
ModelId = existingModel,
41+
ModelName = _filePath.GetFileName(),
42+
VersionMessage = "",
43+
Token = account.token
44+
};
45+
var version = await importer.ImportIfc(args, null, default);
46+
Console.WriteLine($"File was successfully sent {version.id}");
47+
}
48+
}
Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,17 @@
1-
using Ara3D.Utils;
2-
using Microsoft.Extensions.DependencyInjection;
1+
using Microsoft.Extensions.DependencyInjection;
32
using Speckle.Importers.Ifc;
4-
using Speckle.Importers.Ifc.Ara3D.IfcParser;
5-
using Speckle.Importers.Ifc.Converters;
63
using Speckle.Importers.Ifc.Tester2;
7-
using Speckle.Importers.Ifc.Types;
84
using Speckle.Objects.Geometry;
95
using Speckle.Sdk.Host;
106
using Speckle.Sdk.Models;
117

12-
// Settings
13-
var filePath = new FilePath(@"C:\Users\Jedd\Desktop\KLY-ZHQ-B-CPL1_CPL4-0-ELV-SD-210.ifc");
14-
15-
// var filePath = new FilePath(@"C:\Users\Jedd\Desktop\BIM_Projekt_Golden_Nugget-Architektur_und_Ingenieurbau.ifc");
16-
const string PROJECT_ID = "f3a42bdf24";
17-
18-
// Setup
8+
// This is all DI setup, Look in IfcTester.cs for the real goodies
199
TypeLoader.Initialize(typeof(Base).Assembly, typeof(Point).Assembly);
2010

2111
var serviceCollection = new ServiceCollection();
2212
serviceCollection.AddIFCImporter();
23-
serviceCollection.AddSingleton<Sender>();
13+
serviceCollection.AddSingleton<IfcTester>();
2414
var serviceProvider = serviceCollection.BuildServiceProvider();
2515

26-
// Convert IFC to Speckle Objects
27-
var ifcFactory = serviceProvider.GetRequiredService<IIfcFactory>();
28-
var model = ifcFactory.Open(filePath);
29-
var graph = IfcGraph.Load(new FilePath(filePath));
30-
var converter = serviceProvider.GetRequiredService<IGraphConverter>();
31-
var b = converter.Convert(model, graph);
32-
33-
//Send Speckle Objects to server
34-
var sender = serviceProvider.GetRequiredService<Sender>();
35-
await sender.Send(b, PROJECT_ID);
16+
var tester = serviceProvider.GetRequiredService<IfcTester>();
17+
await tester.Run();

Importers/Ifc/Speckle.Importers.Ifc.Tester2/Sender.cs

Lines changed: 0 additions & 48 deletions
This file was deleted.

Importers/Ifc/Speckle.Importers.Ifc/Ara3D.IfcParser/IfcGraph.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public IfcGraph(StepDocument d, ILogger? logger = null)
126126
else if (inst.Type.Equals("IFCELEMENTQUANTITY"))
127127
{
128128
var e = d.GetInstanceWithData(inst);
129-
AddNode(new IfcPropSet(this, e, (StepList)e[5]));
129+
AddNode(new IfcPropSet(this, e, e[5] as StepList));
130130
}
131131
// Aggregate relation
132132
else if (inst.Type.Equals("IFCRELAGGREGATES"))

Importers/Ifc/Speckle.Importers.Ifc/Ara3D.IfcParser/Schema/IfcProject.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace Speckle.Importers.Ifc.Ara3D.IfcParser.Schema;
44

55
public sealed class IfcProject(IfcGraph graph, StepInstance lineData) : IfcNode(graph, lineData)
66
{
7-
public string? ObjectType => (LineData[5] as StepString)?.AsString();
8-
public string? LongName => (LineData[6] as StepString)?.AsString();
9-
public string? Phase => (LineData[7] as StepString)?.AsString();
7+
public string? ObjectType => (LineData[4] as StepString)?.AsString();
8+
public string? LongName => (LineData[5] as StepString)?.AsString();
9+
public string? Phase => (LineData[6] as StepString)?.AsString();
1010
}

Importers/Ifc/Speckle.Importers.Ifc/Ara3D.IfcParser/Schema/IfcPropSet.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System.Diagnostics;
2+
using System.Diagnostics.CodeAnalysis;
23
using Speckle.Importers.Ifc.Ara3D.StepParser;
4+
using Speckle.Sdk.Common;
35

46
namespace Speckle.Importers.Ifc.Ara3D.IfcParser.Schema;
57

@@ -11,9 +13,9 @@ namespace Speckle.Importers.Ifc.Ara3D.IfcParser.Schema;
1113
// https://standards.buildingsmart.org/IFC/RELEASE/IFC2x3/TC1/HTML/ifcproductextension/lexical/ifcelementquantity.htm
1214
public class IfcPropSet : IfcNode
1315
{
14-
public readonly StepList PropertyIdList;
16+
public readonly StepList? PropertyIdList;
1517

16-
public IfcPropSet(IfcGraph graph, StepInstance lineData, StepList propertyIdList)
18+
public IfcPropSet(IfcGraph graph, StepInstance lineData, StepList? propertyIdList)
1719
: base(graph, lineData)
1820
{
1921
Debug.Assert(IsIfcRoot);
@@ -36,7 +38,8 @@ public IEnumerable<IfcProp> GetProperties()
3638

3739
public bool IsQuantity => LineData.AttributeValues.Count == 6;
3840
public string? MethodOfMeasurement => IsQuantity ? this[4]?.AsString() : null;
39-
public int NumProperties => PropertyIdList.Values.Count;
41+
public int NumProperties => PropertyIdList?.Values.Count ?? 0;
4042

41-
public uint PropertyId(int i) => PropertyIdList.Values[i].AsId();
43+
[MemberNotNull(nameof(PropertyIdList))]
44+
public uint PropertyId(int i) => PropertyIdList.NotNull().Values[i].AsId();
4245
}

Importers/Ifc/Speckle.Importers.Ifc/Ara3D.IfcParser/Schema/IfcSpatialStructureElement.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Speckle.Importers.Ifc.Ara3D.IfcParser.Schema;
99
/// <param name="lineData"></param>
1010
public class IfcSpatialStructureElement(IfcGraph graph, StepInstance lineData) : IfcNode(graph, lineData)
1111
{
12-
public string? ObjectType => (LineData[5] as StepString)?.AsString();
13-
public string? LongName => (LineData[8] as StepString)?.AsString();
14-
public string? CompositionType => (LineData[9] as StepString)?.AsString();
12+
public string? ObjectType => (LineData[4] as StepString)?.AsString();
13+
public string? LongName => (LineData[7] as StepString)?.AsString();
14+
public string? CompositionType => (LineData[8] as StepString)?.AsString();
1515
}
Lines changed: 21 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,19 @@
1-
using System.Diagnostics;
21
using System.Reflection;
3-
using Ara3D.Utils;
42
using Microsoft.Extensions.DependencyInjection;
5-
using Speckle.Importers.Ifc.Ara3D.IfcParser;
6-
using Speckle.Importers.Ifc.Converters;
7-
using Speckle.Importers.Ifc.Types;
83
using Speckle.Objects.Geometry;
94
using Speckle.Sdk;
10-
using Speckle.Sdk.Api;
11-
using Speckle.Sdk.Api.GraphQL.Inputs;
12-
using Speckle.Sdk.Api.GraphQL.Models;
13-
using Speckle.Sdk.Credentials;
145
using Speckle.Sdk.Host;
156
using Speckle.Sdk.Models;
16-
using Speckle.Sdk.Serialisation.V2;
17-
using Speckle.Sdk.Serialisation.V2.Send;
187
using Speckle.Sdk.Transports;
8+
using Version = Speckle.Sdk.Api.GraphQL.Models.Version;
199

2010
namespace Speckle.Importers.Ifc;
2111

12+
/// <summary>
13+
/// Static DI Wrapper around <see cref="Importer"/>
14+
/// </summary>
2215
public static class Import
2316
{
24-
public static async Task<string> Ifc(
25-
string url,
26-
string filePath,
27-
string streamId,
28-
string modelId,
29-
string commitMessage,
30-
string token,
31-
IProgress<ProgressArgs>? progress = null
32-
)
33-
{
34-
var serviceProvider = GetServiceProvider();
35-
return await Ifc(serviceProvider, url, filePath, streamId, modelId, commitMessage, token, progress);
36-
}
37-
3817
public static ServiceProvider GetServiceProvider()
3918
{
4019
TypeLoader.Initialize(typeof(Base).Assembly, typeof(Point).Assembly);
@@ -47,67 +26,28 @@ public static void AddIFCImporter(this ServiceCollection serviceCollection)
4726
{
4827
serviceCollection.AddSpeckleSdk(new("IFC", "ifc"), HostAppVersion.v2024, "IFC-Importer");
4928
serviceCollection.AddSpeckleWebIfc();
29+
serviceCollection.AddSingleton<Importer>();
5030
serviceCollection.AddMatchingInterfacesAsTransient(Assembly.GetExecutingAssembly());
5131
}
5232

53-
public static async Task<string> Ifc(
54-
IServiceProvider serviceProvider,
55-
string url,
56-
string filePath,
57-
string streamId,
58-
string modelId,
59-
string commitMessage,
60-
string token,
61-
IProgress<ProgressArgs>? progress = null
33+
public static async Task<Version> Ifc(
34+
ImporterArgs args,
35+
IProgress<ProgressArgs>? progress = null,
36+
CancellationToken cancellationToken = default
6237
)
6338
{
64-
var ifcFactory = serviceProvider.GetRequiredService<IIfcFactory>();
65-
var clientFactory = serviceProvider.GetRequiredService<IClientFactory>();
66-
var baseUri = new Uri(url);
67-
var stopwatch = Stopwatch.StartNew();
68-
69-
var model = ifcFactory.Open(filePath);
70-
var ms = stopwatch.ElapsedMilliseconds;
71-
Console.WriteLine($"Opened with WebIFC: {ms} ms");
72-
73-
var graph = IfcGraph.Load(new FilePath(filePath));
74-
var ms2 = stopwatch.ElapsedMilliseconds;
75-
Console.WriteLine($"Loaded with StepParser: {ms2 - ms} ms");
76-
77-
var converter = serviceProvider.GetRequiredService<IGraphConverter>();
78-
var b = converter.Convert(model, graph);
79-
ms = ms2;
80-
ms2 = stopwatch.ElapsedMilliseconds;
81-
Console.WriteLine($"Converted to Speckle Bases: {ms2 - ms} ms");
82-
83-
var serializeProcessFactory = serviceProvider.GetRequiredService<ISerializeProcessFactory>();
84-
var process = serializeProcessFactory.CreateSerializeProcess(
85-
baseUri,
86-
streamId,
87-
token,
88-
progress,
89-
default,
90-
new SerializeProcessOptions(true, true, false, progress is null)
91-
);
92-
var (rootId, _) = await process.Serialize(b).ConfigureAwait(false);
93-
Account account =
94-
new()
95-
{
96-
token = token,
97-
serverInfo = new ServerInfo { url = baseUri.ToString() },
98-
};
99-
ms = ms2;
100-
ms2 = stopwatch.ElapsedMilliseconds;
101-
Console.WriteLine($"Uploaded to Speckle: {ms2 - ms} ms. Root id: {rootId}");
39+
var serviceProvider = GetServiceProvider();
40+
return await Ifc(serviceProvider, args, progress, cancellationToken);
41+
}
10242

103-
// 8 - Create the version (commit)
104-
using var apiClient = clientFactory.Create(account);
105-
var commit = await apiClient.Version.Create(
106-
new CreateVersionInput(rootId, modelId, streamId, message: commitMessage, sourceApplication: "IFC")
107-
);
108-
ms = ms2;
109-
ms2 = stopwatch.ElapsedMilliseconds;
110-
Console.WriteLine($"Committed to Speckle: {ms2 - ms} ms");
111-
return commit.id;
43+
public static async Task<Version> Ifc(
44+
ServiceProvider serviceProvider,
45+
ImporterArgs args,
46+
IProgress<ProgressArgs>? progress = null,
47+
CancellationToken cancellationToken = default
48+
)
49+
{
50+
var importer = serviceProvider.GetRequiredService<Importer>();
51+
return await importer.ImportIfc(args, progress, cancellationToken);
11252
}
11353
}

0 commit comments

Comments
 (0)