如何通过C# API为AutoCAD块定义中的属性设置多行文本属性?
问题
现有一段C#代码用于创建带属性的AutoCAD块定义,第二个参数是存储属性名称及其可见性的字典。现在需要设置多行文本属性,让该属性在AutoCAD界面通过._battman命令查看块定义时生效,问如何通过C# API实现这一需求?
原代码如下:
internal void AddBlockDefinition(string blockName, OrderedDictionary attrNamesAndVisiblity, double height) { Document doc = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; using (Transaction tr = db.TransactionManager.StartTransaction()) using (BlockTable blockTable = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead)) using (BlockTableRecord modelSpace = (BlockTableRecord)tr.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite)) { BlockTableRecord newBlck = new BlockTableRecord(); newBlck.Name = blockName; blockTable.UpgradeOpen(); ObjectId btrId = blockTable.Add(newBlck); tr.AddNewlyCreatedDBObject(newBlck, true); string value = ""; ObjectId style = new ObjectId(); double y = 0; foreach (DictionaryEntry nameAndVis in attrNamesAndVisiblity) { Point3d position = new Point3d(0, y, 0); string tag = nameAndVis.Key.ToString(); string prompt = tag; AttributeDefinition ad = new AttributeDefinition(position, value, tag, prompt, style); ad.Height = height; ad.Invisible = !((bool)nameAndVis.Value); newBlck.AppendEntity(ad); tr.AddNewlyCreatedDBObject(ad, true); y = y - height * 1.3; } tr.Commit(); } }
解决方案
要让块属性支持多行文本,关键是在创建AttributeDefinition时开启多行属性开关,并配置相关参数:
- 启用多行属性:将
AttributeDefinition的MTextAttribute属性设为true,这是核心设置,告诉AutoCAD该属性是多行文本类型。 - 配置多行文本参数:
Width:设置多行文本框的宽度(可选,不设置的话会自动换行);MTextAttributeHeight:指定多行文本的行高(如果需要和单行文本区分的话,默认可以沿用Height的值);- 若需要指定多行文字样式,可设置
MTextAttributeStyle属性(需传入对应的文字样式ObjectId)。
修改后的代码示例(标注关键修改点):
internal void AddBlockDefinition(string blockName, OrderedDictionary attrNamesAndVisiblity, double height) { Document doc = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; using (Transaction tr = db.TransactionManager.StartTransaction()) using (BlockTable blockTable = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead)) using (BlockTableRecord modelSpace = (BlockTableRecord)tr.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite)) { BlockTableRecord newBlck = new BlockTableRecord(); newBlck.Name = blockName; blockTable.UpgradeOpen(); ObjectId btrId = blockTable.Add(newBlck); tr.AddNewlyCreatedDBObject(newBlck, true); string value = ""; ObjectId style = new ObjectId(); // 可选:获取默认多行文字样式(如果需要自定义样式) // TextStyleTable textStyleTable = (TextStyleTable)tr.GetObject(db.TextStyleTableId, OpenMode.ForRead); // ObjectId mTextStyleId = textStyleTable["Standard"]; double y = 0; foreach (DictionaryEntry nameAndVis in attrNamesAndVisiblity) { Point3d position = new Point3d(0, y, 0); string tag = nameAndVis.Key.ToString(); string prompt = tag; AttributeDefinition ad = new AttributeDefinition(position, value, tag, prompt, style); ad.Height = height; ad.Invisible = !((bool)nameAndVis.Value); // -------------------------- 关键修改 -------------------------- // 启用多行文本属性 ad.MTextAttribute = true; // 设置多行文本框宽度(示例设为100,可根据需求调整) ad.Width = 100; // 可选:设置多行文本行高,默认和Height一致,也可单独指定 // ad.MTextAttributeHeight = height; // 可选:指定多行文字样式 // ad.MTextAttributeStyle = mTextStyleId; // ------------------------------------------------------------- newBlck.AppendEntity(ad); tr.AddNewlyCreatedDBObject(ad, true); y = y - height * 1.3; } tr.Commit(); } }
完成上述修改后,用._battman命令查看块定义时,该属性会被识别为多行文本,插入块时也可以输入多行内容。
内容的提问来源于stack exchange,提问作者user2194358
相关产品推荐
相关产品推荐

