עבודה עם שרטוטי CAD לרוב משמעותה המרת קבצי DWG לתמונות ידידותיות לאינטרנט כגון PNG. ה‑Aspose.BarCode Cloud SDK for Python עושה את ההמרה הזו פשוטה על‑ידי העברת העיבוד לענן. במדריך זה נציג כיצד להגדיר את ה‑SDK, לבצע אימות, לכתוב קוד Python להמרת DWG ל‑PNG, ולהשיג את אותה תוצאה באמצעות בקשת cURL גולמית, כך שתוכל לשלב רינדור DWG ביישומי Python שלך בקלות.

המרת DWG ל-PNG בפייתון - שלבים

  1. הגדר אישורים: הגדר את מזהה הלקוח והסוד של הלקוח שלך כדי שה‑SDK יוכל לאמת עם Aspose cloud.
client_id = "YOUR_CLIENT_ID"
client_secret = "YOUR_CLIENT_SECRET"
  1. אתחול לקוח 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)
  1. קרא את קובץ DWG: פתח את קובץ ה‑DWG המקורי במצב בינארי וקרא את תוכנו לזיכרון.
with open("sample.dwg", "rb") as file_stream:
    dwg_bytes = file_stream.read()
  1. הפעל את נקודת הקצה להמרה: השתמש ב-convert_image וציין "png" כפורמט היעד. השיטה מחזירה את נתוני ה-PNG כמערך בתים.
conversion_response = barcode_api.convert_image(
    file=dwg_bytes,
    format="png"
)
  1. שמור את פלט ה-PNG: כתוב את הבייטים שהוחזרו לקובץ עם סיומת .png.
with open("sample.png", "wb") as out_file:
    out_file.write(conversion_response)

למידע נוסף על השיטה convert_image, ראה את תיעוד API.

המרת DWG ל-PNG ב-Python - דוגמה מלאה עובדת

הדוגמה הבאה מציגה את זרימת העבודה המלאה להמרת DWG ל‑PNG ב‑Python באמצעות Aspose.BarCode Cloud SDK.

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()

הערה: דוגמת קוד זו מדגימה את הפונקציונליות המרכזית. לפני השימוש בה בפרויקט שלך, ודא שאתה מעדכן את כל נתיבי הקבצים וערכי ההגדרה כדי להתאים לסביבה האמיתית שלך, מאמת שכל התלויות הנדרשות מותקנות כראוי, ובודק באופן יסודי בסביבת הפיתוח שלך. אם אתה נתקל בבעיות, אנא הפנה אל התיעוד הרשמי או פנה אל צוות התמיכה לקבלת עזרה.

המרת DWG ל-PNG באמצעות REST API עם cURL

להלן סדרת פקודות cURL שמבצעות את ההמרה זהה באמצעות ממשק ה‑REST.

  1. קבל אסימון גישה
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.

  1. העלה את קובץ 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"
  1. בצע את ההמרה
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
  1. הורד את תוצאת 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 Reference. התאימו אותם לפי הצורך כדי להתאים לדרישות הפרויקט שלכם.

סיכום

המרת DWG ל‑PNG בפייתון הופכת לפשוטה עם הכוח של Aspose.BarCode Cloud SDK for Python. על‑ידי ביצוע הצעדים שלמעלה, ניתן לשלב המרת וקטור‑ל‑רסטר בכל שירות backend, לאוטומט עיבוד באצ׳, או ליצור תצוגות מקדימות לפי דרישה לציורי CAD. זכור לקבל רישיון זמני תקף מדף Aspose temporary license page לפיתוח ובדיקה, ולשקול רכישת רישיון מלא לשימוש בייצור. קידוד נעים!

שאלות נפוצות

  • אילו פורמטי קבצים תומך ה‑SDK בהמרה?
    קצה ההמרה בענן תומך במגוון פורמטים וקטוריים ורסטריים, כולל DWG, DXF, SVG, PDF, ועוד. עיין בתיעוד לקבלת הרשימה המלאה.

  • האם המרת DWG ל‑PNG בפייתון בטוחה לשימוש מרובה חוטים?
    כן, כל מופע של BarcodeApi ניתן לשימוש במקביל כל עוד אתה מנהל אובייקטי אישורים נפרדים לכל חוט.

קרא עוד