public RhinoSender(string _payload, Interop _Context)
        {
            Context = _Context;

            dynamic InitPayload = JsonConvert.DeserializeObject <ExpandoObject>(_payload);

            Client = new SpeckleApiClient(( string )InitPayload.account.restApi, true);

            StreamName = ( string )InitPayload.streamName;

            SetClientEvents();
            SetRhinoEvents();
            SetTimers();

            Display         = new SpeckleDisplayConduit();
            Display.Enabled = true;

            Context.NotifySpeckleFrame("set-gl-load", "", "true");

            Client.IntializeSender(( string )InitPayload.account.apiToken, Context.GetDocumentName(), "Rhino", Context.GetDocumentGuid())
            .ContinueWith(res =>
            {
                StreamId           = Client.Stream.StreamId;
                Client.Stream.Name = StreamName;

                Context.NotifySpeckleFrame("set-gl-load", "", "false");
                Context.NotifySpeckleFrame("client-add", StreamId, JsonConvert.SerializeObject(new { stream = Client.Stream, client = Client }));
                Context.UserClients.Add(this);

                InitTrackedObjects(InitPayload);
                DataSender.Start();
            });
        }
Beispiel #2
0
        public override void AddedToDocument(GH_Document document)
        {
            base.AddedToDocument(document);
            Document = this.OnPingDocument();

            if (mySender == null)
            {
                this.NickName = "Initialising...";
                this.Locked   = true;

                var myForm = new SpecklePopup.MainWindow();

                var some = new System.Windows.Interop.WindowInteropHelper(myForm)
                {
                    Owner = Rhino.RhinoApp.MainWindowHandle()
                };

                myForm.ShowDialog();

                if (myForm.restApi != null && myForm.apitoken != null)
                {
                    mySender = new SpeckleApiClient(myForm.restApi);
                    RestApi  = myForm.restApi;
                    mySender.IntializeSender(myForm.apitoken, Document.DisplayName, "Grasshopper", Document.DocumentID.ToString()).ContinueWith(task =>
                    {
                        Rhino.RhinoApp.MainApplicationWindow.Invoke(ExpireComponentAction);
                    });
                }
                else
                {
                    AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Account selection failed");
                    return;
                }
            }
            else
            {
            }

            mySender.OnReady += (sender, e) =>
            {
                StreamId      = mySender.StreamId;
                this.Locked   = false;
                this.NickName = "Anonymous Stream";
                //this.UpdateMetadata();
                Rhino.RhinoApp.MainApplicationWindow.Invoke(ExpireComponentAction);
            };

            mySender.OnWsMessage += OnWsMessage;

            mySender.OnLogData += (sender, e) =>
            {
                this.Log += DateTime.Now.ToString("dd:HH:mm:ss ") + e.EventData + "\n";
            };

            mySender.OnError += (sender, e) =>
            {
                this.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, e.EventName + ": " + e.EventData);
                this.Log += DateTime.Now.ToString("dd:HH:mm:ss ") + e.EventData + "\n";
            };

            ExpireComponentAction = () => ExpireSolution(true);

            ObjectChanged += (sender, e) => UpdateMetadata();

            foreach (var param in Params.Input)
            {
                param.ObjectChanged += (sender, e) => UpdateMetadata();
            }

            MetadataSender = new System.Timers.Timer(1000)
            {
                AutoReset = false, Enabled = false
            };
            MetadataSender.Elapsed += MetadataSender_Elapsed;

            DataSender = new System.Timers.Timer(2000)
            {
                AutoReset = false, Enabled = false
            };
            DataSender.Elapsed += DataSender_Elapsed;

            ObjectCache = new Dictionary <string, SpeckleObject>();
        }
Beispiel #3
0
        public override void AddedToDocument(GH_Document document)
        {
            base.AddedToDocument(document);
            Document = OnPingDocument();

            if (Client == null)
            {
                Locked   = true;
                NickName = "Initialising";

                Account account = null;
                try
                {
                    account = LocalContext.GetDefaultAccount();
                    RestApi = account.RestApi;
                    Client  = new SpeckleApiClient(account.RestApi);
                    Client.IntializeSender(account.Token, Document.DisplayName, "Grasshopper", Document.DocumentID.ToString()).ContinueWith(task =>
                    {
                        Rhino.RhinoApp.InvokeOnUiThread(ExpireComponentAction);
                    });
                }
                catch (Exception err)
                {
                }

                if (account == null)
                {
                    var signInWindow = new SpecklePopup.SignInWindow(true);
                    var helper       = new System.Windows.Interop.WindowInteropHelper(signInWindow);
                    helper.Owner = Rhino.RhinoApp.MainWindowHandle();

                    signInWindow.ShowDialog();

                    if (signInWindow.AccountListBox.SelectedIndex != -1)
                    {
                        account = signInWindow.accounts[signInWindow.AccountListBox.SelectedIndex];
                        RestApi = account.RestApi;
                        Client  = new SpeckleApiClient(account.RestApi);
                        Client.IntializeSender(account.Token, Document.DisplayName, "Grasshopper", Document.DocumentID.ToString()).ContinueWith(task =>
                        {
                            Rhino.RhinoApp.InvokeOnUiThread(ExpireComponentAction);
                        });
                    }
                    else
                    {
                        AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Account selection failed.");
                        return;
                    }
                }
            }
            else
            {
            }

            Client.OnReady += (sender, e) =>
            {
                StreamId = Client.StreamId;
                if (!WasSerialised)
                {
                    Locked   = false;
                    NickName = "Anonymous Stream";
                }
                ////this.UpdateMetadata();
                Rhino.RhinoApp.InvokeOnUiThread(ExpireComponentAction);
            };

            Client.OnWsMessage += OnWsMessage;

            Client.OnLogData += (sender, e) =>
            {
                Log += DateTime.Now.ToString("dd:HH:mm:ss ") + e.EventData + "\n";
            };

            Client.OnError += (sender, e) =>
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, e.EventName + ": " + e.EventData);
                Log += DateTime.Now.ToString("dd:HH:mm:ss ") + e.EventData + "\n";
            };

            ExpireComponentAction = () => ExpireSolution(true);

            ObjectChanged += (sender, e) => UpdateMetadata();

            foreach (var param in Params.Input)
            {
                param.ObjectChanged += (sender, e) => UpdateMetadata();
            }

            MetadataSender = new System.Timers.Timer(1000)
            {
                AutoReset = false, Enabled = false
            };
            MetadataSender.Elapsed += MetadataSender_Elapsed;

            DataSender = new System.Timers.Timer(2000)
            {
                AutoReset = false, Enabled = false
            };
            DataSender.Elapsed += DataSender_Elapsed;

            ObjectCache = new Dictionary <string, SpeckleObject>();

            Grasshopper.Instances.DocumentServer.DocumentRemoved += DocumentServer_DocumentRemoved;
        }
Beispiel #4
0
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            if (Client == null)
            {
                Account account = null;
                try
                {
                    RestApi = account.RestApi;
                    Client  = new SpeckleApiClient(account.RestApi);
                    Client.IntializeSender(account.Token, "docName", "Revit_Datasmith", "").ContinueWith(task =>
                                                                                                         { });
                }
                catch (Exception err)
                {
                }

                if (account == null)
                {
                    var signInWindow = new SpecklePopup.SignInWindow(true);

                    signInWindow.ShowDialog();

                    if (signInWindow.AccountListBox.SelectedIndex != -1)
                    {
                        account = signInWindow.accounts[signInWindow.AccountListBox.SelectedIndex];
                        RestApi = account.RestApi;
                        Client  = new SpeckleApiClient(account.RestApi);
                        Client.IntializeSender(account.Token, "docName", "Revit_Datasmith", "").ContinueWith(task =>
                                                                                                             { });
                    }
                    else
                    {
                        return(Result.Cancelled);
                    }
                }
            }
            else
            {
            }

            Client.OnReady += (sender, e) =>
            {
                StreamId = Client.StreamId;
            };

            MetadataSender = new System.Timers.Timer(1000)
            {
                AutoReset = false, Enabled = false
            };
            MetadataSender.Elapsed += MetadataSender_Elapsed;

            DataSender = new System.Timers.Timer(2000)
            {
                AutoReset = false, Enabled = false
            };
            DataSender.Elapsed += DataSender_Elapsed;

            ObjectCache = new Dictionary <string, SpeckleObject>();


            // Original datasmith form. Need to remove form.
            DatasmithRevitCommand dmrc = new DatasmithRevitCommand();

            dmrc.SpeckleCommand = this;
            dmrc.Execute(commandData, ref message, elements);



            //ForceUpdateData();


            return(Result.Succeeded);
        }