- 积分
- 8063
- 明经币
- 个
- 注册时间
- 2013-8-9
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
发表于 2014-6-10 08:37:08
|
显示全部楼层
想问您一下,我把这个插入函数改成返回ObjectID的函数后,那么在WHILE循环里面使用
ObjectId Id1=createEntity.InsertBlockRefWithAtt(strBlockName1, pt, new Scale3d(1), 0, read.GetString(0), douSD); 得到的Id1是不是同一个值,因为我想得到每次插入在图纸上块的ObjectID
/// <summary>
/// 插入一个带属性的块
/// </summary>
/// <param name="blockName">图块名称</param>
/// <param name="point">插入点</param>
/// <param name="scale">图块比例</param>
/// <param name="rotateAngle">图块旋转角度</param>
/// <param name="KHstring">属性值:孔号</param>
/// <param name="KSdouble">属性值:孔深</param>
/// <remarks></remarks>
public ObjectId InsertBlockRefWithAtt(string blockName, Point3d point, Scale3d scale, double rotateAngle, string KHstring, double KSdouble)
{
ObjectId blockRefId;
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
if (!bt.Has(blockName))
{
return ObjectId.Null;
}
BlockTableRecord blockwithatt = (BlockTableRecord)trans.GetObject(bt[blockName], OpenMode.ForRead);
BlockReference blockRef = new BlockReference(point, bt[blockName]);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
blockRef.ScaleFactors = scale;
blockRef.Rotation = rotateAngle;
blockRefId = btr.AppendEntity(blockRef);
trans.AddNewlyCreatedDBObject(blockRef, true);
//获取blockName块的遍历器,以实现对块中对象的访问
BlockTableRecordEnumerator iterator = blockwithatt.GetEnumerator();
//如果blockName块包含属性
if (blockwithatt.HasAttributeDefinitions)
{
//利用块遍历器对块中的对象进行遍历
while (iterator.MoveNext())
{
//获取块遍历器当前指向的块中的对象
AttributeDefinition attdef = trans.GetObject(iterator.Current, OpenMode.ForRead) as AttributeDefinition;
//定义一个新的属性参照对象
AttributeReference att = new AttributeReference();
//判断块遍历器当前指向的块中的对象是否为属性定义
if (attdef != null)
{
//从属性定义对象中继承相关的属性到属性参照对象中
att.SetAttributeFromBlock(attdef, blockRef.BlockTransform);
//设置属性参照对象的位置为属性定义的位置+块参照的位置
att.Position = attdef.Position + blockRef.Position.GetAsVector();
//判断属性定义的名称
switch (attdef.Tag)
{
//设置块参照的属性值
case "孔号":
att.TextString = KHstring;
break;
case "孔深":
att.TextString = KSdouble.ToString();
break;
}
//判断块参照是否可写,如不可写,则切换为可写状态
if (!blockRef.IsWriteEnabled)
{
blockRef.UpgradeOpen();
}
//添加新创建的属性参照
blockRef.AttributeCollection.AppendAttribute(att);
//通知事务处理添加新创建的属性参照
trans.AddNewlyCreatedDBObject(att, true);
}
}
}
trans.Commit();//提交事务处理
}
return blockRefId;
}
|
|