How Can I Insert a block with CADAttdef?

Discuss and ask questions about CAD VCL (Delphi and C++ Builder).

Moderators: admin, support, SDS

Post Reply
alex7210
Posts: 5
Joined: 10 Aug 2016, 04:43

How Can I Insert a block with CADAttdef?

Post by alex7210 » 28 Sep 2016, 09:57

HI,

I made a block that has a CADAttdef for text.

and tried to insert it into a drawing through CADInsert entity in many ways but failed.

Could you let me know how to do it?

My code is as follows.

static public void AddBlock(CADImage cadImage, string cadLayerName, string blockName, IPoint addPoint, string textValue) {
try {
CADInsert pCADInsert = new CADInsert();

CADBlock pCADBlock = CADBlocks.GetCADBlock(blockName);

pCADInsert.Block = pCADBlock;

// search CADAttdef Entity and set textvalue
for (int i = 0; i < pCADBlock.Count; i++) {
CADEntity pCADEntity = pCADBlock.Entities;
if (pCADEntity.EntType == EntityType.Attdef) {
CADAttdef pCADAttdef = (CADAttdef)pCADEntity;
pCADAttdef.Text = textValue;
pCADAttdef.Value = textValue;
}
}

pCADInsert.Point = CADFuncs.CreateDPoint(addPoint);
pCADInsert.Layer = cadImage.Converter.LayerByName(cadLayerName);
pCADInsert.Visibility = true;
CADFuncs.AddEntity(cadImage, pCADInsert);
} catch {
}
}

Best regards.

Alex

FieldConsult
Posts: 54
Joined: 14 Mar 2015, 22:33

Re: How Can I Insert a block with CADAttdef?

Post by FieldConsult » 28 Sep 2016, 18:08

Hi:

First, You need to add the attribute to the new block:
Ex:

Code: Select all

// search CADAttdef Entity and set textvalue
if Source.Entities[i] is TsgDXFAttdef then...   
procedure AddAttdef(ATag: string; AValue: string; APoint: TFPoint);
   var
     vDXFAttdef: TsgDXFAttdef;
   begin
     vDXFAttdef := TsgDXFAttdef.Create;
     vDXFAttdef.Tag := ATag;
     vDXFAttdef.Value := AValue;
     vDXFAttdef.Point := APoint;
     vDXFAttdef.Height := 10;
     vDXFAttdef.NotAppear:= True;
     newBlock.AddEntity(vDXFAttdef);
   end;
Then, before adding the new block (TsgDXFBlock) (med new insert (TsgDXFInsert) block propertie) to the drawing, copy the attribute from the new block (TsgDXFBlock) to the insert (TsgDXFInsert), modify the content and load the attribute with the image converter of the drawing and finally add the attribute to the new insert (TsgDXFInsert.Attribs.Add (vAttrib));

I do not use the builder, but if I do it in delphi smoothly.

Cheers. Felipe. :geek:

alex7210
Posts: 5
Joined: 10 Aug 2016, 04:43

Re: How Can I Insert a block with CADAttdef?

Post by alex7210 » 29 Sep 2016, 10:23

Thanks Felipe.

I Added the blocks from a template file.

and try to add a CADInsert entity to a Drawing.

Something happened I didn't expected.

Everything was placed where it was expected but the entitie from CADAttdef was placed only at (0,0);

I don't know what I should do to place it at the proper position (in the extent of the block).


static public void AddBlock(CADImage cadImage, string cadLayerName, string blockName, IPoint addPoint, string textValue) {
try {
CADInsert pCADInsert = new CADInsert();

// Geta block from the dictionary of blocks.
CADBlock pCADBlock = CADBlocks.GetCADBlock(blockName);
pCADInsert.Block = pCADBlock;

// search CADAttdef Entity and set textvalue
for (int i = 0; i < pCADBlock.Count; i++) {
CADEntity pCADEntity = pCADBlock.Entities;
if (pCADEntity.EntType == EntityType.Attdef) {
CADAttdef pCADAttdef = (CADAttdef)pCADEntity;

CADAttrib pCADAttrib = new CADAttrib();
pCADAttrib.AssignEntity(pCADAttdef);
pCADAttrib.Value = textValue;
pCADInsert.Attribs.Add(pCADAttrib);
}
}

pCADInsert.Point = CADFuncs.CreateDPoint(addPoint);
pCADInsert.Layer = cadImage.Converter.LayerByName(cadLayerName);
pCADInsert.Visibility = true;
CADFuncs.AddEntity(cadImage, pCADInsert);
} catch {
Logger.WriteError(string.Format("블럭[{0}]이 등록되지 않았습니다.", blockName));
}
}



Best regards.

Alex

FieldConsult
Posts: 54
Joined: 14 Mar 2015, 22:33

Re: How Can I Insert a block with CADAttdef?

Post by FieldConsult » 29 Sep 2016, 17:44

Hi Alex
if your problem is that the attribute is not positioned in the position it should be, what you need is recalculate the position of the attribute (insertion point) depending on the point of insertion of the new insert (TsgDXFInsert), before adding the new block (TsgDXFBlock).

Code: Select all

          //you need to scale the insertion point of attribute
          if pointScale = 1000 then //milimeters
            begin
              newX:= vAttr.Point.X;
              newY:= vAttr.Point.Y;
            end
          else // meters
            begin
              newX:= vAttr.Point.X / 100;
              newY:= vAttr.Point.Y / 100;
            end;

          //Target new  TsgDXFInsert
          vAttrib.Point := MakeFPoint(Target.Point.X + newX, Target.Point.Y + newY, 0);
          cadImage.Converter.OnCreate(vAttrib);
          cadImage.Converter.Loads(vAttrib);
          Target.Attribs.Add(vAttrib);

how to check if this is your problem, is creating a drawing, add the block with attributes, you want to save the drawing and open it in AutoCAD or vertical and, verify if the attribute is in the proper position, if so, your problem are fixed as I have indicated before, if not in the required position, your problem would be another.

Why must you open the drawing in AutoCAD? The library uses a calculation routine to correctly position the attributes, when loading an AutoCAD drawing, but if you edit an attribute or add one, the library is unable to position the attribute correctly, so you should recalculate the position in reference to the new insert.

Good luck!!

alex7210
Posts: 5
Joined: 10 Aug 2016, 04:43

Re: How Can I Insert a block with CADAttdef?

Post by alex7210 » 30 Sep 2016, 07:11

Thanks.

When I assign the new Point to two properties (point, point1), it worked.

CADAttdef pCADAttdef = (CADAttdef)pCADEntity;

CADAttrib pCADAttrib = new CADAttrib();
pCADAttrib.AssignEntity(pCADAttdef);
pCADAttrib.Value = textValue;

pCADAttrib.Point = CreateDPoint(pCADAttdef.Point.X + addPoint.X , pCADAttdef.Point.Y + addPoint.Y);
pCADAttrib.Point1 = CreateDPoint(pCADAttdef.Point1.X + addPoint.X , pCADAttdef.Point1.Y + addPoint.Y);

cadImage.Converter.OnCreate(pCADAttrib);
cadImage.Converter.Loads(pCADAttrib);
pCADInsert.Attribs.Add(pCADAttrib);

Post Reply