Exemplo n.º 1
1
        /// <summary>
        /// 删除指定名称的图层
        /// </summary>
        /// <param name="db">数据库对象</param>
        /// <param name="layerName">图层名</param>
        /// <returns>如果删除成功,则返回true,否则返回false</returns>
        public static bool DeleteLayer(this Database db, string layerName)
        {
            //打开层表
            LayerTable lt = (LayerTable)db.LayerTableId.GetObject(OpenMode.ForRead);

            //如果层名为0或Defpoints,则返回(这两个图层不能删除)
            if (layerName == "0" || layerName == "Defpoints")
            {
                return(false);
            }
            //如果不存在名为layerName的图层,则返回
            if (!lt.Has(layerName))
            {
                return(false);
            }
            ObjectId layerId = lt[layerName];//获取名为layerName的层表记录的Id

            //如果要删除的图层为当前层,则返回(不能删除当前层)
            if (layerId == db.Clayer)
            {
                return(false);
            }
            //打开名为layerName的层表记录
            LayerTableRecord ltr = (LayerTableRecord)layerId.GetObject(OpenMode.ForRead);

            //如果要删除的图层包含对象或依赖外部参照,则返回(不能删除这些层)
            lt.GenerateUsageData();
            if (ltr.IsUsed)
            {
                return(false);
            }
            ltr.UpgradeOpen(); //切换层表记录为写的状态
            ltr.Erase(true);   //删除名为layerName的图层
            return(true);      //删除图层成功
        }
Exemplo n.º 2
0
        private void LayerOnAndThawRegex(List <string> layerNames)
        {
            var  doc        = _AcAp.Application.DocumentManager.MdiActiveDocument;
            var  db         = doc.Database;
            bool needsRegen = false;

            using (Transaction trans = doc.TransactionManager.StartTransaction())
            {
                try
                {
                    LayerTable layTb             = trans.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                    List <LayerTableRecord> ltrs = new List <LayerTableRecord>();
                    foreach (var ltrOid in layTb)
                    {
                        LayerTableRecord ltr = (LayerTableRecord)trans.GetObject(ltrOid, OpenMode.ForRead);
                        string           ok  = layerNames.FirstOrDefault(x => Regex.IsMatch(ltr.Name, x, RegexOptions.IgnoreCase));
                        if (!string.IsNullOrEmpty(ok))
                        {
                            ltrs.Add(ltr);
                        }
                    }

                    foreach (var ltr in ltrs)
                    {
                        log.InfoFormat("Taue und schalte Layer {0} ein.", ltr.Name);
                        ltr.UpgradeOpen();
                        ltr.IsOff = false;
                        if (string.Compare(_AcAp.Application.GetSystemVariable("CLAYER").ToString(), ltr.Name, StringComparison.OrdinalIgnoreCase) != 0)
                        {
                            if (ltr.IsFrozen)
                            {
                                needsRegen = true;
                            }
                            ltr.IsFrozen = false;
                        }
                    }
                }
                finally
                {
                    trans.Commit();
                    if (needsRegen)
                    {
                        doc.Editor.Regen();
                    }
                }
            }
        }
Exemplo n.º 3
0
        public static void SetLayerIsoff(string layerName, bool isOff, Database db)
        {
            ObjectId layerId = GetLayerId(layerName);

            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                if (layerId != ObjectId.Null)
                {
                    LayerTableRecord layTabRec = trans.GetObject(layerId, OpenMode.ForRead) as LayerTableRecord;
                    if (layTabRec != null)
                    {
                        layTabRec.UpgradeOpen();
                        layTabRec.IsOff = isOff;
                    }
                }
                trans.Commit();
            }
        }
 private static void SetStandartToLayer(Database db, LayerTableRecord stdLayer, Transaction tr, string activeLayerName, string stdLayerName, LayerTable layerTable)
 {
     //  var stdDatabase = new Database(false, true);
     // stdDatabase.ReadDwgFile(@"Z:\AutoCAD_server\ShareSettings\КР-сб\КР-сб.dws", FileShare.Read, true, "");
     foreach (var layer in layerTable)
     {
         LayerTableRecord ltr = (LayerTableRecord)tr.GetObject(layer, OpenMode.ForWrite);
         if (!ltr.Name.Equals(activeLayerName))
         {
             continue;
         }
         ltr.IsLocked = false;
         ltr.UpgradeOpen();
         if (activeLayerName == stdLayerName)
         {
             SetLayerProperties(stdLayer, ltr, db, tr);
         }
         else if (!layerTable.Has(stdLayerName))
         {
             ltr.Name = stdLayerName;
             SetLayerProperties(stdLayer, ltr, db, tr);
         }
         else
         {
             bool isLock = false;
             foreach (var layerStd in layerTable)
             {
                 LayerTableRecord ltrstd = (LayerTableRecord)tr.GetObject(layerStd, OpenMode.ForWrite);
                 if (!ltrstd.Name.Equals(stdLayerName))
                 {
                     continue;
                 }
                 isLock          = ltrstd.IsLocked;
                 ltrstd.IsLocked = false;
                 SetLayerProperties(stdLayer, ltrstd, db, tr);
                 break;
             }
             SetLayerToEntity(db, activeLayerName, stdLayerName);
             ltr.IsLocked = isLock;
             DeleteLayer(activeLayerName, db);
         }
     }
 }
Exemplo n.º 5
0
        private void UnlockLayer()
        {
            Database db = HostApplicationServices.WorkingDatabase;

            using (var tr = db.TransactionManager.StartOpenCloseTransaction())
            {
                if (layerId == ObjectId.Null)
                {
                    if (string.IsNullOrWhiteSpace(this.layerName))
                    {
                        throw new ArgumentNullException("layerName");
                    }

                    LayerTable lt = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead);
                    if (!lt.Has(this.layerName))
                    {
                        throw new ArgumentException("Нет слоя с указанным именем: " + this.layerName);
                    }

                    this.layerId = lt[this.layerName];
                }

                LayerTableRecord ltr = tr.GetObject(this.layerId, OpenMode.ForRead, true) as LayerTableRecord;
                if (ltr == null)
                {
                    throw new ArgumentException("Указанный ObjectId не соответствует слою");
                }

                this.isLayerLocked = ltr.IsLocked;
                this.isLayerFrozen = ltr.IsFrozen;

                if (this.isLayerFrozen || this.isLayerLocked)
                {
                    ltr.UpgradeOpen();
                    ltr.IsLocked = false;
                    if (db.Clayer != ltr.ObjectId)
                    {
                        ltr.IsFrozen = false;
                    }
                }
                tr.Commit();
            }
        }
Exemplo n.º 6
0
        private void LayerOnAndThaw(List <string> layerNames)
        {
            var  doc        = _AcAp.Application.DocumentManager.MdiActiveDocument;
            var  db         = doc.Database;
            bool needsRegen = false;

            using (Transaction trans = doc.TransactionManager.StartTransaction())
            {
                try
                {
                    LayerTable layTb = trans.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;

                    foreach (var layerName in layerNames)
                    {
                        if (!layTb.Has(layerName))
                        {
                            continue;
                        }
                        var layId            = layTb[layerName];
                        LayerTableRecord ltr = trans.GetObject(layId, OpenMode.ForRead) as LayerTableRecord;
                        log.InfoFormat("Taue und schalte Layer {0} ein.", ltr.Name);
                        ltr.UpgradeOpen();
                        ltr.IsOff = false;
                        if (string.Compare(_AcAp.Application.GetSystemVariable("CLAYER").ToString(), ltr.Name, StringComparison.OrdinalIgnoreCase) != 0)
                        {
                            if (ltr.IsFrozen)
                            {
                                needsRegen = true;
                            }
                            ltr.IsFrozen = false;
                        }
                    }
                }
                finally
                {
                    trans.Commit();
                    if (needsRegen)
                    {
                        doc.Editor.Regen();
                    }
                }
            }
        }
Exemplo n.º 7
0
        private void DeleteLayersWithPrefix(string prefix, Transaction tr)
        {
            // Open the Layer table for read
            var lyrTbl = (LayerTable)tr.GetObject(Doc.Database.LayerTableId, OpenMode.ForRead);

            foreach (ObjectId layerId in lyrTbl)
            {
                LayerTableRecord layer     = (LayerTableRecord)tr.GetObject(layerId, OpenMode.ForRead);
                string           layerName = layer.Name;
                if (layerName.StartsWith(prefix))
                {
                    layer.UpgradeOpen();

                    // cannot delete current layer: swap current layer to default layer "0" if current layer is to be deleted
                    if (Doc.Database.Clayer == layerId)
                    {
                        var defaultLayerID = lyrTbl["0"];
                        Doc.Database.Clayer = defaultLayerID;
                    }
                    layer.IsLocked = false;

                    // delete all objects on this layer
                    // TODO: this is ugly! is there a better way to delete layer objs instead of looping through each one?
                    var bt = (BlockTable)tr.GetObject(Doc.Database.BlockTableId, OpenMode.ForRead);
                    foreach (var btId in bt)
                    {
                        var block = (BlockTableRecord)tr.GetObject(btId, OpenMode.ForRead);
                        foreach (var entId in block)
                        {
                            var ent = (Entity)tr.GetObject(entId, OpenMode.ForRead);
                            if (ent.Layer == layerName)
                            {
                                ent.UpgradeOpen();
                                ent.Erase();
                            }
                        }
                    }

                    layer.Erase();
                }
            }
        }
Exemplo n.º 8
0
        /// <summary>
        ///
        ///
        /// </summary>
        /// <param name="layerId">图层的ObjectID</param>
        /// <param name="isOpenOrClose">false:打开图层, true: 关闭图层</param>
        /// <param name="setClayer">true: 设置为可以编辑的图层</param>
        public static void OpenOrCloseLayer(ObjectId layerId, bool isOpenOrClose)
        {
            using (DocumentLock docLock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument())
            {
                Document doc = Application.DocumentManager.MdiActiveDocument;
                Database db  = doc.Database;
                Editor   ed  = doc.Editor;

                Transaction tr = db.TransactionManager.StartTransaction();
                try
                {
                    using (tr)
                    {
                        LayerTableRecord layer = tr.GetObject(layerId, OpenMode.ForRead) as LayerTableRecord;
                        if (layer.IsOff != isOpenOrClose)
                        {
                            layer.UpgradeOpen();
                            layer.IsOff = isOpenOrClose;
                            if (layer.IsFrozen != isOpenOrClose)
                            {
                                layer.IsFrozen = isOpenOrClose;
                            }
                            tr.Commit();
                        }
                    }
                }
                catch (System.Exception ex)
                {
                    ed.WriteMessage("a problem occured because " + ex.Message);
                }
                finally
                {
                    tr.Dispose();
                }
            }
        }
Exemplo n.º 9
0
        // copnvert all layer names to lowercase
        public override String Process()
        {
            //if (!CheckDirPath()) { return "Invalid path: " + _Path; }

            //try
            //{
            //    _Logger = new Logger(String.Concat(_Path, "\\LowercaseLayerErrorLog.txt"));
            //}
            //catch (System.Exception se)
            //{
            //    return "Could not create log file in: " + _Path + " because: " + se.Message;
            //}

            try
            {
                BeforeProcessing();
            }
            catch (System.Exception se)
            {
                //_Logger.Dispose();
                return("Lowercase layer processing exception: " + se.Message);
            }

            try { GetDwgList(SearchOption.TopDirectoryOnly); }
            catch (System.Exception se)
            {
                _Logger.Log("Could not get dwg files because: " + se.Message);
                _Logger.Dispose();
                return("Could not get all dwg files");
            }

            //StartTimer();

            try
            {
                foreach (String currentDwg in DwgList)
                {
                    System.Boolean changeMade = false;

                    using (Database db = new Database(false, true))
                    {
                        try
                        {
                            db.ReadDwgFile(currentDwg, FileOpenMode.OpenForReadAndWriteNoShare, true, String.Empty);
                            db.CloseInput(true);
                        }
                        catch (System.Exception se)
                        {
                            _Logger.Log(String.Concat("Can't read DWG: ", currentDwg, " because: ", se.Message));
                            continue;
                        }
                        using (Transaction acTrans = db.TransactionManager.StartTransaction())
                        {
                            using (LayerTable lt = acTrans.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable)
                            {
                                foreach (ObjectId oid in lt)
                                {
                                    using (LayerTableRecord ltr = acTrans.GetObject(oid, OpenMode.ForRead) as LayerTableRecord)
                                    {
                                        String layerName      = ltr.Name.Trim();
                                        String layerNameLower = layerName.ToLower();

                                        if (!String.Equals(layerName, layerNameLower))
                                        {
                                            changeMade = true;
                                            ltr.UpgradeOpen();
                                            ltr.Name = layerNameLower;
                                        }
                                    }
                                }
                            }
                            acTrans.Commit();
                        }
                        if (changeMade)
                        {
                            db.SaveAs(currentDwg, DwgVersion.Current);
                        }
                    }

                    DwgCounter++;

                    try { _Bw.ReportProgress(Utilities.GetPercentage(DwgCounter, NumDwgs)); }
                    catch { }

                    if (_Bw.CancellationPending)
                    {
                        _Logger.Log("Processing cancelled by user at DWG: " + DwgCounter.ToString());
                        return("Processing cancelled at dwg " + DwgCounter.ToString() + " out of " + NumDwgs.ToString());
                    }
                }
            }
            catch (System.Exception se) { _Logger.Log(String.Concat("Error: ", se.Message)); return("Processing error: " + se.Message); }
            finally
            {
                AfterProcessing();
            }

            return(String.Concat(" ", DwgCounter.ToString(), " out of ", NumDwgs, " dwgs processed in ", TimePassed));
        }