Esempio n. 1
0
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            CultureInfo.DefaultThreadCurrentCulture   = CultureInfo.InvariantCulture;
            CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.InvariantCulture;

            if (e.Args.Length == 0)
            {
                MainWindow wnd = new MainWindow();
                wnd.Show();
            }
            else
            {
                AttachConsole(-1);
                cliMode = e.Args[0];
                if (cliMode == "-h")
                {
                    Console.WriteLine("\n");
                    Console.WriteLine("Usage: SpeckleGSAUI.exe <command>\n\n" +
                                      "where <command> is one of: receiver, sender\n\n");
                    Console.Write("SpeckleGSAUI.exe <command> -h\thelp on <command>\n");
                    Current.Shutdown();
                    return;
                }
                if (cliMode != "receiver" && cliMode != "sender")
                {
                    Console.WriteLine("Unable to parse command");
                    Current.Shutdown();
                    return;
                }

                for (int index = 1; index < e.Args.Length; index += 2)
                {
                    string arg = e.Args[index].Replace("-", "");
                    if (e.Args.Length <= index + 1 || e.Args[index + 1].StartsWith("-"))
                    {
                        arguments.Add(arg, "true");
                        index--;
                    }
                    else
                    {
                        arguments.Add(arg, e.Args[index + 1].Trim(new char[] { '"' }));
                    }
                }

                GSA.Init();
                Status.Init(this.AddMessage, this.AddError, this.ChangeStatus);
                SpeckleCore.SpeckleInitializer.Initialize();

                RunCLI();

                Current.Shutdown();

                return;
            }
        }
Esempio n. 2
0
        public bool RunCLI(params string[] args)
        {
            CultureInfo.DefaultThreadCurrentCulture   = CultureInfo.InvariantCulture;
            CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.InvariantCulture;

            cliMode = args[0];
            if (cliMode == "-h")
            {
                Console.WriteLine("\n");
                Console.WriteLine("Usage: SpeckleGSAUI.exe <command>\n\n" +
                                  "where <command> is one of: receiver, sender\n\n");
                Console.Write("SpeckleGSAUI.exe <command> -h\thelp on <command>\n");
                return(true);
            }
            if (cliMode != "receiver" && cliMode != "sender")
            {
                Console.WriteLine("Unable to parse command");
                return(false);
            }

            for (int index = 1; index < args.Length; index += 2)
            {
                string arg = args[index].Replace("-", "");
                if (args.Length <= index + 1 || args[index + 1].StartsWith("-"))
                {
                    arguments.Add(arg, "true");
                    index--;
                }
                else
                {
                    arguments.Add(arg, args[index + 1].Trim(new char[] { '"' }));
                }
            }

            if (cliMode == "receiver" && arguments.ContainsKey("h"))
            {
                Console.WriteLine("\n");
                Console.WriteLine("Usage: SpeckleGSAUI.exe receiver\n");
                Console.WriteLine("\n");
                Console.Write("Required arguments:\n");
                Console.Write("--server <server>\t\tAddress of Speckle server\n");
                Console.Write("--email <email>\t\t\tEmail of account\n");
                Console.Write("--token <token>\t\tJWT token\n");
                Console.Write("--file <path>\t\t\tFile to save to. If file does not exist, a new one will be created\n");
                Console.Write("--streamIDs <streamIDs>\t\tComma-delimited ID of streams to be received\n");
                Console.WriteLine("\n");
                Console.Write("Optional arguments:\n");
                Console.Write("--layer [analysis|design]\tSet which layer to write to. Default is design layer\n");
                Console.Write("--nodeAllowance <distance>\tMax distance before nodes are not merged\n");
                return(true);
            }
            else if (cliMode == "sender" && arguments.ContainsKey("h"))
            {
                Console.WriteLine("\n");
                Console.WriteLine("Usage: SpeckleGSAUI.exe sender\n");
                Console.WriteLine("\n");
                Console.Write("Required arguments:\n");
                Console.Write("--server <server>\t\tAddress of Speckle server\n");
                Console.Write("--email <email>\t\t\tEmail of account\n");
                Console.Write("--token <token>\t\tJWT token\n");
                Console.Write("--file <path>\t\t\tFile to open. If file does not exist, a new one will be created\n");
                Console.WriteLine("\n");
                Console.Write("Optional arguments:\n");
                Console.Write("--layer [analysis|design]\tSet which layer to write to. Default is design layer\n");
                Console.Write("--sendAllNodes\t\t\tSend all nodes in model. Default is to send only 'meaningful' nodes\n");
                Console.Write("--separateStreams\t\tSeparate model into different streams\n");
                Console.Write("--result <options>\t\tType of result to send. Each input should be in quotation marks. Comma-delimited\n");
                Console.Write("--resultCases <cases>\t\tCases to extract results from. Comma-delimited\n");
                Console.Write("--resultOnly\t\t\tSend only results\n");
                Console.Write("--resultUnembedded\t\tSend results as separate objects\n");
                Console.Write("--resultInLocalAxis\t\tSend results calculated at the local axis. Default is global\n");
                Console.Write("--result1DNumPosition <num>\tNumber of additional result points within 1D elements\n");
                return(true);
            }

            string[] neededArgs = new string[] { "server", "email", "token", "file" };

            foreach (string a in neededArgs)
            {
                if (!arguments.ContainsKey(a))
                {
                    Console.WriteLine("Missing -" + a + " argument");
                    return(false);
                }
            }

            // Login
            EmailAddress = arguments["email"];
            RestApi      = arguments["server"];
            ApiToken     = arguments["token"];

            //This will create the logger
            GSA.App.LocalSettings.LoggingMinimumLevel = 4; //Debug
            GSA.App.Settings.TargetLayer = (arguments.ContainsKey("layer") && (arguments["layer"].ToLower() == "analysis")) ? GSATargetLayer.Analysis : GSATargetLayer.Design;

            //This ensures that if multiple CLI calls are made sequentially from any process, that there is no carry over of static variable values
            //from previous calls
            GSA.Reset();

            GSA.Init(getRunningVersion().ToString());
            GSA.App.LocalMessenger.MessageAdded += this.ProcessMessage;
            SpeckleCore.SpeckleInitializer.Initialize();

            List <SidSpeckleRecord> receiverStreamInfo;
            List <SidSpeckleRecord> senderStreamInfo;

            var fileArg  = arguments["file"];
            var filePath = fileArg.StartsWith(".") ? Path.Combine(AssemblyDirectory, fileArg) : fileArg;

            // GSA File
            if (File.Exists(filePath))
            {
                OpenFile(filePath, EmailAddress, RestApi, out receiverStreamInfo, out senderStreamInfo, false);
            }
            else if (cliMode == "sender")
            {
                Console.WriteLine("Could not locate file: " + filePath);
                //sending needs the file to exist
                return(false);
            }
            else
            {
                receiverStreamInfo = new List <SidSpeckleRecord>();
                senderStreamInfo   = new List <SidSpeckleRecord>();
                GSA.App.Proxy.NewFile(false);

                GSA.App.Messenger.Message(SpeckleGSAInterfaces.MessageIntent.Display, SpeckleGSAInterfaces.MessageLevel.Information, "Created new file.");

                //Ensure this new file has a file name
                GSA.App.Proxy.SaveAs(filePath);
            }

            var calibrateNodeAtTask = Task.Run(() => GSAProxy.CalibrateNodeAt());

            calibrateNodeAtTask.Wait();

            if (cliMode == "receiver")
            {
                if (!arguments.ContainsKey("streamIDs"))
                {
                    Console.WriteLine("Missing -streamIDs argument");
                    return(false);
                }
                //There seem to be some issues with HTTP requests down the line if this is run on the initial (UI) thread, so this ensures it runs on another thread
                return(Task.Run(() => CLIReceiver(receiverStreamInfo)).Result);
            }
            else if (cliMode == "sender")
            {
                //There seem to be some issues with HTTP requests down the line if this is run on the initial (UI) thread, so this ensures it runs on another thread
                return(Task.Run(() => CLISender(senderStreamInfo)).Result);
            }
            return(true);
        }
Esempio n. 3
0
        public MainWindow()
        {
            InitializeComponent();

            mainWindow.Title = mainWindow.Title + " - " + getRunningVersion();

            DataContext = this;

            Messages = new ObservableCollection <string>();

            gsaSender   = new Sender();
            gsaReceiver = new Receiver();

            triggerTimer     = new Timer();
            status           = UIStatus.IDLE;
            previousTabIndex = 0;

            //Default settings
            SendOnlyMeaningfulNodes.IsChecked = GSA.Settings.SendOnlyMeaningfulNodes;
            SeparateStreams.IsChecked         = GSA.Settings.SeparateStreams;
            PollingRate.Text             = GSA.Settings.PollingRate.ToString();
            CoincidentNodeAllowance.Text = GSA.Settings.CoincidentNodeAllowance.ToString();
            SendOnlyResults.IsChecked    = GSA.Settings.SendOnlyResults;
            EmbedResults.IsChecked       = GSA.Settings.EmbedResults;
            ResultCases.Text             = string.Join("\r\n", GSA.Settings.ResultCases);
            ResultInLocalAxis.IsChecked  = GSA.Settings.ResultInLocalAxis;
            Result1DNumPosition.Text     = GSA.Settings.Result1DNumPosition.ToString();

            //Result List
            foreach (string s in Result.NodalResultMap.Keys)
            {
                CheckBox chk = new CheckBox
                {
                    Content = s,
                    Tag     = Result.NodalResultMap[s]
                };
                chk.Checked   += UpdateNodalResult;
                chk.Unchecked += UpdateNodalResult;
                ResultSelection.Children.Add(chk);
            }

            foreach (string s in Result.Element1DResultMap.Keys)
            {
                CheckBox chk = new CheckBox
                {
                    Content = s,
                    Tag     = Result.Element1DResultMap[s]
                };
                chk.Checked   += UpdateElement1DResult;
                chk.Unchecked += UpdateElement1DResult;
                ResultSelection.Children.Add(chk);
            }

            foreach (string s in Result.Element2DResultMap.Keys)
            {
                CheckBox chk = new CheckBox
                {
                    Content = s,
                    Tag     = Result.Element2DResultMap[s]
                };
                chk.Checked   += UpdateElement2DResult;
                chk.Unchecked += UpdateElement2DResult;
                ResultSelection.Children.Add(chk);
            }

            foreach (string s in Result.MiscResultMap.Keys)
            {
                CheckBox chk = new CheckBox
                {
                    Content = s,
                    Tag     = Result.MiscResultMap[s]
                };
                chk.Checked   += UpdateMiscResult;
                chk.Unchecked += UpdateMiscResult;
                ResultSelection.Children.Add(chk);
            }

            //Draw buttons
            SendButtonPath.Data    = Geometry.Parse(PLAY_BUTTON);
            SendButtonPath.Fill    = (SolidColorBrush)FindResource("PrimaryHueMidBrush");
            ReceiveButtonPath.Data = Geometry.Parse(PLAY_BUTTON);
            ReceiveButtonPath.Fill = (SolidColorBrush)FindResource("PrimaryHueMidBrush");

            //Adds event handling delegates to the status events
            Status.Init(this.AddMessage, this.AddError, this.ChangeStatus);
            GSA.Init();

            //Add further event handling delegates - this time for logging - to the status events
            Status.MessageAdded += (sender, eventArgs) => { Log.Information(eventArgs.Message); };
            Status.ErrorAdded   += (sender, eventArgs) =>
            {
                if (eventArgs.Exception == null)
                {
                    Log.Error(eventArgs.Message);
                }
                else
                {
                    Log.Error(eventArgs.Exception, eventArgs.Message);
                    if (eventArgs.Exception.InnerException != null)
                    {
                        Log.Error(eventArgs.Exception.InnerException, eventArgs.Message);
                    }
                }
            };

            MessagePane.ItemsSource = Messages;

            SpeckleCore.SpeckleInitializer.Initialize();
            SpeckleCore.LocalContext.Init();

            try
            {
                //This will throw an exception if there is no default account
                var account = SpeckleCore.LocalContext.GetDefaultAccount();
                if (account != null)
                {
                    EmailAddress = account.Email;
                    RestApi      = account.RestApi;
                    ApiToken     = account.Token;

                    Status.AddMessage("Logged in to default account at: " + RestApi);
                }
            }
            catch
            {
                Status.AddMessage("No default account found - press the Login button to login/select an account");
            }
        }