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

Insert Entity with Attributes, C#, AutoCAD 2005

4 REPLIES 4
Reply
Message 1 of 5
Anonymous
1344 Views, 4 Replies

Insert Entity with Attributes, C#, AutoCAD 2005

Hello,

I've WBLOCKed a block called "Sensing Unit" from AutoCAD to a file. I'd
like to add it to the model and fill in its attributes.

I can get the block inserted, but with my current code it only gets one
attribute (the last one) instead of the 50 or 60 that it should have.

I feel like I've tried everything here. Anyone know how this is done? My
problem is obviously in the iterator, but I'm very new to this so I'm not
exactly sure how it works.

Thanks so much for any help.

-Carlos

public void InsertIntoAutoCAD()

{

// Inserts the sensing unit into the AutoCAD model and writes the
attributes

try

{

//insert the new block

Document doc = Application.DocumentManager.MdiActiveDocument;

string fname = "C:\\Program Files\\Sensametrics Layout
Interface\\Blocks\\Sensing Unit.dwg";

Database db = new Database(false, false);

db.ReadDwgFile(fname, System.IO.FileShare.Read, true, null);

Transaction t = doc.TransactionManager.StartTransaction();

Autodesk.AutoCAD.ApplicationServices.CommandLinePrompts.Message("\n
Inserting a sensing unit...");

ObjectId idBTR = doc.Database.Insert("Sensing Unit",db,true);

//create a ref to the block

BlockTable bt = (BlockTable)t.GetObject(doc.Database.BlockTableId,
OpenMode.ForWrite);

BlockTableRecord btr =
(BlockTableRecord)t.GetObject(bt[BlockTableRecord.ModelSpace],
OpenMode.ForWrite);

Autodesk.AutoCAD.Geometry.Point3d location = new Point3d(0.0, 0.0,
0.0);

BlockReference bref = new BlockReference(location, idBTR);

//Add the reference to the model space

btr.AppendEntity(bref);

*************** (Works so far, but isn't finding all the
attributes)**************


//Iterate the sensing unit block and find the attributes

BlockTableRecord unitBTR = (BlockTableRecord)t.GetObject(idBTR,
OpenMode.ForWrite);

AttributeReference attRef = new AttributeReference();

foreach(ObjectId id in unitBTR)

{

Entity ent = (Entity)t.GetObject(id, OpenMode.ForWrite, false);

if
(ent.GetType().FullName.Equals("Autodesk.AutoCAD.DatabaseServices.AttributeDefinition"))

{

AttributeDefinition attDef = (AttributeDefinition)ent;

attRef.SetPropertiesFrom(attDef);

attRef.Tag = attDef.Tag;

attRef.TextString = attDef.TextString;

}

}

//Add the attribute reference to the block reference

bref.AppendAttribute(attRef);

//Let the transaction know

t.TransactionManager.AddNewlyCreatedDBObject(attRef, true);

t.TransactionManager.AddNewlyCreatedDBObject(bref, true);

t.Commit();

}

catch

{

}

}
4 REPLIES 4
Message 2 of 5
Anonymous
in reply to: Anonymous

Okay, I narrowed down the problem.

I'm trying to insert a block with attributes, but my code only writes the
first attribute to the block when there should be many of them.

The code prints all of the attribute tags to the screen in AutoCAD, so I
know it's finding all of them.

I feel like it should be simple, but it's just not working. Can any of you
see the problem?

Thanks so much for any help.

-Carlos


int attCount = 0;
AttributeDefinition attDef = new AttributeDefinition();
AttributeReference attRef = new AttributeReference();
Transaction attTrans;
Entity ent;

<<<<<<<<<<<<< STARTS LOOPING THOUGH ATTRIBUTES HERE >>>>>>>>>>>>>>
foreach(ObjectId id in BTR)
{
ent = (Entity)trans.GetObject(id, OpenMode.ForWrite);
attTrans = db.TransactionManager.StartTransaction();
try
{
if
(ent.GetType().FullName.Equals("Autodesk.AutoCAD.DatabaseServices.AttributeDefinition"))
{
Autodesk.AutoCAD.Geometry.Matrix3d myMatrix = new
Matrix3d();
myMatrix.SetToIdentity();

attDef = (AttributeDefinition)ent;
attRef.SetAttributeFromBlock(attDef, myMatrix);

attCount++;

<<<<<<<<<<<<< THESE LINES PRINT ALL OF THE TAGS,
NOT JUST THE FIRST >>>>>>>>>>
CommandLinePrompts.Message("\nAttribute #" +
attCount.ToString());
CommandLinePrompts.Message("\nTag: " +
attRef.Tag.ToString());

<<<<<<<<<<< PROBLEM: THIS PART ONLY WORKS THE
FIRST TIME >>>>>>>>>>>
bref.AppendAttribute(attRef);
attTrans.TransactionManager.AddNewlyCreatedDBObject(attRef,
true);
}
attTrans.Commit();
}
catch
{
}
finally
{
attTrans.Dispose();
}
}

trans.Commit();
trans.Dispose();
Message 3 of 5
Anonymous
in reply to: Anonymous

I've only quickly glanced at your code, but try creating your attRef object
inside the loop.

Also, you can simplify your test

if (ent is AttributeDefinition)
{....

--
Bobby C. Jones
http://www.acadx.com

"Carlos" wrote in message
news:4908301@discussion.autodesk.com...
Okay, I narrowed down the problem.

I'm trying to insert a block with attributes, but my code only writes the
first attribute to the block when there should be many of them.

The code prints all of the attribute tags to the screen in AutoCAD, so I
know it's finding all of them.

I feel like it should be simple, but it's just not working. Can any of you
see the problem?

Thanks so much for any help.

-Carlos


int attCount = 0;
AttributeDefinition attDef = new AttributeDefinition();
AttributeReference attRef = new AttributeReference();
Transaction attTrans;
Entity ent;

<<<<<<<<<<<<< STARTS LOOPING THOUGH ATTRIBUTES HERE >>>>>>>>>>>>>>
foreach(ObjectId id in BTR)
{
ent = (Entity)trans.GetObject(id, OpenMode.ForWrite);
attTrans = db.TransactionManager.StartTransaction();
try
{
if
(ent.GetType().FullName.Equals("Autodesk.AutoCAD.DatabaseServices.AttributeD
efinition"))
{
Autodesk.AutoCAD.Geometry.Matrix3d myMatrix = new
Matrix3d();
myMatrix.SetToIdentity();

attDef = (AttributeDefinition)ent;
attRef.SetAttributeFromBlock(attDef, myMatrix);

attCount++;

<<<<<<<<<<<<< THESE LINES PRINT ALL OF THE TAGS,
NOT JUST THE FIRST >>>>>>>>>>
CommandLinePrompts.Message("\nAttribute #" +
attCount.ToString());
CommandLinePrompts.Message("\nTag: " +
attRef.Tag.ToString());

<<<<<<<<<<< PROBLEM: THIS PART ONLY WORKS THE
FIRST TIME >>>>>>>>>>>
bref.AppendAttribute(attRef);

attTrans.TransactionManager.AddNewlyCreatedDBObject(attRef,
true);
}
attTrans.Commit();
}
catch
{
}
finally
{
attTrans.Dispose();
}
}

trans.Commit();
trans.Dispose();
Message 4 of 5
Anonymous
in reply to: Anonymous

Thank you!

Yay! It works.

Oh my gosh. I'm so happy. You seriously made my day.

Thanks.

-Carlos


"Bobby C. Jones" wrote in message
news:4908290@discussion.autodesk.com...
I've only quickly glanced at your code, but try creating your attRef object
inside the loop.

Also, you can simplify your test

if (ent is AttributeDefinition)
{....

--
Bobby C. Jones
http://www.acadx.com

"Carlos" wrote in message
news:4908301@discussion.autodesk.com...
Okay, I narrowed down the problem.

I'm trying to insert a block with attributes, but my code only writes the
first attribute to the block when there should be many of them.

The code prints all of the attribute tags to the screen in AutoCAD, so I
know it's finding all of them.

I feel like it should be simple, but it's just not working. Can any of you
see the problem?

Thanks so much for any help.

-Carlos


int attCount = 0;
AttributeDefinition attDef = new AttributeDefinition();
AttributeReference attRef = new AttributeReference();
Transaction attTrans;
Entity ent;

<<<<<<<<<<<<< STARTS LOOPING THOUGH ATTRIBUTES HERE >>>>>>>>>>>>>>
foreach(ObjectId id in BTR)
{
ent = (Entity)trans.GetObject(id, OpenMode.ForWrite);
attTrans = db.TransactionManager.StartTransaction();
try
{
if
(ent.GetType().FullName.Equals("Autodesk.AutoCAD.DatabaseServices.AttributeD
efinition"))
{
Autodesk.AutoCAD.Geometry.Matrix3d myMatrix = new
Matrix3d();
myMatrix.SetToIdentity();

attDef = (AttributeDefinition)ent;
attRef.SetAttributeFromBlock(attDef, myMatrix);

attCount++;

<<<<<<<<<<<<< THESE LINES PRINT ALL OF THE TAGS,
NOT JUST THE FIRST >>>>>>>>>>
CommandLinePrompts.Message("\nAttribute #" +
attCount.ToString());
CommandLinePrompts.Message("\nTag: " +
attRef.Tag.ToString());

<<<<<<<<<<< PROBLEM: THIS PART ONLY WORKS THE
FIRST TIME >>>>>>>>>>>
bref.AppendAttribute(attRef);

attTrans.TransactionManager.AddNewlyCreatedDBObject(attRef,
true);
}
attTrans.Commit();
}
catch
{
}
finally
{
attTrans.Dispose();
}
}

trans.Commit();
trans.Dispose();
Message 5 of 5
Anonymous
in reply to: Anonymous

Glad to help out!
--
Bobby C. Jones
http://www.acadx.com

"Carlos" wrote in message
news:4908294@discussion.autodesk.com...
Thank you!

Yay! It works.

Oh my gosh. I'm so happy. You seriously made my day.

Thanks.

-Carlos


"Bobby C. Jones" wrote in message
news:4908290@discussion.autodesk.com...
I've only quickly glanced at your code, but try creating your attRef object
inside the loop.

Also, you can simplify your test

if (ent is AttributeDefinition)
{....

--
Bobby C. Jones
http://www.acadx.com

"Carlos" wrote in message
news:4908301@discussion.autodesk.com...
Okay, I narrowed down the problem.

I'm trying to insert a block with attributes, but my code only writes the
first attribute to the block when there should be many of them.

The code prints all of the attribute tags to the screen in AutoCAD, so I
know it's finding all of them.

I feel like it should be simple, but it's just not working. Can any of you
see the problem?

Thanks so much for any help.

-Carlos


int attCount = 0;
AttributeDefinition attDef = new AttributeDefinition();
AttributeReference attRef = new AttributeReference();
Transaction attTrans;
Entity ent;

<<<<<<<<<<<<< STARTS LOOPING THOUGH ATTRIBUTES HERE >>>>>>>>>>>>>>
foreach(ObjectId id in BTR)
{
ent = (Entity)trans.GetObject(id, OpenMode.ForWrite);
attTrans = db.TransactionManager.StartTransaction();
try
{
if
(ent.GetType().FullName.Equals("Autodesk.AutoCAD.DatabaseServices.AttributeD
efinition"))
{
Autodesk.AutoCAD.Geometry.Matrix3d myMatrix = new
Matrix3d();
myMatrix.SetToIdentity();

attDef = (AttributeDefinition)ent;
attRef.SetAttributeFromBlock(attDef, myMatrix);

attCount++;

<<<<<<<<<<<<< THESE LINES PRINT ALL OF THE TAGS,
NOT JUST THE FIRST >>>>>>>>>>
CommandLinePrompts.Message("\nAttribute #" +
attCount.ToString());
CommandLinePrompts.Message("\nTag: " +
attRef.Tag.ToString());

<<<<<<<<<<< PROBLEM: THIS PART ONLY WORKS THE
FIRST TIME >>>>>>>>>>>
bref.AppendAttribute(attRef);

attTrans.TransactionManager.AddNewlyCreatedDBObject(attRef,
true);
}
attTrans.Commit();
}
catch
{
}
finally
{
attTrans.Dispose();
}
}

trans.Commit();
trans.Dispose();

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