Search code examples
c#.netautocadautocad-pluginobjectarx

How to set the multiline property for an attribute inside an AutoCAD block definition using C# API?


Following code fragment creates a block definition with attributes. The second parameter contains a dictionary for attribute names and its visisbility. Additionally I want to set the property for multiline text. This property will be available, when showing the block definition using command ._battman in AutoCAD UI.

How can I set the multiline property to allow multiline texts using the 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();
        }
    }
}

Solution

  • You should set AttributeDefinition.IsMTextAttributeDefinition to true:

    AttributeDefinition ad = new AttributeDefinition(position, value, tag, prompt, style);
    ad.IsMTextAttributeDefinition = true; // Allow multiple lines of text.
    ad.Height = height;
    ad.Invisible = !((bool)nameAndVis.Value);
    

    As explained in the docs:

    IsMTextAttributeDefinition

    Specifies that the attribute value can contain multiple lines of text. When this option is selected, you can specify a boundary width for the attribute.