Repository navigation
Expand file tree
/
Copy pathUploadCommandVerbose.cs
More file actions
97 lines (85 loc) · 4.16 KB
/
Copy pathUploadCommandVerbose.cs
File metadata and controls
97 lines (85 loc) · 4.16 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using CommandLine;
using DotNetSdkSampleConsoleApp.Util;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using GDTO = ShapeDiver.SDK.GeometryBackend.DTO;
using PDTO = ShapeDiver.SDK.PlatformBackend.DTO;
namespace DotNetSdkSampleConsoleApp.Commands
{
/// <summary>
/// Command for uploading a Grasshopper model to ShapeDiver. Uses individual SDK calls to show what's happening behind the scences.
/// Provides more feedback to the user.
/// Compare with <see cref="UploadCommand"/>.
/// </summary>
[Verb("upload-model-verbose", isDefault: false, HelpText = "Upload a Grasshopper model to ShapeDiver (verbose output).")]
class UploadCommandVerbose : BaseCommand, ICommand
{
[Option('f', "filename", HelpText = "Path to Grasshopper model (.gh or .ghx)", Required = true)]
public string Filename { get; set; }
[Option('t', "title", HelpText = "Title of the model on the ShapeDiver Platform")]
public string Title { get; set; }
private List<PDTO.ModelTokenScopeEnum> Scopes = new List<PDTO.ModelTokenScopeEnum>() {
PDTO.ModelTokenScopeEnum.GroupOwner,
PDTO.ModelTokenScopeEnum.GroupExport,
PDTO.ModelTokenScopeEnum.GroupView
};
public async Task Execute()
{
await WrapExceptions(async () =>
{
// check if file exists
if (!File.Exists(Filename))
{
throw new Exception("Could not read Grasshopper file");
}
var fi = new FileInfo(Filename);
if (fi.Extension != ".gh" && fi.Extension != ".ghx")
{
throw new Exception($"{Filename} is not a Grasshopper file");
}
// get authenticated SDK
var sdk = await GetAuthenticatedSDK();
// create model call
var createDto = new PDTO.ModelCreateDto()
{
FileType = fi.Extension == ".ghx" ? PDTO.ModelFileTypeEnum.GrasshopperXml : PDTO.ModelFileTypeEnum.GrasshopperBinary,
Title = Title,
Filename = fi.Name,
};
Console.WriteLine($"Create model...");
var createResult = await sdk.PlatformClient.ModelApi.Create(createDto);
// get a context for the model, upload the grasshopper definition
var context = await sdk.GeometryBackendClient.GetContext(createResult.Data.Id, sdk.PlatformClient, Scopes);
Console.WriteLine($"Upload model...");
await sdk.GeometryBackendClient.UploadModel(context, Filename);
// wait for model check
var modelDto = await GeometryBackendUtils.WaitForModelCheck(sdk, context);
if (modelDto.Model.Status == GDTO.ModelStatusEnum.Pending)
{
Console.WriteLine($"Model checking requires manual intervention by ShapeDiver, please visit https://shapediver.com/app/library/pending");
Process.Start("https://shapediver.com/app/library/pending");
return;
}
if (modelDto.Model.Status != GDTO.ModelStatusEnum.Confirmed)
{
if (!String.IsNullOrEmpty(modelDto.Model.Msg))
{
throw new Exception($"Model checking failed: {modelDto.Model.Msg}");
}
else
{
throw new Exception($"Model checking failed");
}
}
// Update model status on platform and publish it
Console.WriteLine($"Publishing confirmed model...");
await sdk.PlatformClient.ModelApi.Patch(context.PlatformModelId);
await sdk.PlatformClient.ModelApi.Patch(context.PlatformModelId, new PDTO.ModelPatchDto() { Status = PDTO.ModelStatusEnum.Done });
Process.Start($"https://shapediver.com/app/m/{createResult.Data.Slug}");
});
}
}
}