-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBakeDataCommandHandler.cs
More file actions
79 lines (67 loc) · 2.98 KB
/
Copy pathBakeDataCommandHandler.cs
File metadata and controls
79 lines (67 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using ShapeDiver.SDK.Stargate;
using ShapeDiver.SDK.Stargate.Commands.Interfaces;
using ShapeDiver.SDK.Stargate.Dto.Commands;
using System;
using System.Linq;
using System.Threading.Tasks;
namespace StargateDotNetClientExample
{
/// <summary>
/// This handler is called by the ShapeDiver Platform when the user wants to bake data for
/// a given output or export of the ShapeDiver model.
/// </summary>
internal class BakeDataCommandHandler : BaseCommandHandler, IBakeDataCommandHandler
{
public BakeDataCommandHandler(IServices services) : base(services)
{
}
public async Task<BakeDataReplyDto> Handle(BakeDataCommandDto data)
{
Services.LogMessage("Handling BakeDataCommand");
// get session context for model
var sessionContext = await this.Services.GeometryBackendContextCache.GetContextAsync(data.Model.Id, false, ModelTokenScopes);
// get computation results
var computationResult = await this.Services.Sdk.GeometryBackendClient.ComputeOutputs(sessionContext, data.Parameters);
var outputs = computationResult.ResultDto.Outputs;
if (!outputs.ContainsKey(data.Output.Id))
{
throw new StargateException($"An output with id {data.Output.Id} does not exist for model {data.Model.Id}.");
}
// find output and chunk
var output = outputs[data.Output.Id];
if (!String.IsNullOrEmpty(data.Output.Chunk.Id))
{
if (!output.Chunks.Any(x => x.Id == data.Output.Chunk.Id))
{
throw new StargateException($"Chunk id '{data.Output.Chunk.Id}' not found in output '{data.Output.Id}'.");
}
}
else if (!String.IsNullOrEmpty(data.Output.Chunk.Name))
{
if (!output.Chunks.Any(x => x.Name == data.Output.Chunk.Name))
{
throw new StargateException($"Chunk name '{data.Output.Chunk.Name}' not found in output '{data.Output.Id}'.");
}
}
else
{
throw new StargateException("Baking of all chunks not implemented yet. Please specify a chunk id or name.");
}
/// Note: For exporting files, use the <see cref="ExportFileCommandHandler"/> instead.
/// TODO ShapeDiver: Implement this handler once the .NET sdTF SDK has been released.
return Reply(BakeDataResultEnum.Success, 0, $"Handled BakeDataCommand for output {output.Name}");
}
private BakeDataReplyDto Reply(BakeDataResultEnum result, int bakedItems = 0, string message = null)
{
return new BakeDataReplyDto()
{
Info = new BakeDataInfoReplyDto()
{
Count = bakedItems,
Result = result,
Message = message
}
};
}
}
}