Author Topic: Work in more than one open document  (Read 2253 times)

0 Members and 1 Guest are viewing this topic.

DIA4020

  • Guest
Work in more than one open document
« on: October 01, 2011, 06:55:37 AM »
I have 2 open drawings and let the user show entities in the current drawing.
The program calculates new objects and must fill the resultate in the second drawing.

Is ist possible to write to the second drawing???
I used lock of target document but nevertheless
I always get "WRONG DATABASE". :oops:

What's the trick ?

Thanx :-D

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8690
  • AKA Daniel
Re: Work in more than one open document
« Reply #1 on: October 01, 2011, 08:14:28 AM »
get your transaction and database instances from the document, don't mix them between documents  :-)

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Work in more than one open document
« Reply #2 on: October 01, 2011, 09:28:17 AM »
Hi,

Here's a little example:

Code - C#: [Select]
  1. [CommandMethod("Test", CommandFlags.Session)]
  2. public void Test()
  3. {
  4.         DocumentCollection docMgr = AcAp.DocumentManager;
  5.         Document activeDoc = docMgr.MdiActiveDocument;
  6.         Database db = activeDoc.Database;
  7.         ObjectIdCollection idCol = new ObjectIdCollection();
  8.         try
  9.         {
  10.                 // Create a circle in the active document
  11.                 using (DocumentLock docLock = activeDoc.LockDocument())
  12.                 using (Transaction tr = db.TransactionManager.StartTransaction())
  13.                 {
  14.                         BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
  15.                         BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
  16.                         Circle circle = new Circle(new Point3d(300.0, 300.0, 0.0), Vector3d.ZAxis, 100.0);
  17.                         circle.SetDatabaseDefaults();
  18.                         ObjectId id = btr.AppendEntity(circle);
  19.                         idCol.Add(id);
  20.                         tr.AddNewlyCreatedDBObject(circle, true);
  21.                         tr.Commit();
  22.                 }
  23.  
  24.                 // Create a new document, clone the circle from the active document and change its radius and color
  25.                 Document newDoc = docMgr.Add("acadiso.dwt");
  26.                 using (DocumentLock docLock = newDoc.LockDocument())
  27.                 using (Transaction tr = newDoc.TransactionManager.StartTransaction())
  28.                 {
  29.                         IdMapping idMap = new IdMapping();
  30.                         BlockTable bt = (BlockTable)tr.GetObject(newDoc.Database.BlockTableId, OpenMode.ForRead);
  31.                         db.WblockCloneObjects(idCol, bt[BlockTableRecord.ModelSpace], idMap, DuplicateRecordCloning.Ignore, false);
  32.                         foreach (IdPair pair in idMap)
  33.                         {
  34.                                 Circle c = tr.GetObject(pair.Value, OpenMode.ForRead) as Circle;
  35.                                 if (c != null)
  36.                                 {
  37.                                         c.UpgradeOpen();
  38.                                         c.Radius = 200.0;
  39.                                         c.Color = Color.FromColorIndex(ColorMethod.ByAci, 1);
  40.                                 }
  41.                         }
  42.                         tr.Commit();
  43.                 }
  44.                 docMgr.MdiActiveDocument = activeDoc;
  45.         }
  46.         catch (Autodesk.AutoCAD.Runtime.Exception ex)
  47.         {
  48.                 AcAp.ShowAlertDialog(string.Format("{0}\n{1}", ex.Message, ex.StackTrace));
  49.         }
  50. }

edit->kdub code=csharp formatting
« Last Edit: July 30, 2012, 11:21:36 PM by Kerry »
Speaking English as a French Frog