.NET

Reply
*perry
Message 1 of 2 (125 Views)

Can someone tell me why this isnt working?

125 Views, 1 Replies
05-16-2006 12:20 PM
I have a function which is to delete block references that meet a certain
criteria. Namely that they are "revision" blocks, and there revision letter
match what is passed into the function. I now have two versions of this, one
that "works" and one that does not. I want to understand, and fix, the reasons
for the non-working one. The non-working one utilizes a helper function which
creates a collection of object id's and then goes through those id's and deletes
any that meet the criteria. The version that works simply goes through *paperspace
and does the same without using a collection of id's.
The non-working version, does not cause any errors, and seems to behave properly
while stepping through the code, but does not achieve the desired result. If anyone
can see what I have overlooked here, I'd appreciate the pointer.
Sorry for posting so much, code!
Perry

[code]-----------------------------------------------------------------
///
/// Delete a rev block based on the rev letter passed in. This one doesnt work.
/// Utilizes the ObjectIDcollection.
///

public void Delete2(string letter)
{
Utilities.prompt("\nIn revision delete.");
ObjectIdCollection refEnts = Utilities.getBlockIds();

using (Database db = HostApplicationServices.WorkingDatabase)
{
using (Transaction trans = db.TransactionManager.StartTransaction())
{
Utilities.prompt("\nThere are " + refEnts.Count + " block refs in this space.");
foreach (ObjectId id in refEnts)
{
BlockReference blkRef = (BlockReference)trans.GetObject(id, OpenMode.ForWrite, false);
BlockTableRecord blkDef = (BlockTableRecord)trans.GetObject(blkRef.BlockTableRecord, OpenMode.ForWrite);
if (blkDef.Name.ToUpper() == "TBREVISION")
{
//Get the attribute references,if any
AttributeCollection atts;
atts = blkRef.AttributeCollection;
if (atts.Count > 0)
{
foreach (ObjectId attId in atts)
{
AttributeReference att;
string txt = "";
using (att = (AttributeReference)trans.GetObject(attId, OpenMode.ForWrite, false))
{
txt = (att.TextString.ToString());
//If the attribute textstring matches the passed in rev letter
//delete the block reference
if (txt == letter)
{
Utilities.prompt("\ndeleting " + blkDef.Name);
blkRef.Erase();//block ref not erasing!
break;
}
}
}

}
}
blkRef.Dispose();
blkDef.Dispose();
}
}
}
refEnts.Dispose();
}
[helper]---------------------------------------------------------------------------
public static ObjectIdCollection getBlockIds()
{
LayoutManager lManager = LayoutManager.Current;
string currentLayoutName = lManager.CurrentLayout;
ObjectId currentLayout = lManager.GetLayoutId(currentLayoutName);
ObjectIdCollection refEnts = new ObjectIdCollection();

using (Database db = HostApplicationServices.WorkingDatabase)
{
using (Transaction t = db.TransactionManager.StartTransaction())
{
//get the block table
BlockTable bt = (BlockTable)t.GetObject(db.BlockTableId, OpenMode.ForRead);
//get the (x)space table record
BlockTableRecord btr = new BlockTableRecord();
if (currentLayoutName == "Model")
btr = (BlockTableRecord)t.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);
else
// This is supposed to get current paperspace layout
btr = (BlockTableRecord)t.GetObject(bt[BlockTableRecord.PaperSpace], OpenMode.ForRead);

Entity ent;
foreach (ObjectId objId in btr)
{
ent = (Entity)t.GetObject(objId, OpenMode.ForRead);
if (ent is BlockReference)
{
refEnts.Add(objId);
}
ent.Dispose();
}
t.Commit();
btr.Dispose();
bt.Dispose();
}//end using transaction t
}//end using database
return (refEnts);
}
*perry
Message 2 of 2 (125 Views)

Re: Can someone tell me why this isnt working?

05-18-2006 01:17 PM in reply to: *perry
perry wrote:
> I have a function which is to delete block references that meet a certain
> criteria. Namely that they are "revision" blocks, and there revision letter
> match what is passed into the function. I now have two versions of this, one
> that "works" and one that does not. I want to understand, and fix, the reasons
> for the non-working one. The non-working one utilizes a helper function which
> creates a collection of object id's and then goes through those id's and deletes
> any that meet the criteria. The version that works simply goes through *paperspace
> and does the same without using a collection of id's.
> The non-working version, does not cause any errors, and seems to behave properly
> while stepping through the code, but does not achieve the desired result. If anyone
> can see what I have overlooked here, I'd appreciate the pointer.
> Sorry for posting so much, code!
> Perry
>
> [code]-----------------------------------------------------------------
> ///
> /// Delete a rev block based on the rev letter passed in. This one doesnt work.
> /// Utilizes the ObjectIDcollection.
> ///

> public void Delete2(string letter)
> {
> Utilities.prompt("\nIn revision delete.");
> ObjectIdCollection refEnts = Utilities.getBlockIds();
>
> using (Database db = HostApplicationServices.WorkingDatabase)
> {
> using (Transaction trans = db.TransactionManager.StartTransaction())
> {
> Utilities.prompt("\nThere are " + refEnts.Count + " block refs in this space.");
> foreach (ObjectId id in refEnts)
> {
> BlockReference blkRef = (BlockReference)trans.GetObject(id, OpenMode.ForWrite, false);
> BlockTableRecord blkDef = (BlockTableRecord)trans.GetObject(blkRef.BlockTableRecord, OpenMode.ForWrite);
> if (blkDef.Name.ToUpper() == "TBREVISION")
> {
> //Get the attribute references,if any
> AttributeCollection atts;
> atts = blkRef.AttributeCollection;
> if (atts.Count > 0)
> {
> foreach (ObjectId attId in atts)
> {
> AttributeReference att;
> string txt = "";
> using (att = (AttributeReference)trans.GetObject(attId, OpenMode.ForWrite, false))
> {
> txt = (att.TextString.ToString());
> //If the attribute textstring matches the passed in rev letter
> //delete the block reference
> if (txt == letter)
> {
> Utilities.prompt("\ndeleting " + blkDef.Name);
> blkRef.Erase();//block ref not erasing!
> break;
> }
> }
> }
>
> }
> }
> blkRef.Dispose();
> blkDef.Dispose();
> }
> }
> }
> refEnts.Dispose();
> }
> [helper]---------------------------------------------------------------------------
> public static ObjectIdCollection getBlockIds()
> {
> LayoutManager lManager = LayoutManager.Current;
> string currentLayoutName = lManager.CurrentLayout;
> ObjectId currentLayout = lManager.GetLayoutId(currentLayoutName);
> ObjectIdCollection refEnts = new ObjectIdCollection();
>
> using (Database db = HostApplicationServices.WorkingDatabase)
> {
> using (Transaction t = db.TransactionManager.StartTransaction())
> {
> //get the block table
> BlockTable bt = (BlockTable)t.GetObject(db.BlockTableId, OpenMode.ForRead);
> //get the (x)space table record
> BlockTableRecord btr = new BlockTableRecord();
> if (currentLayoutName == "Model")
> btr = (BlockTableRecord)t.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);
> else
> // This is supposed to get current paperspace layout
> btr = (BlockTableRecord)t.GetObject(bt[BlockTableRecord.PaperSpace], OpenMode.ForRead);
>
> Entity ent;
> foreach (ObjectId objId in btr)
> {
> ent = (Entity)t.GetObject(objId, OpenMode.ForRead);
> if (ent is BlockReference)
> {
> refEnts.Add(objId);
> }
> ent.Dispose();
> }
> t.Commit();
> btr.Dispose();
> bt.Dispose();
> }//end using transaction t
> }//end using database
> return (refEnts);
> }

Racked my brains on this for HOURS, so irritating, it SHOULD work.
Then it hit me, like a fish across the face.
Note the lack of a "trans.commit()" near the end of the delete function.

Anyone know where I can get one of those big "cartoon" boots, the one on the stick where
when you pull the rope it kicks you in the arse.
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!