protected override Result RunCommand(RhinoDoc doc, RunMode mode) { GetObject go; go = new GetObject(); go.AcceptNothing(true); go.AcceptEnterWhenDone(true); go.SetCommandPrompt("请选择边界"); go.GeometryFilter = ObjectType.Curve; if (go.Get() != GetResult.Object) { return(Result.Failure); } Curve boundCrv = go.Object(0).Curve(); if (!boundCrv.IsPlanar() || !boundCrv.IsPolyline() || !boundCrv.IsClosed) { return(Result.Failure); } boundCrv.GetBoundingBox(Plane.WorldXY, out Box bound); var dialog = new SelectFolderDialog() { Title = "保存位置" }; DialogResult res = dialog.ShowDialog(Rhino.UI.RhinoEtoApp.MainWindow); if (res == DialogResult.Ok) { try { Dictionary <string, string> pathMap = new Dictionary <string, string>(); foreach (var layer in doc.Layers) { var objects = doc.Objects.FindByLayer(layer); if (!layer.IsVisible || !layer.IsValid || objects.Length == 0) { continue; } string name = layer.FullPath.Replace(Layer.PathSeparator, "$$"); string realPath = IOPath.Combine(dialog.Directory, name + ".eps"); string tmpPath = IOPath.Combine(IOPath.GetTempPath(), layer.Id.ToString()); var eps = new EncapsulatedPostScript(bound); var objs = SelectAllObjectsInBound(bound, objects); if (objs.Count == 0) { continue; } eps.SaveEPS(objs, tmpPath); pathMap.Add(tmpPath, realPath); } if (pathMap.Count == 0) { return(Result.Cancel); } foreach (KeyValuePair <string, string> item in pathMap) { System.IO.File.Move(item.Key, item.Value); RhinoApp.WriteLine($"已写入, {item.Value}"); } System.Diagnostics.Process.Start(dialog.Directory); } catch (Exception ex) { RhinoApp.WriteLine(ex.Message); } finally { doc.Objects.UnselectAll(); doc.Views.Redraw(); } } return(Result.Success); }
private List <string> SaveAdobeDocument(string savePath, List <GeometryBase> objs, AdobeDocType type) { SortedDictionary <string, List <GeometryBase> > geometries = new SortedDictionary <string, List <GeometryBase> >(); GeometryBase boundObj = null; List <string> outputFiles = new List <string>(); DebugEvent($"{type}_BOUND"); foreach (var obj in objs) { if (obj.GetUserString($"{type}_BOUND") == $"{type}_BOUND") { boundObj = obj; DebugEvent($"{type}文档找到边界"); continue; } string page = obj.GetUserString($"{type}_PAGE"); if (type == AdobeDocType.EPS && Directory.Exists(savePath)) { page = Path.Combine(savePath, page + ".eps"); outputFiles.Add(page); } if (!geometries.ContainsKey(page)) { geometries.Add(page, new List <GeometryBase>()); } geometries[page].Add(obj); } if (geometries.Count == 0 || boundObj == null) { return(null); } boundObj.GetBoundingBox(Plane.WorldXY, out Box boundBox); try { var eps = new EncapsulatedPostScript(boundBox, savePath); if (type == AdobeDocType.PDF) { eps.SavePDF(geometries); outputFiles.Add(savePath); } else if (type == AdobeDocType.EPS) { eps.SaveEPS(geometries); } } catch (Exception ex) { ErrorEvent(this, ex.Message); } return(outputFiles); }