public void Main(string argument)
        {
            // The main entry point of the script, invoked every time
            // one of the programmable block's Run actions are invoked.
            //
            // The method itself is required, but the argument above
            // can be removed if not needed.


            BMyLog4PB Log = new BMyLog4PB(this, BMyLog4PB.E_ALL, new BMyLog4PB.BMyTextPanelAppender("DrawLog", this));

            Log.AutoFlush = true;

            IMyTextPanel panel = GridTerminalSystem.GetBlockWithName("DrawPanel") as IMyTextPanel;

            if (panel == null)
            {
                throw new Exception("Panel \"DrawPanel\" not found");
            }

            string[] code = panel?.CustomData.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);

            Log?.PushStack("BaconDraw");
            string image = (new BaconDraw()).run(code, Log).ToString();

            Log.PopStack();
            panel?.WritePublicText(image);
            panel?.ShowPublicTextOnScreen();
        }
            public Canvas run(string[] source, BMyLog4PB Log)
            {
                Log?.PushStack(@"BaconDraw.run");

                Log?.IfDebug?.Debug("creating environment");
                #region Environment
                Environment Env = new Environment(Log);
                #endregion Environment

                #region build plugins
                // prepare pluginhandlers
                Env.DrawPlugins.AddPlugin(new DrawPlugin_Background()); // "background R,G,B" where R G B is 0-7
                Env.DrawPlugins.AddPlugin(new DrawPlugin_Circle());     // "circle RADIUS" where RADIUS is integer
                Env.DrawPlugins.AddPlugin(new DrawPlugin_LineTo());     // "lineto x,y" where x y is integer
                Env.DrawPlugins.AddPlugin(new DrawPlugin_MoveTo());     // "moveto x,y" where x y is integer
                Env.DrawPlugins.AddPlugin(new DrawPlugin_Rect());       // "rect x,y" where x y is integer
                Env.DrawPlugins.AddPlugin(new DrawPlugin_Polygon());    // "poly x,y x,y x,y" where x y is integer
                Env.DrawPlugins.AddPlugin(new DrawPlugin_Color());      // "color R,G,B" where R G B is 0-7
                #endregion build plugins
                Log?.IfDebug?.Debug(@"Plugins loaded: {0}", string.Join(",", Env.DrawPlugins.Values.ToList().ConvertAll <string>(L => string.Join(",", L.ConvertAll <string>(P => string.Format(@"{0}.{1}", P.Vendor, P.Name)).ToArray())).ToArray()));

                #region build code queue
                Queue <Command> codeQueue = new Queue <Command>();
                foreach (string currentLine in source)
                {
                    codeQueue.Enqueue(new Command(currentLine));
                }
                #endregion build code queue
                Log?.IfDebug?.Debug(@"build code queue with {0} commands", codeQueue.Count);

                #region make canvas
                Canvas canvas = new Canvas(50, 50, new Color(0, 0, 0));
                #endregion make canvas
                Log?.IfDebug?.Debug(@"created canvas {0}x{1}", canvas.Width, canvas.Height);

                #region progress codeQueue
                while (codeQueue.Count > 0)
                {
                    // break if over limits
                    Command command = codeQueue.Dequeue();
                    if (Env.TryRunDraw(command, canvas))
                    {
                        // success
                        Log?.IfDebug?.Debug(@"sucessfully run command ""{0} {1}"" ", command.Key, command.Args);
                    }
                    else
                    {
                        // failed
                        Log?.IfDebug?.Debug(@"failed at command ""{0} {1}"" ", command.Key, command.Args);
                    }
                }

                Log?.PopStack();
                return(canvas);

                #endregion progress codeQueue
            }
Exemple #3
0
                public bool TryRunDraw(BMyDrawingCommand command, BMyCanvas canvas)
                {
                    Log?.PushStack("BMyEnvironment.TryRunDraw(BMyDrawingCommand command, BMyCanvas canvas)");
                    List <BMyDrawPlugin> DrawPluginsForCommand = DrawPlugins.FindAllByName(command.Key);

                    Log?.Debug("found {0} commands for {1}", DrawPluginsForCommand.Count, command.Key);
                    bool successfull = false;

                    if (DrawPluginsForCommand.Count > 0)
                    {
                        for (int i = 0; i < DrawPluginsForCommand.Count && !successfull; i++)
                        {
                            successfull = DrawPluginsForCommand[i].TryRun(command, canvas, this);
                            Log?.Debug("{0}: Plugin {1}/{2} with \"{3}\"", successfull?"OK":"FAIL", DrawPluginsForCommand[i].Vendor, DrawPluginsForCommand[i].Name, command);
                        }
                    }
                    Log?.Debug("{0}: Drawing of \"{1}\"", successfull ? "OK" : "FAIL", command);
                    Log?.PopStack();
                    return(successfull);
                }