.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

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

11 REPLIES 11
Reply
Message 1 of 12
Anonymous
3445 Views, 11 Replies

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

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
11 REPLIES 11
Message 2 of 12
Anonymous
in reply to: Anonymous

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:4967475@discussion.autodesk.com...
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
Message 3 of 12
Anonymous
in reply to: Anonymous

Hi, Tony,

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

"Tony Tanzillo" wrote in message
news:4967690@discussion.autodesk.com...
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:4967475@discussion.autodesk.com...
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
Message 4 of 12
Anonymous
in reply to: Anonymous

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:4967872@discussion.autodesk.com...
Hi, Tony,

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

"Tony Tanzillo" wrote in message
news:4967690@discussion.autodesk.com...
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:4967475@discussion.autodesk.com...
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
Message 5 of 12
Anonymous
in reply to: Anonymous

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:4968102@discussion.autodesk.com...
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:4967872@discussion.autodesk.com...
Hi, Tony,

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

"Tony Tanzillo" wrote in message
news:4967690@discussion.autodesk.com...
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:4967475@discussion.autodesk.com...
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
Message 6 of 12
Anonymous
in reply to: Anonymous

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:4968152@discussion.autodesk.com...
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:4968102@discussion.autodesk.com...
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:4967872@discussion.autodesk.com...
Hi, Tony,

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

"Tony Tanzillo" wrote in message
news:4967690@discussion.autodesk.com...
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:4967475@discussion.autodesk.com...
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
Message 7 of 12
Anonymous
in reply to: Anonymous

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:4968338@discussion.autodesk.com...
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:4968152@discussion.autodesk.com...
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:4968102@discussion.autodesk.com...
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:4967872@discussion.autodesk.com...
Hi, Tony,

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

"Tony Tanzillo" wrote in message
news:4967690@discussion.autodesk.com...
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:4967475@discussion.autodesk.com...
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
Message 8 of 12
Anonymous
in reply to: Anonymous

>> 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:4969406@discussion.autodesk.com...
Thank you for your email.
Message 9 of 12
Anonymous
in reply to: Anonymous

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


"Tony Tanzillo" wrote in message
news:4969620@discussion.autodesk.com...
>> 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:4969406@discussion.autodesk.com...
Thank you for your email.
Message 10 of 12
Anonymous
in reply to: Anonymous

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


"Zhongwei Xia" schrieb im Newsbeitrag
news:4969745@discussion.autodesk.com...
I just tried it. I works great.
I appreciate your helps very much.
Zhongwei


"Tony Tanzillo" wrote in message
news:4969620@discussion.autodesk.com...
>> 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:4969406@discussion.autodesk.com...
Thank you for your email.
Message 11 of 12
Anonymous
in reply to: Anonymous

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:4971700@discussion.autodesk.com...
Is it possible for you to post the final code?
Roland


"Zhongwei Xia" schrieb im Newsbeitrag
news:4969745@discussion.autodesk.com...
I just tried it. I works great.
I appreciate your helps very much.
Zhongwei


"Tony Tanzillo" wrote in message
news:4969620@discussion.autodesk.com...
>> 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:4969406@discussion.autodesk.com...
Thank you for your email.
Message 12 of 12
Anonymous
in reply to: Anonymous

Thank you!

--
Roland Feletic

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost