Find a location of specif text
Posted: 20 Jun 2019, 10:35
Hello
I would like to know how to find a location of specific text
I would like to know how to find a location of specific text
CADSoftTools - AutoCAD DWG DXF HPGL (PLT) SVG CGM STEP IGES STL SAT viewers, converters and developer tools. Delphi and C# source code.
https://cadsofttools.com/forum/
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);
}