Export block to image

  Hi, I need to get a preview for each block. I managed to get image for every block. But the block is not in the center of the image. How to place each block in the center of image? And Hoe to fit block to image?

private void BlocksToImages()
{
string FILE_PATH = “ATT file 2.dwg”;
using (CadImage cadImage = (CadImage)Aspose.CAD.Image.Load(FILE_PATH))
{
List filteredEntities = new List();
foreach (var entity in cadImage.Entities)
{
if (entity is CadInsertObject)
{
CadInsertObject cadinsentity = (CadInsertObject)entity;
filteredEntities.Add(entity);
}
}
foreach (var entity in filteredEntities)
{
#region Generate Image
List oneEntiy = new List();
entity.ChildObjects.RemoveAll(x => x is CadAttrib);

            CadInsertObject cadobj = (CadInsertObject)entity;
            oneEntiy.Add(entity);
            cadImage.Entities = oneEntiy.ToArray();

            BmpOptions bmpOptions = new BmpOptions();
            var dwfRasterizationOptions = new CadRasterizationOptions();
            bmpOptions.VectorRasterizationOptions = dwfRasterizationOptions;

            dwfRasterizationOptions.AutomaticLayoutsScaling = true;

            dwfRasterizationOptions.PageHeight = 400;
            dwfRasterizationOptions.PageWidth = 400;

            dwfRasterizationOptions.ScaleMethod = ScaleType.GrowToFit;

            Margins margins = new Margins();
            margins.Top = 5;
            margins.Left = 5;
            margins.Bottom = 5;
            margins.Right = 5;
            dwfRasterizationOptions.Margins = margins;
            string outImage = ((CadInsertObject)entity).Name.Replace("*", "") + ".bmp";

            cadImage.Save(outImage, bmpOptions);
            #endregion
        }
    }
}

ATT file 2.7z (38.4 KB)

@Aleks85,
Hi. Please, try adding this:


cadImage.Entities = oneEntiy.ToArray();
cadImage.UpdateSize();
BmpOptions bmpOptions = new BmpOptions();

You make the operations that may change size of the drawing, UpdateSize will recalculate it.

It worked for me, thanks a lot.

1 Like

I have an issue getting the preview of blocks from attached dwg file. I am getting empty image. Is there a way to fix it? I use the same сщву as in the topic with **cadImage.UpdateSize().

ENV-Links-Survey.zip (420.7 KB)

@Aleks85,
Hi,
we have created CADNET-9713 to investigate this issue.

@Aleks85,
it looks like the problem appears because blocks are tiny. Probably, scaling of the block up to image size could be used for such cases, e.g.:

...
cadImage.Entities = oneEntiy.ToArray();

// get size of the insert
cadImage.GetBounds(cadobj);

// calculate its width and height
List<Cad3DPoint> bounds = cadobj.Bounds;
double width = bounds[1].X - bounds[0].X;
double height = bounds[1].Y - bounds[0].Y;

// for some tiny cases scale insert up to the size of the drawing
if (width < 2 || height < 2)
{
    int scale = (int)(Math.Min(cadImage.Width, cadImage.Height) / Math.Max(width, height));
    cadobj.ScaleX *= scale;
    cadobj.ScaleY *= scale;
    cadobj.ScaleZ *= scale;
}
else
{
    cadImage.UpdateSize();
}
...

It’s draw block, but is not centered and does not fit block to image

@Aleks85,
OK, let us look for other possible solution.