.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

(C#) How to create multiline attribute?

9 REPLIES 9
Reply
Message 1 of 10
Sfinks7s
2964 Views, 9 Replies

(C#) How to create multiline attribute?

My method creates a attribute, how to a create a multi-line attribute?
Please Help!!!

 

using (Transaction trAdding = dbCurrent.TransactionManager.StartTransaction())
                    {
                        BlockTable btTable = (BlockTable)trAdding.GetObject(dbCurrent.BlockTableId, OpenMode.ForRead);
                        string strBlockName = "ATTRBLK" + System.Convert.ToString(blockName);
                        try
                        {
                            if (btTable.Has(strBlockName))
                                edCurrent.WriteMessage("\n A block with this name already exist.");
                        }
                        catch
                        {
                            edCurrent.WriteMessage("\n Invalid block name.");
                        }

                        
                        AttributeDefinition adAttr = new AttributeDefinition();
                        adAttr.Position = new Point3d(x, y, 0);
                        adAttr.WidthFactor = widthFactor;
                        adAttr.Height = height;
                        adAttr.Rotation = rotate;
                        adAttr.Oblique = oblique;

                        adAttr.Tag = attrName;

                        BlockTableRecord btrRecord = new BlockTableRecord();
                        btrRecord.Name = strBlockName;
                        btTable.UpgradeOpen();

                        ObjectId btrID = btTable.Add(btrRecord);

                        trAdding.AddNewlyCreatedDBObject(btrRecord, true);

                        btrRecord.AppendEntity(adAttr);
                        trAdding.AddNewlyCreatedDBObject(adAttr, true);

                        BlockTableRecord btrPapperSpace = (BlockTableRecord)trAdding.GetObject(btTable[BlockTableRecord.PaperSpace], OpenMode.ForWrite);

                        BlockReference brRefBlock = new BlockReference(Point3d.Origin, btrID);

                        btrPapperSpace.AppendEntity(brRefBlock);
                        trAdding.AddNewlyCreatedDBObject(brRefBlock, true);

                        //задаём значение атрибута
                        AttributeReference arAttr = new AttributeReference();
                        arAttr.SetAttributeFromBlock(adAttr, brRefBlock.BlockTransform);

                        arAttr.TextString = text;
                        arAttr.Layer = "Z-TEXT";

                        brRefBlock.AttributeCollection.AppendAttribute(arAttr);


                        trAdding.AddNewlyCreatedDBObject(arAttr, true);

                        trAdding.Commit();
                    }
9 REPLIES 9
Message 2 of 10
Hallex
in reply to: Sfinks7s

Попробуй такой код (на скорую руку - переделай под свои установкиб тестировал на А2009-2014, работает)

Try this code written from the scratch ( quick and dirty one, tested on A209 through A2014, it's working good for me)

        [CommandMethod("Bma")]
        public  void testMultAttribute()
        {
            var docCurrent = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            var dbCurrent = docCurrent.Database;

            var edCurrent = docCurrent.Editor;

            BlockTableRecord btrRecord = null;

            string blockName = "";double x=0;double y=0;double z=0;

            double widthFactor = 1.0; double height = 250; double rotate = 0.0; double oblique = 0.0;

            string attrName = "ATTTAG";string text="first text line\\Psecond text line\\Pthird text line";

            ObjectId btrID = ObjectId.Null;
            try
            {
                docCurrent.TransactionManager.EnableGraphicsFlush(true);

            using (Transaction trAdding = dbCurrent.TransactionManager.StartTransaction())
            {
                BlockTable btTable = (BlockTable)trAdding.GetObject(dbCurrent.BlockTableId, OpenMode.ForRead);

                string strBlockName = "ATTRBLK" + System.Convert.ToString(blockName);

                try
                {
                    if (btTable.Has(strBlockName))

                        edCurrent.WriteMessage("\n A block with this name already exist.");

                    else
                    {
                    
                AttributeDefinition adAttr = new AttributeDefinition();  
       
                adAttr.Position = new Point3d(x, y, z);

                adAttr.WidthFactor = widthFactor;

                adAttr.Height = height;

                adAttr.Rotation = rotate;

                adAttr.Oblique = oblique;

                adAttr.Tag = attrName;
               
                btTable.UpgradeOpen();

                btrRecord = new BlockTableRecord();

                btrRecord.Name = strBlockName;

                btTable.Add(btrRecord);

                trAdding.AddNewlyCreatedDBObject(btrRecord, true);

                btTable.DowngradeOpen();

                adAttr.IsMTextAttributeDefinition = true;

                adAttr.MTextAttributeDefinition.Contents = text;

                btrRecord.AppendEntity(adAttr);

                trAdding.AddNewlyCreatedDBObject(adAttr, true);

                    }
                }
                catch
                {
                    edCurrent.WriteMessage("\n Invalid block name.");
                }

                BlockTableRecord btrPapperSpace = (BlockTableRecord)trAdding.GetObject(btTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite);

                BlockReference brRefBlock = new BlockReference(new Point3d(x, y, z), btrRecord.ObjectId);

                btrPapperSpace.AppendEntity(brRefBlock);

                trAdding.AddNewlyCreatedDBObject(brRefBlock, true);

                //задаём значение атрибута
              
                foreach (ObjectId id in btrRecord)
                {

                    DBObject obj = id.GetObject(OpenMode.ForRead);

                    AttributeDefinition adAttr = obj as AttributeDefinition;

                    if ((adAttr != null) && (!adAttr.Constant))
                    {
                        AttributeReference arAttr = new AttributeReference();

                        arAttr.SetAttributeFromBlock(adAttr, brRefBlock.BlockTransform);

                        if (arAttr.IsMTextAttribute)
                        {
                  
                            arAttr.Layer = "0";

                            arAttr.TextString = text;

                            arAttr.UpdateMTextAttribute();

                            brRefBlock.RecordGraphicsModified(true);
                        }
                        else
                        {
                            // add usual attribute reference here
                        }

                        brRefBlock.AttributeCollection.AppendAttribute(arAttr);

                        trAdding.AddNewlyCreatedDBObject(arAttr, true);
                    }
                    
                    trAdding.TransactionManager.QueueForGraphicsFlush();
                }

                docCurrent.TransactionManager.FlushGraphics();

                trAdding.Commit();

               }
            }
             catch (System.Exception ex)
             {
                 edCurrent.WriteMessage("\n{0}\n{1}", ex.Message, ex.StackTrace);
             }
             finally
             {
                 Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\n\t---\tSee results\t---\n");
             }
        }

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 4 of 10
pjfontes
in reply to: Hallex

Hallex,

 

I was going to post  a new thread but this thread caught my eye.

 

Do you know how to access and change the Boundary Width?

 

I tried adAttr.MTextAttributeDefinition.Width = 100 (along with a few other "Read Only" properties) with no luck.

 

 

Message 5 of 10
Hallex
in reply to: pjfontes

You have to calculate first scale factor you need
accordingly to a text height, then just use this line:

arAttr.WidthFactor = 2.5;// put scale factor here

 

Or am I missed something?

 

Well probably this will be usefull instead:

 

arAttr.MTextAttribute.Width = arAttr.MTextAttribute.ActualWidth;

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 6 of 10
pjfontes
in reply to: Hallex

My intent is to set the width of the 'MText box' so that the text will word-wrap.

 

MultiLine Attribute BoundaryWidth.png

Message 7 of 10
Hallex
in reply to: pjfontes

Sorry for the belating,

probably I've sorted this out:

                    if (arAttr.IsMTextAttribute)
                        {
                  
                            arAttr.Layer = "0";                           
                            arAttr.TextString = text;
                            MText mtx = arAttr.MTextAttribute;
                            mtx.Width = 2400;
                            arAttr.MTextAttribute = mtx;
                           // OR
                           //  arAttr.MTextAttribute.Width = adAttr.MTextAttributeDefinition.Width;
                            arAttr.UpdateMTextAttribute();
                            brRefBlock.RecordGraphicsModified(true);
                        }

Change a width to your suit

Cheers

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 8 of 10
pjfontes
in reply to: Hallex

Smiley HappySmiley HappySmiley HappySmiley HappySmiley HappySmiley Happy

 

Thank You!

 

Im my code I used mtx.Contents instead of arAttr.Textstring and it worked perfect.

 

Message 9 of 10
Hallex
in reply to: pjfontes

Glad I could help

Cheers, buddy Smiley Happy

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 10 of 10
earth2150
in reply to: Hallex

Всё отлично заработало, благодарю!

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost