} // End Sub WriteDefaultLayoutToPdf /// <summary> /// Write a complete model to a PDF file. /// </summary> /// <remarks> /// The model is scaled to fit the paper inside the margin. /// This code will center the model on top of the page. /// </remarks> /// <param name="model">DXF model to write</param> /// <param name="paperSize">targeted paper size</param> /// <param name="margin">margin around model print in inches</param> /// <param name="outfile">path of PDF output file</param> /// <param name="embedFonts">embed fonts into PDF?</param> /// <param name="lineWeight">default line weight in 100th of mm</param> private static void WriteModelToPdf(DxfModel model, System.Drawing.Printing.PaperSize paperSize, float margin, string outfile, bool embedFonts, short lineWeight) { WW.Cad.Drawing.BoundsCalculator boundsCalculator = new WW.Cad.Drawing.BoundsCalculator(); boundsCalculator.GetBounds(model); Bounds3D bounds = boundsCalculator.Bounds; // Lengths in inches. float pageWidth = paperSize.Width / 100f; float pageHeight = paperSize.Height / 100f; // Scale and transform such that its fits max width/height // and the top middle of the cad drawing will match the // top middle of the pdf page. // The transform transforms to pdf pixels. double scaling; Matrix4D to2DTransform = DxfUtil.GetScaleTransform( bounds.Corner1, bounds.Corner2, new Point3D(bounds.Center.X, bounds.Corner2.Y, 0d), new Point3D(new Vector3D(margin, margin, 0d) * PdfExporter.InchToPixel), new Point3D(new Vector3D(pageWidth - margin, pageHeight - margin, 0d) * PdfExporter.InchToPixel), new Point3D(new Vector3D(pageWidth / 2d, pageHeight - margin, 0d) * PdfExporter.InchToPixel), out scaling ); using (System.IO.Stream stream = System.IO.File.Create(outfile)) { PdfExporter pdfGraphics = new PdfExporter(stream); pdfGraphics.ExportLayers = true; pdfGraphics.UseMultipleLayers = false; pdfGraphics.EmbedFonts = embedFonts; WW.Cad.Drawing.GraphicsConfig config = (WW.Cad.Drawing.GraphicsConfig)WW.Cad.Drawing.GraphicsConfig.AcadLikeWithWhiteBackground.Clone(); config.TryDrawingTextAsText = embedFonts; config.DefaultLineWeight = lineWeight; pdfGraphics.DrawPage( model, config, to2DTransform, scaling, null, null, paperSize ); pdfGraphics.EndDocument(); } // End Using stream } // End Sub WriteModelToPdf
public static void ExportToSvg(WW.Cad.Model.DxfModel model, string path) { WW.Cad.Drawing.BoundsCalculator boundsCalculator = new WW.Cad.Drawing.BoundsCalculator(); boundsCalculator.GetBounds(model); WW.Math.Bounds3D bounds = boundsCalculator.Bounds; System.Drawing.Printing.PaperSize paperSize = WW.Cad.Drawing.PaperSizes.GetPaperSize(System.Drawing.Printing.PaperKind.A4); // Lengths in hundredths of cm. const float hundredthsInchToCm = 2.54f / 100f; float pageWidth = paperSize.Width * hundredthsInchToCm * 100f; float pageHeight = paperSize.Height * hundredthsInchToCm * 100f; // float margin = 200f; float margin = 0f; // Scale and transform such that its fits max width/height // and the top left middle of the cad drawing will match the // top middle of the svg page. WW.Math.Matrix4D to2DTransform = WW.Cad.Base.DxfUtil.GetScaleTransform( bounds.Corner1, bounds.Corner2, new WW.Math.Point3D(bounds.Center.X, bounds.Corner2.Y, 0d), new WW.Math.Point3D(margin, pageHeight - margin, 0d), new WW.Math.Point3D(pageWidth - margin, margin, 0d), new WW.Math.Point3D(pageWidth / 2d, margin, 0d) ); /* * using (System.IO.Stream stream = System.IO.File.Create(path)) * { * WW.Cad.IO.SvgExporter exporter = new WW.Cad.IO.SvgExporter(stream, paperSize); * * exporter.Title = "tempSvg"; * exporter.ExportCadLayersAsSvgGroups = true; * exporter.WriteSvgXmlElementAttributes += AdditionalAttribute; * // exporter.WriteSvgXmlElementAttributes += OverwriteStrokeWidth; * exporter.WriteBackgroundRectangle = true; * * * WW.Cad.Drawing.GraphicsConfig gc = new WW.Cad.Drawing.GraphicsConfig(); * gc.FixedForegroundColor = WW.Drawing.ArgbColor.FromArgb(0, WW.Drawing.ArgbColor.FromRgb(0, 0, 0)); // stroke * // gc.FixedForegroundColor = WW.Drawing.ArgbColors.HotPink; // Stroke * gc.CorrectColorForBackgroundColor = false; * * gc.NodeColor = WW.Drawing.ArgbColors.HotPink; * // gc.BackColor = WW.Drawing.ArgbColors.HotPink; * * gc.ShowDimensionDefinitionPoints = false; * gc.BackColor = WW.Drawing.ArgbColor.FromArgb(0, WW.Drawing.ArgbColors.White); * * // exporter.Draw(model, WW.Cad.Drawing.GraphicsConfig.WhiteBackgroundCorrectForBackColor, to2DTransform); * exporter.Draw(model, gc, to2DTransform); * } // End using stream */ System.Text.StringBuilder sb = new System.Text.StringBuilder(); using (System.IO.StringWriter stringWriter = new System.IO.StringWriter(sb)) { using (System.Xml.XmlTextWriter xmlWriter = new System.Xml.XmlTextWriter(stringWriter)) { WW.Cad.IO.SvgExporter exporter = new WW.Cad.IO.SvgExporter(xmlWriter, paperSize); exporter.Title = "tempSvg"; exporter.ExportCadLayersAsSvgGroups = true; exporter.WriteSvgXmlElementAttributes += AdditionalAttribute; // exporter.WriteSvgXmlElementAttributes += OverwriteStrokeWidth; exporter.WriteBackgroundRectangle = true; WW.Cad.Drawing.GraphicsConfig gc = new WW.Cad.Drawing.GraphicsConfig(); gc.FixedForegroundColor = WW.Drawing.ArgbColor.FromArgb(0, WW.Drawing.ArgbColor.FromRgb(0, 0, 0)); // stroke // gc.FixedForegroundColor = WW.Drawing.ArgbColors.HotPink; // Stroke gc.CorrectColorForBackgroundColor = false; gc.TryDrawingTextAsText = true; gc.NodeColor = WW.Drawing.ArgbColors.HotPink; // gc.BackColor = WW.Drawing.ArgbColors.HotPink; gc.ShowDimensionDefinitionPoints = false; gc.BackColor = WW.Drawing.ArgbColor.FromArgb(0, WW.Drawing.ArgbColors.White); // exporter.Draw(model, WW.Cad.Drawing.GraphicsConfig.WhiteBackgroundCorrectForBackColor, to2DTransform); exporter.Draw(model, gc, to2DTransform); // System.Console.WriteLine(str); // This is "not scalable for many different encodings"... string strSVG = sb.ToString(); // byte[] encoded = System.Text.Encoding.GetEncoding((int)model.Header.DrawingCodePage).GetBytes(strSVG); // string corrected = System.Text.Encoding.GetEncoding("iso-8859-1").GetString(encoded); // System.Console.WriteLine(corrected); System.IO.File.WriteAllText(@"d:\testfile_OG14.svg", strSVG, System.Text.Encoding.UTF8); System.Console.WriteLine("Finished"); } // End Using xmlWriter } // End Using stringWriter /* * using (System.IO.MemoryStream stream = new System.IO.MemoryStream()) * { * WW.Cad.IO.SvgExporter exporter = new WW.Cad.IO.SvgExporter(stream, paperSize); * * exporter.Title = "tempSvg"; * exporter.ExportCadLayersAsSvgGroups = true; * exporter.WriteSvgXmlElementAttributes += AdditionalAttribute; * // exporter.WriteSvgXmlElementAttributes += OverwriteStrokeWidth; * exporter.WriteBackgroundRectangle = true; * * * WW.Cad.Drawing.GraphicsConfig gc = new WW.Cad.Drawing.GraphicsConfig(); * gc.FixedForegroundColor = WW.Drawing.ArgbColor.FromArgb(0, WW.Drawing.ArgbColor.FromRgb(0, 0, 0)); // stroke * // gc.FixedForegroundColor = WW.Drawing.ArgbColors.HotPink; // Stroke * gc.CorrectColorForBackgroundColor = false; * gc.TryDrawingTextAsText = true; * * gc.NodeColor = WW.Drawing.ArgbColors.HotPink; * // gc.BackColor = WW.Drawing.ArgbColors.HotPink; * * gc.ShowDimensionDefinitionPoints = false; * gc.BackColor = WW.Drawing.ArgbColor.FromArgb(0, WW.Drawing.ArgbColors.White); * * // exporter.Draw(model, WW.Cad.Drawing.GraphicsConfig.WhiteBackgroundCorrectForBackColor, to2DTransform); * exporter.Draw(model, gc, to2DTransform); * * byte[] ba = stream.ToArray(); * string str = System.Text.Encoding.UTF8.GetString(ba); * // string str = System.Text.Encoding.UTF8.GetString(ba); * // string str = System.Text.Encoding.Unicode.GetString(ba); * // string str = System.Text.Encoding.Default.GetString(ba); * // string str = System.Text.Encoding.ASCII.GetString(ba); * // string str = System.Text.Encoding.UTF7.GetString(ba); * // string str = System.Text.Encoding.UTF32.GetString(ba); * // string str = System.Text.Encoding.GetEncoding("iso-8859-1").GetString(ba); * // string str = System.Text.Encoding.GetEncoding("iso-8859-2").GetString(ba); * // string str = System.Text.Encoding.GetEncoding(850).GetString(ba); * * * // System.Console.WriteLine(str); * // This is "not scalable for many different encodings"... * byte[] encoded = System.Text.Encoding.GetEncoding((int)model.Header.DrawingCodePage).GetBytes(str); * string corrected = System.Text.Encoding.GetEncoding("iso-8859-1").GetString(encoded); * * * // System.Console.WriteLine(corrected); * System.IO.File.WriteAllText(@"d:\testfile_OG14.svg", corrected, System.Text.Encoding.UTF8); * System.Console.WriteLine("Finished"); * * //ba = System.Text.Encoding.UTF8.GetBytes(str); * } // End using stream */ } // End Sub ExportToSvg