c# cad 二次开发 类库 块的操作
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _14块操作
{
public class Class1
{
//简单块
[CommandMethod(“BlockDemo”)]
public void BlockDemo()
{
Database db = HostApplicationServices.WorkingDatabase;
//using (Transaction trans = db.TransactionManager.StartTransaction())
//{
// BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
// foreach (var item in bt)
// {
// BlockTableRecord btr = (BlockTableRecord)item.GetObject(OpenMode.ForRead);
// }
//}
MyBlockTableRecord.Block1Id = db.AddBlockTableRecord(MyBlockTableRecord.Block1Name, MyBlockTableRecord.Block1Ents);

    }[CommandMethod("InsertBlockDemo")]public void InsertBlockDemo(){Database db = HostApplicationServices.WorkingDatabase;db.InsertBlockBlockReference(MyBlockTableRecord.Block1Id, new Point3d(10, 10, 0));db.InsertBlockBlockReference(MyBlockTableRecord.Block1Id, new Point3d(40, 10, 0),Math.PI/4,new Scale3d(2));db.InsertBlockBlockReference(MyBlockTableRecord.Block1Id, new Point3d(100, 10, 0), Math.PI / 4, new Scale3d(2,1.5,1));}//注释样式的箭头[CommandMethod("ModifyDimDemo")]public void ModifyDimDemo(){Database db = HostApplicationServices.WorkingDatabase;MyBlockTableRecord.DimBlock1Id = db.AddBlockTableRecord(MyBlockTableRecord.DimBlock1Name, MyBlockTableRecord.DimBlock1Ents);using (Transaction trans = db.TransactionManager.StartTransaction()){DimStyleTable dst = (DimStyleTable)trans.GetObject(db.DimStyleTableId, OpenMode.ForRead);if (dst.Has("Standard")){DimStyleTableRecord dstr = (DimStyleTableRecord)dst["Standard"].GetObject(OpenMode.ForRead);if (MyBlockTableRecord.DimBlock1Id != ObjectId.Null){dstr.UpgradeOpen();dstr.Dimblk = MyBlockTableRecord.DimBlock1Id;dstr.Dimasz = 1;dstr.DowngradeOpen();db.SetDimstyleData(dstr);}}trans.Commit();}}//属性快[CommandMethod("AttrBlockDemo")]public void AttrBlockDemo(){Database db = HostApplicationServices.WorkingDatabase;MyBlockTableRecord.AttrBlock1Id = db.AddBlockTableRecord(MyBlockTableRecord.AttrBlock1Name, MyBlockTableRecord.AttrBlock1Ents);ObjectId brId = db.InsertAttrBlockReference(MyBlockTableRecord.AttrBlock1Id, new Point3d(10, 10, 0), 0, new Scale3d(1, 1, 1));Dictionary<string, string> attrNameValues = new Dictionary<string, string>();attrNameValues.Add("编号", "天线11");attrNameValues.Add("功率", "10.0dBm");attrNameValues.Add("功率222", "10.0dBm");brId.UpdateBlockAttr(attrNameValues);}[CommandMethod("TestDemo")]public void TestDemo(){Database db = HostApplicationServices.WorkingDatabase;MyBlockTableRecord.AttrBlock1Id = db.AddBlockTableRecord(MyBlockTableRecord.AttrBlock1Name, MyBlockTableRecord.AttrBlock1Ents);using (Transaction trans = db.TransactionManager.StartTransaction()){BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);//DimStyleTable dst = (DimStyleTable)trans.GetObject(db.DimStyleTableId, OpenMode.ForRead);//if (dst.Has("Standard"))//{//    DimStyleTableRecord dstr = (DimStyleTableRecord)dst["Standard"].GetObject(OpenMode.ForRead);//}foreach (var item in bt){if (bt.Has(MyBlockTableRecord.AttrBlock1Id)){if (item  == MyBlockTableRecord.AttrBlock1Id){BlockTableRecord btr = (BlockTableRecord)item.GetObject(OpenMode.ForRead);foreach (var item1  in btr){if (item1.GetObject(OpenMode.ForRead) is AttributeDefinition){AttributeDefinition attr = (AttributeDefinition)item1.GetObject(OpenMode.ForRead);}}}  }}}}[CommandMethod("PickDemo")]public void PickDemo(){Database db = HostApplicationServices.WorkingDatabase;Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;PromptEntityResult per = ed.GetEntity("选择块");if (per.Status == PromptStatus.OK){using (Transaction trans = db.TransactionManager.StartTransaction()){BlockReference br = (BlockReference)per.ObjectId.GetObject(OpenMode.ForRead);} }}

}
}
BlockTool.cs

using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _14块操作
{
public static class BlockTool
{
///
/// 添加块表记录到图形数据库
///
/// 图形数据库
/// 块表记录名
/// 图形对象
/// ObjectId
public static ObjectId AddBlockTableRecord(this Database db, string btrName, List ents)
{
ObjectId btrId = ObjectId.Null;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
if (!bt.Has(btrName))
{
BlockTableRecord btr = new BlockTableRecord();
btr.Name = btrName;
for (int i = 0; i < ents.Count; i++)
{
btr.AppendEntity(ents[i]);
}
bt.UpgradeOpen();
bt.Add(btr);
trans.AddNewlyCreatedDBObject(btr, true);
bt.DowngradeOpen();
}
btrId = bt[btrName];
trans.Commit();
}
return btrId;
}
///
/// 向模型空间插入块参照
///
/// 图形数据库
/// 块的ObjectId
/// 插入位置
/// ObjectId
public static ObjectId InsertBlockBlockReference(this Database db, ObjectId blockRecordId, Point3d position)
{
ObjectId blkReferId = ObjectId.Null;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
if (bt.Has(blockRecordId))
{
//声明块参照
BlockReference br = new BlockReference(position, blockRecordId);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
blkReferId = btr.AppendEntity(br);
trans.AddNewlyCreatedDBObject(br, true);
}
trans.Commit();
}

        return blkReferId;}/// <summary>/// 向模型空间插入块参照/// </summary>/// <param name="db">图形数据库</param>/// <param name="blockRecordId">块的ObjectId</param>/// <param name="position">插入位置</param>/// <param name="rotation">旋转角度</param>/// <param name="scale">缩放比例</param>/// <returns>ObjectId</returns>public static ObjectId InsertBlockBlockReference(this Database db, ObjectId blockRecordId, Point3d position,double rotation,Scale3d scale){ObjectId blkReferId = ObjectId.Null;using (Transaction trans = db.TransactionManager.StartTransaction()){BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);if (bt.Has(blockRecordId)){BlockReference br = new BlockReference(position, blockRecordId);br.Rotation = rotation;br.ScaleFactors = scale;BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);blkReferId = btr.AppendEntity(br); trans.AddNewlyCreatedDBObject(br, true);}trans.Commit();}return blkReferId;}/// <summary>/// 向模型空间插入属性块参照/// </summary>/// <param name="db">图形数据库</param>/// <param name="blockRecordId">块的ObjectId</param>/// <param name="position">插入位置</param>/// <param name="rotation">旋转角度</param>/// <param name="scale">缩放比例</param>/// <returns>ObjectId</returns>public static ObjectId InsertAttrBlockReference(this Database db, ObjectId blockRecordId, Point3d position, double rotation, Scale3d scale){ObjectId blkReferId = ObjectId.Null;using (Transaction trans = db.TransactionManager.StartTransaction()){BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);if (bt.Has(blockRecordId)){//声明块参照BlockReference br = new BlockReference(position, blockRecordId);br.Rotation = rotation;br.ScaleFactors = scale;BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);blkReferId = btr.AppendEntity(br);//添加属性定义BlockTableRecord blockRecord = (BlockTableRecord)blockRecordId.GetObject(OpenMode.ForRead);if (blockRecord.HasAttributeDefinitions){foreach (ObjectId item in blockRecord){DBObject obj = item.GetObject(OpenMode.ForRead);if (obj is AttributeDefinition){//声明属性参照AttributeReference attrRef = new AttributeReference();attrRef.SetAttributeFromBlock((AttributeDefinition)obj, br.BlockTransform);br.AttributeCollection.AppendAttribute(attrRef);trans.AddNewlyCreatedDBObject(attrRef, true);}}}trans.AddNewlyCreatedDBObject(br, true);}trans.Commit();}return blkReferId;}/// <summary>/// 更新块参照的属性/// </summary>/// <param name="BlockRefId">块参照的ObjectId</param>/// <param name="attrNameValues">属性字典</param>public static void UpdateBlockAttr(this ObjectId BlockRefId, Dictionary<string, string> attrNameValues){using (Transaction trans = BlockRefId.Database.TransactionManager.StartTransaction()){if (BlockRefId != ObjectId.Null){BlockReference br = (BlockReference)BlockRefId.GetObject(OpenMode.ForRead);foreach (ObjectId item in br.AttributeCollection){AttributeReference attRef = (AttributeReference)item.GetObject(OpenMode.ForRead);//判断属性字典中是否包含要更改的属性值if (attrNameValues.ContainsKey(attRef.Tag.ToString())){attRef.UpgradeOpen();attRef.TextString = attrNameValues[attRef.Tag.ToString()].ToString();attRef.DowngradeOpen();}}}trans.Commit();}}
}

}

c# cad 二次开发 类库 块的操作相关推荐

  1. c# cad 二次开发 类库 netload 图层操作、创建图层、删除图层、设置当前图层等

    c# cad 二次开发 类库 netload 图层操作.创建图层.删除图层.设置当前图层等 using Autodesk.AutoCAD.ApplicationServices; using Auto ...

  2. c# CAD二次开发 类库 创建各种图形、直线、圆、多段线、正方形、点等

    c# CAD二次开发 类库 创建各种图形.直线.圆.多段线.正方形.点等 using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD ...

  3. c# cad 二次开发 类库 CAD表格的操作,给CAD添加一个表格

    c# cad 二次开发 类库 CAD表格的操作,给CAD添加一个表格 using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCA ...

  4. c# cad 二次开发 类库 对话框 将frame界面添加到类库中

    c# cad 二次开发 类库 对话框 将frame界面添加到类库中 using System; using System.Collections.Generic; using System.Compo ...

  5. 我的CAD二次开发程序中的操作excel的部分

    这是我的CAD二次开发程序中的操作excel的部分,特此分享 using System; using System.Collections.Generic; using System.Text; us ...

  6. C#之CAD二次开发实例 (13) 图层操作

    # 0. 引言 图层在CAD中也是一个重要的角色,自然而然二次开发也必不可少,先来回顾一下图形数据库,也就是下面这个东西 Database db = HostApplicationServices.W ...

  7. 关于objectArx /CAD二次开发中“属性块”操作

    关于objectArx /CAD二次开发中"属性块"操作 属性块就是在图块上附加一些文字属性(Attribute),这些文字可以非常方便地修改.属性块被广泛应用在工程设计和机械设计 ...

  8. c#CAD二次开发 块、属性块的一些重要的知识点概念

    本人录制c#CAD二次开发视频 基础入门全套,感兴趣可以淘宝搜索店铺 PT的cad小店 https://i.xue.taobao.com/detail.htm?spm=a2174.7765247.0. ...

  9. 使用C#中的AutoCAD .NET API对CAD二次开发,获取动态块可见性值

    使用C#中的AutoCAD .NET API对CAD二次开发,获取动态块可见性值 0.效果 1.获取选择集,得到ObjectId 2.将ObjectId转换为BlockReference对象并获取其D ...

最新文章

  1. (灌水)如何限制一个WinForm应用程序只能在一个进程运行
  2. [LeetCode] Rotate List
  3. golang协程退出
  4. aaynctask控制多个下载进度_史上最强的进度图绘制十大注意事项!
  5. 搜索二维矩阵 II—leetcode240
  6. python中soup_python – 使用带有UTF-8的soup.get_text()
  7. kettle-实现每个分组的前N的数据
  8. 为什么使用HashMap需要重写hashcode和equals方法_不同时重写equals和hashCode又会怎样?听听过来人的经验...
  9. (17)HTML标准文档流
  10. 小程序获取运动步数php,微信小程序怎么获取php页面数据?
  11. Miller Robbin测试模板(无讲解)
  12. apache-xmlrpc-3.1.3-bin.tar.bz2
  13. getTickCount()函数 区别GetTickCount()函数
  14. 加粉软件直接把你的银行卡信息给泄露了
  15. [MTEX]EBSD_数据处理
  16. 计算机提示无法识别优盘,插入U盘显示无法识别怎么办
  17. 揭开均线系统的神秘面纱_揭开极限编程的神秘面纱,重新探讨“ XP蒸馏”,第3部分...
  18. NFS动态分配PV理解
  19. 洛谷P1873 Java
  20. 网易云音乐人申请教程(会唱歌即可)

热门文章

  1. nvidia+cuda+cudnn+pytorch安装流程(外加ubuntu黑屏解决方案)
  2. Apache Drill学习笔记一:环境搭建和简单试用
  3. springboot+mysql+安卓app团购网站-计算机毕业设计源码26449
  4. 编辑PDF的软件有哪些 怎样删除PDF页码
  5. Final Cut Pro X效果插件开发总结
  6. 动态规划——最长回文子序列
  7. 某程序丢失xinput1_1.dll文件如何解决?
  8. 基础实验5-2.3:QQ帐户的申请与登陆(map的各种用法)
  9. 染色问题(n个格子,3种颜色)
  10. 辩论赛基础技巧培训PPT模板