|
| 1 | +/* |
| 2 | + * This file is part of the Buildings and Habitats object Model (BHoM) |
| 3 | + * Copyright (c) 2015 - 2022, the respective contributors. All rights reserved. |
| 4 | + * |
| 5 | + * Each contributor holds copyright over their respective contributions. |
| 6 | + * The project versioning (Git) records all such contribution source information. |
| 7 | + * |
| 8 | + * |
| 9 | + * The BHoM is free software: you can redistribute it and/or modify |
| 10 | + * it under the terms of the GNU Lesser General Public License as published by |
| 11 | + * the Free Software Foundation, either version 3.0 of the License, or |
| 12 | + * (at your option) any later version. |
| 13 | + * |
| 14 | + * The BHoM is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | + * GNU Lesser General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU Lesser General Public License |
| 20 | + * along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>. |
| 21 | + */ |
| 22 | + |
| 23 | +using BH.oM.Base; |
| 24 | +using BH.oM.Base.Attributes; |
| 25 | +using System; |
| 26 | +using System.Collections.Generic; |
| 27 | +using System.ComponentModel; |
| 28 | +using System.Linq; |
| 29 | +using System.IO; |
| 30 | +using Rhino; |
| 31 | +using Rhino.Display; |
| 32 | +using System.Drawing; |
| 33 | +using BH.oM.Rhinoceros.ViewCapture; |
| 34 | +using System.Drawing.Imaging; |
| 35 | + |
| 36 | +namespace BH.Engine.Rhinoceros |
| 37 | +{ |
| 38 | + public static partial class Compute |
| 39 | + { |
| 40 | + /***************************************************/ |
| 41 | + /**** Public Methods ****/ |
| 42 | + /***************************************************/ |
| 43 | + |
| 44 | + [Description("Captures the currently active view to file.")] |
| 45 | + [Input("active", "Toggle to activate. Toggle to true to capture the view port.")] |
| 46 | + [Input("folderPath", "Folder path to store the image in. To folder that the currently open rhino model is stored in will be used if nothing is provided.")] |
| 47 | + [Input("imageName", "Name of the image, without file ending. To update the file ending, please see the viewcapture settings. The viewport name will be used if nothing is provided.")] |
| 48 | + [Input("settings", "Settings to control the view capture.")] |
| 49 | + [Output("success", "Returns true if the view capture was successful.")] |
| 50 | + public static bool CaptureView(bool active = false, string folderPath = "", string imageName = "", IViewCaptureSettings settings = null) |
| 51 | + { |
| 52 | + RhinoDoc doc = Rhino.RhinoDoc.ActiveDoc; |
| 53 | + |
| 54 | + folderPath = ValidateFolderPath(folderPath, doc); |
| 55 | + if (folderPath == null) |
| 56 | + return false; |
| 57 | + |
| 58 | + if (!active) |
| 59 | + return false; |
| 60 | + |
| 61 | + settings = settings ?? new ScaleViewCaptureSettings(); //Default view capture settings |
| 62 | + |
| 63 | + return CaptureActiveView(doc, settings, folderPath, imageName); |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | + /***************************************************/ |
| 68 | + /**** Private Methods ****/ |
| 69 | + /***************************************************/ |
| 70 | + |
| 71 | + [Description("")] |
| 72 | + [Input("", "")] |
| 73 | + [Output("", "")] |
| 74 | + private static bool CaptureActiveView(RhinoDoc doc, IViewCaptureSettings settings, string folderName, string imageName) |
| 75 | + { |
| 76 | + var view = doc.Views.ActiveView; |
| 77 | + |
| 78 | + if (string.IsNullOrWhiteSpace(imageName)) |
| 79 | + { |
| 80 | + Engine.Base.Compute.RecordNote("No image name provided. Name of active viewport will be used."); |
| 81 | + imageName = view.ActiveViewport.Name; |
| 82 | + } |
| 83 | + |
| 84 | + ViewCapture viewCapture = settings.IViewCapture(view.ActiveViewport); |
| 85 | + |
| 86 | + if (viewCapture == null) |
| 87 | + return false; |
| 88 | + |
| 89 | + var bitmap = viewCapture.CaptureToBitmap(view); |
| 90 | + |
| 91 | + if (null != bitmap) |
| 92 | + { |
| 93 | + string fileEnding; |
| 94 | + ImageFormat imageFormat = settings.GetImageFormat(out fileEnding); |
| 95 | + if (imageFormat == null) |
| 96 | + return false; |
| 97 | + |
| 98 | + var filename = Path.Combine(folderName, imageName + fileEnding); |
| 99 | + bitmap.Save(filename, imageFormat); |
| 100 | + return true; |
| 101 | + } |
| 102 | + |
| 103 | + return false; |
| 104 | + } |
| 105 | + |
| 106 | + /***************************************************/ |
| 107 | + |
| 108 | + private static string ValidateFolderPath(string folderPath, RhinoDoc doc) |
| 109 | + { |
| 110 | + if (string.IsNullOrEmpty(folderPath)) |
| 111 | + { |
| 112 | + folderPath = Path.GetDirectoryName(doc.Path); |
| 113 | + Engine.Base.Compute.RecordNote($"No path provided. Images will be saved in the same folder as the Open rhino model."); |
| 114 | + } |
| 115 | + |
| 116 | + if (!Directory.Exists(folderPath)) |
| 117 | + { |
| 118 | + Engine.Base.Compute.RecordError($"Directory {folderPath} does not exist."); |
| 119 | + return null; |
| 120 | + } |
| 121 | + return folderPath; |
| 122 | + } |
| 123 | + |
| 124 | + /***************************************************/ |
| 125 | + |
| 126 | + private static ImageFormat GetImageFormat(this IViewCaptureSettings settings, out string fileEnding) |
| 127 | + { |
| 128 | + string imageFormat = settings.FileFormat.ToUpper(); |
| 129 | + |
| 130 | + switch (imageFormat) |
| 131 | + { |
| 132 | + case "BMP": |
| 133 | + fileEnding = ".bmp"; |
| 134 | + return ImageFormat.Bmp; |
| 135 | + case "EMF": |
| 136 | + fileEnding = ".emf"; |
| 137 | + return ImageFormat.Emf; |
| 138 | + case "WMF": |
| 139 | + fileEnding = ".wmf"; |
| 140 | + return ImageFormat.Wmf; |
| 141 | + case "GIF": |
| 142 | + fileEnding = ".gif"; |
| 143 | + return ImageFormat.Gif; |
| 144 | + case "JPG": |
| 145 | + case "JPEG": |
| 146 | + fileEnding = ".jpg"; |
| 147 | + return ImageFormat.Jpeg; |
| 148 | + case "PNG": |
| 149 | + fileEnding = ".png"; |
| 150 | + return ImageFormat.Png; |
| 151 | + case "TIFF": |
| 152 | + fileEnding = ".tiff"; |
| 153 | + return ImageFormat.Tiff; |
| 154 | + case "EXIF": |
| 155 | + fileEnding = ".exif"; |
| 156 | + return ImageFormat.Exif; |
| 157 | + case "ICON": |
| 158 | + fileEnding = ".icon"; |
| 159 | + return ImageFormat.Icon; |
| 160 | + default: |
| 161 | + Engine.Base.Compute.RecordError("Unknown image format."); |
| 162 | + fileEnding = ""; |
| 163 | + return null; |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + /***************************************************/ |
| 168 | + |
| 169 | + private static ViewCapture IViewCapture(this IViewCaptureSettings settings, RhinoViewport viewport) |
| 170 | + { |
| 171 | + ViewCapture viewCapture = ViewCapture(settings as dynamic, viewport); |
| 172 | + viewCapture.ScaleScreenItems = settings.ScaleScreenItems; |
| 173 | + viewCapture.DrawAxes = settings.DrawAxes; |
| 174 | + viewCapture.DrawGrid = settings.DrawGrid; |
| 175 | + viewCapture.DrawGridAxes = settings.DrawGridAxes; |
| 176 | + viewCapture.TransparentBackground = settings.TransparentBackground; |
| 177 | + viewCapture.Preview = settings.Preview; |
| 178 | + return viewCapture; |
| 179 | + } |
| 180 | + |
| 181 | + /***************************************************/ |
| 182 | + |
| 183 | + private static ViewCapture ViewCapture(this ScaleViewCaptureSettings settings, RhinoViewport viewport) |
| 184 | + { |
| 185 | + return new ViewCapture |
| 186 | + { |
| 187 | + Height = (int)Math.Round(viewport.Size.Height * settings.Scale), |
| 188 | + Width = (int)Math.Round(viewport.Size.Width * settings.Scale) |
| 189 | + }; |
| 190 | + } |
| 191 | + |
| 192 | + /***************************************************/ |
| 193 | + |
| 194 | + private static ViewCapture ViewCapture(this DimensionViewCaptureSettings settings, RhinoViewport viewport) |
| 195 | + { |
| 196 | + return new ViewCapture |
| 197 | + { |
| 198 | + Height = settings.Height, |
| 199 | + Width = settings.Width |
| 200 | + }; |
| 201 | + } |
| 202 | + |
| 203 | + /***************************************************/ |
| 204 | + } |
| 205 | +} |
0 commit comments