Author Topic: Reason For Extra Viewport  (Read 1628 times)

0 Members and 1 Guest are viewing this topic.

GumbyCAD

  • Newt
  • Posts: 84
Reason For Extra Viewport
« on: May 23, 2014, 04:14:26 AM »
Hi Team,

I was wonderinf if anyone had tryed the attached Url Sample code.

http://adndevblog.typepad.com/autocad/2012/07/using-wblockcloneobjects-to-copy-a-layout-from-the-active-document-to-a-new-drawing.html

Code - Visual Basic: [Select]
  1. <CommandMethod("copyLayout")> _
  2.     Sub copyLayoutToNewDwg()
  3.       Dim layout As New Layout
  4.       Dim layoutNameInCurDwg As String = "Layout1"
  5.       Dim lytMgr As LayoutManager
  6.       Dim ed As Editor =
  7.         Application.DocumentManager.MdiActiveDocument.Editor()
  8.       Dim layoutId As ObjectId
  9.       'Get orignal drawing
  10.      Dim db As Database =
  11.         Application.DocumentManager.MdiActiveDocument.Database
  12.       'create a new drawing
  13.      Dim newDb As Database = New Database(True, False)
  14.       Using tr As Transaction = newDb.TransactionManager.StartTransaction()
  15.         'Make the working database the new database
  16.        HostApplicationServices.WorkingDatabase = newDb
  17.         ' Create a new Layout
  18.        Dim newLytMgr As LayoutManager = LayoutManager.Current()
  19.         Dim newLayoutId As ObjectId = newLytMgr.CreateLayout("newLayout")
  20.         Dim newLayout As Layout = tr.GetObject(newLayoutId, OpenMode.ForWrite)
  21.         'Make the original database the working database
  22.        HostApplicationServices.WorkingDatabase = db
  23.         Using tr2 As Transaction = db.TransactionManager.StartTransaction()
  24.           ' Get the dictionary of the original database
  25.          Dim lytDict As DBDictionary =
  26.             tr2.GetObject(db.LayoutDictionaryId, OpenMode.ForRead)
  27.           'Make sure the layout existes in the original database
  28.          If Not lytDict.Contains(layoutNameInCurDwg) Then
  29.             ed.WriteMessage("Layout named ""Layout1"" does not exist in current dwg")
  30.             Return
  31.           End If
  32.           'Get the layout in the original database
  33.          lytMgr = LayoutManager.Current()
  34.           layoutId = lytMgr.GetLayoutId(layoutNameInCurDwg)
  35.           layout = tr2.GetObject(layoutId, OpenMode.ForRead)
  36.           newLayout.CopyFrom(layout)
  37.           'Get the block table record of the existing layout
  38.          Dim blkTableRec As BlockTableRecord
  39.           blkTableRec =
  40.             tr2.GetObject(layout.BlockTableRecordId, OpenMode.ForRead)
  41.           'Get the object ids of the objects in the existing block table record
  42.          Dim objIdCol As New ObjectIdCollection()
  43.           For Each objId As ObjectId In blkTableRec
  44.             objIdCol.Add(objId)
  45.           Next
  46.           ' Clone the objects to the new layout
  47.          Dim idMap As IdMapping = New IdMapping()
  48.           newDb.WblockCloneObjects(objIdCol,
  49.                                    newLayout.BlockTableRecordId,
  50.                                    idMap,
  51.                                    DuplicateRecordCloning.MangleName,
  52.                                    False)
  53.           tr2.Commit()
  54.         End Using
  55.         tr.Commit()
  56.         newDb.SaveAs("c:\newLayout.dwg", DwgVersion.Newest)
  57.       End Using
  58.     End Sub
  59.  

Code - C: [Select]
  1. [CommandMethod("copyLayout")]
  2. public void copyLayoutToNewDwg()
  3. {
  4.         Layout layout = new Layout();
  5.         string layoutNameInCurDwg = "Layout1";
  6.         LayoutManager lytMgr = default(LayoutManager);
  7.         Editor ed = Application.DocumentManager.MdiActiveDocument.Editor();
  8.         ObjectId layoutId = default(ObjectId);
  9.         //Get orignal drawing
  10.         Database db = Application.DocumentManager.MdiActiveDocument.Database;
  11.         //create a new drawing
  12.         Database newDb = new Database(true, false);
  13.         using (Transaction tr = newDb.TransactionManager.StartTransaction()) {
  14.                 //Make the working database the new database
  15.                 HostApplicationServices.WorkingDatabase = newDb;
  16.                 // Create a new Layout
  17.                 LayoutManager newLytMgr = LayoutManager.Current();
  18.                 ObjectId newLayoutId = newLytMgr.CreateLayout("newLayout");
  19.                 Layout newLayout = tr.GetObject(newLayoutId, OpenMode.ForWrite);
  20.                 //Make the original database the working database
  21.                 HostApplicationServices.WorkingDatabase = db;
  22.                 using (Transaction tr2 = db.TransactionManager.StartTransaction()) {
  23.                         // Get the dictionary of the original database
  24.                         DBDictionary lytDict = tr2.GetObject(db.LayoutDictionaryId, OpenMode.ForRead);
  25.                         //Make sure the layout existes in the original database
  26.                         if (!lytDict.Contains(layoutNameInCurDwg)) {
  27.                                 ed.WriteMessage("Layout named \"Layout1\" does not exist in current dwg");
  28.                                 return;
  29.                         }
  30.                         //Get the layout in the original database
  31.                         lytMgr = LayoutManager.Current();
  32.                         layoutId = lytMgr.GetLayoutId(layoutNameInCurDwg);
  33.                         layout = tr2.GetObject(layoutId, OpenMode.ForRead);
  34.                         newLayout.CopyFrom(layout);
  35.                         //Get the block table record of the existing layout
  36.                         BlockTableRecord blkTableRec = default(BlockTableRecord);
  37.                         blkTableRec = tr2.GetObject(layout.BlockTableRecordId, OpenMode.ForRead);
  38.                         //Get the object ids of the objects in the existing block table record
  39.                         ObjectIdCollection objIdCol = new ObjectIdCollection();
  40.                         foreach (ObjectId objId in blkTableRec) {
  41.                                 objIdCol.Add(objId);
  42.                         }
  43.                         // Clone the objects to the new layout
  44.                         IdMapping idMap = new IdMapping();
  45.                         newDb.WblockCloneObjects(objIdCol, newLayout.BlockTableRecordId, idMap, DuplicateRecordCloning.MangleName, false);
  46.                         tr2.Commit();
  47.                 }
  48.                 tr.Commit();
  49.                 newDb.SaveAs("c:\\newLayout.dwg", DwgVersion.Newest);
  50.         }
  51. }
  52.  
  53.  

The Code copies the "A Layouts" from the current Drawing to an external Drawing.

Works wonderfully but as you can see in the comments (on the page) and I have experienced first hand you get an extra viewport around what you export.

Can anyone tell me why this is happening or an idea on how to combat this?

Thanks in advance Stephan

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Reason For Extra Viewport
« Reply #1 on: May 26, 2014, 09:38:24 PM »
One idea is that the first item in a paperspace layout is a viewport.  If you copy all the objects, then the main paperspace viewport gets copied, which you do not need, as the new layout will have its own main viewport.  I would skip the first item in the layout to be copied, and I think it should solve your problem.  If you want to copy the modelspace layout, then you would not skip the first object.
Tim

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

Please think about donating if this post helped you.