Author Topic: Speed up code  (Read 5190 times)

0 Members and 1 Guest are viewing this topic.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Speed up code
« Reply #15 on: January 17, 2015, 10:42:37 PM »
try newDb.DisableUndoRecording(), see if it makes a difference

I tried and didn't see any difference.  Copying them in smaller does seems to work, though there seems that issue can arise, as in testing I could not do what they Arx doc's said to do, and sell the deferTranslation to true for all but the last WblockCloneObjects.  Maybe it is only allowed in Arx applications, as it is not listed in the managed docs.

Quote from: Arx docs AcDbDatabase::wblockCloneObjects
The wblockCloneObjects() method is then called once for each array, with the deferXlation argument set to true for all but the last call, so that ID translation will be done to complete the cloning.
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: Speed up code
« Reply #16 on: January 17, 2015, 11:21:03 PM »
Just to kind of complete the thread, I set my max count variable at 33000.  I tried it as low as 25000 which copied all the items in the drawing in about 16 seconds, if I remember correctly.

I left in the DisableUndoRecording to true until after I have copied in all the items, then set it back to false before saving.  I had this idea before, and I see not harm in leaving it in there.

Thanks again for all the help and ideas.
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: Speed up code
« Reply #17 on: January 17, 2015, 11:29:14 PM »

Hi Tim,
For anyone following along later, can you post the end result of your adventure ?


Regards,
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.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Speed up code
« Reply #18 on: January 18, 2015, 10:03:16 AM »

Hi Tim,
For anyone following along later, can you post the end result of your adventure ?


Regards,


I thought I had already?  I cannot post my full code, as it does not belong to me, but I can post again what my test code was that shows how I will use my newly gained information.

Code: [Select]
[ CommandMethod( "TestCopy", CommandFlags.Session ) ]
public void CopyObjects () {
    try {
        Document doc = AcadApp.DocumentManager.MdiActiveDocument;
        Database db = doc.Database;
        using ( DocumentLock dlock = doc.LockDocument() )
        using ( Transaction trans = doc.TransactionManager.StartTransaction() )
        using ( Database newDb = new Database( true, true ) )
        {
            PromptSelectionResult psr = doc.Editor.GetSelection();
            System.Diagnostics.Stopwatch watch = System.Diagnostics.Stopwatch.StartNew();
            ObjectIdCollection copyIds = new ObjectIdCollection( psr.Value.GetObjectIds() );
            ObjectIdCollection tempIds = new ObjectIdCollection();;
            Int32 cnt = 0;
            Int32 max = 33000;
            newDb.DisableUndoRecording( true );
            foreach ( ObjectId oid in copyIds ) {
                tempIds.Add( oid );
                cnt++;
                if ( cnt == max ) {
                    db.WblockCloneObjects( tempIds, aitrUtils.getTableItem( newDb.BlockTableId, "*Model_Space" ),
                                             new IdMapping(), DuplicateRecordCloning.Ignore, false );
                    cnt = 0;
                    tempIds.Clear();
                }
            }
            if ( cnt > 0 ) {
                db.WblockCloneObjects( tempIds, aitrUtils.getTableItem( newDb.BlockTableId, "*Model_Space" ),
                                         new IdMapping(), DuplicateRecordCloning.Ignore, false );
            }
            watch.Stop();
            newDb.DisableUndoRecording( false );
            MessageBox.Show( watch.Elapsed.ToString() );
            newDb.SaveAs( @"c:\test\testcopy.dwg", DwgVersion.Current );
        }
    }
    catch ( Autodesk.AutoCAD.Runtime.Exception ex ) { MessageBox.Show( ex.ToString(), "Autocad" ); }
    catch ( System.Exception ex ) { MessageBox.Show( ex.ToString(), "System" ); }
}
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: Speed up code
« Reply #19 on: January 18, 2015, 03:27:13 PM »
Thanks Tim
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.