C#之CAD插入外部块文件

更新时间:2023-04-21 19:09:50 阅读: 评论:0


2023年4月21日发(作者:志愿四川)

C#CAD插⼊外部块⽂件

1、需求

插⼊外部⽂件中指定的块。

2、思路

将外部dwg⽂件加载到临时数据库中,把外部dwg⽂件中⾃定义的命名块复制到当前⽂档的数据库中;新建指定块的块参照加⼊到当前块表

记录。

3、环境

AutoCAD 2018/vs2015

4、步骤:

(1) 新建⼀个临时图小孩的简笔画 形数据库

Databa tempDB = new Databa(fal, true);

(2) 调⽤Databa类的 WblockCloneObjects()⽅法复制块

(3) 新建⼀个块参照:BlockReference

public BlockReference(Point3d position, ObjectId blockTableRecord);

(4) 把块参照添加到块表记录中

Entity()

lyCreatedDBObject();

关键代码:

///

/// 复制块⽂件

///

///

/// 动态块⽂件路径

/// 动态块名称

///

public static void CopyBlockFromFile(this Databa TargetDB, string FileName, string BlockName, DuplicateRecordCloning Method = DuplicateRecordCl

e)

{

using (Databa db = new Databa(fal, true))

{

gFile(FileName, ite, true, "");

ObjectIdCollection ids = new ObjectIdCollection();

Obj万金不换 ectIdCollection newoids = new ObjectIdCollection();

IdMapping im = new IdMapping();

using (Transaction tr = ransaction())

{

BlockTable bt = (BlockTable)ect(ableId, d, true, true);

(bt[BlockName]);

();

}

try

{

//CloneObjects(ids, ableId, im, , true);

CloneObjects(ids, ableId, im, e, fal);

}

catch (Exception ex)

{

}

}

}

///

/// 插⼊带属性的参照快(插⼊动态块)

///

/// 空间的ID

/// 块要加⼊的图层名

/// 快参照所属的快名

/// 插⼊点

/// 缩放⽐例

/// 旋转⾓度

/// 属性名称与取值

///

public static ObjectId InrtBlockrefenceModelSpace(this ObjectId spaceId, string blockName, Point3d postion, Scale3d scale, double rotateAngle, Dictio

nary> attNameValues, Dictionary> attDNameValues, bool isRoate = fal)

{

// 获取数据库对象

Databa db = ;

//以读的⽅式打开块表

BlockTable bt = ect(d) as BlockTable;

//如果没有blockName d的块,则程序返回

if (!(blockName))

return ;//如果没有blockName的块,程序返回

//以写的模式打开空间

BlockTableRecord space = (BlockTableRecord)ect(te);

//获取块表的记录ID

ObjectId btrId = bt[blockName];

//打开块表记录

BlockTableRecord record = ect(d) as BlockTableRecord;

//创建⼀个快参照并设置插⼊点

BlockReference br = new BlockReference(postion, bt[blockName]);

//DynamicBlockReferenceProper霸气女 tyCollection properties= cBlockReferencePropertyCollection;

actors = scale;

on = rotateAngle;

Entity(br);

//判断块表记录是否包含属性定义

if (ributeDefinitions)

{

//若包含,则遍历属性定义

foreach (ObjectId id in record)

{

//检查是否是属性定义

AttributeDefinition attDef = ect(d) as AttributeDefinition;

if (attDef != null)

{

//创建⼀个新的属性对象

AttributeReference attribute = new AttributeReference();

//从属性定义获取属性对象的对象特性

ributeFromBlock(attDef, ransform);

on = on;

on = ormBy(ransform);

Alignment(db);

//判断是否包含指定的属性名称

if (nsKey(r()))

{

//设置属性值

ring = attNameValues[r()].ToString();

}

// 向块参照添加属性对象

// 向块参照添加属性对象

Attribute(attribute);

lyCreatedDBObject(attribute, true);

}

}

}

if (isRoate )

{

ormBy(on(ToRadian(180), , postion));

}

ChangeDynamicValue(br, attDNameValues);

lyCreatedDBObject(br, true);

return Id;//返回添加的快参照的ID

}

说明:

Dictionary> : Dictionary<属性设置值, Dictionary<属性名称, 类型(stringdouble)>>

///

///设置⾃定义属性值

///

///

///

public static void ChangeDynamicValue(BlockReference br, Dictionary> attDNameValues)

{

if (br != null && micBlock)

{

DynamicBlockReferencePropertyCollection pc = cBlockReferencePropertyCollection;

AttributeCollection a老虎虾 r = uteCollection;

foreach (DynamicBlockReference世界最长的隧道 Property prop in pc)

{

if (ns(tyName) && !ly)

{

try

{

Dictionary dic = attDNameValues[tyName];

List values = ();

List黄家驹经典歌曲 types 诗歌朗诵串词 = ();

switch(types[0].ToLower())

{

ca "double":

string[] sp = ('_');

if (sp != null)

{

//if(sp[-1]=="mm")

// = (values[0])/1000;

//el

= (values[0]);

}

break;

ca "string":

= values[0];

break;

}

// = attDNameValues[tyName];

}

catch (ion ex)

{

}

}

el

{

continue;

}

}

}

}


本文发布于:2023-04-21 19:09:50,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/841514.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图