cart: empty [ Login ]

Cannot convert DWG with xref

1 
Dror
1/5/2015 11:50 AM
I'm probably doing something wrong. I'm trying to create pdf from each layout but I have this problem with this specific DWG. In the attached zip is the main file - papa.dwg and the xref dwg (the one in hebrew fonts) The results is black pdf. Note: When I'm showing this file in WinFormsViewExampleCS and adding LoadExternalReferences to the code I can see it OK. My code loads dwg and then load external dependencies: model = DwgReader.Read(filename); model.LoadExternalReferences(@"c:\dwg"); Now I want to convert each layout to pdf: foreach (DxfLayout l in model.Layouts) { BoundsCalculator boundsCalculator = new BoundsCalculator(); boundsCalculator.GetBounds(model,l); Bounds3D bounds = boundsCalculator.Bounds; PaperSize paperSize = PaperSizes.GetPaperSize(PaperKind.Letter); // Lengths in inches. float pageWidth = (float)paperSize.Width / 100f; float pageHeight = (float)paperSize.Height / 100f; float margin = 0.5f; double scaling = 1; 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 (stream = File.Create(outfile +l.Name+ ".pdf")) { PdfExporter pdfGraphics = new PdfExporter(stream); pdfGraphics.DrawPage(model, GraphicsConfig.BlackBackgroundCorrectForBackColor, to2DTransform, scaling, l, null, paperSize); pdfGraphics.EndDocument(); } ...... Thankss Dror
rammi
1/6/2015 2:05 PM
In the zip the name of the referenced file is mangled and appears as üÅ ÿÇÖàÅ.dwg. Could you please tell how the name is expected to look like originally? The papa DWG file itself is loading 3 external references:
  1. בן ראשון
  2. בן שני
  3. בן שלישי
- Rammi
Dror
1/6/2015 2:10 PM
Hi Rammi, it is number 1: בן ראשון.dwg BTW - it is looks fine in the viewer samples. Dror
rammi
1/6/2015 2:27 PM
Thanks. After renaming the file to its correct hebrew name it was loaded automatically by CadLib when placed in the same directory as its papa DWG. I had no problems creating the attached PDF which contains the rectangle box with the 1 contained in the referenced file. I don't see how PDF conversion should fail after the XREF is loaded correctly. So as a next step please make sure that the file is loaded during the LoadExternalReferences() call. There are overloads which accept an out IList<DxfBlock> parameter in which XREF blocks which couldn't be resolved are returned. - Rammi
Dror
1/7/2015 7:33 AM
Hi Rammi, Wired - I'm getting empty pdf. The only difference I have from your original DxfExportExample is that I'm exporting layer by layer: model = DwgReader.Read(filename) model.LoadExternalReferences(@"c:\dwg", out missing); foreach (DxfLayout l in model.Layouts) { BoundsCalculator boundsCalculator = new BoundsCalculator(); boundsCalculator.GetBounds(model,l); Bounds3D bounds = boundsCalculator.Bounds; PaperSize paperSize = PaperSizes.GetPaperSize(PaperKind.A4); // Lengths in inches. float pageWidth = (float)paperSize.Width / 100f; float pageHeight = (float)paperSize.Height / 100f; float margin = 0.5f; // 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 pdf page. // The transform transforms to pdf pixels. double scaling = 1; 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 (stream = File.Create(outfile +l.Name+ ".pdf")) { PdfExporter pdfGraphics = new PdfExporter(stream); pdfGraphics.DrawPage(model, GraphicsConfig.AcadLikeWithWhiteBackground, to2DTransform, scaling, l, null, paperSize); pdfGraphics.EndDocument(); } Thanks Dor
rammi
1/7/2015 11:41 AM
Often the viewport defined in the Model layout is not perfect, so in my experience it is preferable to not output this layout as layout, but directly. As this may be interesting not only for you here's a collection of static methods for PDF output which I'm using here during my tests:
C# Code:
/// <summary> /// Write all non-empty layouts of a model into a multi-page PDF file. /// </summary> /// <remarks> /// AutoCAD is creating some layouts by default, even if they don't contain any new data. /// CadLib only uses the layouts really contained in the file. /// </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> public static void WriteLayoutsToPdf(DxfModel model, PaperSize paperSize, float margin, string outfile, bool embedFonts, short lineWeight) { using (Stream stream = File.Create(outfile)) { PdfExporter pdfGraphics = new PdfExporter(stream); foreach (var layout in model.Layouts) { BoundsCalculator boundsCalculator = new BoundsCalculator(); if (layout.PaperSpace) { boundsCalculator.GetBounds(model, layout); } else { // model space viewports are often broken, so output model directly boundsCalculator.GetBounds(model); } Bounds3D bounds = boundsCalculator.Bounds; if (!bounds.Initialized || bounds.Delta == Vector3D.Zero) { // if bounds are empty nothing is displayed, so skip this page continue; } // 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); pdfGraphics.ExportLayers = true; pdfGraphics.UseMultipleLayers = false; pdfGraphics.EmbedFonts = embedFonts; GraphicsConfig config = (GraphicsConfig)GraphicsConfig.AcadLikeWithWhiteBackground.Clone(); config.TryDrawingTextAsText = embedFonts; config.DefaultLineWeight = lineWeight; if (layout.PaperSpace) { pdfGraphics.DrawPage( model, config, to2DTransform, scaling, layout, null, paperSize); } else { // special handle model space, see comment above pdfGraphics.DrawPage( model, config, to2DTransform, paperSize); } } pdfGraphics.EndDocument(); } } /// <summary> /// Write the default layout of a model to a PDF file. /// </summary> /// <remarks> /// Depending on the model's setting this either writes the active layout or the model space to the PDF. /// Model/layout are scaled to fit the paper inside the margin. /// This code will center the model/layout 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> public static void WriteDefaultLayoutToPdf(DxfModel model, PaperSize paperSize, float margin, string outfile, bool embedFonts, short lineWeight) { DxfLayout layout = model.Header.ShowModelSpace ? null : model.ActiveLayout; if (layout != null) { // output layout WriteLayoutToPdf(model, layout, paperSize, margin, outfile, embedFonts, lineWeight); } else { // output model WriteModelToPdf(model, paperSize, margin, outfile, embedFonts, lineWeight); } } /// <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, and use lineweight 0 /// </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> public static void WriteModelToPdf(DxfModel model, PaperSize paperSize, float margin, string outfile, bool embedFonts) { WriteModelToPdf(model, paperSize, margin, outfile, embedFonts, 0); } /// <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> public static void WriteModelToPdf(DxfModel model, PaperSize paperSize, float margin, string outfile, bool embedFonts, short lineWeight) { BoundsCalculator boundsCalculator = new 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.Min, // bottom left corner of model bounds.Max, // top right corner of model new Point3D(bounds.Center.X, bounds.Max.Y, 0d), // top center of model new Point3D(new Vector3D(margin, margin, 0d) * PdfExporter.InchToPixel), // bottom left corner of page new Point3D(new Vector3D(pageWidth - margin, pageHeight - margin, 0d) * PdfExporter.InchToPixel), // top right corner of page new Point3D(new Vector3D(pageWidth / 2d, pageHeight - margin, 0d) * PdfExporter.InchToPixel), // top center of page out scaling ); using (Stream stream = File.Create(outfile)) { PdfExporter pdfGraphics = new PdfExporter(stream); pdfGraphics.ExportLayers = true; pdfGraphics.UseMultipleLayers = false; pdfGraphics.EmbedFonts = embedFonts; GraphicsConfig config = (GraphicsConfig)GraphicsConfig.AcadLikeWithWhiteBackground.Clone(); config.TryDrawingTextAsText = embedFonts; config.DefaultLineWeight = lineWeight; pdfGraphics.DrawPage( model, config, to2DTransform, scaling, null, null, paperSize ); pdfGraphics.EndDocument(); } } /// <summary> /// Write a layout to a PDF file. /// </summary> /// <remarks> /// The layout is scaled to fit the paper inside the margin. /// This code will center the layout on top of the page. /// </remarks> /// <param name="model">DXF model to write</param> /// <param name="layout">DXF layout 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> public static void WriteLayoutToPdf(DxfModel model, DxfLayout layout, PaperSize paperSize, float margin, string outfile, bool embedFonts, short lineWeight) { BoundsCalculator boundsCalculator = new BoundsCalculator(); boundsCalculator.GetBounds(model, layout); 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 left 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.Min, // bottom left corner of layout bounds.Max, // top right corner of layout new Point3D(bounds.Center.X, bounds.Max.Y, 0d), // top center of layout new Point3D(new Vector3D(margin, margin, 0d) * PdfExporter.InchToPixel), // bottom left corner of page new Point3D(new Vector3D(pageWidth - margin, pageHeight - margin, 0d) * PdfExporter.InchToPixel), // top right corner of page new Point3D(new Vector3D(pageWidth / 2d, pageHeight - margin, 0d) * PdfExporter.InchToPixel), // top center of page out scaling ); using (Stream stream = File.Create(outfile)) { PdfExporter pdfGraphics = new PdfExporter(stream); pdfGraphics.EmbedFonts = embedFonts; GraphicsConfig config = new GraphicsConfig(Color.White, true); config.TryDrawingTextAsText = embedFonts; config.DefaultLineWeight = lineWeight; pdfGraphics.DrawPage( model, config, to2DTransform, scaling, layout, null, paperSize ); pdfGraphics.EndDocument(); } }
The special handling of the model layout in two places in method WriteLayoutsToPdf() should be of interest to you. The PDF from my last post was created with the following code (including access to missing XREFs when using a debugger):
C# Code:
string papa = @"Y:\dxf\wout\incoming\2015\20150106_xref\dwg\papa.dwg"; DxfModel model = DwgReader.Read(papa); IList<DxfBlock> missingReferences; model.LoadExternalReferences(out missingReferences); PaperSize paperSize = PaperSizes.GetPaperSize(PaperKind.A4Rotated); WriteDefaultLayoutToPdf(model, paperSize, 0.5f, papa + ".pdf", true, 0);
Hope this helps. - Rammi
rammi
2/11/2015 1:19 PM
I just updated my latest posting and removed a line
C# Code:
pdfGraphics.PackContent = false;
from the WriteLayoutToPdf() method which was just there for debugging purposes. The consequence of this line was that unpacked PDF files are written which are a lot larger than necessary. Sorry for the inconvenience! - Rammi
Top Ramdor
2/19/2015 10:04 AM
Hi Rammi, We just purchased the license so I'm using the name we used to register the site. We have decided to convert to Images and not PDFs. Now we have the same problem in Image conversion. When converting the model the image looks good. However when converting the paperspace layout we are getting a blank white image. This is the code (assume the same dwg and xrefs from the above thread) foreach (DxfLayout layout in model.Layouts) { if (!layout.PaperSpace) { bitmap = ImageExporter.CreateAutoSizedBitmap( model, Matrix4D.Identity, GraphicsConfig.AcadLikeWithBlackBackground, SmoothingMode.HighQuality, maxSize ); } else { bitmap = ImageExporter.CreateAutoSizedBitmap( model, layout, layout.Viewports, Matrix4D.Identity, GraphicsConfig.AcadLikeWithWhiteBackground, SmoothingMode.HighQuality, maxSize ); } using (Stream stream = File.Create(Helper.PathCombine(layouyPath, fileId + "_" + i + ".jpg"))) { ImageExporter.EncodeImageToJpeg(bitmap, stream); } i++; } Thanks Dror
rammi
3/13/2015 11:47 AM
Check Plotting correct layout extent revisioned which might be helpful. - Rammi
1