Author Topic: Question: Delete a named layout from Database  (Read 10257 times)

0 Members and 1 Guest are viewing this topic.

Glenn R

  • Water Moccasin
  • Posts: 1932
  • What idiot child of married cousins wrote this?!
Re: Question: Delete a named layout from Database
« Reply #15 on: January 28, 2008, 09:14:36 AM »
You're welcome Dan.

You could still change this:

Code: [Select]
AcDbObjectPointer<AcDbDictionary> pDictionary(pDb->layoutDictionaryId(),AcDb::kForRead);

to this though:

Code: [Select]
AcDbDictionaryPointer pDictionary(pDb->layoutDictionaryId(),AcDb::kForRead);

As I mentioned previously, AcDbDictionaryPointer is already defined for you...just a thought.

Cheers,
Glenn.
Me

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6963
  • AKA Daniel
Re: Question: Delete a named layout from Database
« Reply #16 on: January 28, 2008, 09:18:46 AM »
Doh! You did say it was already defined
Edited code.

Thanks Again

Glenn R

  • Water Moccasin
  • Posts: 1932
  • What idiot child of married cousins wrote this?!
Re: Question: Delete a named layout from Database
« Reply #17 on: January 28, 2008, 09:25:50 AM »
Dan,

Have you tried just deleting the layout itself and NOT iterating over all the entities in the layout and erasing each one first? I would have thought just deleting the layout would do the trick...

Cheers,
Glenn.
Me

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6963
  • AKA Daniel
Re: Question: Delete a named layout from Database
« Reply #18 on: January 28, 2008, 09:37:48 AM »
Well…Um…No…I just assumed…I’ll need to test this  :-o

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6963
  • AKA Daniel
Re: Question: Delete a named layout from Database
« Reply #19 on: January 28, 2008, 09:56:07 AM »
new code  :lol:

Code: [Select]
  static Acad::ErrorStatus deletelayout(const CString &FileName , const CString &LayoutName)
  {
    Acad::ErrorStatus es;
    AcDbObjectId LayoutId = AcDbObjectId::kNull;
    AcDbBlockTableRecordIterator *pBlkTblRcdItr = NULL;

    //a Smart Pointer to the database
    std::auto_ptr<AcDbDatabase> pDb(new AcDbDatabase(Adesk::kFalse));
    es = pDb->readDwgFile(FileName);
    if(es != Acad::eOk)
      return es;

    //Get the layout Dictionary from the Database
    AcDbDictionaryPointer pDictionary(pDb->layoutDictionaryId(),AcDb::kForRead);
    if(pDictionary.openStatus() != Acad::eOk)
      return es;

    //Get the Layout
    es = pDictionary->getAt(LayoutName, LayoutId);
    if(es != Acad::eOk)
      return es;

    AcDbObjectPointer<AcDbLayout> pLayout(LayoutId, AcDb::kForWrite);
    if(pLayout.openStatus() != Acad::eOk)
      return es;

    //erase the layout
    es = pLayout->erase();

    //closeup
    es = pDb->saveAs(FileName);
    return es;
  }

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6963
  • AKA Daniel
Re: Question: Delete a named layout from Database
« Reply #20 on: January 28, 2008, 09:59:31 AM »
Dan,

Have you tried just deleting the layout itself and NOT iterating over all the entities in the layout and erasing each one first? I would have thought just deleting the layout would do the trick...

Cheers,
Glenn.

You were right, just delete the layout.  :kewl:

Glenn R

  • Water Moccasin
  • Posts: 1932
  • What idiot child of married cousins wrote this?!
Re: Question: Delete a named layout from Database
« Reply #21 on: January 28, 2008, 10:05:57 AM »
Try this:

Code: [Select]
  static Acad::ErrorStatus deletelayout(const CString &FileName , const CString &LayoutName)
  {
    Acad::ErrorStatus es;
    AcDbObjectId LayoutId = AcDbObjectId::kNull;

    //a Smart Pointer to the database
    std::auto_ptr<AcDbDatabase> pDb(new AcDbDatabase(Adesk::kFalse));
    es = pDb->readDwgFile(FileName);
    if(es != Acad::eOk)
      return es;

    //Get the layout Dictionary from the Database
    AcDbDictionaryPointer pDictionary(pDb->layoutDictionaryId(),AcDb::kForRead);
    if(pDictionary.openStatus() != Acad::eOk)
      return es;

    //Get the Layout
    es = pDictionary->getAt(LayoutName, LayoutId);
    if(es != Acad::eOk)
      return es;

    es = pDictionary->remove(LayoutId); // remove the layout
    if (es != Acad::eOk)
       acutPrintf("\nError: Unable to remove layout. Error code = %s.", acadErrorStatusText(es));

    //closeup
    es = pDb->saveAs(FileName);
    return es;
  }

Cheers,
Glenn.
Me

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6963
  • AKA Daniel
Re: Question: Delete a named layout from Database
« Reply #22 on: January 28, 2008, 10:20:54 AM »
Hmm got this

Glenn R

  • Water Moccasin
  • Posts: 1932
  • What idiot child of married cousins wrote this?!
Re: Question: Delete a named layout from Database
« Reply #23 on: January 28, 2008, 10:27:36 AM »
That code I posted was done in Visual Notepad (read not tested at all), so maybe you do need to cast the dictionary entry to a layout and erase it, rather than just blatantly deleting the dictionary entry...was just a thought.

My suggestion/modification was taken directly from some old code of mine which was just blatantly removing the layout from the dictionary as I showed...

Cheers,
Glenn.
Me

Glenn R

  • Water Moccasin
  • Posts: 1932
  • What idiot child of married cousins wrote this?!
Re: Question: Delete a named layout from Database
« Reply #24 on: January 28, 2008, 10:32:05 AM »
Dan,

Try changing the open status of pDictionary from AcDb::kForRead to AcDb::kForWrite or just before the 'remove', use upgradeOpen adn see how that goes...

Cheers,
Glenn.
Me

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6963
  • AKA Daniel
Re: Question: Delete a named layout from Database
« Reply #25 on: January 28, 2008, 10:51:20 AM »
Dan,

Try changing the open status of pDictionary from AcDb::kForRead to AcDb::kForWrite or just before the 'remove', use upgradeOpen adn see how that goes...

Cheers,
Glenn.

I had changed that., I wonder if Erase() does some internal mojo such as removing all the objects that the layout owns..

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6963
  • AKA Daniel
Re: Question: Delete a named layout from Database
« Reply #26 on: January 28, 2008, 10:51:57 AM »

Glenn R

  • Water Moccasin
  • Posts: 1932
  • What idiot child of married cousins wrote this?!
Re: Question: Delete a named layout from Database
« Reply #27 on: January 28, 2008, 10:55:23 AM »
Dan,

Try changing the open status of pDictionary from AcDb::kForRead to AcDb::kForWrite or just before the 'remove', use upgradeOpen adn see how that goes...

Cheers,
Glenn.

I had changed that., I wonder if Erase() does some internal mojo such as removing all the objects that the layout owns..

Possibly...The old fucntion I mentioned was working on a drawing that was currently loaded into the editor, so maybe opening the dbase in memory as you've done makes a difference...small difference at the end of the day the extra cast to a layout and open...

Cheers,
Glenn.
Me

Dream.Fei

  • Guest
Re: Question: Delete a named layout from Database
« Reply #28 on: January 29, 2008, 01:11:32 AM »
Try this:

Code: [Select]
  static Acad::ErrorStatus deletelayout(const CString &FileName , const CString &LayoutName)
  {
    Acad::ErrorStatus es;
    .......
  }

Cheers,
Glenn.
If didn't erase all entity on this layout?
« Last Edit: January 29, 2008, 01:16:56 AM by Dream.Fei »

MickD

  • Gator
  • Posts: 3314
  • (x-in)->[process]->(y-out)
Re: Question: Delete a named layout from Database
« Reply #29 on: January 29, 2008, 06:14:30 AM »
... I wonder if Erase() does some internal mojo such as removing all the objects that the layout owns..

As the layout is an AcDbObject and it's another block table record (i.e. it holds all of its own ent data), 'erase' should do the trick. IOW, is you set the erase flag to true, all the ent's int the layout btr should also be flagged and therefore deleted (not saved) on the save.
I don't know if it would make much difference but setting modelspace as current after the erase may make a difference (I'd like to think that if the layout didn't exist it would open in mspace or one that existed at least anyway).
Forth is like the Tao: it is a Way, and is realized when followed.
Its fragility is its strength; its simplicity is its direction - Michael Ham

Lao Tzu: “To attain knowledge, add things
every day; to obtain wisdom, remove things every day.”