Find a location of specif text

Discuss and ask questions about CAD .NET library.

Moderators: SDS, support, admin

Post Reply
dlwotj4274
Posts: 13
Joined: 19 Jun 2019, 07:30

Find a location of specif text

Post by dlwotj4274 » 20 Jun 2019, 10:35

Hello

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

support
Posts: 3271
Joined: 30 Mar 2005, 11:36
Contact:

Re: Find a location of specif text

Post by support » 20 Jun 2019, 18:48

Hello,

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

Mikhail
Technical Support E-mail: support@cadsofttools.com
Chat support on Skype: cadsofttools.support

dlwotj4274
Posts: 13
Joined: 19 Jun 2019, 07:30

Re: Find a location of specif text

Post by dlwotj4274 » 21 Jun 2019, 03:37

both. I want to know location of specific text in layout and block.

support
Posts: 3271
Joined: 30 Mar 2005, 11:36
Contact:

Re: Find a location of specif text

Post by support » 27 Jun 2019, 18:30

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
Technical Support E-mail: support@cadsofttools.com
Chat support on Skype: cadsofttools.support

Post Reply