Convert CAD Drawings to Raster Image Formats Introduction
Raster images such as JPEG, PNG, TIFF, BMP are some of the widely used file formats and as we are aware that in order to view CAD files, we need specific software to view and edit such files. But in order to share and view the files without any dependency over any specific application, one of the viable solutions is to render the DWG into raster image formats and share across multiple platforms. Aspose.CAD Cloud provides the capabilities to perform this transformation in the cloud.
This article explains how to convert CAD drawing/image to raster image format. You can convert CAD Drawings to BMP, PNG, JPG, JPEG, JPEG2000, TIF, TIFF, PSD, GIF and WMF formats. Aspose.CAD provides the following two APIs to perform the said task:
GET /cad/{name}/saveAs
POST /cad/saveAs
Resource URI
Aspose.CAD Cloud APIs Swagger UI lets you call these APIs directly from the browser. The description of the APIs and their parameters are also given there.
cURL Example
Option 1: Export existing drawing to another format
Option 2: Export existing images to another format. Image is passed as a request body
Input Document:
Output Document:
SDKs
Using an SDK (API client) is the quickest way for a developer to speed up the development. An SDK takes care of a lot of low-level details of making requests and handling responses and lets you focus on writing code specific to your particular project. Checkout our GitHub repository for a complete list of Aspose.CAD SDKs along with working examples, to get you started in no time. Please check Available SDKs article to learn how to add an SDK to your project.
SDK Examples
Option 1: Export existing drawing to another format
C#
This file contains 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
// For complete examples and data files, please go to https://github.com/aspose-cad-cloud/aspose-cad-cloud-dotnet/
using Aspose.CAD.Cloud.Sdk.Api;
using Aspose.CAD.Cloud.Sdk.Model.Requests;
namespace Aspose.CAD_Cloud_APIs_Examples
{
public class CADExamples
{
protected string AppKey = ""; // Get App Key and App SID from https://dashboard.aspose.cloud/
protected string AppSid = ""; // Get App Key and App SID from https://dashboard.aspose.cloud/
static void Main(string[] args)
{
CADExamples cadExamples = new CADExamples();
// Export existing drawing to another format
cadExamples.GetImageSaveAs();
}
// Export existing drawing to another format
private void GetImageSaveAs()
{
CadApi cadApi = new CadApi(AppKey, AppSid);
string fileName = "01.026.385.01.0.I SOPORTE ENFRIADOR.dwg";
// Possible values are jpg, bmp, psd, tiff, gif, png, j2k, wmf and pdf
string formatToExport = "pdf";
string destFileName = "01.026.385.01.0.I SOPORTE ENFRIADOR.pdf";
// Upload document to Cloud Storage
uploadFileToCloudStorage(fileName);
var request = new GetDrawingSaveAsRequest(fileName, formatToExport);
var responseStream = cadApi.GetDrawingSaveAs(request);
// Save the output file to disk
saveFileToDisk(responseStream, destFileName);
}
private string pathToDataDir()
{
string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
var folders = Path.GetDirectoryName(baseDirectory).Split('\\').ToList();
// Get Path to the TestData directory
var foldersUpToRootDir = folders.Take(folders.Count - 5);
var pathToRootDir = string.Join("\\", foldersUpToRootDir);
return Path.Combine(pathToRootDir, "TestData");
}
private void uploadFileToCloudStorage(String fileName)
{
CadApi storageApi = new CadApi(AppKey, AppSid);
string dataFilePath = Path.Combine(pathToDataDir(), fileName);
var putCreateRequest = new UploadFileRequest(fileName, File.OpenRead(dataFilePath));
storageApi.UploadFile(putCreateRequest);
}
private void saveFileToDisk(Stream responseStream, String destFileName)
{
var fileStream = File.Create(Path.Combine(pathToDataDir(), "Output", destFileName));
responseStream.Seek(0, SeekOrigin.Begin);
responseStream.CopyTo(fileStream);
fileStream.Close();
}
}
}
Java
This file contains 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
public void getImageSaveAsTest(String formatExtension, Boolean saveResultToStorage, String... additionalExportFormats) throws Exception {
String name = null;
String folder = CloudTestFolder;
String storage = DefaultStorage;
String outName = null;
ArrayList<String> formatsToExport = new ArrayList<String>();
Collections.addAll(formatsToExport, this.BasicExportFormats);
for (String additionalExportFormat : additionalExportFormats)
{
if (additionalExportFormat != null && !additionalExportFormat.trim().equals("") && !formatsToExport.contains(additionalExportFormat))
{
formatsToExport.add(additionalExportFormat);
}
}
for (FileResponse inputFile : InputTestFiles)
{
if (inputFile.getName().endsWith(formatExtension))
{
name = inputFile.getName();
}
else
{
continue;
}
for (String format : formatsToExport)
{
outName = name + "." + format;
getImageSaveAsRequest = new GetDrawingSaveAsRequest(name, format, folder, outName, storage);
Method propertiesTester = SaveAsApiTests.class.getDeclaredMethod("getImageSaveAsPropertiesTester", CadResponse.class, CadResponse.class);
propertiesTester.setAccessible(true);
Method requestInvoker = SaveAsApiTests.class.getDeclaredMethod("getImageSaveAsGetRequestInvoker", String.class, String.class);
requestInvoker.setAccessible(true);
this.testGetRequest(
"getImageSaveAsTest; save result to storage: " + saveResultToStorage,
saveResultToStorage,
String.format("Input image: %s; Output format: %s", name, format),
name,
outName,
requestInvoker,
propertiesTester,
folder,
storage);
}
}
}
PHP
This file contains 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
/**
*
* Convert drawing to a specified format with detailed settings and saves result to storage.
* For complete examples, please visit https://github.com/aspose-cad-cloud/aspose-cad-cloud-php
*/
<?php
public function getDrawingSaveAs()
{
$localName = "galeon.stl";
$outputFormat = "jpg";
$remoteName = $localName;
$subfolder = "";
$fullName = self::$baseRemoteFolder . $subfolder . $remoteName;
$destName = self::$baseTestOut . $remoteName ."." . $outputFormat;
$file = realpath(__DIR__ . self::$relativeRootPath) . '/TestData/' . $localName;
$putRequest = new \Aspose\Storage\Model\Requests\PutCreateRequest($fullName, $file);
$this->storage->PutCreate($putRequest);
$request = new \Aspose\CAD\Model\Requests\GetDrawingSaveAsRequest($remoteName, $outputFormat, $folder=trim(self::$baseRemoteFolder . $subfolder), null, null);
list($response, $code, $headers) = $this->CAD->getDrawingSaveAsWithHttpInfo($request);
Assert::assertEquals(200, $code);
}
?>
Python
This file contains 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
# For complete examples and data files, please go to https://github.com/aspose-cad-cloud/aspose-cad-cloud-python/
from asposecadcloud import CadApi
import asposecadcloud.models.requests as Requests
import json as json
class GetImageSaveAs(object):
def __init__(self):
# Setup CAD and Storage API clients
with open("config.json") as f:
server_file_info = json.load(f)
api_key = server_file_info['AppKey']
api_sid = server_file_info['AppSid']
base_url = server_file_info['BaseUrl']
self.cad_api = CadApi(api_key, api_sid, base_url)
self.storage_api = CadApi(api_key, api_sid, base_url)
def save_file_to_storage(self, filename):
local_folder = "data"
remote_file_name = filename
storage_name = ""
# Upload file to remote storage
f = open(local_folder + "/" + filename, "rb").read()
upload_request = Requests.UploadFileRequest(remote_file_name, f, storage_name)
self.storage_api.upload_file(upload_request)
def get_image_save_as(self, filename):
self.save_file_to_storage(filename)
# Possible values are jpg, bmp, psd, tiff, gif, png, j2k, wmf and pdf
output_format = "pdf"
destFileName = "result.pdf"
# Convert image to PDF
save_as_request = Requests.GetDrawingSaveAsRequest(filename, output_format, out_path=destFileName)
resized_file = self.cad_api.get_drawing_save_as(save_as_request)
return resized_file
obj=GetImageSaveAs()
print(obj.get_image_save_as("01.026.385.01.0.I SOPORTE ENFRIADOR.dwg"))
Option 2: Export existing images to another format. Image is passed as a request body
C#
This file contains 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
// For complete examples and data files, please go to https://github.com/aspose-cad-cloud/aspose-cad-cloud-dotnet/
using Aspose.CAD.Cloud.Sdk.Api;
using Aspose.CAD.Cloud.Sdk.Model.Requests;
namespace Aspose.CAD_Cloud_APIs_Examples
{
class CADExamples
{
protected string AppKey = ""; // Get App Key and App SID from https://dashboard.aspose.cloud/
protected string AppSid = ""; // Get App Key and App SID from https://dashboard.aspose.cloud/
static void Main(string[] args)
{
CADExamples cadExamples = new CADExamples();
// Export existing image to another format. Image is passed as request body
cadExamples.PostImageSaveAs();
}
// Export existing image to another format. Image is passed as request body
private void PostImageSaveAs()
{
CadApi cadApi = new CadApi(AppKey, AppSid);
string fileName = "01.026.385.01.0.I SOPORTE ENFRIADOR.dwg";
// Possible values are jpg, bmp, psd, tiff, gif, png, j2k, wmf and pdf
string formatToExport = "pdf";
string dataFilePath = Path.Combine(pathToDataDir(), fileName);
string destFileName = "01.026.385.01.0.I SOPORTE ENFRIADOR.pdf";
var request = new PostDrawingSaveAsRequest(File.OpenRead(dataFilePath), formatToExport);
var responseStream = cadApi.PostDrawingSaveAs(request);
// Save the output file to disk
saveFileToDisk(responseStream, destFileName);
}
private string pathToDataDir()
{
string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
var folders = Path.GetDirectoryName(baseDirectory).Split('\\').ToList();
// Get Path to the TestData directory
var foldersUpToRootDir = folders.Take(folders.Count - 5);
var pathToRootDir = string.Join("\\", foldersUpToRootDir);
return Path.Combine(pathToRootDir, "TestData");
}
private void uploadFileToCloudStorage(String fileName)
{
CadApi storageApi = new CadApi(AppKey, AppSid);
string dataFilePath = Path.Combine(pathToDataDir(), fileName);
UploadFileRequest putCreateRequest = new UploadFileRequest(fileName, File.OpenRead(dataFilePath));
storageApi.UploadFile(putCreateRequest);
}
private void saveFileToDisk(Stream responseStream, String destFileName)
{
var fileStream = File.Create(Path.Combine(pathToDataDir(), "Output", destFileName));
responseStream.Seek(0, SeekOrigin.Begin);
responseStream.CopyTo(fileStream);
fileStream.Close();
}
}
}
PHP
This file contains 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
/**
*
* Convert document to destination format with detailed settings and retrieves result on response.
* For complete examples, please visit https://github.com/aspose-cad-cloud/aspose-cad-cloud-php
*/
<?php
public function postDrawingSaveAs()
{
$localName = "910609.dxf";
$outputFormat = "pdf";
$remoteName = $localName;
$subfolder = "";
$fullName = self::$baseRemoteFolder . $subfolder . $remoteName;
$destName = self::$baseTestOut . $remoteName . "." . $outputFormat;
$file = realpath(__DIR__ . self::$relativeRootPath) . '/TestData/' . $localName;
$putRequest = new \Aspose\Storage\Model\Requests\PutCreateRequest($fullName, $file);
$this->storage->PutCreate($putRequest);
$request = new \Aspose\CAD\Model\Requests\PostDrawingSaveAsRequest($file, $outputFormat, null, $folder=trim(self::$baseRemoteFolder . $subfolder, ""));
list($response, $code, $headers) = $this->CAD->postDrawingSaveAsWithHttpInfo($request);
Assert::assertEquals(200, $code);
}
?>
Python
This file contains 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
# For complete examples and data files, please go to https://github.com/aspose-cad-cloud/aspose-cad-cloud-python/
from asposecadcloud import CadApi
import asposecadcloud.models.requests as Requests
import json as json
class PostImageSaveAs(object):
def __init__(self):
# Setup CAD and Storage API clients
with open("config.json") as f:
server_file_info = json.load(f)
api_key = server_file_info['AppKey']
api_sid = server_file_info['AppSid']
base_url = server_file_info['BaseUrl']
self.cad_api = CadApi(api_key, api_sid, base_url)
self.storage_api = CadApi(api_key, api_sid, base_url)
def get_file_data(self, filename):
file_data = open("data/" + filename, "rb").read()
return file_data
def post_image_save_as(self, filename):
file_data = self.get_file_data(filename)
# Possible values are jpg, bmp, psd, tiff, gif, png, j2k, wmf and pdf
output_format = "pdf"
destFileName = "result.pdf"
# Convert image to PDF
save_as_request = Requests.PostDrawingSaveAsRequest(file_data, output_format, out_path=destFileName)
resized_file = self.cad_api.post_drawing_save_as(save_as_request)
return resized_file
obj = PostImageSaveAs()
print(obj.post_image_save_as("01.026.385.01.0.I SOPORTE ENFRIADOR.dwg"))