string dgnLsDictName = "ACAD_DGNLINESTYLECOMP";// this distionary has been deleted
        public void DgnDeleting(List <string> allDwgPath)
        {
            HostMgdApp.Application ncadMgpApp = Marshal.GetActiveObject("nanocad.Application") as HostMgdApp.Application;
            foreach (string oneDwg in allDwgPath)
            {
                HostMgdApp.Document docMgb;
                try
                {
                    docMgb = HostMgdApp.Application.DocumentManager.Open(oneDwg, false);
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("Ошибка: " + ex.Message);
                    return;
                }

                Database db = docMgb.Database;

                ObjectIdCollection objIdColl = new ObjectIdCollection();
                bool dgnSt;
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    var nod = (DBDictionary)tr.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead);
                    if ((ObjectId)nod[dgnLsDictName] != ObjectId.Null)
                    {
                        dgnSt = true;
                        DBDictionary dgnLsDict = (DBDictionary)tr.GetObject((ObjectId)nod[dgnLsDictName], OpenMode.ForWrite);
                        dgnLsDict.Erase();
                        //dgnLsDict.Remove(dgnLsDictName);
                        tr.Commit();
                    }
                    else
                    {
                        dgnSt = false;
                        tr.Commit();
                    }
                }
                // был ли словарь
                if (dgnSt == false)
                {
                    docMgb.Dispose();
                    mcObj.McDocument mcDocClose = mcObj.McDocument.ActiveDocument;
                    mcDocClose.Close();
                    continue;
                }
                docMgb.Database.Save();
                docMgb.Dispose();

                mcObj.McDocument mcDoc = mcObj.McDocument.ActiveDocument;
                mcDoc.Save();
                mcDoc.Close();

                HostMgdApp.Document docMgbItem = HostMgdApp.Application.DocumentManager.Open(oneDwg, false);
                Database            dbItem     = docMgbItem.Database;

                ObjectIdCollection objIdCollItem = new ObjectIdCollection();
                using (Transaction trItem = dbItem.TransactionManager.StartTransaction())
                {
                    LinetypeTable LnTb;
                    LnTb = trItem.GetObject(dbItem.LinetypeTableId, OpenMode.ForRead) as LinetypeTable;

                    foreach (ObjectId oneObjId in LnTb)
                    {
                        objIdCollItem.Add(oneObjId);
                    }
                    dbItem.Purge(objIdCollItem);
                    trItem.Commit();
                }
                dbItem.Purge(objIdCollItem);

                using (Transaction t = dbItem.TransactionManager.StartTransaction())
                {
                    foreach (ObjectId ltypeId in objIdCollItem)
                    {
                        DBObject ltype = t.GetObject(ltypeId, OpenMode.ForWrite);
                        ltype.Erase();
                    }
                    t.Commit();
                }
                dbItem.ReclaimMemoryFromErasedObjects(objIdCollItem);
                docMgbItem.Database.Save();
                docMgbItem.Dispose();

                mcObj.McDocument mcDocItem = mcObj.McDocument.ActiveDocument;
                mcDocItem.Save();
                mcDocItem.Close();
            }
        }
예제 #2
0
        public static void Save(ref Document doc)
        {
            var ed = doc.Editor;

            ed.WriteMessage("CustomAutosaveRun");

            var    db      = doc.Database;
            var    hs      = HostApplicationServices.Current;
            string DocPath = hs.FindFile(doc.Name, db, FindFileHint.Default);

            if (string.IsNullOrEmpty(DocPath))
            {
                ed.WriteMessage("Документ: " + ((char)34).ToString() + doc.Name + ((char)34).ToString() + " не был ни разу сохранен. Автосохранение отменено.");
                return;
            }

            if (!File.Exists(DocPath))
            {
                ed.WriteMessage("Документ, путь: " + ((char)34).ToString() + DocPath + ((char)34).ToString() + " не прошел проверку. Автосохранение отменено.");
                return;
            }

            string FileNameToAutosave = GetFilenameToAutosave(DocPath, ref ed);

            if (string.IsNullOrEmpty(FileNameToAutosave))
            {
                return;
            }

            if (File.Exists(FileNameToAutosave))
            {
                ed.WriteMessage("Документ уже автосохранен: " + ((char)34).ToString() + FileNameToAutosave + ((char)34).ToString() + " не прошел проверку.");
                return;
            }

            if (Path.GetDirectoryName(FileNameToAutosave) == Path.GetDirectoryName(DocPath))
            {
                ed.WriteMessage("Документ из папки автосохранени: " + ((char)34).ToString() + doc.Name + ((char)34).ToString() + ". Автосохранение отменено.");
                return;
            }

            var fs = new DirectoryInfo(Path.GetDirectoryName(FileNameToAutosave)).GetFiles();

            var dt0 = DateTime.Now;

            foreach (var f in fs)
            {
                TimeSpan ts = dt0 - f.LastWriteTime;
                if (ts.Days > Autosave1.StorageTime)
                {
                    try
                    {
                        f.Delete();
                    }
                    catch
                    {
                        ed.WriteMessage("Нет удалось удалить файл: " + ((char)34).ToString() + f.FullName + ((char)34).ToString());
                    }
                }
            }

            db.SaveAs(FileNameToAutosave, DwgVersion.Current);

            ed.WriteMessage(FileNameToAutosave);
        }