
DWG 绘图文件是存储有趣的2D和3D设计数据至关重要,通常由设计师,工程师和建筑师使用。 如果您正在寻找在这些文件中找到一个特定的文本链,您已经到达了正确的位置。 在本文中,我们将通过在DWG文件中的文字搜索过程编程使用C#。
内容表
C# API 设置为 DWG 文本搜索
要开始,你需要 ASPOSE.CAD 为 .NET 图书馆. 这个强大的 .NET 插件允许您创建和操纵各种 CAD 绘图,包括 DWG 和 DXF 文件。 下载 部分,或者您可以轻松地使用下列 诺基亚 命令:
PM> Install-Package Aspose.CAD
在 DWG 文件中搜索文本的步骤指南
遵循以下步骤,以有效地使用 C# 在 DWG 文件中搜索文本:
- 下载现有 DWG 文件:使用
CadImage
您可以下载 DWG 文件。 - 通过 CadText 实体进行 iterate: 搜索您想要的文本
CadText
在绘画中存在的实体。 - 在特定配置中搜索:如有必要,将搜索缩小到 DWG 文件中的特定配置。
- 出口到 PDF 格式:可选,将修改的文件出口到 PDF 以便更容易查看。
完整的 C# 代码示例
下面是一个代码剪辑,展示如何使用C#在DWG文件中搜索文本:
This file contains hidden or 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
// Load an existing DWG file as CadImage. | |
using (CadImage cadImage = (CadImage)Aspose.CAD.Image.Load(sourceFilePath)) | |
{ | |
// Search for text in the file | |
foreach (CadBaseEntity entity in cadImage.Entities) | |
{ | |
// We iterate through CadText entities, 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 | |
CadLayoutDictionary layouts = cadImage.Layouts; | |
string[] layoutNames = new string[layouts.Count]; | |
int i = 0; | |
foreach (CadLayout layout in layouts.Values) | |
{ | |
layoutNames[i++] = layout.LayoutName; | |
System.Console.WriteLine("Layout " + layout.LayoutName + " is found"); | |
// Find block, applicable for DWG only | |
CadBlockTableObject blockTableObjectReference = null; | |
foreach (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 | |
CadBlockEntity cadBlockEntity = cadImage.BlockEntities[blockTableObjectReference.BlockName]; | |
} | |
} | |
// Export to PDF | |
CadRasterizationOptions rasterizationOptions = new CadRasterizationOptions(); | |
rasterizationOptions.PageWidth = 1600; | |
rasterizationOptions.PageHeight = 1600; | |
rasterizationOptions.AutomaticLayoutsScaling = true; | |
// If cadBlockEntity collection for selected layout or entitiesOnLayouts collection by layout's BlockTableRecordHandle (for dxf) is empty | |
rasterizationOptions.Layouts = new[] { "Layout1" }; | |
ImageOptions.PdfOptions pdfOptions = new PdfOptions(); | |
pdfOptions.VectorRasterizationOptions = rasterizationOptions; | |
cadImage.Save("SearchText_CAD.pdf", pdfOptions); | |
} | |
public static void SearchTextInDWGAutoCADFile() | |
{ | |
// The path to the documents directory. | |
string sourceFilePath = "search.dwg"; | |
// Load an existing DWG file as CadImage. | |
CadImage cadImage = (CadImage)Aspose.CAD.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; | |
} | |
} |
获得您的免费临时许可证
解锁 Aspose.CAD API 的全部潜力,无需任何限制,请求 免费临时许可证.
结论(结论)
在这篇文章中,您将学习如何使用 C# 编程搜索 DWG 文件中的文本。 Aspose 插件提供了一个可靠的解决方案,用于使用 CAD 檔案,提供多种功能,以改善工作流。 阿波斯文档如果您有任何疑问或需要帮助,请自由地在我们的 阿斯波斯论坛.
通过利用 Aspose.CAD 图书馆的功能仅为 99 美元,您可以显著简化您的 CAD 数据处理任务。