Esempio n. 1
0
        private void StoreOutput()
        {
            DebugEvent("Start Store Output");
            GH_DocumentServer doc_server = Instances.DocumentServer;

            if (doc_server == null)
            {
                throw new Exception("No Document Server exist!");
            }

            GH_Document doc = doc_server.ToList().Find(x => x.Properties.ProjectFileName == ID.ToString());

            if (doc == null)
            {
                throw new Exception("Tasker 未找到GH_Document");
            }

            if (string.IsNullOrEmpty(workspace) || string.IsNullOrEmpty(ticket))
            {
                throw new Exception("工作目录和Ticket为空");
            }

            string outDir = Path.Combine(taskspace, ID.ToString(), ticket);

            var hooks = doc.ClusterOutputHooks();

            if (hooks == null)
            {
                return;
            }

            foreach (var hook in hooks)
            {
                string info = hook.CustomDescription;
                if (string.IsNullOrEmpty(info))
                {
                    continue;
                }
                var paraMap = ConvertUrlParam(info);

                if (!paraMap.TryGetValue("Index", out string index) ||
                    !paraMap.TryGetValue("Type", out string type))
                {
                    continue;
                }

                DebugEvent($"Index: {index}; Type: {type}");

                string fileName = Path.Combine(outDir, index + "@" + DateTime.Now.ToString("HH-mm-ss MM-dd"));

                var volatileData = hook.VolatileData;

                if (volatileData.IsEmpty)
                {
                    continue;
                }

                dynamic content = null;

                switch (type)
                {
                case "CSV":
                {
                    var           allData = volatileData.AllData(true);
                    List <string> sList   = new List <string>();
                    allData.ToList().ForEach(el =>
                        {
                            GH_Convert.ToString(el, out string tmp, GH_Conversion.Both);
                            sList.Add(tmp);
                        });

                    string csv = string.Join(Environment.NewLine, sList);

                    fileName += ".csv";
                    File.WriteAllText(fileName, csv, Encoding.UTF8);
                    content = fileName;
                    break;
                }

                case "3DM":
                {
                    fileName += ".3dm";

                    File3dmWriter writer = new File3dmWriter(fileName);

                    foreach (var data in volatileData.AllData(true))
                    {
                        GeometryBase obj = GH_Convert.ToGeometryBase(data);

                        if (obj == null)
                        {
                            continue;
                        }

                        string layer = obj.GetUserString("Layer");
                        if (layer == null)
                        {
                            continue;
                        }
                        ObjectAttributes att = new ObjectAttributes
                        {
                            LayerIndex = writer.GetLayer(layer, Color.Black)
                        };

                        writer.ObjectMap.Add(att, obj);
                    }

                    writer.Write();
                    content = fileName;
                    break;
                }

                case "Data":
                {
                    try
                    {
                        GH_Structure <IGH_Goo> tree = volatileData as GH_Structure <IGH_Goo>;

                        content = IO.SerializeGrasshopperData(tree, hook.CustomName, volatileData.IsEmpty);
                    }
                    catch (Exception ex)
                    {
                        ErrorEvent(this, ex.Message);
                    }

                    break;
                }

                case "EPS":
                {
                    List <GeometryBase> objs = new List <GeometryBase>();

                    foreach (var data in volatileData.AllData(true))
                    {
                        if (data == null)
                        {
                            continue;
                        }
                        GeometryBase obj = GH_Convert.ToGeometryBase(data);
                        if (obj == null)
                        {
                            continue;
                        }

                        objs.Add(obj);
                    }

                    if (!Directory.Exists(fileName))
                    {
                        Directory.CreateDirectory(fileName);
                    }

                    content = JsonConvert.SerializeObject(SaveAdobeDocument(fileName, objs, AdobeDocType.EPS));
                    break;
                }

                case "PDF":
                {
                    List <GeometryBase> objs = new List <GeometryBase>();

                    fileName += ".pdf";

                    foreach (var data in volatileData.AllData(true))
                    {
                        if (data == null)
                        {
                            continue;
                        }
                        GeometryBase obj = GH_Convert.ToGeometryBase(data);
                        if (obj == null)
                        {
                            continue;
                        }

                        objs.Add(obj);
                    }

                    var res = SaveAdobeDocument(fileName, objs, AdobeDocType.PDF);
                    if (res == null || res.Count == 0)
                    {
                        break;
                    }
                    content = res[0];
                    break;
                }

                case "RhinoPDF":
                {
                    var pdf = FilePdf.Create();

                    fileName += ".pdf";

                    DebugEvent("RhinoPDF Begin");

                    foreach (var page in volatileData.AllData(true))
                    {
                        DebugEvent("RhinoPDF got 1 page");
                        if (!page.CastTo(out RhinoPageView view))
                        {
                            DebugEvent(string.Format("{0} can not convert to RhinoPageView", page.GetType()));
                            continue;
                        }

                        DebugEvent("Data is converted to RhinoPage");

                        ViewCaptureSettings settings = new ViewCaptureSettings(view, 300)
                        {
                            OutputColor = ViewCaptureSettings.ColorMode.DisplayColor,
                            RasterMode  = true
                        };

                        pdf.AddPage(settings);
                    }

                    pdf.Write(fileName);
                    content = fileName;
                    break;
                }

                case "DXF":
                {
                    break;
                }

                default:
                    break;
                }

                StoreEvent(this, new JObject
                {
                    ["route"]   = "task-stored",
                    ["id"]      = ID.ToString(),
                    ["index"]   = index.ToString(),
                    ["type"]    = type,
                    ["content"] = content
                }.ToString());
            }
        }
        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            var pdf = FilePdf.Create();

            var pdf_doc = pdf.PdfDocumentImplementation() as Xfinium.Pdf.PdfFixedDocument;

            if (null == pdf_doc)
            {
                return(Result.Failure);
            }

            var cover_page = pdf_doc.Pages.Add();

            using (var font = new System.Drawing.Font("Arial", 30))
            {
                var pdf_font = new Xfinium.Pdf.Graphics.PdfUnicodeTrueTypeFont(font, false)
                {
                    Underline = true
                };
                var color      = new Xfinium.Pdf.Graphics.PdfRgbColor(0, 0, 0);
                var appearance = new Xfinium.Pdf.Graphics.PdfStringAppearanceOptions(pdf_font, null, new Xfinium.Pdf.Graphics.PdfBrush(color));
                var layout     = new Xfinium.Pdf.Graphics.PdfStringLayoutOptions
                {
                    HorizontalAlign = Xfinium.Pdf.Graphics.PdfStringHorizontalAlign.Center,
                    X = cover_page.Width / 2,
                    Y = cover_page.Height / 2
                };
                cover_page.Graphics.DrawString(EnglishName, appearance, layout);
            }

            const int w = (int)(8.5 * 300);
            const int h = 11 * 300;

            var views = doc.Views.GetViewList(true, false);

            for (var i = 0; i < views.Length; i++)
            {
                var view        = views[i];
                var viewcapture = new Rhino.Display.ViewCaptureSettings(view, new System.Drawing.Size(w, h), 300);
                var page_index  = pdf.AddPage(viewcapture) - 1;
                using (var font = new System.Drawing.Font("Arial", 80))
                {
                    var pdf_font   = new Xfinium.Pdf.Graphics.PdfUnicodeTrueTypeFont(font, false);
                    var color      = new Xfinium.Pdf.Graphics.PdfRgbColor(40, 40, 40);
                    var pen        = new Xfinium.Pdf.Graphics.PdfPen(color, 3);
                    var appearance = new Xfinium.Pdf.Graphics.PdfStringAppearanceOptions(pdf_font, pen, null);
                    var layout     = new Xfinium.Pdf.Graphics.PdfStringLayoutOptions
                    {
                        HorizontalAlign = Xfinium.Pdf.Graphics.PdfStringHorizontalAlign.Center,
                        Rotation        = 45,
                        X = cover_page.Width / 2,
                        Y = cover_page.Height / 2
                    };
                    pdf_doc.Pages[page_index].Graphics.DrawString("Top Secret", appearance, layout);
                }

                using (var font = new System.Drawing.Font("Arial", 20))
                {
                    var pdf_font   = new Xfinium.Pdf.Graphics.PdfUnicodeTrueTypeFont(font, false);
                    var color      = new Xfinium.Pdf.Graphics.PdfRgbColor(0, 0, 0);
                    var appearance = new Xfinium.Pdf.Graphics.PdfStringAppearanceOptions(pdf_font, null, new Xfinium.Pdf.Graphics.PdfBrush(color));
                    var layout     = new Xfinium.Pdf.Graphics.PdfStringLayoutOptions
                    {
                        HorizontalAlign = Xfinium.Pdf.Graphics.PdfStringHorizontalAlign.Right,
                        X = cover_page.Width * 0.9,
                        Y = cover_page.Height * 0.9
                    };
                    pdf_doc.Pages[page_index].Graphics.DrawString($"Page {i + 1} of {views.Length}", appearance, layout);
                }
            }

            var path = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);

            path = System.IO.Path.Combine(path, EnglishName + ".pdf");
            pdf.Write(path);

            return(Result.Success);
        }