텍스트 작업
텍스트 작업
거의 모든 그림은 텍스트 객체를 포함하고 있으며, 이 예제는 그것들과 함께 작업하는 방법을 보여줍니다. DWG/DXF에서 텍스트를 저장할 수 있는 다양한 유형의 엔티티가 있으며, 이들은 CadText, CadMText, CadAttDef, CadAttrib입니다. 마지막 두 유형은 일반적으로 CadInsertObject와 관련이 있으며, 그 안이나 해당 블록에 저장됩니다.
다음은 텍스트와의 작업을 설명하는 몇 가지 예제입니다.
텍스트 검색
이 예제에서는 DWG/DXF 파일에서 텍스트 값을 찾는 방법을 보여주며, 텍스트 값 교체에도 사용할 수 있습니다.
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()를 호출하는 것이 권장됩니다.
// 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(); |