using Autodesk.AutoCAD.ApplicationServices.Core;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace TestOpenClose.AcadCommands
{
public static class TestOpenCloseDwg
{
[CommandMethod("test1", CommandFlags.Session)]
public static void TestOpenCloseWithLog()
{
LogService log = new LogService(@"c:\net.log");
Database curDwgDatabase = Application.DocumentManager.MdiActiveDocument.Database;
List<string> fileNames = new List<string>(Directory.GetFiles(_folder, "*.dwg", SearchOption.AllDirectories));
foreach (string fileName in fileNames)
{
Database _fileDatabase = new Database(false, true);
using (WorkingDatabaseSwitcher dbSwitcher = new WorkingDatabaseSwitcher(_fileDatabase))
{
try
{
_fileDatabase.ReadDwgFile(fileName, FileShare.Read, false, "");
}
catch
{
string _copyFileName = Path.Combine(Path.GetTempPath(), Path.GetFileName(fileName));
File.Copy(fileName, _copyFileName, true);
_fileDatabase.ReadDwgFile(_copyFileName, FileShare.Read, false, "");
}
_fileDatabase.CloseInput(true);
using (Transaction trans = _fileDatabase.TransactionManager.StartTransaction())
{
BlockTable blockTable =
(BlockTable)trans.GetObject(_fileDatabase.BlockTableId, OpenMode.ForRead);
BlockTableRecord modelSpace =
(BlockTableRecord)trans.GetObject(blockTable[BlockTableRecord.ModelSpace],
OpenMode.ForRead);
log.OpenFileLog(fileName, modelSpace.OfType<ObjectId>().Count());
}
}
log.CloseFileLog(fileName);
}
}
[CommandMethod("test2", CommandFlags.Session)]
public static void TestOpenCloseWithLogNoModel()
{
LogService log = new LogService(@"c:\netNoModel.log");
Database curDwgDatabase = Application.DocumentManager.MdiActiveDocument.Database;
List<string> fileNames = new List<string>(Directory.GetFiles(_folder, "*.dwg", SearchOption.AllDirectories));
foreach (string fileName in fileNames)
{
Database _fileDatabase = new Database(false, true);
using (WorkingDatabaseSwitcher dbSwitcher = new WorkingDatabaseSwitcher(_fileDatabase))
{
try
{
_fileDatabase.ReadDwgFile(fileName, FileShare.Read, false, "");
}
catch
{
string _copyFileName = Path.Combine(Path.GetTempPath(), Path.GetFileName(fileName));
File.Copy(fileName, _copyFileName, true);
_fileDatabase.ReadDwgFile(_copyFileName, FileShare.Read, false, "");
}
log.OpenFileLog(fileName);
_fileDatabase.CloseInput(true);
}
log.CloseFileLog(fileName);
}
}
[CommandMethod("test3", CommandFlags.Session)]
public static void TestOpenCloseWithLogDiffDbases()
{
LogService log = new LogService(@"c:\net_using.log");
Database curDwgDatabase = Application.DocumentManager.MdiActiveDocument.Database;
List<string> fileNames = new List<string>(Directory.GetFiles(_folder, "*.dwg", SearchOption.AllDirectories));
foreach (string fileName in fileNames)
{
log.OpenFileLog(fileName);
using (Database db = new Database(false, true))
{
db.ReadDwgFile(fileName, FileShare.Read, false, "");
HostApplicationServices.WorkingDatabase = db;
}
log.CloseFileLog(fileName);
}
HostApplicationServices.WorkingDatabase = curDwgDatabase;
}
private static string _folder = @"C:\Autodesk\OpenClose";
}
public class LogService
{
public LogService(string LogFileName)
{
_logFile = LogFileName;
if (File.Exists(_logFile))
{
File.Delete(_logFile);
}
}
public void OpenFileLog(string FileName, int? ModelSpaceCount = null)
{
LogMessage(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "\tstart " + FileName + "\t" +
new FileInfo(FileName).Length + "\t" + (ModelSpaceCount == null
? ""
: $"\t{ModelSpaceCount}"));
}
public void CloseFileLog(string FileName)
{
LogMessage($"{DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")}\tclose {FileName}");
}
private void LogMessage(string Message)
{
using (StreamWriter sw = new StreamWriter(_logFile, true, Encoding.UTF8))
{
sw.WriteLine(Message);
}
}
private string _logFile;
}
internal sealed class WorkingDatabaseSwitcher : IDisposable
{
public WorkingDatabaseSwitcher(Database db)
{
_prevDb = HostApplicationServices.WorkingDatabase;
HostApplicationServices.WorkingDatabase = db;
}
private Database _prevDb = null;
public void Dispose()
{
HostApplicationServices.WorkingDatabase = _prevDb;
}
}
}