-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUploadCommand.cs
More file actions
79 lines (69 loc) · 3.1 KB
/
Copy pathUploadCommand.cs
File metadata and controls
79 lines (69 loc) · 3.1 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 CommandLine;
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 the CreateAndUploadModel function from the SDK.
/// Compare with <see cref="UploadCommandVerbose"/>, which provides more feedback to the user.
/// </summary>
[Verb("upload-model", isDefault: false, HelpText = "Upload a Grasshopper model to ShapeDiver.")]
class UploadCommand : 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($"Starting model upload and check, please wait...");
var context = await sdk.GeometryBackendClient.CreateAndUploadModel(sdk.PlatformClient, createDto, Filename, true);
if (context.ModelData.Model.Status != GDTO.ModelStatusEnum.Confirmed)
{
if (!String.IsNullOrEmpty(context.ModelData.Model.Msg))
{
throw new Exception($"Model checking failed: {context.ModelData.Model.Msg}");
}
else
{
throw new Exception($"Model checking failed");
}
}
// Open model on platform
Console.WriteLine($"Model upload and check completed.");
Process.Start($"https://shapediver.com/app/m/{context.PlatformModelId}");
});
}
}
}