与文本合作
Contents
[
Hide
]与文本合作
几乎所有的图形都包含文本对象,这些例子展示了如何对它们执行操作。 DWG/DXF 中有不同类型的实体可以存储文本,它们是 CadText, CadMText, CadAttDef,CadAttrib。最后两种类型通常与 CadInsertObject 相关,并存储在其中或相应的块中。
以下是一些描述与文本操作的示例。
搜索文本
这个例子展示了如何在 DWG/DXF 文件中查找文本值,也可用于替换文本值。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} | |
} | |
} |
添加新的文本和 MText 项目
以下示例展示了如何向绘图中添加新的文本/MText 对象。添加新实体可能会改变绘图的大小,因此建议在这些操作后调用 UpdateSize()。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); |