Page 1 of 1

Find a location of specif text

Posted: 20 Jun 2019, 10:35
by dlwotj4274
Hello

I would like to know how to find a location of specific text

Re: Find a location of specif text

Posted: 20 Jun 2019, 18:48
by support
Hello,

Is the text located on a certain CAD drawing layout (Model, Layout1, etc.) or in a block?

Mikhail

Re: Find a location of specif text

Posted: 21 Jun 2019, 03:37
by dlwotj4274
both. I want to know location of specific text in layout and block.

Re: Find a location of specif text

Posted: 27 Jun 2019, 18:30
by support
Hello,

Please try the code below:

Code: Select all

private static CADText FindCADTextByValue(CADEntityCollection entities, string value)
{
    Predicate<CADEntity> cadTextEntities = new Predicate<CADEntity>(ent => ent.EntType == EntityType.Text);
    Predicate<CADEntity> cadTextByValue = new Predicate<CADEntity>(ent => (ent as CADText).Text == value);
    return entities.FindAll(cadTextEntities).Find(cadTextByValue) as CADText;
}

public static DPoint GetTextLocation(CADImage cadImage, string text, string cadBlockName = "")
{
    CADText cadText = null;   
    Predicate<CADEntity> cadInsertEntities = new Predicate<CADEntity>(ent => ent.EntType == EntityType.Insert);
    Predicate<CADEntity> cadAttribByValue = new Predicate<CADEntity>(ent => (ent as CADAttrib).Value == text);

    if (cadBlockName != "")
    {
        // Search a CADBlock by a given name
        CADBlock cadBlock = cadImage.Converter.BlockByName(cadBlockName);
        if (cadBlock != null)
        {
            // Search the text in the given CADBlock
            cadText = FindCADTextByValue(cadBlock.Entities, text);
            if (cadText != null)
                return cadText.Point;
        }
    }
    else
    {
        // Search the text on the current layout
        cadText = FindCADTextByValue(cadImage.CurrentLayout.Entities, text);
        if (cadText != null)
            return cadText.Point;
        else
        {
            // Search the text in CADInsert objects on the current layout
            var cadInserts = cadImage.CurrentLayout.Entities.FindAll(cadInsertEntities);
            foreach (CADEntity ent in cadInserts)
            {
                CADInsert cadInsert = ent as CADInsert;
                // Search the text in the CADInsert's block
                cadText = FindCADTextByValue(cadInsert.Block.Entities, text);
                if (cadText != null)
                    return DPoint.Offset(cadText.Point, cadInsert.Point, true);
                else
                {
                    // Search the text in the CADInsert attributes
                    CADAttrib cadAttrib = cadInsert.Attribs.Find(cadAttribByValue) as CADAttrib;
                    if (cadAttrib != null)
                        return cadAttrib.Point;
                }
            }
        }
    }           
    return new DPoint(0, 0, 0); 
}
cadBlockName parameter in the GetTextLocation method is optional and should be specified when you need to search a specific text in the block definition (CADBlock) with a given name.

Mikhail