Universal DWG to PDF conversion

Hi,

We are building a simple library that converts CAD formats to PDF.
While playing with DWG format we realized that converting 3D objects to PDF requires special code (sample file here) that does not work with the one to convert DWG containing 2D objects (sample file here).

We are using this code with Aspose.CAD 18.3.0 and Aspose.PDF 18.1.0:

        using (var cadImage = (CadImage)Image.Load(input))
        {
            var rasterizationOptions = new CadRasterizationOptions
            {
                //TypeOfEntities = ?
            };

            DefineUnitSystem(cadImage.UnitType, out var currentUnitIsMetric, out var currentUnitCoefficient);

            if (currentUnitIsMetric)
            {
                const double metersCoeff = 1 / 1000.0;

                var 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;
            }

            rasterizationOptions.AutomaticLayoutsScaling = true;

            cadImage.Save(output, new PdfOptions
            {
                VectorRasterizationOptions = rasterizationOptions
            });
        }

The code comes from here.

And we would like to know if there is a way to calculate TypeOfEntities based on the information in cadImage?
Or any other general code that works for converting any DWG to PDF - that could contain 3D objects?

Thank you!

@gwert,

I have observed the requirement shared by you and have not been able to completely understand that. Do you mean that if a DWG has only 2D object and other has 3D objects and same code shall be used to convert DWG to PDF. If this is your requirement? Can you please elaborate your requirements.

Hi,

I think you got it. :slight_smile:

What I learned when playing with the code you have around converting to PDF is that:

  1. You need to specify TypeOfEntities = TypeOfEntities.Entities3D in the CadRasterizationOptions in order to convert all 3D objects to PDF. If you do not specify that, the 3D objects will not be exported to PDF and you get an empty PDF.
  2. In order to export 2D objects to PDF you can go 2 paths: either to set TypeOfEntities = TypeOfEntities.Entities2D; but if you do so, it will not export the 3D objects… (here is the catch)

So what we need:

  1. Either a piece of code that works for all DWGs and exports all objects inside regardless if they are 2D or 3D OR
  2. At least be able to check somehow if the DWG contains 3D objects so that the TypeOfEntities property is properly set when exporting to PDF.

I hope this is more clear.

Best regards!

@gwert,

Unfortunately, there is no automatic routine available to identify if the DWG has TypeOfEntities in 2D or 3D and automatically sets the rasterizationOptions.TypeOfEntities either to 2D or 3D. However, I have written a below routine for your convenience, that traverse all the entities in CAD image to identify 2D or 3D entities. If 3D entity is found, it set a local Boolean variable if3d to true. Based on set value of if3d, the appropriate value is set in rasterizationOptions.TypeOfEntities.

        bool if3d=false;
    foreach (CadBaseEntity obj in image.Entities)
    {
    //     IterateCADNodes2(entity);
        if (obj.TypeName == CadEntityTypeName.FACE3D||obj.TypeName== CadEntityTypeName.SOLID3D)
        {
            if3d=true;
        }
        else
        {
            if3d=false;
        }
    }


    if(if3d)
        rasterizationOptions.TypeOfEntities=TypeOfEntities.Entities3D;
    else
        rasterizationOptions.TypeOfEntities=TypeOfEntities.Entities2D;

Hi,

Thank you for the piece of code. Works perfectly!

Best regards!