loading...

Introduction to Aspose.CAD, Part 5

nnevod profile image Dmitry Matuzko ・2 min read

Part 1

Part 2

Part 3

Part 4

In this part I will again continue to describe Aspose.CAD features specific to DWG\DXF files, as these file formats are the most common use for Aspose.CAD.

Export of 3D objects

To export 3D objects from DWG\DXF file, just set TypeOfEntities property of CadRasterizationOptions instance to TypeOfEntities.Entities3D and perform export:

using (Aspose.CAD.Image cadImage = Aspose.CAD.Image.Load("source.dxf"))
{
    Aspose.CAD.ImageOptions.CadRasterizationOptions rasterizationOptions 
        = new Aspose.CAD.ImageOptions.CadRasterizationOptions();
    rasterizationOptions.PageWidth = 500;
    rasterizationOptions.PageHeight = 500;
    rasterizationOptions.TypeOfEntities = TypeOfEntities.Entities3D;


    PdfOptions pdfOptions = new PdfOptions();
    pdfOptions.VectorRasterizationOptions = rasterizationOptions;

    cadImage.Save("export3d.pdf", pdfOptions);
}

Searching the text in a CAD file

This is a bit more complex task, as there are several kinds of entities that might contain text, including nested entities. Here is a code snippet that will show how to handle all of them:


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; 

          //Iterate over subnodes
          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; 
  } 
}

And to iterate over both block and non-block entities, use this code:

public static void SearchTextInDWGAutoCADFile(CadImage cadImage) 
{ 

   // 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); 
       } 
   } 
} 

For now, that's all, stay tuned!

For more examples please visit the Aspose.CAD GitHub page. There's also Twitter and Facebook pages for news on Aspose.CAD.

Posted on by:

Discussion

pic
Editor guide