Insert image into block

1
Mamachan
7/15/2021 1:47 PM

I have a .DWG file with block . try to insert image into it. but it fail

Version : 4.0.38.187


C# Code:
string templateFilePath = "I:\\AUTOCAD_10589436.dwg";
try
{

if (System.IO.File.Exists(templateFilePath))
{


DxfModel modelTemp = DwgReader.Read(templateFilePath);
DxfBlock result = new DxfBlock("TITLEBLOCK");

DxfModel DestinationModel = DwgReader.Read(templateFilePath);

//
Point3D minToFitIn = new Point3D(30, 60, 0);
Point3D maxToFitIn = new Point3D(558, 390, 0);
BoundsCalculator boundsCalculator = new BoundsCalculator();
boundsCalculator.GetBounds(DestinationModel);
Bounds3D bounds = boundsCalculator.Bounds;
double scale = 1;
Matrix4D to2DTransform = DxfUtil.GetScaleTransform(bounds.Min, bounds.Max, minToFitIn, maxToFitIn, out scale);

BoundsCalculator boundsCalculatorTemplate = new BoundsCalculator();
boundsCalculatorTemplate.GetBounds(modelTemp);
Bounds3D boundsTemplate = boundsCalculatorTemplate.Bounds;
Matrix4D to2DTransform1 = DxfUtil.GetScaleTransform(boundsTemplate.Min, boundsTemplate.Max, minToFitIn, maxToFitIn, out scale);

CloneContext cT = new CloneContext(modelTemp, DestinationModel, ReferenceResolutionType.CloneMissing);
cT.RenameClashingBlocks = true;

#region working mapping hardcoded
int iBlkCheck = -1;

List<string> sBlocks1 = new List<string>();

sBlocks1.Add("dxfImgBlock");

if (sBlocks1 != null && sBlocks1.Count > 0)
{
if (!DestinationModel.Blocks.Contains(sBlocks1[0]))// if the title block is not inserted already inserted it will map the properties and will insert the title block.
{

DxfImageDef imageDef = new DxfImageDef(modelTemp);
imageDef.Filename = "I:\\Account.png";
imageDef.Size = new Size2D(118d, 179d);
imageDef.DefaultPixelSize = new Size2D(1d, 1d);
imageDef.ImageIsLoaded = true;
modelTemp.Images.Add(imageDef);

Point3D insertionPoint1 = new Point3D(16d, 3d, 0d);
{ // default size
DxfImage image = new DxfImage();
image.ImageDef = imageDef;
image.InsertionPoint = insertionPoint1;
image.ImageDisplayFlags = ImageDisplayFlags.ShowImage;
image.SetDefaultBoundaryVertices();
//image.XAxis=
//image.YAxis =
modelTemp.Entities.Add(image);
DxfBlock dxfImgBlock = new DxfBlock("QR_ImgBlock");
modelTemp.Blocks.Add(dxfImgBlock);
dxfImgBlock.Entities.Add(image);
}


foreach (DxfEntity sourceEntity in modelTemp.Entities)
{
DxfEntity clonedEntity = (DxfEntity)sourceEntity.Clone(cT);
result.Entities.Add(clonedEntity);
}


cT.ResolveReferences();

DxfInsert odx = new DxfInsert(result);
double xCord = bounds.Corner1.X;
double yCord = bounds.Corner1.Y;
odx.InsertionPoint = new Point3D(0, 0, 0);

DestinationModel.Blocks.Add(result);
DestinationModel.Entities.Add(odx);
foreach (DxfLayout layout in DestinationModel.Layouts)
{
layout.Entities.Add(odx);
}


modelTemp.Dispose();
}

//389
}

#region commented

//}

#endregion

#endregion

Size maxSize = new Size(1000, 1000);

Bitmap bitmap = ImageExporter.CreateAutoSizedBitmap(DestinationModel, Matrix4D.Identity,
GraphicsConfig.BlackBackgroundCorrectForBackColor,
SmoothingMode.HighQuality,
maxSize);

DwgWriter.Write("I:\\Sheet40.dwg", DestinationModel);
}

}
catch (Exception)
{

throw;
}

Regards
Mamachan

Wout
7/15/2021 3:07 PM

Hi,

You'll have to purchase a support ticket for me to look into this.

- Wout

Mamachan
7/16/2021 6:27 AM

Hi Wout

Yes done..Can you check

Mamachan

Mamachan
7/21/2021 9:16 AM

Hi Wout

Yes done ...Can you check

Mamachan

Wout
7/21/2021 11:21 AM

Will do, as soon as the payment is in (it's not in yet).

- Wout

Mamachan
9/1/2021 11:50 AM

Yes payment done..

Can u check my queries

Mamachan

Wout
9/7/2021 9:15 AM

Hi,

Can you please supply the DWG and PNG files that the program references?

- Wout

Mamachan
9/8/2021 7:33 AM

please find attachments


1) Original drawing file is "DRWNG-7400-ENGG-SP-7S-00005.dwg"

2) Template file "Template.dwg"

3) Image file is "qr.jpg"


Process

a. Insert template into original drawing

b.There is a block name called "Details " and it has an attribute name "Sign"

c.What we are try to achieve is insert image(qr) in to an attribute

Wout
9/10/2021 1:18 PM

Hi,

The program created 1 insert and added that to multiple layouts, this is invalid. Below is the corrected program (tested OK).

C# Code:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;

using WW.Cad.Base;
using WW.Cad.Drawing;
using WW.Cad.Drawing.Wireframe;
using WW.Cad.IO;
using WW.Cad.Model;
using WW.Cad.Model.Entities;
using WW.Cad.Model.Objects;
using WW.Cad.Model.Tables;
using WW.Math;

namespace ConsoleApplication {
    public static class CustomerProblem {
        public static void CustomerMain(string[] args) {
            string templateFilePath = @"C:\support\1\Template.dwg";
            try {

                if (System.IO.File.Exists(templateFilePath)) {


                    DxfModel modelTemp = DwgReader.Read(templateFilePath);
                    DxfBlock result = new DxfBlock("TITLEBLOCK");

                    DxfModel DestinationModel = DwgReader.Read(templateFilePath);

                    //
                    Point3D minToFitIn = new Point3D(30, 60, 0);
                    Point3D maxToFitIn = new Point3D(558, 390, 0);
                    BoundsCalculator boundsCalculator = new BoundsCalculator();
                    boundsCalculator.GetBounds(DestinationModel);
                    Bounds3D bounds = boundsCalculator.Bounds;
                    double scale = 1;
                    Matrix4D to2DTransform = DxfUtil.GetScaleTransform(bounds.Min, bounds.Max, minToFitIn, maxToFitIn, out scale);

                    BoundsCalculator boundsCalculatorTemplate = new BoundsCalculator();
                    boundsCalculatorTemplate.GetBounds(modelTemp);
                    Bounds3D boundsTemplate = boundsCalculatorTemplate.Bounds;
                    Matrix4D to2DTransform1 = DxfUtil.GetScaleTransform(boundsTemplate.Min, boundsTemplate.Max, minToFitIn, maxToFitIn, out scale);

                    CloneContext cT = new CloneContext(modelTemp, DestinationModel, ReferenceResolutionType.CloneMissing);
                    cT.RenameClashingBlocks = true;

                    #region working mapping hardcoded
                    int iBlkCheck = -1;

                    List<string> sBlocks1 = new List<string>();

                    sBlocks1.Add("dxfImgBlock");

                    if (sBlocks1 != null && sBlocks1.Count > 0) {
                        if (!DestinationModel.Blocks.Contains(sBlocks1[0]))// if the title block is not inserted already inserted it will map the properties and will insert the title block.
                        {

                            DxfImageDef imageDef = new DxfImageDef(modelTemp);
                            imageDef.Filename = @"C:\support\1\qr.jpg";
                            imageDef.Size = new Size2D(118d, 179d);
                            imageDef.DefaultPixelSize = new Size2D(1d, 1d);
                            imageDef.ImageIsLoaded = true;
                            modelTemp.Images.Add(imageDef);

                            Point3D insertionPoint1 = new Point3D(16d, 3d, 0d);
                            { // default size
                                DxfImage image = new DxfImage();
                                image.ImageDef = imageDef;
                                image.InsertionPoint = insertionPoint1;
                                image.ImageDisplayFlags = ImageDisplayFlags.ShowImage;
                                image.SetDefaultBoundaryVertices();
                                //image.XAxis=
                                //image.YAxis =
                                modelTemp.Entities.Add(image);
                                DxfBlock dxfImgBlock = new DxfBlock("QR_ImgBlock");
                                modelTemp.Blocks.Add(dxfImgBlock);
                                dxfImgBlock.Entities.Add(image);
                            }


                            foreach (DxfEntity sourceEntity in modelTemp.Entities) {
                                DxfEntity clonedEntity = (DxfEntity)sourceEntity.Clone(cT);
                                result.Entities.Add(clonedEntity);
                            }


                            cT.ResolveReferences();

                            // Incorrect.
                            //DxfInsert odx = new DxfInsert(result);
                            //double xCord = bounds.Corner1.X;
                            //double yCord = bounds.Corner1.Y;
                            //odx.InsertionPoint = new Point3D(0, 0, 0);

                            DestinationModel.Blocks.Add(result);
                            //DestinationModel.Entities.Add(odx);
                            foreach (DxfLayout layout in DestinationModel.Layouts) {
                                // Entities cannot be part of more than 1 block/layout.
                                // So create a new one for every insertion.
                                DxfInsert odx = new DxfInsert(result);
                                double xCord = bounds.Corner1.X;
                                double yCord = bounds.Corner1.Y;
                                odx.InsertionPoint = new Point3D(0, 0, 0);

                                layout.Entities.Add(odx);
                            }

                            modelTemp.Dispose();
                        }

                        //389
                    }

                    #region commented

                    //}

                    #endregion

                    #endregion

                    Size maxSize = new Size(1000, 1000);

                    Bitmap bitmap = ImageExporter.CreateAutoSizedBitmap(DestinationModel, Matrix4D.Identity,
                    GraphicsConfig.BlackBackgroundCorrectForBackColor,
                    SmoothingMode.HighQuality,
                    maxSize);

                    DwgWriter.Write(@"C:\support\1\Sheet40.dwg", DestinationModel);
                }

            } catch (Exception) {

                throw;
            }
            Console.ReadLine();
        }
    }
}

- Wout

1