How to insert block with attributes into a drawing using .NET

How to insert block with attributes into a drawing using .NET

Anonymous
Not applicable
4,601 Views
11 Replies
Message 1 of 12

How to insert block with attributes into a drawing using .NET

Anonymous
Not applicable
Hello,

Has everyone tried to insert blocks with attributes into a drawing file by
using dotnet (C# or VB.NET)?

There is an example "How to insert block with attributes into a drawing
using VB.NET" from ADN web site. But unfortunately, it doesn't work. Anytime
the attributes are inserted on Original position no matter how Position
properties of the attributes are set.

Following is part of my code:

AttributeDefinition attDef = (AttributeDefinition)ent;

AttributeReference attRef = new AttributeReference();

attDef = (AttributeDefinition)ent;

attRef.SetAttributeFromBlock(attDef, Matrix3d.Identity);

Point3d ptBase = new Point3d(blkRef.Position.X + attDef.Position.X,

blkRef.Position.Y + attDef.Position.Y,

blkRef.Position.Z + attDef.Position.Z);

//attRef.Position = ptBase;

//attRef.Position = insPoint;

attRef.Position = new Point3d(50, 50, 50);

attRef.Rotation = attDef.Rotation;

attRef.Tag = attDef.Tag;

attRef.TextString = "Attribute Text";

attRef.Height = attDef.Height;

attRef.FieldLength = attDef.FieldLength;


Can anybody help? Thank you very much.
Zhongwei Xia
0 Likes
4,602 Views
11 Replies
Replies (11)
Message 2 of 12

Anonymous
Not applicable
Does the sample code from ADN actually show a call
to SetAttributeFromBlock(), with Matrix3d.Identity as
the second parameter???

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006
http://www.acadxtabs.com

"Zhongwei Xia" wrote in message news:[email protected]...
Hello,

Has everyone tried to insert blocks with attributes into a drawing file by
using dotnet (C# or VB.NET)?

There is an example "How to insert block with attributes into a drawing
using VB.NET" from ADN web site. But unfortunately, it doesn't work. Anytime
the attributes are inserted on Original position no matter how Position
properties of the attributes are set.

Following is part of my code:

AttributeDefinition attDef = (AttributeDefinition)ent;

AttributeReference attRef = new AttributeReference();

attDef = (AttributeDefinition)ent;

attRef.SetAttributeFromBlock(attDef, Matrix3d.Identity);

Point3d ptBase = new Point3d(blkRef.Position.X + attDef.Position.X,

blkRef.Position.Y + attDef.Position.Y,

blkRef.Position.Z + attDef.Position.Z);

//attRef.Position = ptBase;

//attRef.Position = insPoint;

attRef.Position = new Point3d(50, 50, 50);

attRef.Rotation = attDef.Rotation;

attRef.Tag = attDef.Tag;

attRef.TextString = "Attribute Text";

attRef.Height = attDef.Height;

attRef.FieldLength = attDef.FieldLength;


Can anybody help? Thank you very much.
Zhongwei Xia
0 Likes
Message 3 of 12

Anonymous
Not applicable
Hi, Tony,

Yes, it does. Thank you for your reply.
Zhongwei

"Tony Tanzillo" wrote in message
news:[email protected]...
Does the sample code from ADN actually show a call
to SetAttributeFromBlock(), with Matrix3d.Identity as
the second parameter???

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006
http://www.acadxtabs.com

"Zhongwei Xia" wrote in message
news:[email protected]...
Hello,

Has everyone tried to insert blocks with attributes into a drawing file by
using dotnet (C# or VB.NET)?

There is an example "How to insert block with attributes into a drawing
using VB.NET" from ADN web site. But unfortunately, it doesn't work. Anytime
the attributes are inserted on Original position no matter how Position
properties of the attributes are set.

Following is part of my code:

AttributeDefinition attDef = (AttributeDefinition)ent;

AttributeReference attRef = new AttributeReference();

attDef = (AttributeDefinition)ent;

attRef.SetAttributeFromBlock(attDef, Matrix3d.Identity);

Point3d ptBase = new Point3d(blkRef.Position.X + attDef.Position.X,

blkRef.Position.Y + attDef.Position.Y,

blkRef.Position.Z + attDef.Position.Z);

//attRef.Position = ptBase;

//attRef.Position = insPoint;

attRef.Position = new Point3d(50, 50, 50);

attRef.Rotation = attDef.Rotation;

attRef.Tag = attDef.Tag;

attRef.TextString = "Attribute Text";

attRef.Height = attDef.Height;

attRef.FieldLength = attDef.FieldLength;


Can anybody help? Thank you very much.
Zhongwei Xia
0 Likes
Message 4 of 12

Anonymous
Not applicable
Amazing.

The Matrix3d argument to SetAttributeFromBlock()
should be set to the value of the BlockTransform
property of the owning BlockReference object.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006
http://www.acadxtabs.com

"Zhongwei Xia" wrote in message news:[email protected]...
Hi, Tony,

Yes, it does. Thank you for your reply.
Zhongwei

"Tony Tanzillo" wrote in message
news:[email protected]...
Does the sample code from ADN actually show a call
to SetAttributeFromBlock(), with Matrix3d.Identity as
the second parameter???

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006
http://www.acadxtabs.com

"Zhongwei Xia" wrote in message
news:[email protected]...
Hello,

Has everyone tried to insert blocks with attributes into a drawing file by
using dotnet (C# or VB.NET)?

There is an example "How to insert block with attributes into a drawing
using VB.NET" from ADN web site. But unfortunately, it doesn't work. Anytime
the attributes are inserted on Original position no matter how Position
properties of the attributes are set.

Following is part of my code:

AttributeDefinition attDef = (AttributeDefinition)ent;

AttributeReference attRef = new AttributeReference();

attDef = (AttributeDefinition)ent;

attRef.SetAttributeFromBlock(attDef, Matrix3d.Identity);

Point3d ptBase = new Point3d(blkRef.Position.X + attDef.Position.X,

blkRef.Position.Y + attDef.Position.Y,

blkRef.Position.Z + attDef.Position.Z);

//attRef.Position = ptBase;

//attRef.Position = insPoint;

attRef.Position = new Point3d(50, 50, 50);

attRef.Rotation = attDef.Rotation;

attRef.Tag = attDef.Tag;

attRef.TextString = "Attribute Text";

attRef.Height = attDef.Height;

attRef.FieldLength = attDef.FieldLength;


Can anybody help? Thank you very much.
Zhongwei Xia
0 Likes
Message 5 of 12

Anonymous
Not applicable
Hi, Tony,

Guess what!? I just comment the following line out, and set TextStyle from
AttributeDefinition. It works!!!.

// attRef.SetAttributeFromBlock(attDef, Matrix3d.Identity);
attRef.TextStyle = attDef.TextStyle;

Thank you very much. You emails help me alot.
Zhongwei

"Tony Tanzillo" wrote in message
news:[email protected]...
Amazing.

The Matrix3d argument to SetAttributeFromBlock()
should be set to the value of the BlockTransform
property of the owning BlockReference object.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006
http://www.acadxtabs.com

"Zhongwei Xia" wrote in message
news:[email protected]...
Hi, Tony,

Yes, it does. Thank you for your reply.
Zhongwei

"Tony Tanzillo" wrote in message
news:[email protected]...
Does the sample code from ADN actually show a call
to SetAttributeFromBlock(), with Matrix3d.Identity as
the second parameter???

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006
http://www.acadxtabs.com

"Zhongwei Xia" wrote in message
news:[email protected]...
Hello,

Has everyone tried to insert blocks with attributes into a drawing file by
using dotnet (C# or VB.NET)?

There is an example "How to insert block with attributes into a drawing
using VB.NET" from ADN web site. But unfortunately, it doesn't work. Anytime
the attributes are inserted on Original position no matter how Position
properties of the attributes are set.

Following is part of my code:

AttributeDefinition attDef = (AttributeDefinition)ent;

AttributeReference attRef = new AttributeReference();

attDef = (AttributeDefinition)ent;

attRef.SetAttributeFromBlock(attDef, Matrix3d.Identity);

Point3d ptBase = new Point3d(blkRef.Position.X + attDef.Position.X,

blkRef.Position.Y + attDef.Position.Y,

blkRef.Position.Z + attDef.Position.Z);

//attRef.Position = ptBase;

//attRef.Position = insPoint;

attRef.Position = new Point3d(50, 50, 50);

attRef.Rotation = attDef.Rotation;

attRef.Tag = attDef.Tag;

attRef.TextString = "Attribute Text";

attRef.Height = attDef.Height;

attRef.FieldLength = attDef.FieldLength;


Can anybody help? Thank you very much.
Zhongwei Xia
0 Likes
Message 6 of 12

Anonymous
Not applicable
Most of the code from the example is not required if
you make the change I noted. For example, there
should be no need for manual translation, rotation,
and scaling since the BlockTransform matrix contains
all of that information, and SetAttributeFromBlock()
should do the transformation for you.

If I'm wrong about that, please let me know.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006
http://www.acadxtabs.com

"Zhongwei Xia" wrote in message news:[email protected]...
Hi, Tony,

Guess what!? I just comment the following line out, and set TextStyle from
AttributeDefinition. It works!!!.

// attRef.SetAttributeFromBlock(attDef, Matrix3d.Identity);
attRef.TextStyle = attDef.TextStyle;

Thank you very much. You emails help me alot.
Zhongwei

"Tony Tanzillo" wrote in message
news:[email protected]...
Amazing.

The Matrix3d argument to SetAttributeFromBlock()
should be set to the value of the BlockTransform
property of the owning BlockReference object.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006
http://www.acadxtabs.com

"Zhongwei Xia" wrote in message
news:[email protected]...
Hi, Tony,

Yes, it does. Thank you for your reply.
Zhongwei

"Tony Tanzillo" wrote in message
news:[email protected]...
Does the sample code from ADN actually show a call
to SetAttributeFromBlock(), with Matrix3d.Identity as
the second parameter???

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006
http://www.acadxtabs.com

"Zhongwei Xia" wrote in message
news:[email protected]...
Hello,

Has everyone tried to insert blocks with attributes into a drawing file by
using dotnet (C# or VB.NET)?

There is an example "How to insert block with attributes into a drawing
using VB.NET" from ADN web site. But unfortunately, it doesn't work. Anytime
the attributes are inserted on Original position no matter how Position
properties of the attributes are set.

Following is part of my code:

AttributeDefinition attDef = (AttributeDefinition)ent;

AttributeReference attRef = new AttributeReference();

attDef = (AttributeDefinition)ent;

attRef.SetAttributeFromBlock(attDef, Matrix3d.Identity);

Point3d ptBase = new Point3d(blkRef.Position.X + attDef.Position.X,

blkRef.Position.Y + attDef.Position.Y,

blkRef.Position.Z + attDef.Position.Z);

//attRef.Position = ptBase;

//attRef.Position = insPoint;

attRef.Position = new Point3d(50, 50, 50);

attRef.Rotation = attDef.Rotation;

attRef.Tag = attDef.Tag;

attRef.TextString = "Attribute Text";

attRef.Height = attDef.Height;

attRef.FieldLength = attDef.FieldLength;


Can anybody help? Thank you very much.
Zhongwei Xia
0 Likes
Message 7 of 12

Anonymous
Not applicable
Thank you for your email.

SetAttributeFromBlock() can't be called. If I call this function, attributes
will go to (0,0,0) position.
And also, if I set HorisontalMode of attribute, the attributes go to (0,0,0)
again.
attRef.HorizontalMode =
Autodesk.AutoCAD.DatabaseServices.TextHorizontalMode.TextCenter;

Zhongwei


"Tony Tanzillo" wrote in message
news:[email protected]...
Most of the code from the example is not required if
you make the change I noted. For example, there
should be no need for manual translation, rotation,
and scaling since the BlockTransform matrix contains
all of that information, and SetAttributeFromBlock()
should do the transformation for you.

If I'm wrong about that, please let me know.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006
http://www.acadxtabs.com

"Zhongwei Xia" wrote in message
news:[email protected]...
Hi, Tony,

Guess what!? I just comment the following line out, and set TextStyle from
AttributeDefinition. It works!!!.

// attRef.SetAttributeFromBlock(attDef, Matrix3d.Identity);
attRef.TextStyle = attDef.TextStyle;

Thank you very much. You emails help me alot.
Zhongwei

"Tony Tanzillo" wrote in message
news:[email protected]...
Amazing.

The Matrix3d argument to SetAttributeFromBlock()
should be set to the value of the BlockTransform
property of the owning BlockReference object.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006
http://www.acadxtabs.com

"Zhongwei Xia" wrote in message
news:[email protected]...
Hi, Tony,

Yes, it does. Thank you for your reply.
Zhongwei

"Tony Tanzillo" wrote in message
news:[email protected]...
Does the sample code from ADN actually show a call
to SetAttributeFromBlock(), with Matrix3d.Identity as
the second parameter???

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006
http://www.acadxtabs.com

"Zhongwei Xia" wrote in message
news:[email protected]...
Hello,

Has everyone tried to insert blocks with attributes into a drawing file by
using dotnet (C# or VB.NET)?

There is an example "How to insert block with attributes into a drawing
using VB.NET" from ADN web site. But unfortunately, it doesn't work. Anytime
the attributes are inserted on Original position no matter how Position
properties of the attributes are set.

Following is part of my code:

AttributeDefinition attDef = (AttributeDefinition)ent;

AttributeReference attRef = new AttributeReference();

attDef = (AttributeDefinition)ent;

attRef.SetAttributeFromBlock(attDef, Matrix3d.Identity);

Point3d ptBase = new Point3d(blkRef.Position.X + attDef.Position.X,

blkRef.Position.Y + attDef.Position.Y,

blkRef.Position.Z + attDef.Position.Z);

//attRef.Position = ptBase;

//attRef.Position = insPoint;

attRef.Position = new Point3d(50, 50, 50);

attRef.Rotation = attDef.Rotation;

attRef.Tag = attDef.Tag;

attRef.TextString = "Attribute Text";

attRef.Height = attDef.Height;

attRef.FieldLength = attDef.FieldLength;


Can anybody help? Thank you very much.
Zhongwei Xia
0 Likes
Message 8 of 12

Anonymous
Not applicable
>> SetAttributeFromBlock() can't be called.
>> If I call this function, attributes will go to (0,0,0) position.

Did you pass the value of the BlockTransform property of
the BlockReference object to the second argument to
SetAttributesFromBlock, as I had mentioned earlier?


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006
http://www.acadxtabs.com

"Zhongwei Xia" wrote in message news:[email protected]...
Thank you for your email.
0 Likes
Message 9 of 12

Anonymous
Not applicable
I just tried it. I works great.
I appreciate your helps very much.
Zhongwei


"Tony Tanzillo" wrote in message
news:[email protected]...
>> SetAttributeFromBlock() can't be called.
>> If I call this function, attributes will go to (0,0,0) position.

Did you pass the value of the BlockTransform property of
the BlockReference object to the second argument to
SetAttributesFromBlock, as I had mentioned earlier?


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006
http://www.acadxtabs.com

"Zhongwei Xia" wrote in message
news:[email protected]...
Thank you for your email.
0 Likes
Message 10 of 12

Anonymous
Not applicable
Is it possible for you to post the final code?
Roland


"Zhongwei Xia" schrieb im Newsbeitrag
news:[email protected]...
I just tried it. I works great.
I appreciate your helps very much.
Zhongwei


"Tony Tanzillo" wrote in message
news:[email protected]...
>> SetAttributeFromBlock() can't be called.
>> If I call this function, attributes will go to (0,0,0) position.

Did you pass the value of the BlockTransform property of
the BlockReference object to the second argument to
SetAttributesFromBlock, as I had mentioned earlier?


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006
http://www.acadxtabs.com

"Zhongwei Xia" wrote in message
news:[email protected]...
Thank you for your email.
0 Likes
Message 11 of 12

Anonymous
Not applicable
Following is the code to insert AttributeReference.

public static void InsertBlockAttibuteRef(ObjectId blockDefID,
BlockReference blkRef,

string strAttributeText, Point3d ptInsert)

{

Database dbCurrent = HostApplicationServices.WorkingDatabase;

Autodesk.AutoCAD.DatabaseServices.TransactionManager tm =
dbCurrent.TransactionManager;


using (Transaction tr = tm.StartTransaction())

{

DBObject objBlkRef = tr.GetObject(blkRef.ObjectId, OpenMode.ForWrite);

BlockTableRecord btAttRec;

btAttRec = (BlockTableRecord)tr.GetObject(blockDefID, OpenMode.ForRead);

foreach (ObjectId idAtt in btAttRec)

{

Entity ent = (Entity)tr.GetObject(idAtt, OpenMode.ForRead);

if (ent is AttributeDefinition)

{

AttributeDefinition attDef = (AttributeDefinition)ent;

AttributeReference attRef = new AttributeReference();


attRef.SetAttributeFromBlock(attDef, blkRef.BlockTransform);

attRef.TextString = strAttributeText;

ObjectId idTemp = blkRef.AttributeCollection.AppendAttribute(attRef);

tr.AddNewlyCreatedDBObject(attRef, true);

}

}

tr.Commit();

} // end of using

}


wrote in message news:[email protected]...
Is it possible for you to post the final code?
Roland


"Zhongwei Xia" schrieb im Newsbeitrag
news:[email protected]...
I just tried it. I works great.
I appreciate your helps very much.
Zhongwei


"Tony Tanzillo" wrote in message
news:[email protected]...
>> SetAttributeFromBlock() can't be called.
>> If I call this function, attributes will go to (0,0,0) position.

Did you pass the value of the BlockTransform property of
the BlockReference object to the second argument to
SetAttributesFromBlock, as I had mentioned earlier?


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006
http://www.acadxtabs.com

"Zhongwei Xia" wrote in message
news:[email protected]...
Thank you for your email.
0 Likes
Message 12 of 12

Anonymous
Not applicable
Thank you!

--
Roland Feletic
0 Likes