C# and Java APIs to View CAD Documents

CAD (Computer-Aided Design) files may contain 2D or 3D designs. They are generated by CAD software programs, which can be used to create models or architectural plans. 2D CAD files are often referred to as drawings, while 3D files are often called models, parts, or assemblies.

The AutoCAD is arguably the most prevalent CAD software on the market. It’s used by professionals and hobbyists alike. If you work with CAD, you’re likely to already be familiar with some of AutoCAD’s most common file formats, such as DWG and DXF

Render the CAD Files using GroupDocs.Viewer

The CAD file formats such as .dwg, .dgn, .dxf, .dwf, .ifc and .stl are also included in the wide range of GroupDocs.Viewer API’s supported file formats

Let’s pick a .dwg file and discuss some of the cases to view the using GroupDocs.Viewer API.

When CAD documents are rendered, the size of the rendering result is adjusted by API automatically depending on the size of the initial document.

You can also adjust the size of the resulting document by setting CadOptions in the following way.

  • Create ViewerHandler
  • Create ImageOptions
  • Set ImageOptions.CadOptions.Heightand ImageOptions.CadOptions.Width properties
  • Call GetPages

The Following code sample demonstrates it.

Java guys will write the code like below:

Rendering CAD Model along with Layouts

Representation of a CAD File with non-empty layouts

When CAD documents are rendered, by default we get only Model representation. In order to render Model and all non-empty Layouts within CAD document, the property CadOptions.RenderLayouts of ImageOptions and HtmlOptions is used.

Following are the steps to render all the non-empty layouts along with the Model.

  • Create HtmlOptions/ImageOptions 
  • Set CadOptions.RenderLayouts of HtmlOptions/ImageOptions to true
  • Call GetPages

The following code sample shows how to render layouts along with the Model of a CAD document.

Java guys will write the code like below:

Working with Layers

Using layers in CAD documents is a way of organizing objects in the drawing by associating them with a specific function or a purpose. For example, when we have a complex drawing of the building, all objects can be divided (associated) into several layers – e.g. electrical, water plumbing, furniture, walls and so on. According to your needs, you can temporarily hide or show some of the objects by turning off their layers. GroupDocs.Viewer allows you to get the list of layers from the drawing and supports rendering specified layers.

Getting List of Layers

The following are the steps to get the list of layer names from a CAD document.

  • Create ViewerHandler (ViewerHtmlHandler or ViewerImageHandler)
  • Get document’s information in DocumentInfoContainer object using GetDocumentInfo method
  • Cast the DocumentInfoContainer object returned by GetDocumentInfo with CadDocumentInfoContainer
  • Access each layer using CadDocumentInfoContainer.Layers collection

The following code sample shows how to get the list of the layers in a CAD document.

Java guys will write the code like below:

Rendering Specific Layers

After you have got the list of layers contained in the drawing, you can specify those that you want to render by adding layer names into the CadOptions.Layers property of corresponding RenderOptions (ImageOptions or HtmlOptions). Please note, when you do not specify layers, all layers are rendered. 

The following code sample shows how to render a specific layer of a CAD document.

Java guys will write the code like below:

The complete ready to run code sample is available on GitHub.