Cannot convert DWG with xref
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:
|
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: / C# Code: string papa = @"Y:\ |
rammi
2/11/2015 1:19 PM
|
---|
I just updated my latest posting and removed a line
C# Code: pdfGraphics. |
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
|