当前位置: 首页 > news >正文

041集——封装之:新建图层(CAD—C#二次开发入门)

如图所示:增加一个图层“新图层”,颜色为红(1),当图层颜色定义为黄(2)时,直接覆盖之前图层颜色,图层名不变。

代码如下:

 

  /// </summary>
  /// <param name="数据库db"></param>
  /// <param name="图层名layname"></param>
  /// <param name="图层颜色colorindex"></param>
  /// <returns>图层名layname</returns>
  public static string AddLayer(this Database db, string layname, short colorindex)
  {
      if ((colorindex < 0) || (colorindex > 256))
      {
          colorindex = 0;
      };
      //var pt = Point3d.Origin;
      
      //Application.DocumentManager.MdiActiveDocument.Editor.GetPoint(out pt, "asdf");
      // 获得当前文档和数据库   Get the current document and database
      Document acDoc = Application.DocumentManager.MdiActiveDocument;
      //Database db = acDoc.Database;

      // 启动一个事务  Start a transaction
      using (Transaction acTrans = db.TransactionManager.StartTransaction())
      {
          // 以只读方式打开图层表   Open the Layer table for read
          LayerTable acLyrTbl;
          acLyrTbl = acTrans.GetObject(db.LayerTableId,
                                       OpenMode.ForRead) as LayerTable;

          if (acLyrTbl.Has(layname) == false)
          {
              LayerTableRecord acLyrTblRec = new LayerTableRecord();

              // Assign the layer the ACI color 1 and a name
              acLyrTblRec.Color = Color.FromColorIndex(ColorMethod.ByAci, colorindex);
              acLyrTblRec.Name = layname;
              // Upgrade the Layer table for write
              acLyrTbl.UpgradeOpen();

              // Append the new layer to the Layer table and the transaction
              acLyrTbl.Add(acLyrTblRec);
              acTrans.AddNewlyCreatedDBObject(acLyrTblRec, true);
          }
          else//已存在图层名
          {
              ObjectId layerId = acLyrTbl[layname];
              LayerTableRecord layerRecord = acTrans.GetObject(layerId, OpenMode.ForRead) as LayerTableRecord;
              layerRecord.UpgradeOpen();
              layerRecord.Color = Color.FromColorIndex(ColorMethod.ByAci, colorindex);
             // acTrans.AddNewlyCreatedDBObject(layerRecord, true);
          }

           以只读方式打开块表   Open the Block table for read
          //BlockTable acBlkTbl;
          //acBlkTbl = acTrans.GetObject(db.BlockTableId,
          //                             OpenMode.ForRead) as BlockTable;

           以写方式打开模型空间块表记录   Open the Block table record Model space for write
          //BlockTableRecord acBlkTblRec;
          //acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
          //                                OpenMode.ForWrite) as BlockTableRecord;
          //Save the changes and dispose of the transaction
          acTrans.Commit();
      }
      return layname;
  }

http://www.dtcms.com/a/32148.html

相关文章:

  • 动态订阅kafka mq实现(消费者组动态上下线)
  • 代码随想录-训练营-day35
  • 基于ffmpeg+openGL ES实现的视频编辑工具-添加转场(九)
  • C语言进阶习题【3】(7预处理)——写一个宏计算结构体变量相对于首地址的偏移
  • 先进制造aps专题三十 用免费生产排程软件isuperaps进行长期生产计划制定
  • 计算机图形学:实验环境配置
  • 基于Matlab实现串口实时显示波形GUI界面(源码)
  • Linux 驱动入门(6)—— IRDA(红外遥控模块)驱动
  • 代码随想录算法训练营day40(补0208)
  • “死”循环(查漏补缺)
  • 055 SpringCache
  • cs106x-lecture14(Autumn 2017)-SPL实现
  • 【Java进阶学习 第五篇】JDK8、9中的接口新特性
  • ARM Cortex-M3 技术解析:核寄存器R1-R15介绍及使用
  • 第五章:工程化实践 - 第三节 - Tailwind CSS 大型项目最佳实践
  • kafka+spring cloud stream 发送接收消息
  • 华为OD机试真题-相对开音节-OD统一考试(E卷)
  • Meterpreter之getsystem命令提权原理详解
  • Zotero 快速参考文献导出(特定期刊引用)
  • 区块链相关方法-波士顿矩阵 (BCG Matrix)
  • Codes 开源免费研发项目管理平台 2025年第一个大版本3.0.0 版本发布及创新的轻IPD实现
  • 在LangFlow中集成OpenAI Compatible API类型的大语言模型
  • 不同类型的网站选择不同的服务器,那么应该怎么选择服务器呢?
  • STM32-心知天气项目
  • python包重要修改
  • 如何把windows机器作为SSH客户端免密登录
  • Markdown使用方法文字版解读
  • 数据表的存储过程和函数介绍
  • OpenBMC:BmcWeb app.run
  • tortoiseGit的使用和上传拉取