Vyhledávání textu v souboru DWG AutoCAD
Contents
[
Hide
]Vyhledávání textu v souboru DWG AutoCAD
Aspose.CAD pro API .NET vám umožňuje vyhledávat text v souboru DWG AutoCAD. API Aspose.CAD vystavuje třídu CadText, která reprezentuje textové entity v souboru DWG AutoCAD. Třída CadMText je také zahrnuta v API Aspose.CAD, protože některé další entity mohou také obsahovat text. Následuje ukázka kódu pro vyhledávání textu v souboru DWG AutoCAD. Úryvek kódu je samo o sobě vysvětlující.
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
// The path to the documents directory. | |
string MyDir = RunExamples.GetDataDir_DWGDrawings(); | |
string sourceFilePath = MyDir + "search.dwg"; | |
// Load an existing DWG file as CadImage. | |
using (Aspose.CAD.FileFormats.Cad.CadImage cadImage = (Aspose.CAD.FileFormats.Cad.CadImage)Aspose.CAD.Image.Load(sourceFilePath)) | |
{ | |
// Search for text in the file | |
foreach (Aspose.CAD.FileFormats.Cad.CadObjects.CadBaseEntity entity in cadImage.Entities) | |
{ | |
// Please, note: we iterate through CadText entities here, but some other entities may contain text also, e.g. CadMText and others | |
IterateCADNodes(entity); | |
} | |
// Search for text on specific layout get all layout names and link each layout with corresponding block with entities | |
Aspose.CAD.FileFormats.Cad.CadLayoutDictionary layouts = cadImage.Layouts; | |
string[] layoutNames = new string[layouts.Count]; | |
int i = 0; | |
foreach (Aspose.CAD.FileFormats.Cad.CadObjects.CadLayout layout in layouts.Values) | |
{ | |
layoutNames[i++] = layout.LayoutName; | |
System.Console.WriteLine("Layout " + layout.LayoutName + " is found"); | |
// Find block, applicable for DWG only | |
Aspose.CAD.FileFormats.Cad.CadTables.CadBlockTableObject blockTableObjectReference = null; | |
foreach (Aspose.CAD.FileFormats.Cad.CadTables.CadBlockTableObject tableObject in cadImage.BlocksTables) | |
{ | |
if (string.Equals(tableObject.HardPointerToLayout, layout.ObjectHandle)) | |
{ | |
blockTableObjectReference = tableObject; | |
break; | |
} | |
} | |
if (blockTableObjectReference !=null) | |
{ | |
// Collection cadBlockEntity.Entities contains information about all entities on specific layout if this collection has no elements it means layout is a copy of Model layout and contains the same entities | |
Aspose.CAD.FileFormats.Cad.CadObjects.CadBlockEntity cadBlockEntity = cadImage.BlockEntities[blockTableObjectReference.BlockName]; | |
} | |
} | |
//Export to pdf | |
Aspose.CAD.ImageOptions.CadRasterizationOptions rasterizationOptions = new Aspose.CAD.ImageOptions.CadRasterizationOptions(); | |
rasterizationOptions.PageWidth = 1600; | |
rasterizationOptions.PageHeight = 1600; | |
rasterizationOptions.AutomaticLayoutsScaling = true; | |
// Please, note: if cadBlockEntity collection mentioned above (for dwg) for selected layout or entitiesOnLayouts collection by layout's BlockTableRecordHandle (for dxf) is empty - export result file will be empty and you should draw Model layout instead | |
rasterizationOptions.Layouts = new[] { "Layout1" }; | |
Aspose.CAD.ImageOptions.PdfOptions pdfOptions = new Aspose.CAD.ImageOptions.PdfOptions(); | |
pdfOptions.VectorRasterizationOptions = rasterizationOptions; | |
cadImage.Save(MyDir + "SearchText_out.pdf", pdfOptions); | |
} | |
} | |
public static void SearchTextInDWGAutoCADFile() | |
{ | |
// The path to the documents directory. | |
string MyDir = RunExamples.GetDataDir_DWGDrawings(); | |
string sourceFilePath = MyDir + "search.dwg"; | |
// Load an existing DWG file as CadImage. | |
CadImage cadImage = (CadImage) Image.Load(sourceFilePath); | |
// Search for text in the entities section | |
foreach (var entity in cadImage.Entities) { | |
IterateCADNodes(entity); | |
} | |
// Search for text in the block section | |
foreach (CadBlockEntity blockEntity in cadImage.BlockEntities.Values) { | |
foreach (var entity in blockEntity.Entities) { | |
IterateCADNodes(entity); | |
} | |
} | |
} | |
private static void IterateCADNodes(CadBaseEntity obj) | |
{ | |
switch (obj.TypeName) { | |
case CadEntityTypeName.TEXT: | |
CadText childObjectText = (CadText) obj; | |
Console.WriteLine(childObjectText.DefaultValue); | |
break; | |
case CadEntityTypeName.MTEXT: | |
CadMText childObjectMText = (CadMText) obj; | |
Console.WriteLine(childObjectMText.Text); | |
break; | |
case CadEntityTypeName.INSERT: | |
CadInsertObject childInsertObject = (CadInsertObject) obj; | |
foreach (var tempobj in childInsertObject.ChildObjects) { | |
IterateCADNodes(tempobj); | |
} | |
break; | |
case CadEntityTypeName.ATTDEF: | |
CadAttDef attDef = (CadAttDef) obj; | |
Console.WriteLine(attDef.DefaultString); | |
break; | |
case CadEntityTypeName.ATTRIB: | |
CadAttrib attAttrib = (CadAttrib) obj; | |
Console.WriteLine(attAttrib.DefaultText); | |
break; | |
} | |
} | |
} |