Exemple #1
0
        public void BringToFront(bool restore = false)
        {
            GH_DocumentServer doc_server = Instances.DocumentServer;

            if (doc_server == null)
            {
                ErrorEvent(this, "No Document Server exist!");
                return;
            }

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

            if (doc == null)
            {
                return;
            }

            GH_Canvas activeCanvas = Instances.ActiveCanvas;

            if (activeCanvas == null)
            {
                ErrorEvent(this, "No Active Canvas exist!");
                return;
            }

            activeCanvas.Document = doc;
            activeCanvas.Refresh();

            IsTaskRestore = restore;

            // SolutionEndCnt = 0;
            UpdateData(true, restore);
        }
Exemple #2
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());
            }
        }
Exemple #3
0
        private void UpdateData(bool recomputeOnTheEnd, bool restore = false)
        {
            if (dataList.Count == 0 || dataList == null)
            {
                return;
            }

            GH_DocumentServer doc_server = Instances.DocumentServer;

            if (doc_server == null)
            {
                ErrorEvent(this, "No Document Server exist!");
                return;
            }

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

            if (doc == null)
            {
                return;
            }

            var hooks = doc.ClusterInputHooks();

            if (!restore)
            {
                var rndData = dataList.FindAll(data => data.type == "4" || data.type == "5");

                history.Add(new TaskRecord
                {
                    name      = name,
                    TaskID    = ID,
                    HistoryID = Guid.NewGuid(),
                    date      = DateTime.Now,
                    table     = dataTable,
                    taskDatas = JsonConvert.SerializeObject(rndData)
                });
            }

            foreach (var hook in hooks)
            {
                TaskData data = dataList.Find(x => x.name == hook.CustomNickName || x.dataID == hook.CustomNickName);

                if (data == null)
                {
                    continue;
                }

                GH_Structure <IGH_Goo> m_data;

                if (data.type == "5")
                {
                    m_data = IO.DeserializeGrasshopperData(Convert.FromBase64String((string)data.value));
                }
                else
                {
                    m_data = SingleDataStructrue(data.value);
                }

                hook.ClearPlaceholderData();

                // if (Equals(hook.VolatileData, m_data)) continue;
                if (!m_data.IsEmpty)
                {
                    hook.SetPlaceholderData(m_data);
                }

                if (!recomputeOnTheEnd)
                {
                    hook.ExpireSolution(true);
                }
            }

            // for data placeholder inside cluster (deep = 1)

            var clusters = new List <GH_Cluster>();

            foreach (var obj in doc.Objects)
            {
                if (!(obj is GH_Cluster cluster))
                {
                    continue;
                }
                clusters.Add(cluster);
            }

            if (clusters.Count == 0)
            {
                return;
            }


            foreach (var cluster in clusters)
            {
                foreach (var obj in cluster.Document("").Objects)
                {
                    if (!(obj is IGH_Param param))
                    {
                        continue;
                    }

                    string nickname = param.NickName;

                    if (!nickname.StartsWith("@", StringComparison.Ordinal))
                    {
                        continue;
                    }

                    TaskData data = dataList.Find(x => x.name == nickname || x.dataID == nickname);

                    if (data == null)
                    {
                        continue;
                    }

                    Utility.InvokeMethod(param, "Script_ClearPersistentData");
                    Utility.InvokeMethod(param, "Script_AddPersistentData", new List <object>()
                    {
                        data.value
                    });

                    if (!recomputeOnTheEnd)
                    {
                        param.ExpireSolution(true);
                    }
                    if (!recomputeOnTheEnd)
                    {
                        cluster.ExpireSolution(true);
                    }
                }
            }

            if (recomputeOnTheEnd)
            {
                doc.NewSolution(true);
            }

            GH_Canvas activeCanvas = Instances.ActiveCanvas;

            if (activeCanvas == null)
            {
                ErrorEvent(this, "No Active Canvas exist!");
                return;
            }

            activeCanvas.Document.IsModified = false;
            activeCanvas.Refresh();

            dataList.Clear();
        }