Computer-Aided Design - CAD files are normally used for 2D and 3D designs. These designs are generated by CAD software programs and are normally used to create models and architectural plans. If you have worked with CAD, you’re most likely familiar with some of AutoCAD’s file formats, such as DWG, DXF, DGN, DWF. This article will discuss how to programmatically view CAD files within Java applications.

The following topics are covered below in brief:

Java API to Render CAD files - DWG, DXF, DWF, DGN

GroupDocs.Viewer for Java is the API that allows rendering various documents and image files into HTML, Image, or PDF format to view these files within your Java application. The API supports more than 100 file formats to programmatically render as HTML, JPG, PNG, or PDF.

In this article, we will stick to the CAD files. In addition to the already mentioned DWG and DGN formats, you can further view AutoCAD formats such as DWF, DWT, DXF, and also IFC, STL, IGS, CF2, Plotter document (PLT, HPG) files within your java applications.

Download and Configure

Get the library from downloads or just add the following pom.xml configuration in your Maven-based Java applications to try the below-mentioned examples. For the details, you may visit the API Reference.

<repository>
	<id>GroupDocsArtifactRepository</id>
	<name>GroupDocs Artifact Repository</name>
	<url>https://repository.groupdocs.com/repo/</url>
</repository>
<dependency>
        <groupId>com.groupdocs</groupId>
        <artifactId>groupdocs-viewer</artifactId>
        <version>21.2</version> 
</dependency>

Convert CAD Drawings to Render as HTML, PNG, JPG, or PDF in Java

The API allows rendering the CAD documents to HTML, JPG, PNG, and PDF formats. In this article, I am sticking to the DWG format for the conversion and rendering to other formats using examples. To start with, let’s convert the DWG design and render it as HTML with embedded as well as external resource options.

Convert DWG to HTML with Embedded Resources

The following are the steps of how to convert the DWG file to render as HTML.

The following source code converts the DWG file and renders it as HTML with embedded resources in Java.

Convert DWG to HTML with External Resources

The following are the steps of how to convert the DWG file to render as HTML file(s) and with external resources.

The following source code renders the DWG file as HTML with external resources in Java.

Convert DWG to PDF, JPG, and PNG in Java

Similar to the conversion to HTML format, DWG files can be rendered as PDF, JPG, and PNG format using the respective ViewOptions as follows:

Get Layouts and Layers of DWG in Java

As CAD files may consist of multiple layouts and layers, you can easily get their layouts and layers using the following steps.

The following code shows how to get all the layouts and layers of the DWG file using Java.

Render CAD Layers of DWG file in Java

By default, all the layers of the CAD drawing get rendered as shown above. However, you can render any specific layer of DWG by selecting the chosen ones using the setLayers method of the Java API as shown below.

  • Initialize the Viewer class object using the source .dwg file.
  • Instantiate HtmlViewOptions.
  • Add the layer(s) to render using setLayers method of CadOptions.
  • Render .dwg to HTML using view method.

The following code renders the layers of a CAD file of DWG format in Java.

Render CAD Layouts of DWG file in Java

When we render the CAD drawing, we only get the model presentation by default. To render the model as well as all the non-empty layouts, we just have to set the RenderLayout property of CadOptions as true.

  • Initialize the Viewer class object using the source .dwg file.
  • Instantiate HtmlViewOptions.
  • Set the RenderLayout property of CadOptions to true.
  • Render .dwg as HTML using view method.

The following code renders all the non-empty layouts along with the model of a CAD drawing with DWG format in Java.

Get a Free API License

You can get a free temporary license in order to use the API without evaluation limitations.

Conclusion

In this article, you have learned how to view CAD files within Java applications. I hope you will be confident to build your own CAD Viewer using Java. You can further show models, layouts, and layers of CAD files within the application. You can learn more about GroupDocs.Viewer for Java using the documentation. In case you would have any queries, feel free to let us know via our forum.

See Also