CAD図面を扱う際には、DWG ファイルを PNG のようなウェブフレンドリーな画像に変換することがよくあります。Aspose.BarCode Cloud SDK for Python は、処理をクラウドにオフロードすることで、この変換をシンプルにします。このチュートリアルでは、SDK のセットアップ方法、認証手順、DWG を PNG に変換する Python コードの記述方法、そして生の cURL リクエストで同じ結果を得る方法を示します。これにより、Python アプリケーションに DWG レンダリングを簡単に統合できます。
PythonでのDWGからPNGへの変換 - 手順
- 認証情報の構成: SDK が Aspose cloud で認証できるように、クライアント ID とクライアント シークレットを設定します。
client_id = "YOUR_CLIENT_ID"
client_secret = "YOUR_CLIENT_SECRET"
- API クライアントの初期化:
Configurationオブジェクトを作成し、認証情報を割り当て、ApiClientとBarcodeApiのインスタンスを構築します。
config = Configuration()
config.client_id = client_id
config.client_secret = client_secret
api_client = ApiClient(configuration=config)
barcode_api = BarcodeApi(api_client)
- DWG ファイルの読み取り: ソース DWG ファイルをバイナリモードで開き、その内容をメモリに読み込みます。
with open("sample.dwg", "rb") as file_stream:
dwg_bytes = file_stream.read()
- 変換エンドポイントを呼び出す:
convert_imageを使用し、ターゲット形式として"png"を指定します。メソッドは PNG データをバイト配列として返します。
conversion_response = barcode_api.convert_image(
file=dwg_bytes,
format="png"
)
- PNG 出力の保存: 返されたバイトを
.png拡張子のファイルに書き込みます。
with open("sample.png", "wb") as out_file:
out_file.write(conversion_response)
convert_image メソッドの詳細については、API リファレンス を参照してください。
PythonでのDWGからPNGへの変換 - 完全動作サンプル
以下の例は、Python で Aspose.BarCode Cloud SDK を使用した DWG から PNG への変換の完全なワークフローを示しています。
import os
from asposebarcodecloud import Configuration, ApiClient, BarcodeApi, ApiException
# -------------------- Configuration --------------------
# Replace with your actual Aspose.BarCode Cloud credentials
client_id = "YOUR_CLIENT_ID"
client_secret = "YOUR_CLIENT_SECRET"
config = Configuration()
config.client_id = client_id
config.client_secret = client_secret
# Initialize API client
api_client = ApiClient(configuration=config)
barcode_api = BarcodeApi(api_client)
# -------------------- File Paths --------------------
input_path = "sample.dwg" # Path to the source DWG file
output_path = "sample.png" # Desired PNG output path
# -------------------- Conversion --------------------
try:
# Read the DWG file into memory
with open(input_path, "rb") as file_stream:
dwg_bytes = file_stream.read()
# The Aspose.BarCode Cloud SDK provides a generic image conversion endpoint.
# Here we invoke it, specifying the target format as PNG.
# The method name and parameters are based on the SDK's conversion API.
conversion_response = barcode_api.convert_image(
file=dwg_bytes, # Binary content of the DWG file
format="png" # Target image format
)
# Write the resulting PNG bytes to the output file
with open(output_path, "wb") as out_file:
out_file.write(conversion_response)
print(f"Conversion successful: '{input_path}' → '{output_path}'")
except ApiException as api_err:
print(f"API error during conversion: {api_err}")
except Exception as err:
print(f"Unexpected error: {err}")
finally:
# Explicitly close the API client session if needed
if hasattr(api_client, "close"):
api_client.close()
注意: このコード例はコア機能を示しています。プロジェクトで使用する前に、ファイルパスや設定値を実際の環境に合わせて更新し、すべての必須依存関係が正しくインストールされていることを確認し、開発環境で徹底的にテストしてください。問題が発生した場合は、公式ドキュメント を参照するか、サポートチーム にお問い合わせください。
cURL を使用した REST API による DWG から PNG への変換
以下は、REST インターフェイスを使用して同じ変換を実行する一連の cURL コマンドです。
- アクセストークンを取得する
curl -X POST "https://api.aspose.cloud/connect/token" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=client_credentials&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET"
レスポンスには access_token が含まれます。
- DWG ファイルのアップロード
curl -X PUT "https://api.aspose.cloud/v3.0/barcode/storage/file/sample.dwg" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/octet-stream" \
--data-binary "@sample.dwg"
- 変換を実行する
curl -X POST "https://api.aspose.cloud/v3.0/barcode/convert?format=png" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/octet-stream" \
--data-binary "@sample.dwg" \
-o sample.png
- PNG結果をダウンロード(上で
-oを使用した場合はオプション)
curl -X GET "https://api.aspose.cloud/v3.0/barcode/storage/file/sample.png" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-o sample.png
パラメータの完全な一覧については、公式 API ドキュメントをご覧ください。
Aspose.BarCode Cloud SDK for Python のインストールと構成
SDK は PyPI を通じて配布されています。次のコマンドでインストールしてください:
pip install aspose-barcode-cloud
前提条件: Python 3.6 以降、アクティブな Aspose アカウント、および有効なクライアント認証情報が必要です。SDK は Aspose クラウド エンドポイントにアクセスするためにインターネット接続が必要です。
パッケージはリリースページから直接ダウンロードすることもできます。
DWG から PNG への変換パラメータの設定
変換呼び出しは、出力画像タイプを決定する format パラメータを受け取ります。例ではそれを "png" に設定しています。
conversion_response = barcode_api.convert_image(
file=dwg_bytes,
format="png"
)
その他のオプション パラメータ(outPath、storage、または画像品質設定など)は、API リファレンス に記載されています。プロジェクトの要件に合わせて必要に応じて調整してください。
結論
DWG から PNG への変換は、Aspose.BarCode Cloud SDK for Python の力により Python で簡単になります。上記の手順に従うことで、ベクトルからラスタへの変換を任意のバックエンドサービスに統合したり、バッチ処理を自動化したり、CAD 図面のオンデマンドプレビューを生成したりできます。開発およびテストのために、Aspose temporary license page から有効な一時ライセンスを取得し、実運用ではフルライセンスの購入を検討してください。コーディングをお楽しみください!
よくある質問
-
SDKが変換でサポートするファイル形式は何ですか?
クラウド変換エンドポイントは、DWG、DXF、SVG、PDF など、多くのベクターおよびラスタ形式を処理します。完全なリストについては、ドキュメントをご参照ください。 -
PythonでのDWGからPNGへの変換はスレッドセーフですか?
はい、各BarcodeApiインスタンスは、スレッドごとに別々の認証オブジェクトを管理すれば同時に使用できます。