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

Can someone tell me why this isnt working?

1 REPLY 1
Reply
Message 1 of 2
Anonymous
270 Views, 1 Reply

Can someone tell me why this isnt working?

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);
}
1 REPLY 1
Message 2 of 2
Anonymous
in reply to: Anonymous

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.

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