9
« on: November 24, 2010, 03:00:09 PM »
Here is a simple VB & C# example of copying a layout from a external drawing,
There is no error handling or checking if the drawing, layout exist to keep it short.
This is pretty much the same as copying the PlotSettings or the "Page Setup"
You must have a drawing and layout named the same or change it.
A simple App example would let the user select a location or drawing where they setup all the different "Page Setups" from a context menu or something similiar. Then you could fill a listbox or whatever with the names on a palette, and on the click event bring in the copy the layout.
This example is hard-coded for simplicity
C#
[CommandMethod("CopyExternalLayout")]
public static void CopyExternalLayout()
{
Database db = HostApplicationServices.WorkingDatabase;
Database stdDb = new Database();
stdDb.ReadDwgFile(@"C:\Test\PageSetup.dwg", FileOpenMode.OpenForReadAndAllShare, false,"");
using (Transaction trx = db.TransactionManager.StartTransaction())
using (Transaction stdTrx = stdDb.TransactionManager.StartTransaction())
{
DBDictionary stdLayoutDictionary = stdDb.LayoutDictionaryId.GetObject(OpenMode.ForRead) as DBDictionary;
Layout stdLayout = stdLayoutDictionary.GetAt("TestPageSetUp").GetObject(OpenMode.ForRead) as Layout;
BlockTableRecord paperSpace = SymbolUtilityServices.GetBlockPaperSpaceId(db).GetObject(OpenMode.ForRead) as BlockTableRecord;
Layout layout = paperSpace.LayoutId.GetObject(OpenMode.ForWrite) as Layout;
layout.CopyFrom(stdLayout);
trx.Commit();
}
}
VB
<CommandMethod("CopyExternalLayout")> _
Public Shared Sub CopyExternalLayout()
Dim db As Database = HostApplicationServices.WorkingDatabase
Dim stdDb As Database = New Database()
stdDb.ReadDwgFile("C:\Test\PageSetup.dwg", FileOpenMode.OpenForReadAndAllShare, False, "")
Using trx As Transaction = db.TransactionManager.StartTransaction()
Using stdTrx As Transaction = stdDb.TransactionManager.StartTransaction()
Dim stdLayoutDictionary As DBDictionary = stdDb.LayoutDictionaryId.GetObject(OpenMode.ForRead)
Dim stdLayout As Layout = stdLayoutDictionary.GetAt("TestPageSetUp").GetObject(OpenMode.ForRead)
Dim paperspace As BlockTableRecord = SymbolUtilityServices.GetBlockPaperSpaceId(db).GetObject(OpenMode.ForRead)
Dim layout As Layout = paperspace.LayoutId.GetObject(OpenMode.ForWrite)
layout.CopyFrom(stdLayout)
End Using
trx.Commit()
End Using
End Sub