Trabajar con texto
Trabajar con texto
Casi todos los dibujos contienen objetos de texto y estos ejemplos muestran cómo realizar operaciones con ellos. Existen diferentes tipos de entidades para DWG/DXF que pueden almacenar texto, son CadText, CadMText, CadAttDef, CadAttrib. Los dos últimos tipos están típicamente relacionados con CadInsertObject y se almacenan dentro de él o en el bloque correspondiente.
Aquí hay algunos ejemplos que describen operaciones con texto.
Buscar el texto
Este ejemplo muestra cómo encontrar valores de texto en el archivo DWG/DXF y también se puede utilizar para reemplazar valores de texto.
using (CadImage cadImage = (CadImage)Image.Load(fileName)) | |
{ | |
foreach (CadBaseEntity entity in cadImage.Entities) | |
{ | |
if (entity.GetType() == typeof(CadText)) | |
{ | |
CadText text = (CadText)entity; | |
System.Console.WriteLine(text.DefaultValue); | |
} | |
if (entity.GetType() == typeof(CadMText)) | |
{ | |
CadMText mtext = (CadMText)entity; | |
System.Console.WriteLine(mtext.FullClearText); | |
} | |
if (entity.GetType() == typeof(CadAttrib)) | |
{ | |
CadAttrib attrib = (CadAttrib)entity; | |
System.Console.WriteLine(attrib.DefaultText); | |
} | |
if (entity.GetType() == typeof(CadAttDef)) | |
{ | |
CadAttDef attdef = (CadAttDef)entity; | |
System.Console.WriteLine(attdef.DefinitionTagString); | |
} | |
if (entity.TypeName == CadEntityTypeName.INSERT) | |
{ | |
CadInsertObject insert = (CadInsertObject)entity; | |
CadBlockEntity block = cadImage.BlockEntities[insert.Name]; | |
foreach (CadBaseEntity blockEntity in block.Entities) | |
{ | |
if (blockEntity.GetType() == typeof(CadAttDef)) | |
{ | |
CadAttDef attdef = (CadAttDef)blockEntity; | |
System.Console.WriteLine(attdef.PromptString); | |
} | |
} | |
foreach (CadBaseEntity e in insert.ChildObjects) | |
{ | |
if (e.TypeName == CadEntityTypeName.ATTRIB) | |
{ | |
CadAttrib attrib = (CadAttrib)e; | |
System.Console.WriteLine(attrib.DefinitionTagString); | |
} | |
} | |
} | |
} | |
} |
Agregar nuevos elementos de Texto y MText
Aquí está el ejemplo de cómo agregar nuevos objetos de Texto/Mtext al dibujo. La adición de nuevas entidades puede cambiar el tamaño del dibujo, por lo que se recomienda llamar a UpdateSize() después de estas operaciones.
// adding of Text | |
CadText text = new CadText(); | |
text.DefaultValue = "Watermark text"; | |
text.TextHeight = 40; | |
text.FirstAlignment = new Cad3DPoint(300, 40); | |
text.LayerName = "0"; | |
cadImage.BlockEntities["*Model_Space"].AddEntity(text); | |
cadImage.UpdateSize(); | |
// adding of Mtext | |
CadMText watermark = new CadMText(); | |
watermark.Text = "Watermark message"; | |
watermark.InitialTextHeight = 40; | |
watermark.InsertionPoint = new Cad3DPoint(300, 40); | |
watermark.LayerName = "0"; | |
cadImage.BlockEntities["*Model_Space"].AddEntity(watermark); | |
cadImage.UpdateSize(); |