Author Topic: Rewriting program - opinions?  (Read 13727 times)

0 Members and 1 Guest are viewing this topic.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Rewriting program - opinions?
« Reply #30 on: December 27, 2007, 03:40:09 PM »
They seem to be about the same speed wise, but yours also purged out xref layers, while I make sure I don't.  Don't know if that matters or not, but I noticed it did on my little drawing, and a drawing that purged out 162 layers.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Rewriting program - opinions?
« Reply #31 on: December 27, 2007, 03:49:01 PM »
Glenn, whilst what you are saying makes sense in my head, Im not sure how to do that.  Can you give me a nudge in the right direction?  :-)
Maybe something like this.
Code: [Select]
public ObjectIdCollection GetPurgableIds (Database db, ObjectId TabId) {
ObjectIdCollection idsToPurge = new ObjectIdCollection();
using (Transaction Trans = db.TransactionManager.StartTransaction()) {
SymbolTable SymTbl = Trans.GetObject(TabId, OpenMode.ForRead) as SymbolTable;
foreach (ObjectId ObjId in SymTbl) {
idsToPurge.Add(ObjId);
}
db.Purge(idsToPurge);
}
return idsToPurge;
}
Then called like
Code: [Select]
ObjectIdCollection PurgableIds = GetPurgableIds (Db, Db.LayerTableId);

Edit:  Show complete code to erase layers.
Edit3:  Made it show it won't try and erase items that are dependent on xrefs.  Shown in the code in red.

Code: [Select]
[CommandMethod("PC2")]
public void PurgeCurrentDocument2() {
Document Doc = acadApp.DocumentManager.MdiActiveDocument;
Database Db = Doc.Database;
Editor Ed = Doc.Editor;
using (Transaction Trans = Db.TransactionManager.StartTransaction()) {
ObjectIdCollection PurgeIds = GetPurgableIds(Db, Db.LayerTableId);
[color=red] int CountIds = PurgeIds.Count;[/color]
foreach (ObjectId ObjId in PurgeIds) {
SymbolTableRecord SymTblRec = Trans.GetObject(ObjId, OpenMode.ForWrite) as SymbolTableRecord;
[color=red] if (SymTblRec.IsDependent)
--CountIds;
else {
Ed.WriteMessage("\n Deleted layer: {0}", SymTblRec.Name);
SymTblRec.Erase();
}[/color]
}
[color=red] Ed.WriteMessage("\n {0} total number of layers deleted.", CountIds);[/color]
}
}

I learned something new.  Now to go read up on the Purge method.  :-)

Edit2:  Glenn beat me to the punch.  See code below.  Thanks Glenn.
« Last Edit: December 27, 2007, 05:35:28 PM by T.Willey »
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Glenn R

  • Guest
Re: Rewriting program - opinions?
« Reply #32 on: December 27, 2007, 03:58:24 PM »
Tim,

Spot on, but you can also apply that to the foreach loop you would use to delete the id's as well.

Writing this in line off the top of my head without testing:

foreach (objectId symbolTableRecordId in SomeSymbolTable) {
  SymbolTableRecord symbolTableRecord = tr.Getobject(symbolTableRecordId, openMode.ForWrite, true) as SymbolTableRecord;
  symbolTableRecord.Erase();
}

Cheers,
Glenn.

BeanBag

  • Guest
Re: Rewriting program - opinions?
« Reply #33 on: December 28, 2007, 08:13:35 AM »
T Willey I got your first program to work but seems to be a problem when using symboltablerecord.

Your program runs through and tells me its deleted the expected layers but the layers still exist.

It would seem this line is not doing what it should:

Code: [Select]
SymTblRec.Erase();
Using Express 2008 and Autocad 2008 - only learning  .Net for Cad right now so can't be much help.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Rewriting program - opinions?
« Reply #34 on: December 28, 2007, 10:52:01 AM »
I am a little confused on this piece here.
Code: [Select]
using (Transaction Trans = Db.TransactionManager.StartTransaction())
        {
            ObjectIdCollection PurgeIds = GetPurgableIds(Db, Db.BlockTableId );
            //ObjectIdCollection PurgeIds = GetPurgableIds(Db, Db.LayerTableId);
            int CountIds = PurgeIds.Count;
            foreach (ObjectId ObjId in PurgeIds)
            {
                SymbolTableRecord SymTblRec = Trans.GetObject(ObjId, OpenMode.ForWrite) as SymbolTableRecord;
                if (SymTblRec.IsDependent)
                    --CountIds;
                else
                {
                    Ed.WriteMessage("\n Deleted block: {0}", SymTblRec.Name);
                    SymTblRec.Erase();
                }
            }
            Ed.WriteMessage("\n {0} total number of layers deleted.", CountIds);
        }

from Acad
Command: PC2
 Deleted block: 3D-TTL-A


I swapped the LayerTableId for BlockTableId.  As I step through the code, I see the SymTblRec.Erase() work, yet when its done, the block definition is not purged.  Any ideas?
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Rewriting program - opinions?
« Reply #35 on: December 28, 2007, 10:57:05 AM »
David, I haven't looked at the code, but ..

Are the blocks/layers still there if you save and re-open the drawing ?

As I understand it ;
The table entries are not actually erased, they are marked as 'to be erased' ( or somesuch ) and don't actually get removed till the drawing is closed. ... think about 'UNDO' capability. 

added:
They should not show in the blocks/layers dialog however ... they are still accessable if you iterate the table.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Rewriting program - opinions?
« Reply #36 on: December 28, 2007, 10:59:46 AM »
I just tried, and its a no-go.  I wonder if the Layer/Block Table Ids have to be cast back to their "proper" Ids not generic SymbolTableId.  Ill try that next
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8696
  • AKA Daniel
Re: Rewriting program - opinions?
« Reply #37 on: December 28, 2007, 11:07:07 AM »
                SymbolTableRecord SymTblRec = Trans.GetObject(ObjId, OpenMode.ForWrite) as SymbolTableRecord;


this?

Code: [Select]
SymbolTableRecord SymTblRec = Trans.GetObject(ObjId, OpenMode.ForWrite,false) as SymbolTableRecord;


edit ...DoH Sorry I should have read the post
« Last Edit: December 28, 2007, 11:11:06 AM by Daniel »

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Rewriting program - opinions?
« Reply #38 on: December 28, 2007, 11:07:37 AM »
T Willey I got your first program to work but seems to be a problem when using symboltablerecord.

Your program runs through and tells me its deleted the expected layers but the layers still exist.

It would seem this line is not doing what it should:

Code: [Select]
SymTblRec.Erase();
Using Express 2008 and Autocad 2008 - only learning  .Net for Cad right now so can't be much help.
I am a little confused on this piece here.
Code: [Select]
using (Transaction Trans = Db.TransactionManager.StartTransaction())
        {
            ObjectIdCollection PurgeIds = GetPurgableIds(Db, Db.BlockTableId );
            //ObjectIdCollection PurgeIds = GetPurgableIds(Db, Db.LayerTableId);
            int CountIds = PurgeIds.Count;
            foreach (ObjectId ObjId in PurgeIds)
            {
                SymbolTableRecord SymTblRec = Trans.GetObject(ObjId, OpenMode.ForWrite) as SymbolTableRecord;
                if (SymTblRec.IsDependent)
                    --CountIds;
                else
                {
                    Ed.WriteMessage("\n Deleted block: {0}", SymTblRec.Name);
                    SymTblRec.Erase();
                }
            }
            Ed.WriteMessage("\n {0} total number of layers deleted.", CountIds);
        }

from Acad
Command: PC2
 Deleted block: 3D-TTL-A


I swapped the LayerTableId for BlockTableId.  As I step through the code, I see the SymTblRec.Erase() work, yet when its done, the block definition is not purged.  Any ideas?
That is because I forgot to 'commit' the transaction.  Sorry.   :cry:  After the 'Ed.WriteMessage....' line add 'Trans.Commit()' and all should be fine again.   :-)
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Rewriting program - opinions?
« Reply #39 on: December 28, 2007, 11:11:46 AM »
>>>>>>>>>>>>>  add 'Trans.Commit()' and all should be fine again.   :-)

just came back to ask about that possibility :-)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Rewriting program - opinions?
« Reply #40 on: December 28, 2007, 11:14:56 AM »
DUH!!!!!!!!!!
I was so wrapped up in trying to switch to the BlockTable and LayerTable at same time, I didn't read all the way down, I just copied and pasted.
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Rewriting program - opinions?
« Reply #41 on: December 28, 2007, 11:15:53 AM »
That did it
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Rewriting program - opinions?
« Reply #42 on: December 28, 2007, 11:18:41 AM »
That did it
Good to hear!  :-)


ps.  BeanBag  Welcome to theSwamp.  :-)  I'm just starting with .Net also, and this is a great place to be to learn.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Rewriting program - opinions?
« Reply #43 on: December 28, 2007, 11:23:08 AM »
DUH!!!!!!!!!!
I was so wrapped up in trying to switch to the BlockTable and LayerTable at same time, I didn't read all the way down, I just copied and pasted.
On a side note, you might want to read the Arx doc's about the 'purge' method and the two ways in which it can be called.  It says that if you want to purge something that can reference something within the same table, ie. blocks, then you want to pass it something besides an ObjectIdCollection.

Quote from: Arx Docs
<snip>

purge(

AcDbObjectIdGraph& idGraph);

idGraph Input graph of objects in the database. The graph will be returned containing only those objects that may safely be removed from the database.

This version of the purge() method works in one pass. The method looks for references between the objects passed in so that it does not need to be called multiple times. In other words, if a Layer and a Linetype are passed in, and the only reference to the Linetype is from the Layer, then the graph returned will indicate that both the Layer and the Linetype can be purged. (The older AcDbObjectIdArray version of purge() would first indicate that only the Layer could be purged. Then a second call, after erasing the Layer, would say that the Linetype could be purged.)

<snip>
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.