/// <summary> /// This is the method that actually does the work. /// </summary> /// <param name="DA">The DA object can be used to retrieve data from input parameters and /// to store data in output parameters.</param> protected override void SolveInstance(IGH_DataAccess DA) { Hp.Drawing drawing = new Hp.Drawing(); if (!DA.GetData <Hp.Drawing>(0, ref drawing)) { return; } Sm.DrawingVisual dwg = drawing.ToGeometryVisual(); double width = drawing.Width; double height = drawing.Height; if (width < 100) { width = 100; } if (height < 100) { height = 100; } img = dwg.ToBitmap(width, height); message = drawing.ToString(); UpdateMessage(); }
/// <summary> /// This is the method that actually does the work. /// </summary> /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param> protected override void SolveInstance(IGH_DataAccess DA) { Hp.Drawing drawing = new Hp.Drawing(); if (!DA.GetData<Hp.Drawing>(0, ref drawing)) return; string path = "C:\\Users\\Public\\Documents\\"; string name = DateTime.UtcNow.ToString("yyyy-dd-M_HH-mm-ss"); ; bool save = false; bool hasPath = DA.GetData(1, ref path); bool hasName = DA.GetData(2, ref name); if (!DA.GetData(3, ref save)) return; if (!hasPath) { if (this.OnPingDocument().FilePath != null) { path = Path.GetDirectoryName(this.OnPingDocument().FilePath) + "\\"; } } else { path += "//"; } string filepath = path + name + ".svg"; if (save) { StreamWriter Writer = new StreamWriter(filepath); Writer.Write(drawing.ToSVG()); Writer.Close(); } DA.SetData(0, filepath); }
public static string ToSVG(this Hp.Drawing input) { double scale = input.GetScale(); string drawing = "<svg width=\"" + input.Width + "\" height=\"" + input.Height + "\" shape-rendering=\"geometricPrecision\" xmlns=\"http://www.w3.org/2000/svg\" >" + Environment.NewLine; double shiftW = (input.Width / scale / 2 - input.Frame.Center.X); double shiftH = -(input.Height / scale / 2 + input.Frame.Center.Y); drawing += "<g class=\"Canvas\" id=\"Canvas\" transform=\"scale(" + scale + "," + (-1) * scale + ") translate(" + shiftW + "," + shiftH + ") \">" + Environment.NewLine; Dictionary <string, Wg.Effects> effects = new Dictionary <string, Wg.Effects>(); Dictionary <string, Wg.Graphic> graphics = new Dictionary <string, Wg.Graphic>(); foreach (Shape shape in input.Shapes) { drawing += shape.ToPath(); if (!graphics.ContainsKey(shape.Graphic.ID)) { graphics.Add(shape.Graphic.ID, shape.Graphic); } if (shape.Graphic.Effects.HasEffects) { if (!effects.ContainsKey(shape.Graphic.Effects.ID)) { effects.Add(shape.Graphic.Effects.ID, shape.Graphic.Effects); } } } drawing += "</g >" + Environment.NewLine; drawing += "<defs>" + Environment.NewLine; drawing += "<clipPath id=\"Frame\"> <rect x=\"0\" y=\"0\" width=\"" + input.Width + "\" height=\"" + input.Height + "\" /> </clipPath>" + Environment.NewLine; foreach (Wg.Effects effect in effects.Values) { drawing += effect.ToSVG(); } foreach (Wg.Graphic graphic in graphics.Values) { drawing += graphic.ToSVG(); } drawing += "</defs>" + Environment.NewLine; drawing += "</svg >"; return(drawing); }
/// <summary> /// This is the method that actually does the work. /// </summary> /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param> protected override void SolveInstance(IGH_DataAccess DA) { Hp.Drawing drawing = new Hp.Drawing(); if (!DA.GetData <Hp.Drawing>(0, ref drawing)) { return; } int dpi = 96; string path = "C:\\Users\\Public\\Documents\\"; string name = DateTime.UtcNow.ToString("yyyy-dd-M_HH-mm-ss");; int extension = 0; string format = ".png"; bool save = false; if (!DA.GetData(1, ref dpi)) { return; } bool hasPath = DA.GetData(2, ref path); bool hasName = DA.GetData(3, ref name); if (!DA.GetData(4, ref extension)) { return; } if (!DA.GetData(5, ref save)) { return; } Si.BitmapEncoder encoding = new Si.PngBitmapEncoder(); switch (extension) { case 1: encoding = new Si.JpegBitmapEncoder(); format = ".jpeg"; break; case 2: encoding = new Si.BmpBitmapEncoder(); format = ".bmp"; break; case 3: encoding = new Si.TiffBitmapEncoder(); format = ".tiff"; break; case 4: encoding = new Si.GifBitmapEncoder(); format = ".gif"; break; } if (!hasPath) { if (this.OnPingDocument().FilePath != null) { path = Path.GetDirectoryName(this.OnPingDocument().FilePath) + "\\"; } } else { path += "//"; } string filepath = path + name + format; Sm.DrawingVisual dwg = drawing.ToGeometryVisual(); Bitmap bitmap = dwg.ToBitmap(drawing.Width, drawing.Height, dpi, encoding); if (save) { bitmap.Save(filepath); bitmap.Dispose(); } DA.SetData(0, filepath); }