Пример #1
0
        public void LayerDemo()
        {
            Database       db  = HostApplicationServices.WorkingDatabase;
            AddLayerResult alr = db.AddLayer("文字");

            db.ChangeLayerColor("文字", 1);
            //db.LockLayer("文字");
            //db.UnLockLayer("文字");
            //db.ChangleLineWeight("文字", LineWeight.LineWeight050);
            //db.SetCurrentLayer("文字");
            //List<LayerTableRecord> list = db.GetAllLayers();
            //db.DeleteLayer("图层1",true);
            db.DeleteNotUsedLayer();
        }
        /// <summary>
        /// 添加图层
        /// </summary>
        /// <param name="db">图形数据库</param>
        /// <param name="layerName">图层名</param>
        /// <returns>AddLayerResult</returns>
        public static AddLayerResult AddLayer(this Database db, string layerName)
        {
            //声明AddLayerResult类型的数据,用户返回
            AddLayerResult res = new AddLayerResult();

            try
            {
                SymbolUtilityServices.ValidateSymbolName(layerName, false);
            }
            catch (Exception)
            {
                res.statuts = AddLayerStatuts.IllegalLayerName;
                return(res);
            }

            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                //打开层表
                LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId, OpenMode.ForRead);
                //新建层表记录
                if (!lt.Has(layerName))
                {
                    LayerTableRecord ltr = new LayerTableRecord();
                    //判断要创建的图层名是否已经存在,不存在则创建
                    ltr.Name = layerName;
                    //升级层表打开权限
                    lt.UpgradeOpen();
                    lt.Add(ltr);
                    //降低层表打开权限
                    lt.DowngradeOpen();
                    trans.AddNewlyCreatedDBObject(ltr, true);
                    trans.Commit();
                    res.statuts   = AddLayerStatuts.AddLayerOK;
                    res.layerName = layerName;
                }
                else
                {
                    res.statuts = AddLayerStatuts.LayerNameExist;
                }
            }
            return(res);
        }