Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Windows.ToolPalette;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyApp
{
    public class Attributes
    {
        [CommandMethod("NLTAG")]
        public void ListAttributes()
        {
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            Database db = HostApplicationServices.WorkingDatabase;
            Transaction tr = db.TransactionManager.StartTransaction();
            PromptPointResult ppr;
            PromptPointOptions ppo = new PromptPointOptions("");

            // Start the transaction
            try
            {
                // Build a filter list so that only
                // block references are selected
                TypedValue[] filList = new TypedValue[1] { new TypedValue((int)DxfCode.Start, "INSERT") };
                SelectionFilter filter = new SelectionFilter(filList);
                PromptSelectionOptions opts = new PromptSelectionOptions();
                opts.MessageForAdding = "Select block references: ";
                PromptSelectionResult res = ed.GetSelection(opts, filter);

                //get the coordinates from user
                ppo.Message = "\n Select the place for print output:";
                ppr = acDoc.Editor.GetPoint(ppo);
                Point3d ptstart = ppr.Value;
                ppo.UseBasePoint = true;
                ppo.BasePoint = ptstart;
                if (ppr.Status == PromptStatus.Cancel) return;
                double x = ptstart.X;
                double y = ptstart.Y;

                double z = 0;
                // Do nothing if selection is unsuccessful
                if (res.Status != PromptStatus.OK)
                    return;

                SelectionSet selSet = res.Value;
                ObjectId[] idArray = selSet.GetObjectIds();
                foreach (ObjectId blkId in idArray)
                {
                    BlockReference blkRef = (BlockReference)tr.GetObject(blkId, OpenMode.ForRead);
                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(blkRef.BlockTableRecord, OpenMode.ForRead);
                    //ed.WriteMessage( "\nBlock: " + btr.Name );

                    btr.Dispose();

                    AttributeCollection attCol = blkRef.AttributeCollection;
                    foreach (ObjectId objId in attCol)
                    {
                        MText mtext = new MText();
                        mtext.Width = 6;
                        mtext.Location = new Point3d(x, y, z);
                        AttributeReference attRef = (AttributeReference)tr.GetObject(objId, OpenMode.ForRead);

                        string str = ("\n attribute is :" + attRef.TextString);
                        ed.WriteMessage(str);

                        var entity = tr.GetObject(objId, OpenMode.ForRead);

                        mtext.Contents = "\n" + attRef;
                        attCol.AppendAttribute(attRef);
                        tr.AddNewlyCreatedDBObject(mtext, true);
                    }
                }
                tr.Commit();
            }
            catch (Autodesk.AutoCAD.Runtime.Exception ex)
            {
                ed.WriteMessage(("Exception: " + ex.Message));
            }
            finally
            {
                tr.Dispose();
            }
        }
    }
}


What I have tried:

I am trying to print my output on my dwg file but mtext is not working output is not display on the dwg file.
Posted
Comments
@j@y123 29-Jul-16 0:59am    
The output is correct in command prompt but mtext is not display on dwg

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900