CAD.NET二次开发 新建图层 删除图层 指定图层颜色以及线形等
基于浩辰CAD 2019测试 功能实现
直接上代码:
[CommandMethod("CreateAndAssignAlayer")] //新建图层 然后添加到图层表里
public static void CreateAndAssignAlyer()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
using (Transaction Trans = db.TransactionManager.StartTransaction())
{
//以读模式打开图层表
LayerTable layerTable;
layerTable = Trans.GetObject(db.LayerTableId, OpenMode.ForNotify) as LayerTable;
string sLayerName = "test1";
if (layerTable.Has(sLayerName) == false)
{
LayerTableRecord layerTableRecord = new LayerTableRecord();
layerTableRecord.Color = Color.FromColorIndex(ColorMethod.ByAci, 1);
layerTableRecord.Name = sLayerName;
layerTable.UpgradeOpen();
layerTable.Add(layerTableRecord);
Trans.AddNewlyCreatedDBObject(layerTableRecord, true);
}
BlockTable blockTable;
blockTable = Trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord blockTableRecord;
blockTableRecord = Trans.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
Circle circle = new Circle();
circle.Center = new Point3d(100, 200, 0);
circle.Radius = 300;
circle.Layer = sLayerName;
blockTableRecord.AppendEntity(circle);
Trans.AddNewlyCreatedDBObject(circle, true);
db.Clayer = layerTable[sLayerName];
Trans.Commit();
}
}
[CommandMethod("TurnLayerOff")] //关闭图层表
public void TurnLayerOff()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database database = doc.Database;
using (Transaction trans = database.TransactionManager.StartTransaction())
{
LayerTable layerTable;
layerTable = trans.GetObject(database.LayerTableId, OpenMode.ForWrite) as LayerTable;
String slayer = "20180415";
LayerTableRecord layerTableRecord;
if (layerTable.Has(slayer) == false)
{
layerTableRecord = new LayerTableRecord();
layerTableRecord.Color = Color.FromColorIndex(ColorMethod.ByColor, 2);
layerTableRecord.Name = slayer;
layerTable.UpgradeOpen();
layerTable.Add(layerTableRecord);
trans.AddNewlyCreatedDBObject(layerTableRecord, true);
}
else
{
layerTableRecord = trans.GetObject(layerTable[slayer], OpenMode.ForWrite) as LayerTableRecord;
}
layerTableRecord.IsOff = true;
BlockTable blockTable;
blockTable = trans.GetObject(database.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord blockTableRecord = trans.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
Circle cir = new Circle();
cir.Center = new Point3d(200, 300, 400);
cir.Radius = 500;
cir.Layer = slayer;
blockTableRecord.AppendEntity(cir);
trans.AddNewlyCreatedDBObject(cir, true);
trans.Commit();
}
}
[CommandMethod("FrozenLayer")]
public void FrozenLayer()
{
Document document = Application.DocumentManager.MdiActiveDocument;
Database database = document.Database;
using (Transaction transaction=database.TransactionManager.StartTransaction())
{
LayerTable layerTable;
layerTable = transaction.GetObject(database.LayerTableId,OpenMode.ForWrite) as LayerTable;
string aLayer = "test1";
LayerTableRecord layerTableRecord;
if (layerTable.Has(aLayer)==false)
{
layerTableRecord = new LayerTableRecord();
layerTableRecord.Color = Color.FromColorIndex(ColorMethod.ByAci,3);
layerTableRecord.Name = aLayer;
layerTable.UpgradeOpen();
layerTable.Add(layerTableRecord);
transaction.AddNewlyCreatedDBObject(layerTableRecord, true);
}
else
{
layerTableRecord = transaction.GetObject(layerTable[aLayer],OpenMode.ForWrite) as LayerTableRecord;
}
layerTableRecord.IsFrozen = true;
BlockTable blockTable = transaction.GetObject(database.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord blockTableRecord = transaction.GetObject(blockTable[BlockTableRecord.ModelSpace],OpenMode.ForWrite) as BlockTableRecord;
Circle cir = new Circle();
cir.Center = new Point3d(200, 300, 400);
cir.Radius = 500;
cir.Layer = aLayer;
blockTableRecord.AppendEntity(cir);
transaction.AddNewlyCreatedDBObject(cir, true);
transaction.Commit();
}
}
[CommandMethod("SetLayerColor")] //指定图层颜色
public void SetLayerColor()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database database = doc.Database;
using (Transaction trans=database.TransactionManager.StartTransaction())
{
LayerTable layerTable = trans.GetObject(database.LayerTableId, OpenMode.ForWrite) as LayerTable;
string[] sLayerNames = new string[3];
sLayerNames[0] = "ACIRed";
sLayerNames[1] = "TrueBlue";
sLayerNames[2] = "ColorBookYellow";
Color[] acColors = new Color[3];
acColors[0] = Color.FromColorIndex(ColorMethod.ByAci,1);
acColors[1] = Color.FromRgb(23,54,232);
acColors[2] = Color.FromNames("PANTONE Yellow 0131 C","PANTONE(R) pastel coated");
int nCnt = 0;
foreach (string sLayerName in sLayerNames)
{
LayerTableRecord layerTableRecord;
if (layerTable.Has(sLayerName) == false)
{
layerTableRecord = new LayerTableRecord();
layerTableRecord.Name = sLayerName;
if (layerTable.IsWriteEnabled == false) layerTable.UpgradeOpen();
layerTable.Add(layerTableRecord);
trans.AddNewlyCreatedDBObject(layerTableRecord, true);
}
else
{
layerTableRecord = trans.GetObject(layerTable[sLayerName], OpenMode.ForWrite) as LayerTableRecord;
}
layerTableRecord.Color = acColors[nCnt];
nCnt++;
}
trans.Commit();
}
}
[CommandMethod("SetLayerLineType")]
public void SetLayerLineType() //设置图层线型
{
Document document = Application.DocumentManager.MdiActiveDocument;
Database database = document.Database;
using (Transaction transaction = database.TransactionManager.StartTransaction())
{
LayerTable layerTable = transaction.GetObject(database.LayerTableId,OpenMode.ForRead) as LayerTable;
string sLayerName = "ABC";
LayerTableRecord layerTableRecord;
if (layerTable.Has(sLayerName) == false)
{
layerTableRecord = new LayerTableRecord();
layerTableRecord.Name = sLayerName;
layerTable.UpgradeOpen();
layerTable.Add(layerTableRecord);
transaction.AddNewlyCreatedDBObject(layerTableRecord, true);
}
else
{
layerTableRecord = transaction.GetObject(layerTable[sLayerName], OpenMode.ForRead) as LayerTableRecord;
}
LinetypeTable linetypeTable = transaction.GetObject(database.LinetypeTableId, OpenMode.ForRead) as LinetypeTable;
if (linetypeTable.Has("Center") == true)
{
layerTableRecord.UpgradeOpen();
layerTableRecord.LinetypeObjectId = linetypeTable["Center"];
}
transaction.Commit();
}
}
[CommandMethod("EraseLayer")]
public void EraseLayer() //删除图层
{
Document document = Application.DocumentManager.MdiActiveDocument;
Database database = document.Database;
using (Transaction transaction=database.TransactionManager.StartTransaction())
{
LayerTable layerTable = transaction.GetObject(database.LayerTableId, OpenMode.ForRead) as LayerTable;
string sLayerName = "test1";
if (layerTable.Has(sLayerName) == true)
{
ObjectIdCollection objectIdCollection = new ObjectIdCollection();
objectIdCollection.Add(layerTable[sLayerName]);
//database.Purge(objectIdCollection);
if (objectIdCollection.Count>0)
{
LayerTableRecord layerTableRecord = transaction.GetObject(objectIdCollection[0], OpenMode.ForWrite) as LayerTableRecord;
try
{
layerTableRecord.Erase(true);
transaction.Commit();
}
catch(GrxCAD.Runtime.Exception ex)
{
Application.ShowAlertDialog("Error:\n" + ex.Message);
}
}
}
}
}
CAD.NET二次开发 新建图层 删除图层 指定图层颜色以及线形等的更多相关文章
- Qt+QGis二次开发:加载栅格图层和矢量图层
一.加载栅格图像 加载栅格图像的详细步骤在下面代码里: //添加栅格数据按钮槽函数 void MainWindow::addRasterlayers() { //步骤1:打开文件选择对话框 QStri ...
- NX二次开发-将对象移动到图层UF_OBJ_set_layer
#include <uf.h> #include <uf_obj.h> #include <uf_modl.h> #include <uf_layer.h&g ...
- 【NX二次开发】Block UI 指定矢量
属性说明 属性 类型 描述 常规 BlockID String 控件ID Enable Logical 是否可操作 Group ...
- Civil 3D 二次开发 新建CLR项目出现错误C2143
新建CLR项目出现错误C2143 按照Objectarx Training创建.net混合项目,编译时出现一下错误: 原因不明: 解决方法: 在Stdafx.h文件中添加: #define WIN32 ...
- Qt+QGIS二次开发:向shp矢量图层中添加新的字段
添加一个新的字段到shp文件中,并且从Excel里导入数据到该字段.原shp文件里的字段ID应该与Excel里的字段ID一一对应才能正确的导入.下图分别是shp的字段和Excel的字段 将class字 ...
- NX二次开发-UFUN编辑添加哪些图层UF_LAYER_edit_category_layer
1 NX11+VS2013 2 3 #include <uf.h> 4 #include <uf_layer.h> 5 6 7 UF_initialize(); 8 9 //创 ...
- NX二次开发-UFUN获取工程图视图边界线颜色UF_DRAW_ask_border_color
#include <uf.h> #include <uf_draw.h> #include <uf_ui.h> UF_initialize(); ; UF_DRAW ...
- 【NX二次开发】Block UI 指定轴
属性说明 属性 类型 描述 常规 BlockID String 控件ID Enable Logical 是否可操作 Group ...
- 【NX二次开发】Block UI 指定平面
属性说明 属性 类型 描述 常规 BlockID String 控件ID Enable Logical 是否可操作 Group ...
随机推荐
- python常见的报错提示
在运行或编写一个程序时常会遇到错误异常,这时python会给你一个错误提示类名,告诉出现了什么样的问题(Python是面向对象语言,所以程序抛出的异常也是类).能很好的理解这些错误提示类名所代表的意思 ...
- js面试题1
1.介绍js的基本数据类型 Undefined.Null.Boolean.Number.String 2.js有哪些内置对象? 数据封装类对象:Object.Array.Boolean.Number ...
- Charpter3 名字 作用域 约束
一个对象拥有其语义价值的区域<其作用域 当一个变量将不再被使用,那它应该被理想的回收机制回收.但现实是我们仅当一个变量离开了其作用域,或变成不可访问,才考虑回收. 然而,作用域规则有其优点:1. ...
- 认识容器和Docker(一)
前言: 这句话应该是开发人员经常挂在嘴边的吧! “在我的机器上是正常工作的啊,MD,怎么到你这就不行了?” 开发人员就会联想到: 1. 肯定是你环境有问题: 2. 要么就是你个傻*不会用吧: 带着这句 ...
- FreemarkerJavaDemo【Android将表单数据生成Word文档的方案之一(基于freemarker2.3.28,只能java生成)】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 这个方案只能在java中运行,无法在Android项目中运行.所以此方案是:APP将表单数据发送给后台,后台通过freemarker ...
- 简单工厂模式--java代码实现
简单工厂模式 工厂,生产产品的场所.比如农夫山泉工厂,生产农夫山泉矿泉水.茶π等饮料.矿泉水和茶π都属于饮料,都具有解渴的功能,但是每种饮料给人的感觉是不一样的.矿泉水和茶π在Java中相当于子类,饮 ...
- 游戏AI之决策结构—有限状态机/行为树(2)
目录 有限状态机 行为树 控制节点 条件节点 行为节点 装饰节点 总结 额外/细节/优化 游戏AI的决策部分是比较重要的部分,游戏程序的老前辈们留下了两种经过考验的用于AI决策的结构: 有限状态机 行 ...
- PHP扩展高性能日志系统SeasLog简单上手
Windows部分:-------------------------------------------------------- https://pecl.php.net/package/Seas ...
- c#中@标志的作用
参考微软官方文档-特殊字符@,地址 https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/tokens/verbatim ...
- C#删除字符串最后一个字符
例:字符串 string str="2,3,5,7,9," 去掉最后一个逗号 ","; 常用的方法: 1.SubString()方法 str=str.SubSt ...