1

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();
        }
    }
}
dbc
  • 104,963
  • 20
  • 228
  • 340

2 Answers2

0

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.

dbc
  • 104,963
  • 20
  • 228
  • 340
-1

To set the multiline property for an AttributeDefinition in AutoCAD using the C# API, you can use the AttributeDefinition.AllowMultiLine property. By default, this property is set to false, which means single-line text is allowed. To enable multiline text, you need to set it to true.

Here's how you can modify your code to enable multiline text for the AttributeDefinition:

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);

            // Set the AllowMultiLine property to true for multiline text
            ad.AllowMultiLine = true;
            
            newBlck.AppendEntity(ad);
            tr.AddNewlyCreatedDBObject(ad, true);
            y = y - height * 1.3;
        }
        tr.Commit();
    }
}

By setting ad.AllowMultiLine = true;, you are enabling the multiline property for the AttributeDefinition, and multiline text will be allowed when using the ._battman command in AutoCAD UI to show the block definition.

Yuriy A.
  • 750
  • 3
  • 19
  • 1
    @https://stackoverflow.com/users/3888877/yuriy-a Which AutoCAD version are you using? When I set the reference to AcDbMgd.dll in version 24.2.0.0 (AutoCAD 2023) I can't see the property AllowMultiLine – user2194358 Jul 21 '23 at 12:11
  • 2
    Fake ChatGPT answer, possibly by bot. 12 fake answers in 30 minutes – Panagiotis Kanavos Jul 21 '23 at 12:14
  • As shown in the [docs](https://help.autodesk.com/view/OARX/2023/ENU/?guid=OARX-ManagedRefGuide-__MEMBERTYPE_Properties_Autodesk_AutoCAD_DatabaseServices_AttributeDefinition), `AttributeDefinition` has no property named `AllowMultiLine`. – dbc Jul 26 '23 at 22:19