Polish
This commit is contained in:
+101
-19
@@ -2,45 +2,127 @@
|
||||
using Speckle.Core.Kits;
|
||||
using Speckle.Core.Credentials;
|
||||
using Speckle.Core.Api;
|
||||
using System.Threading;
|
||||
using Objects;
|
||||
using Speckle.Core.Models;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Objects.Geometry;
|
||||
using Objects.Primitive;
|
||||
using Speckle.Core.Transports;
|
||||
using System.Linq;
|
||||
|
||||
namespace CSharpStarter
|
||||
{
|
||||
class Program
|
||||
{
|
||||
// Running this program will pull the latest commit from the main branch
|
||||
// of the specified stream and duplicate it inside a different branch.
|
||||
// (branch should exist already or the program will fail)
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Hello Speckle!");
|
||||
// The id of the stream to work with (we're assuming it already exists in your default account's server)
|
||||
var streamId = "c1800d795b";
|
||||
// The name of the branch we'll send data to.
|
||||
var branchName = "processed";
|
||||
|
||||
// Get default account on this machine
|
||||
var defaultAccount = AccountManager.GetDefaultAccount();
|
||||
// Or get all the accounts and manually choose the one you want
|
||||
// var accounts = AccountManager.GetAccounts();
|
||||
// var defaultAccount = accounts.ToList().FirstOrDefault();
|
||||
|
||||
// Create a new "base" object
|
||||
var commitObj = new Base();
|
||||
// Base objects are dynamic, so you can assign any properties just like a Dictionary
|
||||
commitObj["myProp"] = "myPropValue";
|
||||
commitObj["myOtherProp"] = new List<string> { "A", "list", "of", "values" };
|
||||
// Authenticate using the account
|
||||
var client = new Client(defaultAccount);
|
||||
|
||||
var boxes = new List<Point>();
|
||||
for (int i = 0; i < 10; i++)
|
||||
// Get the main branch with it's latest commit reference
|
||||
var branch = client.BranchGet(streamId, "main", 1).Result;
|
||||
// Get the id of the object referenced in the commit
|
||||
var hash = branch.commits.items[0].referencedObject;
|
||||
|
||||
|
||||
// Create the server transport for the specified stream.
|
||||
var transport = new ServerTransport(defaultAccount, streamId);
|
||||
// Receive the object
|
||||
var receivedBase = Operations.Receive(hash, transport).Result;
|
||||
|
||||
// Process the object however you'd like
|
||||
Console.WriteLine("Received object:" + receivedBase);
|
||||
|
||||
// Sending the object will return it's unique identifier.
|
||||
var newHash = Operations.Send(receivedBase, new List<ITransport> { transport }).Result;
|
||||
|
||||
// Create a commit in `processed` branch (it must previously exist)
|
||||
var commitId = client.CommitCreate(new CommitCreateInput()
|
||||
{
|
||||
for (int j = 0; j < 10; j++)
|
||||
{
|
||||
boxes.Add(new Point(i, j, 0));
|
||||
}
|
||||
}
|
||||
commitObj["boxes"] = boxes;
|
||||
// Send the object to Speckle, get back the commit id
|
||||
var commitId = Helpers.Send("2d9b814ed6", commitObj, "Upload from my console app", null, 0, defaultAccount, false).Result;
|
||||
branchName = branchName,
|
||||
message = "Automatic commit created by AEC Tech Demo C# console app.",
|
||||
objectId = newHash,
|
||||
streamId = streamId,
|
||||
sourceApplication = "AEC Tech C# Script"
|
||||
|
||||
// To receive the latest commit
|
||||
var receivedObj = Helpers.Receive("2d9b814ed6", defaultAccount).Result;
|
||||
Console.WriteLine(receivedObj["myProp"]);
|
||||
}).Result;
|
||||
|
||||
Console.WriteLine($"Successfully created commit with id: {commitId}");
|
||||
|
||||
// Remember to dispose of the client once you've finished with it.
|
||||
client.Dispose();
|
||||
}
|
||||
|
||||
static void ReactToCommit(string[] args)
|
||||
|
||||
{
|
||||
// Get default account on this machine
|
||||
var defaultAccount = AccountManager.GetDefaultAccount();
|
||||
var client = new Client(defaultAccount);
|
||||
var streamId = "42c06de34f";
|
||||
var branch = client.BranchGet(streamId, "main", 1).Result;
|
||||
var hash = branch.commits.items[0].referencedObject;
|
||||
|
||||
|
||||
var exit = false;
|
||||
client.OnCommitCreated += (sender, e) =>
|
||||
{
|
||||
// Ignore commits from any branch other than 'main'
|
||||
if (e.branchName != "main") return;
|
||||
|
||||
Console.WriteLine("Commit was created in Main! Processing data...");
|
||||
|
||||
// Create the server transport for the specified stream.
|
||||
var transport = new ServerTransport(defaultAccount, streamId);
|
||||
// Receive the object
|
||||
var receivedBase = Operations.Receive(hash, transport).Result;
|
||||
|
||||
var newHash = Operations.Send(receivedBase, new List<ITransport> { transport }).Result;
|
||||
|
||||
// Create a commit in `processed` branch (it must previously exist)
|
||||
var commitId = client.CommitCreate(new CommitCreateInput()
|
||||
{
|
||||
branchName = "processed",
|
||||
message = "Automatic commit created by AEC Tech Demo C# console app.",
|
||||
objectId = newHash,
|
||||
streamId = streamId,
|
||||
sourceApplication = "C#"
|
||||
|
||||
}).Result;
|
||||
Console.WriteLine($"Successfully created commit with id: {commitId}");
|
||||
exit = true;
|
||||
};
|
||||
|
||||
// Subscribe to commits created on the stream.
|
||||
client.SubscribeCommitCreated(streamId);
|
||||
|
||||
// HACK: This is a super hacky way to get a C# console app to wait for an event to happen.
|
||||
// YOU SHOULD NOT DO THIS IN A PRODUCTION APPLICATION 🤣
|
||||
Console.WriteLine("Waiting for commit created event...");
|
||||
while (!exit)
|
||||
{
|
||||
Thread.Sleep(500);
|
||||
Console.WriteLine("Still waiting...");
|
||||
}
|
||||
|
||||
// Remember to dispose of the client once you've finished with it.
|
||||
client.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user