
Aspose.CAD Cloud SDK for Python
Python REST API 生成和操作 CAD 图纸
强大的 Python API,完全支持在云中创建、编辑、读取、操作和转换 AutoCAD DWG、DXF DWF 和 BIM 文件的有用功能。
什么是 Aspose.CAD Cloud Python SDK?
Aspose.CAD Cloud Python SDK 是一款极其实用的软件开发工具包 (SDK),它使程序员能够设计出强大的应用程序来处理 AutoCAD 和 BIM 图纸。借助强大的 REST API,开发人员可以将 CAD 图纸转换为多种文件类型,包括 PDF、SVG、BMP、PNG、JPEG 等。此 API 包含在 Aspose.CAD Cloud Python SDK 中,方便程序员轻松地将各种功能添加到他们的 Python 应用程序中。
借助可靠的 Aspose.CAD Cloud Python SDK,软件开发人员可以将 CAD 文件转换和操作功能集成到他们的 Python 应用程序中。它使软件开发人员能够将 CAD 图纸从一种格式转换为另一种格式,并提供极其强大的转换功能,可以将 CAD 图纸从一种格式(例如 DWG、DWF 和 DXF)转换为 PDF 和图像格式(例如 BMP、PNG、JPG、JPEG、JPEG2000、TIF、TIFF、PSD、GIF 等)。Aspose.CAD Cloud Python SDK 可在任何支持 Python 的平台上使用,包括 Windows、Linux 和 macOS。
Aspose.CAD Cloud Python SDK 提供了一种经济实惠的 CAD 文件处理方式。它提供了一个可扩展且适应性强的云平台,无需昂贵的 CAD 软件和设备。用于处理 CAD 文件的 SDK 具有许多关键功能,包括以编程方式编辑 CAD 文件、添加或修改 CAD 实体、添加或删除图层或布局、将 CAD 文件渲染为不同的图像格式(PNG、JPG、BMP、TIFF)、支持 CAD 文件验证、与云存储集成等等。总而言之,对于必须在基于 Python 的应用程序中管理 CAD 文件的程序员来说,它是理想的选择。
Aspose.CAD Cloud Python SDK 入门
Aspose.CAD Cloud Python SDK 可以使用 Python 包管理器 pip 安装。只需运行以下命令即可安装。
通过 Python API 将 CAD 图纸转换为图像
Aspose.CAD Cloud SDK for Python 全面支持将 CAD 图纸转换为云端其他几种受支持的图像文件格式。该库支持将 CAD 图纸转换为 BMP、PNG、JPG、JPEG、JPEG2000、TIF、TIFF、PSD、GIF 和 WMF 文件格式。软件开发人员还可以从 CAD 图纸中导出选定的图层和布局。以下示例演示了如何使用 Python REST API 将现有图像导出为其他格式。
通过 Python API 导出部分图像
Aspose.CAD Cloud SDK for Python 使软件开发人员能够使用 Python 命令将 CAD 图像的一部分导出为其他支持的文件格式。要完成此任务,首先需要加载图像并使用 get_drawing_properties 方法获取图像信息。然后,您需要获取要导出的图像区域的尺寸。您需要传递图像的文件名和文件夹名称,以及该区域左上角和右下角的坐标。现在图像已准备好导出,请传递图像的文件名和文件夹名称,以及要导出图像的格式。以下代码用于将部分图像导出为其他支持的文件格式。
如何通过 Python API 导出部分图像?
import asposecadcloud
from asposecadcloud.apis.cad_api import CadApi
# Your Aspose Cloud credentials
client_id = 'your_client_id'
client_secret = 'your_client_secret'
base_url = 'https://api.aspose.cloud'
# Create an instance of the ApiClient class
configuration = asposecadcloud.Configuration()
configuration.api_key['ClientId'] = client_id
configuration.api_key['ClientSecret'] = client_secret
configuration.host = base_url
api_client = asposecadcloud.ApiClient(configuration)
# Create an instance of the CadApi class
cad_api = CadApi(api_client)
# Get the properties of the drawing
filename = 'sample.dwg'
folder_name = 'CAD'
drawing_properties = cad_api.get_drawing_properties(filename, folder=folder_name)
# Get the dimensions of the area to export
top_left_x = 0
top_left_y = 0
bottom_right_x = 100
bottom_right_y = 100
drawing_area = cad_api.get_drawing_area(filename, folder=folder_name, x=top_left_x, y=top_left_y, width=bottom_right_x-top_left_x, height=bottom_right_y-top_left_y)
# Export the area as a PNG image
export_format = 'png'
output_filename = 'output.png'
export_options = asposecadcloud.PngOptions()
export_options.area = drawing_area
cad_api.get_drawing_save_as(filename, export_format, folder=folder_name, out_path=output_filename, export_options=export_options)
通过 Python API 管理 CAD 图纸尺寸
Aspose.CAD Cloud SDK for Python 包含强大的 CAD 图纸及其尺寸处理支持。该库支持自动调整尺寸或使用 UnitType 枚举调整尺寸。要调整图像大小,首先需要加载现有图像并获取图纸的当前尺寸,然后创建新导出选项的实例。之后,设置图纸的新尺寸,并将图纸转换为具有新尺寸的新格式。现在,将其保存到指定的输出文件路径。以下示例展示了如何实现此操作。
如何通过 Python API 调整 CAD 绘图大小?
import asposecadcloud
from asposecadcloud.apis.cad_api import CadApi
from asposecadcloud.models.cad_bmp_export_options import CadBmpExportOptions
# set up the API client
cad_api = CadApi(api_key, app_sid, api_base_url)
# specify the input and output file paths
input_file = "input.dwg"
output_file = "output.dwg"
# get the current size of the drawing
size = cad_api.get_drawing_properties(input_file).drawing_size
# create an instance of the BMP export options
bmp_export_options = CadBmpExportOptions()
# set the new size of the drawing
bmp_export_options.width = size.width * 2
bmp_export_options.height = size.height * 2
# convert the drawing to BMP format with the new size
cad_api.put_drawing_bmp(input_file, bmp_export_options, output_file)
通过 Python API 旋转和翻转 CAD 图像
Aspose.CAD Cloud SDK for Python 包含一些实用功能,可使用 Python REST API 进行图像处理以及转换为其他支持文件格式。要旋转和翻转现有图像,首先需要设置输入和输出文件名及格式。之后,您需要设置旋转和翻转属性,然后旋转或翻转绘图。现在,您可以下载旋转和翻转后的绘图,并将其保存到您选择的位置。
如何在 Python 应用程序中旋转和翻转 CAD 图像?
import asposecadcloud
from asposecadcloud.apis.cad_api import CadApi
from asposecadcloud.models.requests import PutDrawingRotateFlipRequest
from asposecadcloud.models.rotate_flip_type import RotateFlipType
# Configure API key authorization
configuration = asposecadcloud.Configuration()
configuration.api_key['api_key'] = 'YOUR_APP_KEY'
configuration.api_key['app_sid'] = 'YOUR_APP_SID'
# Create a CAD API instance
cad_api = CadApi(asposecadcloud.ApiClient(configuration))
# Set input file name and format
filename = 'input.dwg'
format = 'dwg'
# Set output file name and format
output_filename = 'output.dwg'
output_format = 'dwg'
# Set rotation and flip properties
rotate_flip_type = RotateFlipType.FlipX
# Rotate and flip the drawing
request = PutDrawingRotateFlipRequest(filename, rotate_flip_type, output_format, folder=None, storage=None, output_path=output_filename)
response = cad_api.put_drawing_rotate_flip(request)
# Download the rotated and flipped drawing
download_request = asposecadcloud.DownloadFileRequest(output_filename)
download_response = cad_api.download_file(download_request)