using System;
using Autodesk.AutoCAD.Interop;
using System.Threading;
using System.IO;
namespace DxfToDwg
{
class Program
{
static public AcadApplication app;
static AcadDocument doc;
static bool isReady;
static string path = "";
const int sleep_time = 500;
static void Main(string[] args)
{
if (args.Length > 0)
{
path = args[0];
}
else
{
path = Environment.CurrentDirectory;
}
string[] files= Directory.GetFiles(path, "*.dxf");
Console.WriteLine("Начинается обработка файлов в папке: " + path);
foreach (string f in files)
{
OpenAndSave(f);
}
Console.WriteLine("Обработка завершена. Нажмите любую клавишу...");
Console.ReadKey();
}
private static void OpenAndSave(string fileName)
{
isReady = false;
app = new AcadApplication();
app.Visible = true;
Console.WriteLine("Открытие файла: " + fileName);
doc = app.Documents.Open(fileName, false); //открываем документ
while (!isReady) //ожидаем, пока приложение будет не занято
{
try
{
AcadState state = app.GetAcadState();
isReady = state.IsQuiescent;
}
catch (Exception ex) { Console.WriteLine(ex); }
Thread.Sleep(sleep_time);
}
string oldName = doc.Name;
string newName = oldName.Substring(0, oldName.LastIndexOf('.')) + @".dwg";
string pa = doc.Path + @"\";
doc.SaveAs(pa + newName);
Console.WriteLine("Сохранено: " + pa + newName);
doc.Close();
app.Quit();
}
}
}