Author Topic: Get the MText object in a cell  (Read 915 times)

0 Members and 1 Guest are viewing this topic.

Offline (gile)

  • C#
  • *
  • Posts: 87
  • Karma: +8/-0
  • Gender: Male
    • prefered language: F
    • Prog expertise: Good
    • View Profile
Get the MText object in a cell
« on: October 13, 2012, 06:40:44 PM »
Hi,

Here's a little class which extends the Cell type with a method to get the MText object the cell contains (if any, returns null otherwise).

Code: [Select]
using System.Linq;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;

namespace TableSample
{
    static class Extensions
    {
        // Retourne l'objet MText contenu dans la cellule (ou null s'il nexiste pas).
        // Returns the MText object contained in the cell (or null if it don't exist).
        public static MText GetMtext(this Cell cell)
        {
            Table table = cell.ParentTable;
            Extents3d extents = cell.GetExtents().ToExtents3d();
            extents.TransformBy(table.BlockTransform.Inverse());
            BlockTableRecord btr = (BlockTableRecord)table.BlockTableRecord.GetObject(OpenMode.ForRead);
            RXClass mtextClass = RXClass.GetClass(typeof(MText));
            foreach (ObjectId id in btr)
            {
                if (id.ObjectClass == mtextClass)
                {
                    MText mtext = (MText)id.GetObject(OpenMode.ForRead);
                    if (mtext.Location.IsInside(extents))
                        return mtext;
                }
            }
            return null;
        }

        // Evalue si un point est à l'intérieur de la boite (BoundingBox).
        // Evaluates if the point is within the extents (BoundingBox).
        private static bool IsInside(this Point3d pt, Extents3d extents)
        {
            return
                pt.X >= extents.MinPoint.X &&
                pt.Y >= extents.MinPoint.Y &&
                pt.Z >= extents.MinPoint.Z &&
                pt.X <= extents.MaxPoint.X &&
                pt.Y <= extents.MaxPoint.Y &&
                pt.Z <= extents.MaxPoint.Z;
        }

        // Retourne la boite (BoundingBox) définie par la collection de points.
        // Returns the extents (BoundingBox) defined by the points collection
        private static Extents3d ToExtents3d(this Point3dCollection pts)
        {
            return pts.Cast<Point3d>()
                .Aggregate(new Extents3d(pts[0], pts[0]), (ext, pt) =>
                { ext.AddPoint(pt); return ext; });
        }
    }
}

Using example, assuming table is an AutoCAD Table object, row and column the indices of the cell row and column:
Code: [Select]
MText mtext = table.Cells[row, column].GetMtext();
« Last Edit: October 13, 2012, 06:50:59 PM by (gile) »

Offline fixo

  • Full Member
  • ***
  • Posts: 135
  • Karma: +4/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Good
    • View Profile
Re: Get the MText object in a cell
« Reply #1 on: June 01, 2013, 10:45:54 PM »
In AutoCAD 2010 it might be resolved like this
Code: [Select]
Cell cell = mytable.Cells[row, column];

 string celltext = cell.GetTextString(FormatOption.IgnoreMtextFormat);