同事提出了这样一个需求,
将文本(DBText)转换为块的属性值,
我尝试着使用如下的流程实现了该操作,
使用Civil 3d将文本移动到对应高程,
创建Civil 3d曲面,
提取点(AutoCAD的Point),
转换生成几何空间点(CogoPoint),
之后通过几何空间点生成属性块。
但生成的属性块属性标签无法设置也无法修改,
不能满足要求,
于是就试着写下了如下的代码,
经过测试能够满足要求:
public void C_AddATT() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = Application.DocumentManager.MdiActiveDocument.Database; var ppr = doc.Editor.GetString("\n输入块名"); if (ppr.Status != PromptStatus.OK) return; using (Transaction tr = doc.TransactionManager.StartTransaction()) { BlockTable bt = db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable; BlockTableRecord blockDef = bt[ppr.StringResult].GetObject(OpenMode.ForRead) as BlockTableRecord; BlockTableRecord ms = bt[BlockTableRecord.ModelSpace].GetObject(OpenMode.ForWrite) as BlockTableRecord; if (blockDef != null) { TypedValue[] acTypValAr = new TypedValue[1]; acTypValAr.SetValue(new TypedValue((int)DxfCode.Start, "TEXT"), 0); SelectionFilter acSelFtr = new SelectionFilter(acTypValAr); PromptSelectionOptions pso = new PromptSelectionOptions(); pso.MessageForAdding = "\n选择文本"; var psr = doc.Editor.GetSelection(pso,acSelFtr); if (psr.Status == PromptStatus.OK) { foreach (ObjectId id in psr.Value.GetObjectIds()) { DBText text = id.GetObject(OpenMode.ForRead) as DBText; using (BlockReference blockRef = new BlockReference(text.Position, blockDef.ObjectId)) { ms.AppendEntity(blockRef); tr.AddNewlyCreatedDBObject(blockRef, true); foreach (ObjectId attid in blockDef) { DBObject obj = attid.GetObject(OpenMode.ForRead); AttributeDefinition attDef = obj as AttributeDefinition; if ((attDef != null) && (!attDef.Constant)) { using (AttributeReference attRef = new AttributeReference()) { attRef.SetAttributeFromBlock(attDef, blockRef.BlockTransform); attRef.TextString = text.TextString; //Add the AttributeReference to the BlockReference blockRef.AttributeCollection.AppendAttribute(attRef); tr.AddNewlyCreatedDBObject(attRef, true); } } } } } } } else { doc.Editor.WriteMessage("\n输入的块名不存在,请重新输入"); } tr.Commit(); } }
前提:需要提前定义一个属性块,
包含一个属性即可,
属性标签按需设置,
程序将在文本插入点位置插入块,
并将文本值作文块的属性值….
标签:OpenMode,文本,attRef,new,为块,BlockTableRecord,属性 From: https://www.cnblogs.com/myzw/p/17964068