Aspose.CAD for .Net是一款专门针对CAD文件操作处理的控件,以前该功能包含在了
Aspose.Imaging中,Aspose公司为了更好地完善CAD文件的处理功能,专门把该功能从
Aspose.Imaging中剥离出来作为单独的产品,现在开发人员可以利用该控件更好地对CAD文件进行各种操作处理和转换了,下面的代码主要演示了如何快速从CAD文件中进行相关文本的搜索,具体如下:
/ Load an existing DWG file as CadImage.
using (Aspose.CAD.FileFormats.Cad.CadImage cadImage = (Aspose.CAD.FileFormats.Cad.CadImage)Aspose.CAD.Image.Load("sample.dwg"))
{
// 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
if (entity.GetType() == typeof(Aspose.CAD.FileFormats.Cad.CadObjects.CadText))
{
Aspose.CAD.FileFormats.Cad.CadObjects.CadText text = (Aspose.CAD.FileFormats.Cad.CadObjects.CadText)entity;
System.Console.WriteLine(text.DefaultValue);
}
}
// 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;
}
}
// 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;
rasterizationOptions.CenterDrawing = 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("sample_dwg_out.pdf", pdfOptions);
}