How to retrieve text in a Table?

Jun 13, 2016 at 11:44 PM
The .Texts and .MTexts do not seem to have my text in tables. How do I get the text inside a Table?
Coordinator
Jun 14, 2016 at 5:42 PM
The Table is not one of the supported entities. You could check if your drawing contains blocks with the name "*T#", AutoCad uses anonymous blocks to represent a table, and even if that block will have no users it contains the lines and mtexts entities that make it up. Besides that you can explode it before saving it to a file and read it normally.

Daniel
Jun 16, 2016 at 3:44 PM
Perfect. Thanks so much. This is a quick version (but it works) so take it with a grain of salt but we are trying for a "Find" option like:
static List<DxfObject> FindObjectByText(DxfDocument myDoc, string findText, bool contains = true)
{
    var myList = new List<DxfObject>();

    // FIRST LOOK IN ALL MTEXTS
    foreach (var texts in myDoc.MTexts)
    {
        if (contains) {
            if (texts.Value.Contains(findText)) myList.Add(texts);
        } else {
            if (texts.Value == findText) myList.Add(texts);
        }
    }

    // Some MTexts are hidding in Tables which are BLOCK Entities so look inside those
    foreach (var block in myDoc.Blocks)
    {
        foreach (var entity in block.Entities)
        {
            if (contains) {
                if (entity is MText && ((MText)entity).Value.Contains(findText)) myList.Add((MText)entity);
                if (entity is Text && (entity as Text).Value.Contains(findText)) myList.Add((Text)entity);
            } else {
                if (entity is MText && (entity as MText).Value == findText) myList.Add((MText)entity);
                if (entity is Text && (entity as Text).Value == findText) myList.Add((Text)entity);
            }
        }
    }

    // TEXTS
    foreach (var texts in myDoc.Texts)
    {
        if (contains) {
            if (texts.Value.Contains(findText)) myList.Add(texts);
        } else {
            if (texts.Value == findText) myList.Add(texts);
        }
    }

    // LAYERS
    foreach (var layer in myDoc.Layers)
    {
        if (contains) {
            if (layer.Name.Contains(findText)) myList.Add(layer);
        } else {
            if (layer.Name == findText) myList.Add(layer);
        }
    }

    // TOLERANCES
    foreach (var tol in myDoc.Tolerances)
    {
        if (contains) {
            if (tol.Entry1.Tolerance1 == null && tol.Entry1.Tolerance2 == null) continue;
            if (tol.Entry1.Datum1 != null && tol.Entry1.Datum1.Value.Contains(findText)) { myList.Add(tol); continue; }
            if (tol.Entry1.Tolerance1 != null && tol.Entry1.Tolerance1.Value.Contains(findText)) { myList.Add(tol); continue; }
            if (tol.Entry2 != null && tol.Entry2.Tolerance1 != null && tol.Entry2.Tolerance1.Value.Contains(findText)) myList.Add(tol);
        }  else {
            if (tol.Entry1.Tolerance1 == null && tol.Entry1.Tolerance2 == null) continue;
            if (tol.Entry1.Datum1 != null && tol.Entry1.Datum1.Value == findText) { myList.Add(tol); continue; }
            if (tol.Entry1.Tolerance1 != null && tol.Entry1.Tolerance1.Value == findText) { myList.Add(tol); continue; }
            if (tol.Entry2 != null && tol.Entry2.Tolerance1 != null && tol.Entry2.Tolerance1.Value == findText)  myList.Add(tol);
        }
    }

    // DIMENSIONS
    foreach (var dim in myDoc.Dimensions)
    {
        if (contains) {
            if (dim.Block.Name.Contains(findText) || Convert.ToDecimal(dim.Measurement).ToString("g7").Contains(findText)) myList.Add(dim);
            if (dim.UserText != null && dim.UserText.Contains(findText)) myList.Add(dim);
        } else {
            if (dim.Block.Name == findText || Convert.ToDecimal(dim.Measurement).ToString("g7") == findText) myList.Add(dim);
            if (dim.UserText != null && dim.UserText == findText) myList.Add(dim);
        }
    }

    return myList; // List<DxfObject>
}