Hello!
First, let me thank you for the great work in ACadSharp!
I'm trying to export a dxf file that I'm creating programatically, but I can't for the life of me position and scale things as I want. Here's the code that I have, it's probably very wrong, whatever I do my document is always glued to the margins of the page, with a bit cropped on top (maybe because it might be on negative coordinates?
private bool SavePDF(string filePath, CadDocument cadDoc)
{
if (cadDoc == null)
{
GD.PrintErr("SavePDF: CadDocument is null!");
return false;
}
try
{
Layout layout = new Layout("FullDocumentLayout");
layout.MinExtents = new XYZ(-500, -500, 0);
layout.MaxExtents = new XYZ(2970, 2100, 0); // Example: A4 size in mm
layout.MinLimits = new XY(-500, -500);
layout.MaxLimits = new XY(2970, 2100);
cadDoc.Layouts.Add(layout);
//cadDoc.BlockRecords.Add(layout.AssociatedBlock);
XY drawingMin = new XY(double.MaxValue, double.MaxValue);
XY drawingMax = new XY(double.MinValue, double.MinValue);
foreach (var entity in cadDoc.Entities)
{
var bBox = entity.GetBoundingBox();
if (bBox.Min.X < drawingMin.X) drawingMin.X = bBox.Min.X;
if (bBox.Min.Y < drawingMin.Y) drawingMin.Y = bBox.Min.Y;
if (bBox.Max.X > drawingMax.X) drawingMax.X = bBox.Max.X;
if (bBox.Max.Y > drawingMax.Y) drawingMax.Y = bBox.Max.Y;
}
var viewport = new ACadSharp.Entities.Viewport()
{
Center = new XYZ((drawingMin.X + drawingMax.X) / 2, (drawingMin.Y + drawingMax.Y) / 2, 0),
Width = drawingMax.X - drawingMin.X+500,
Height = drawingMax.Y - drawingMin.Y+500,
ViewCenter = new XY((drawingMin.X + drawingMax.X) / 2, (drawingMin.Y + drawingMax.Y) / 2)
};
//layout.Viewports = new ViewportCollection();
layout.Viewports.Append(viewport);
layout.AssociatedBlock.Entities.Add(viewport);
//cadDoc.PaperSpace.Entities.Add(viewport);w
PdfExporter exporter = new PdfExporter(filePath);
exporter.AddModelSpace(cadDoc);
exporter.AddPaperLayouts(cadDoc);
exporter.Close();
GD.Print($"Successfully saved PDF to: {filePath}");
return true;
}
catch (System.Exception e)
{
GD.PrintErr($"Error saving PDF file: {e.Message}");
return false;
}
}
Basically I'd like to be able to position my entities on the center of a A4 or A3 page, with some margin from the borders of the page.
Many thanks!
Hello!
First, let me thank you for the great work in ACadSharp!
I'm trying to export a dxf file that I'm creating programatically, but I can't for the life of me position and scale things as I want. Here's the code that I have, it's probably very wrong, whatever I do my document is always glued to the margins of the page, with a bit cropped on top (maybe because it might be on negative coordinates?
Basically I'd like to be able to position my entities on the center of a A4 or A3 page, with some margin from the borders of the page.
Many thanks!