.NET
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
*Carlos
Insert Entity with Attributes , C#, AutoCAD 2005
Options
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
773 Views, 4 Replies
07-20-2005 05:09 PM
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.CommandLinePr ompts.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.D atabaseServices.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(attRe f, true);
t.TransactionManager.AddNewlyCreatedDBObject(bref, true);
t.Commit();
}
catch
{
}
}
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.CommandLinePr
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.
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.D
{
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(attRe
t.TransactionManager.AddNewlyCreatedDBObject(bref, true);
t.Commit();
}
catch
{
}
}
*Carlos
It sees the attributes , but it only adds the first one
Options
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
07-21-2005 03:53 PM in reply to:
*Carlos
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.D atabaseServices.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.AddNewlyCreatedDBObjec t(attRef,
true);
}
attTrans.Commit();
}
catch
{
}
finally
{
attTrans.Dispose();
}
}
trans.Commit();
trans.Dispose();
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.D
{
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.AddNewlyCreatedDBObjec
true);
}
attTrans.Commit();
}
catch
{
}
finally
{
attTrans.Dispose();
}
}
trans.Commit();
trans.Dispose();
*Bobby C. Jones
Re: It sees the attributes , but it only adds the first one
Options
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
07-21-2005 04:01 PM in reply to:
*Carlos
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.D atabaseServices.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.AddNewlyCreatedDBObjec t(attRef,
true);
}
attTrans.Commit();
}
catch
{
}
finally
{
attTrans.Dispose();
}
}
trans.Commit();
trans.Dispose();
inside the loop.
Also, you can simplify your test
if (ent is AttributeDefinition)
{....
--
Bobby C. Jones
http://www.acadx.com
"Carlos"
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.D
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.AddNewlyCreatedDBObjec
true);
}
attTrans.Commit();
}
catch
{
}
finally
{
attTrans.Dispose();
}
}
trans.Commit();
trans.Dispose();
*Carlos
Re: It sees the attributes , but it only adds the first one
Options
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
07-21-2005 04:16 PM in reply to:
*Carlos
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.D atabaseServices.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.AddNewlyCreatedDBObjec t(attRef,
true);
}
attTrans.Commit();
}
catch
{
}
finally
{
attTrans.Dispose();
}
}
trans.Commit();
trans.Dispose();
Yay! It works.
Oh my gosh. I'm so happy. You seriously made my day.
Thanks.
-Carlos
"Bobby C. Jones"
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"
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.D
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.AddNewlyCreatedDBObjec
true);
}
attTrans.Commit();
}
catch
{
}
finally
{
attTrans.Dispose();
}
}
trans.Commit();
trans.Dispose();
*Bobby C. Jones
Re: It sees the attributes , but it only adds the first one
Options
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
07-21-2005 04:53 PM in reply to:
*Carlos
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.D atabaseServices.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.AddNewlyCreatedDBObjec t(attRef,
true);
}
attTrans.Commit();
}
catch
{
}
finally
{
attTrans.Dispose();
}
}
trans.Commit();
trans.Dispose();
--
Bobby C. Jones
http://www.acadx.com
"Carlos"
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"
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"
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.D
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.AddNewlyCreatedDBObjec
true);
}
attTrans.Commit();
}
catch
{
}
finally
{
attTrans.Dispose();
}
}
trans.Commit();
trans.Dispose();
