File size: 8,998 Bytes
25d2b3e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
"""
معالج ملفات DWG
"""
import os
import re
import subprocess
import shutil
import tempfile
import traceback
import pandas as pd
from pathlib import Path
import config
from utils.helpers import create_directory_if_not_exists
class DWGHandler:
"""معالج ملفات DWG (AutoCAD)"""
def __init__(self, converter_path=None):
"""
تهيئة معالج ملفات DWG
المعلمات:
converter_path: مسار برنامج تحويل DWG (اختياري)
"""
# محاولة تحديد مسار برنامج تحويل DWG
self.converter_path = converter_path
if not self.converter_path:
# البحث عن المسار في إعدادات النظام
if hasattr(config, 'DWG_CONVERTER_PATH') and config.DWG_CONVERTER_PATH:
self.converter_path = config.DWG_CONVERTER_PATH
else:
# محاولة البحث عن البرامج المعروفة
possible_paths = [
r"C:\Program Files\Autodesk\AutoCAD 2022\accoreconsole.exe",
r"C:\Program Files\Autodesk\AutoCAD 2021\accoreconsole.exe",
r"C:\Program Files\Autodesk\AutoCAD 2020\accoreconsole.exe",
r"C:\Program Files\ODA\ODAFileConverter\ODAFileConverter.exe"
]
for path in possible_paths:
if os.path.exists(path):
self.converter_path = path
break
def convert_to_pdf(self, dwg_path, output_path=None):
"""
تحويل ملف DWG إلى PDF
المعلمات:
dwg_path: مسار ملف DWG
output_path: مسار ملف الإخراج (اختياري)
الإرجاع:
مسار ملف PDF الناتج
"""
try:
# التحقق من وجود الملف
if not os.path.exists(dwg_path):
raise FileNotFoundError(f"ملف DWG غير موجود: {dwg_path}")
# التحقق من وجود برنامج التحويل
if not self.converter_path or not os.path.exists(self.converter_path):
raise FileNotFoundError("لم يتم العثور على برنامج تحويل DWG")
# تحديد مسار الإخراج
if not output_path:
output_path = os.path.splitext(dwg_path)[0] + ".pdf"
# إنشاء مجلد الإخراج إذا لم يكن موجودًا
output_dir = os.path.dirname(output_path)
create_directory_if_not_exists(output_dir)
# تنفيذ عملية التحويل باستخدام برنامج التحويل المناسب
if "accoreconsole.exe" in self.converter_path.lower():
# استخدام AutoCAD Core Console لتحويل الملف
script_content = f"""
/i "{dwg_path}"
/e
/o "{output_path}"
"""
script_path = tempfile.mktemp(suffix=".scr")
with open(script_path, "w") as f:
f.write(script_content)
cmd = [self.converter_path, "/s", script_path]
result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
os.unlink(script_path) # حذف ملف النص البرمجي المؤقت
if result.returncode != 0:
raise RuntimeError(f"فشل في تحويل ملف DWG: {result.stderr}")
elif "odafileconverter.exe" in self.converter_path.lower():
# استخدام ODA File Converter لتحويل الملف
input_dir = os.path.dirname(dwg_path)
output_dir = os.path.dirname(output_path)
input_filename = os.path.basename(dwg_path)
output_format = "PDF"
cmd = [
self.converter_path,
input_dir,
output_dir,
"DWG",
output_format,
"1",
"1",
input_filename
]
result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if result.returncode != 0:
raise RuntimeError(f"فشل في تحويل ملف DWG: {result.stderr}")
else:
raise NotImplementedError(f"برنامج التحويل غير مدعوم: {self.converter_path}")
# التحقق من وجود ملف الإخراج
if not os.path.exists(output_path):
raise FileNotFoundError(f"لم يتم إنشاء ملف PDF: {output_path}")
return output_path
except Exception as e:
error_msg = f"خطأ في تحويل ملف DWG إلى PDF: {str(e)}"
print(error_msg)
traceback.print_exc()
raise Exception(error_msg)
def extract_quantities(self, dwg_path):
"""
استخراج الكميات من ملف DWG
المعلمات:
dwg_path: مسار ملف DWG
الإرجاع:
DataFrame يحتوي على الكميات المستخرجة
"""
try:
# تحويل ملف DWG إلى PDF أولاً
pdf_path = self.convert_to_pdf(dwg_path)
# استخدام معالج ملفات PDF لاستخراج الكميات
from utils.pdf_handler import extract_quantities_from_pdf
return extract_quantities_from_pdf(pdf_path)
except Exception as e:
error_msg = f"خطأ في استخراج الكميات من ملف DWG: {str(e)}"
print(error_msg)
traceback.print_exc()
raise Exception(error_msg)
def extract_text(self, dwg_path):
"""
استخراج النص من ملف DWG
المعلمات:
dwg_path: مسار ملف DWG
الإرجاع:
النص المستخرج من ملف DWG
"""
try:
# تحويل ملف DWG إلى PDF أولاً
pdf_path = self.convert_to_pdf(dwg_path)
# استخدام معالج ملفات PDF لاستخراج النص
from utils.pdf_handler import extract_text_from_pdf
return extract_text_from_pdf(pdf_path)
except Exception as e:
error_msg = f"خطأ في استخراج النص من ملف DWG: {str(e)}"
print(error_msg)
traceback.print_exc()
raise Exception(error_msg)
def get_dwg_info(self, dwg_path):
"""
الحصول على معلومات ملف DWG
المعلمات:
dwg_path: مسار ملف DWG
الإرجاع:
قاموس يحتوي على معلومات الملف
"""
try:
# التحقق من وجود الملف
if not os.path.exists(dwg_path):
raise FileNotFoundError(f"ملف DWG غير موجود: {dwg_path}")
# الحصول على معلومات الملف الأساسية
file_info = {
'filename': os.path.basename(dwg_path),
'path': dwg_path,
'size': os.path.getsize(dwg_path),
'modified_date': os.path.getmtime(dwg_path)
}
# محاولة استخراج معلومات إضافية من الملف
# ملاحظة: هذا يتطلب مكتبات إضافية أو استخدام برامج خارجية
return file_info
except Exception as e:
error_msg = f"خطأ في الحصول على معلومات ملف DWG: {str(e)}"
print(error_msg)
traceback.print_exc()
raise Exception(error_msg)
def is_converter_available(self):
"""
التحقق من توفر برنامج تحويل DWG
الإرجاع:
True إذا كان برنامج التحويل متوفرًا، False خلاف ذلك
"""
return self.converter_path is not None and os.path.exists(self.converter_path) |