2
« on: February 06, 2012, 07:56:38 AM »
hi!
thank you for your reply and ideas, maybe i have to say a little bit more.
i want to include the functionality into a summary of different conversions. so i dont know it its possible to open drawings during an other active command.
my first idea was to iterrate through the layouts delete each except de modelspace and one layout and then saveas. but this is not possible, because the transaction is still opened and then i cannot save the database.
here is my last try, but it works not properly enough. the layouts are exported thats fine, but if i open the drawings i have errors in it and i need a recover.
this errors are in each drawing except the one with the first layout from the original.
so the first layouts is exported correctly, the drawing has no errors and works fine, but the rest needs a recover...
the next questions is, if it is good to use the Wblock Function to get a temporary Database. i'll loose the template settings from the original or?
maybe anybody has another good idea.
[UPDATE] i got it that the files are opened correctly without errors, but i still have 2 layouts in each new drawing. the one which should be there and a new layout named Layout1 with the content of the original layout with tabindex 1. i deleted it in the code, but it remains in the drawing ???
should i set the taborder property of the layout which should remain in the drawing to 1?
public void ExportLayouts(Document acDwg)
DocumentLock acDocLock = null;
int i = 0;
Database acDB = null;
Transaction acTrans = null;
BlockTable acBT = null;
BlockTableRecord acBTR = null;
SymbolTableEnumerator acBTRE = null;
Layout acLayout = null;
Layout acLayoutToExport = null;
List<String> liExportedLayouts = new List<string>();
FileInfo fiDrawing = new FileInfo(mydwg.Name);
try
{
//acDwg = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
acDocLock = acDwg.LockDocument();
acDB = HostApplicationServices.WorkingDatabase;
acBT = (BlockTable)(acDB.BlockTableId.GetObject(OpenMode.ForWrite));
acBTRE = acBT.GetEnumerator();
while (acBTRE.MoveNext())
{
if (acBTRE.Current.IsErased == false)
{
acBTR = (BlockTableRecord)(acBTRE.Current.GetObject(OpenMode.ForWrite));
if (acBTR.IsLayout)
{
acLayout = (Layout)(acBTR.LayoutId.GetObject(OpenMode.ForWrite));
if (acLayout.ModelType == false)
{
i += 1;
}
}
}
}
LayoutManager.Current.CurrentLayout = "Model";
for (int j = 1; j <= i; j++)
{
Database acTmpDatabase = new Database(true, false);
acLayoutToExport = null;
acTmpDatabase = acDB.Wblock();
acTrans = acTmpDatabase.TransactionManager.StartTransaction();
using (acTrans)
{
acBT = (BlockTable)(acTrans.GetObject(acTmpDatabase.BlockTableId, OpenMode.ForRead));
acBTRE = acBT.GetEnumerator();
while (acBTRE.MoveNext())
{
if (acBTRE.Current.IsErased == false)
{
acBTR = (BlockTableRecord)(acTrans.GetObject(acBTRE.Current, OpenMode.ForRead));
if (acBTR.IsLayout)
{
acLayout = (Layout)(acTrans.GetObject(acBTR.LayoutId, OpenMode.ForWrite));
if (acLayout.ModelType == false)
{
if (acLayoutToExport == null)
{
if (liExportedLayouts.Contains(acLayout.LayoutName) == false)
{
acLayoutToExport = acLayout;
liExportedLayouts.Add(acLayout.LayoutName);
acLayout.DowngradeOpen();
continue;
}
else
acLayout.Erase();
}
else
acLayout.Erase();
}
}
}
}
acTrans.Commit();
}
acTmpDatabase.CloseInput(true);
if (acLayoutToExport != null)
{
if (File.Exists(fiDrawing.Directory.FullName + "\\" + acLayoutToExport.LayoutName + fiDrawing.Extension) == false)
acTmpDatabase.SaveAs(fiDrawing.Directory.FullName + "\\" + acLayoutToExport.LayoutName + fiDrawing.Extension, DwgVersion.AC1024);
else
acTmpDatabase.SaveAs(fiDrawing.Directory.FullName + "\\" + acLayoutToExport.LayoutName + sTimeStamp + fiDrawing.Extension, DwgVersion.AC1024);
}
else
{
acTmpDatabase.SaveAs(fiDrawing.Directory.FullName + "\\" + Path.GetFileNameWithoutExtension(fiDrawing.FullName) + fiDrawing.Extension, DwgVersion.AC1024);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "\n" + ex.Stacktrace);
}
finally
{
if (acDocLock != null)
{
acDocLock.Dispose();
}
}
}