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

news/2025/2/23 6:48:54

如图所示:增加一个图层“新图层”,颜色为红(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.niftyadmin.cn/n/5863145.html

相关文章

《A++ 敏捷开发》- 16 评审与结对编程

客户&#xff1a;我们的客户以银行为主&#xff0c;他们很注重质量&#xff0c;所以一直很注重评审。他们对需求评审、代码走查等也很赞同&#xff0c;也能找到缺陷&#xff0c;对提升质量有作用。但他们最困惑的是通过设计评审很难发现缺陷。 我&#xff1a;你听说过敏捷的结对…

PolyOS 是面向 RISC-V 架构的智能终端和 AIoT 开源操作系统(基于开源鸿蒙)

PolyOS 是面向 RISC-V 架构的智能终端和 AIoT 开源操作系统 官网&#xff1a;PolyOS | PolyOS PolyOS Mobile&#xff0c;作为一款面向 RISC-V 架构的智能终端操作系统&#xff0c;我们希望能够构建卓越的特性和创新的生态系统来引领未来移动操作系统技术的发展。该操作系统基…

Windows 上源码安装 FastGPT

FastGPT 是一个强大的 AI RAG 平台&#xff0c;值得我们去学习了解。与常见的 Python 体系不同&#xff0c;Fast GPT 采用 Node.js/Next.js 平台&#xff08;对于广大 JS 开发者或前端开发者比较亲切友好&#xff09;&#xff0c;安装或部署比较简单。虽然一般情况下推荐简单的…

微软CEO-纳德拉访谈-AGI计划

在与知名科技播客主播 Dwarkesh Patel 的深度访谈中,微软 CEO 萨提亚・纳德拉围绕 AI、量子计算、微软发展等多方面分享深刻见解,下面是针对访谈内容的介绍,其中还是有很多值得我们学习的地方。 1 AI 领域见解 影响力评估:纳德拉直言行业所标榜的 AGI 里程碑是 “无意义的基…

C++——list模拟实现

目录 前言 一、list的结构 二、默认成员函数 构造函数 析构函数 clear 拷贝构造 赋值重载 swap 三、容量相关 empty size 四、数据访问 front/back 五、普通迭代器 begin/end 六、const迭代器 begin/end 七、插入数据 insert push_back push_front 八、…

A097基于SpringBoot实现的社区博客管理系统

系统介绍 基于SpringBootVue实现的社区博客管理系统设计了超级管理员、系统观察者、用户三种角色&#xff0c;超级管理员可对用户授权&#xff0c;具体实现的功能如下 • 文章采用了比较火的Markdown编辑器。 • 系统集成了邮件发送功能。 • 系统可以自定义友情链接。…

无人机避障——Mid360+Fast-lio感知建图+Ego-planner运动规划(胎教级教程)

电脑配置&#xff1a;Xavier-nx、ubuntu 18.04、ros melodic 激光雷达&#xff1a;Livox_Mid-360 结果展示&#xff1a;左边Mid360Fast-lio感知建图&#xff0c;右边Ego-planner运动规划 1、读取雷达数据并显示 无人机避障——感知篇&#xff08;采用Livox-Mid360激光雷达获…

普通人怎样用好Deepseek?

第一章&#xff1a;准备篇&#xff08;30分钟上手&#xff09; DeepSeek 是一款功能强大的数据处理和分析工具&#xff0c;广泛应用于数据挖掘、机器学习、商业智能等领域。 本文将通过图文结合的方式&#xff0c;详细介绍 DeepSeek 的安装、配置、基本使用、高级功能以及实用…