c# cad 二次开发 类库 块的操作

news/2024/11/15 1:00:18/

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();}}
}

}


http://www.ppmy.cn/news/101113.html

相关文章

Linux之进程地址空间

文章目录 前言一、是什么1.例子2.感性的理解虚拟地址空间3.现象的具体解释4.写时拷贝 二、为什么三、怎么办总结 前言 内存区域划分&#xff1a; 在学习C/C时我们都有接触过内存区域划分这个概念&#xff0c;也知道它表示的是程序加载到内存中不同的数据所分布的不同的区域&a…

《数据库应用系统实践》------ 酒店客房管理系统

系列文章 《数据库应用系统实践》------ 酒店客房管理系统 文章目录 系列文章一、需求分析1、系统背景2、 系统功能结构&#xff08;需包含功能结构框图和模块说明&#xff09;3&#xff0e;系统功能简介 二、概念模型设计1&#xff0e;基本要素&#xff08;符号介绍说明&…

Linux下通过 rm -f 删除大量文件时报错:Argument list too long

Linux下通过 rm -f 删除大量的小文件时出现类似如下错误信息&#xff1a; -bash: /bin/rm: Argument list too long 如下图所示&#xff1a; 问题原因 如果待删除文件中包含的小文件数量过多&#xff0c;通常是由于受到 shell 参数个数限制所致。 这个是Linux系统存在的限制&…

第四十章 不忍和尚

虽如此说&#xff0c;不忍还是乐滋滋的挪开屁股&#xff0c;甲乙丙忍一拥而上&#xff0c;掀开蒲团&#xff0c;下面果然有个大洞&#xff0c;他们仨连连掏出许多熟食&#xff0c;斗大的禅房内顿时香气四溢。 “哎哎哎&#xff0c;差不多得了&#xff01;给我留点儿&#xff01…

PC端网页特效

1. offset属性 1.1 offset系列 <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><meta http-equiv"X-UA-C…

Hive ---- 分区表和分桶表

Hive ---- 分区表和分桶表 1. 分区表1. 分区表基本语法2. 二级分区表3. 动态分区 2. 分桶表1. 分桶表基本语法2. 分桶排序表 1. 分区表 Hive中的分区就是把一张大表的数据按照业务需要分散的存储到多个目录&#xff0c;每个目录就称为该表的一个分区。在查询时通过where子句中…

Studio One6简体中文版全新版本功能详解

Studio One 6是一款强大的音乐编曲软件,可以帮助您使用灵活的和弦轨道功能实现音乐创作。通过新的智能模板、直观的拖放工作流、可定制的用户界面和强大的集成工具&#xff0c;使创建快速而轻松。 无论你选择 Studio One 哪个版本&#xff0c;你都可以得到无限的音轨、通道和插…

JetBrains的多数据库管理和SQL工具DataGrip 2023版本在Linux系统的下载与安装配置教程

目录 前言一、DataGrip安装二、使用配置总结 前言 DataGrip是一款多数据库管理和SQL工具&#xff0c;适用于不同类型的数据库。它提供了丰富的功能和工具&#xff0c;可以帮助开发人员更高效地管理数据库、编写SQL查询和执行数据操作。注&#xff1a;已在CentOS7.9和Ubuntu20.…