Hi,
I have these three files, where the conversion starts, but never finishes. Am I doing something wrong in my code? I noticed that the files have a UNIT set where as the previous file I converted didn’t. You can download the files from here:
My code:
Blockquoteusing System;
using System.IO;
using Aspose.CAD;
using Aspose.CAD.FileFormats.Cad;
using Aspose.CAD.FileFormats.Dwf;
using Aspose.CAD.FileFormats.Ifc.IFC2X3.Entities;
using Aspose.CAD.ImageOptions;
using static System.Net.Mime.MediaTypeNames;
namespace AsposeCad
{
internal class Program
{
static void Main(string[] args)
{
var inputFilePath = “input.dwfx”;
var outputFilePath = “input.pdf”;
Console.WriteLine($"Loading file: {inputFilePath}");
try
{
/*using (var cadImage = Image.Load(inputFilePath))
{
var rasterizationOptions = new CadRasterizationOptions()
{
PageWidth = 3000,
PageHeight = 3000
};
var pdfOptions = new PdfOptions
{
VectorRasterizationOptions = rasterizationOptions
cadRasterizationOptions.UnitType = Aspose.CAD.ImageOptions.UnitType.Centimenter;
};
Console.WriteLine($"Converting {inputFilePath} to {outputFilePath}");
cadImage.Save(outputFilePath, pdfOptions);
}*/
//using (Aspose.CAD image = Aspose.CAD.Image.Load(inputFilePath))
using (Aspose.CAD.Image image = Aspose.CAD.Image.Load(inputFilePath))
{
CadImage cadImage = (CadImage)image;// -> working for DWG
//DwfImage cadImage = (DwfImage)image; -> working for DWXF
//DxfImage cadImage = (DxfImage)image;
cadImage.UpdateSize(); //--> works for all other filetypes except DXF
/*foreach (var entity in cadImage.Entities)
{
entity.LineWeight = 0;
}*/
/*foreach (Aspose.CAD.FileFormats.Cad.CadTables.CadLayerTable layer in cadImage.Layers)
{
layer.LineWeight = 0;
}*/
// Create an instance of CadRasterizationOptions and set its various properties
Aspose.CAD.ImageOptions.CadRasterizationOptions rasterizationOptions = new Aspose.CAD.ImageOptions.CadRasterizationOptions();
//rasterizationOptions.PageWidth = 1600;
//rasterizationOptions.PageHeight = 1600;
//rasterizationOptions.AutomaticLayoutsScaling = false;
//rasterizationOptions.ScaleMethod = ScaleType.GrowToFit;
//rasterizationOptions.NoScaling = false;
rasterizationOptions.DrawType = Aspose.CAD.FileFormats.Cad.CadDrawTypeMode.UseObjectColor;
//rasterizationOptions.Layers = new string[] { "0" };
//rasterizationOptions.ContentAsBitmap = true;
rasterizationOptions.Quality = new RasterizationQuality();
var quality = new RasterizationQuality();
quality.Arc = RasterizationQualityValue.High;
quality.Text = RasterizationQualityValue.High;
quality.TextThicknessNormalization = true;
quality.Hatch = RasterizationQualityValue.High;
quality.ObjectsPrecision = RasterizationQualityValue.High; // -> works for all fileformats
// Export to specific size
bool currentUnitIsMetric = false;
double currentUnitCoefficient = 1.0;
DefineUnitSystem(image.UnitType, out currentUnitIsMetric, out currentUnitCoefficient);
Console.WriteLine("Dealing with units");
Console.WriteLine(currentUnitIsMetric + " unit " + currentUnitCoefficient);
if (currentUnitIsMetric)
{
double metersCoeff = 1 / 1000.0;
double scaleFactor = metersCoeff / currentUnitCoefficient;
//rasterizationOptions.PageWidth = (float)(210 * scaleFactor);
//rasterizationOptions.PageHeight = (float)(297 * scaleFactor);
rasterizationOptions.UnitType = UnitType.Millimeter;
}
else
{
//rasterizationOptions.PageWidth = (float)(8.27f / currentUnitCoefficient);
//rasterizationOptions.PageHeight = (float)(11.69f / currentUnitCoefficient);
rasterizationOptions.UnitType = UnitType.Inch;
} // works for all file types
// Create an instance of PdfOptions
Aspose.CAD.ImageOptions.PdfOptions pdfOptions = new Aspose.CAD.ImageOptions.PdfOptions();
// Set the VectorRasterizationOptions property
pdfOptions.VectorRasterizationOptions = rasterizationOptions;
//Export CAD to PDF
image.Save(inputFilePath + "_OUTPUT", pdfOptions);
// Create an instance of TiffOptions
Aspose.CAD.ImageOptions.TiffOptions tiffOptions = new Aspose.CAD.ImageOptions.TiffOptions(Aspose.CAD.FileFormats.Tiff.Enums.TiffExpectedFormat.Default);
// Set the VectorRasterizationOptions property
tiffOptions.VectorRasterizationOptions = rasterizationOptions;
//Export CAD to TIFF
//image.Save(inputFilePath + "result_out.tiff", tiffOptions);
}
//Console.WriteLine($"File {inputFilePath} converted to {outputFilePath}");
}
catch (Exception ex)
{
Console.WriteLine($"Error converting CAD to PDF: {ex}");
}
}
private static void DefineUnitSystem(UnitType unitType, out bool isMetric, out double coefficient)
{
isMetric = false;
coefficient = 1.0;
switch (unitType)
{
case UnitType.Parsec:
coefficient = 3.0857 * 10000000000000000.0;
isMetric = true;
break;
case UnitType.LightYear:
coefficient = 9.4607 * 1000000000000000.0;
isMetric = true;
break;
case UnitType.AstronomicalUnit:
coefficient = 1.4960 * 100000000000.0;
isMetric = true;
break;
case UnitType.Gigameter:
coefficient = 1000000000.0;
isMetric = true;
break;
case UnitType.Kilometer:
coefficient = 1000.0;
isMetric = true;
break;
case UnitType.Decameter:
isMetric = true;
coefficient = 10.0;
break;
case UnitType.Hectometer:
isMetric = true;
coefficient = 100.0;
break;
case UnitType.Meter:
isMetric = true;
coefficient = 1.0;
break;
case UnitType.Centimenter:
isMetric = true;
coefficient = 0.01;
break;
case UnitType.Decimeter:
isMetric = true;
coefficient = 0.1;
break;
case UnitType.Millimeter:
isMetric = true;
coefficient = 0.001;
break;
case UnitType.Micrometer:
isMetric = true;
coefficient = 0.000001;
break;
case UnitType.Nanometer:
isMetric = true;
coefficient = 0.000000001;
break;
case UnitType.Angstrom:
isMetric = true;
coefficient = 0.0000000001;
break;
case UnitType.Inch:
coefficient = 1.0;
break;
case UnitType.MicroInch:
coefficient = 0.000001;
break;
case UnitType.Mil:
coefficient = 0.001;
break;
case UnitType.Foot:
coefficient = 12.0;
break;
case UnitType.Yard:
coefficient = 36.0;
break;
case UnitType.Mile:
coefficient = 63360.0;
break;
}
}
}
}