public static void ExportAllLayouts()
{
System.Windows.Forms.OpenFileDialog dia
= new System.Windows.Forms.OpenFileDialog(); dia.Filter = "dwg Files (*.dwg)|*.dwg|All files (*.*)|*.*";
dia.Multiselect = true;
if (dia.ShowDialog() != DialogResult.OK) return;
string fileName = dia.FileNames[0];
FileInfo fi
= new FileInfo
(fileName
); string othersDir = System.IO.Directory.GetParent(fi.DirectoryName) + @"\Others";
if (!System.IO.Directory.Exists(othersDir)) System.IO.Directory.CreateDirectory(othersDir);
for (int i = 0; i < dia.FileNames.Length; i++)
{
string oldFileFullName = dia.FileNames[i];
string forOthersFileFullName = othersDir + "\\" + dia.SafeFileNames[i];
if (GetAllPaperSpaceLayouts(oldFileFullName).Count != 1)
{
Arx.Prompts.Editor.WriteMessage(oldFileFullName + "has more than one layout!!!");
continue;
}
ExportLayout(oldFileFullName, forOthersFileFullName);
}
}
public static void ExportLayout(string oldFileName, string newFileName)
{
// sometimes error occurred here,"Error Decrypting Data"
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.Open(oldFileName, true);
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument = doc;
//System.Threading.Thread.Sleep(3000);
var db = doc.Database;
using (doc.LockDocument())
{
ObjectId layoutId
= new ObjectId
(); using (Transaction tr = db.TransactionManager.StartTransaction())
{
LayoutManager lm = LayoutManager.Current;
// ACAD_LAYOUT dictionary.
DBDictionary layoutDict = tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead) as DBDictionary;
// Iterate dictionary entries.
foreach (DBDictionaryEntry de in layoutDict)
{
string layoutName = de.Key;
if (layoutName != "Model")
{
layoutId = lm.GetLayoutId(layoutName);
lm.CurrentLayout = layoutName;
break;
}
}
tr.Commit();
}
Autodesk.AutoCAD.ExportLayout.Engine engine = Autodesk.AutoCAD.ExportLayout.Engine.Instance();
//sometimes carash here, ePermanentlyErased error occurred
using (Database Outdb = engine.ExportLayout(layoutId))
{
if (engine.EngineStatus != AcExportLayout.ErrorStatus.Succeeded)
{
Arx.Prompts.Editor.WriteMessage("\nExportLayout failed: ", engine.EngineStatus.ToString());
return;
}
else
{
Outdb.SaveAs(newFileName, DwgVersion.AC1800);
}
}
}
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.CloseAndDiscard();
//System.Threading.Thread.Sleep(3000);
}