File size: 9,357 Bytes
e305028
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252

"""
محلل الرسومات الهندسية وملفات BIM المتقدم
"""

import ezdxf
import cv2
import numpy as np
from PIL import Image
import pytesseract
from shapely.geometry import Point, Polygon
import json
import os
import logging
from datetime import datetime

class CADBIMAnalyzer:
    """محلل الرسومات الهندسية وملفات BIM"""
    
    def __init__(self, claude_client=None):
        """تهيئة المحلل"""
        self.claude_client = claude_client
        self.analysis_results = {}
        
    def analyze_drawing(self, file_path):
        """تحليل الرسم الهندسي"""
        try:
            file_extension = os.path.splitext(file_path)[1].lower()
            
            if file_extension in ['.dwg', '.dxf']:
                return self._analyze_cad_file(file_path)
            elif file_extension == '.ifc':
                return self._analyze_bim_file(file_path)
            elif file_extension in ['.pdf', '.jpg', '.jpeg', '.png']:
                return self._analyze_image_file(file_path)
            else:
                raise ValueError(f"نوع الملف غير مدعوم: {file_extension}")
                
        except Exception as e:
            logging.error(f"خطأ في تحليل الرسم: {str(e)}")
            raise
            
    def _analyze_cad_file(self, file_path):
        """تحليل ملف CAD"""
        doc = ezdxf.readfile(file_path)
        msp = doc.modelspace()
        
        # تحليل العناصر
        elements = {
            'lines': len(msp.query('LINE')),
            'circles': len(msp.query('CIRCLE')),
            'arcs': len(msp.query('ARC')),
            'polylines': len(msp.query('LWPOLYLINE')),
            'dimensions': len(msp.query('DIMENSION')),
            'text': len(msp.query('TEXT')),
            'blocks': len(doc.blocks)
        }
        
        # تحليل الطبقات
        layers = {layer.dxf.name: {
            'color': layer.dxf.color,
            'linetype': layer.dxf.linetype,
            'is_on': layer.dxf.on,
            'is_frozen': layer.dxf.frozen
        } for layer in doc.layers}
        
        # استخراج الأبعاد
        extents = msp.get_extents()
        if extents:
            dimensions = {
                'width': abs(extents.size.x),
                'height': abs(extents.size.y),
                'area': abs(extents.size.x * extents.size.y)
            }
        else:
            dimensions = {'width': 0, 'height': 0, 'area': 0}
            
        # تحليل متقدم باستخدام Claude
        if self.claude_client:
            drawing_info = {
                'elements': elements,
                'layers': layers,
                'dimensions': dimensions
            }
            
            analysis_prompt = f"""
            قم بتحليل معلومات الرسم الهندسي التالية وتقديم:
            1. وصف عام للرسم
            2. تحليل العناصر والطبقات
            3. تحديد نوع المشروع والتخصص
            4. توصيات فنية
            5. ملاحظات هامة
            
            معلومات الرسم:
            {json.dumps(drawing_info, indent=2, ensure_ascii=False)}
            """
            
            response = self.claude_client.messages.create(
                model="claude-3-sonnet-20240229",
                max_tokens=4000,
                temperature=0.7,
                messages=[{"role": "user", "content": analysis_prompt}]
            )
            
            ai_analysis = response.content[0].text
        else:
            ai_analysis = "تحليل الذكاء الاصطناعي غير متاح"
        
        return {
            'file_info': {
                'path': file_path,
                'type': os.path.splitext(file_path)[1],
                'size': os.path.getsize(file_path),
                'modified': datetime.fromtimestamp(os.path.getmtime(file_path)).strftime('%Y-%m-%d %H:%M:%S')
            },
            'elements': elements,
            'layers': layers,
            'dimensions': dimensions,
            'ai_analysis': ai_analysis
        }
        
    def _analyze_bim_file(self, file_path):
        """تحليل ملف BIM"""
        try:
            # تحليل ملف IFC
            import ifcopenshell
            ifc_file = ifcopenshell.open(file_path)
            
            # تحليل العناصر
            elements = {
                'structural': self._analyze_structural_elements(ifc_file),
                'architectural': self._analyze_architectural_elements(ifc_file),
                'mep': self._analyze_mep_elements(ifc_file)
            }
            
            # تحليل الأبعاد
            dimensions = self._analyze_dimensions(ifc_file)
            
            # تحليل متقدم باستخدام Claude
            if self.claude_client:
                building_info = {
                    'elements': elements,
                    'dimensions': dimensions,
                    'properties': self._extract_properties(ifc_file)
                }
                
                analysis_prompt = f"""
                قم بتحليل معلومات نموذج BIM التالية وتقديم:
                1. تحليل شامل للنموذج
                2. تقييم جودة النمذجة
                3. توصيات للتحسين
                4. تقدير التكاليف
                5. تحليل الاستدامة
                
                معلومات النموذج:
                {json.dumps(building_info, indent=2, ensure_ascii=False)}
                """
                
                response = self.claude_client.messages.create(
                    model="claude-3-sonnet-20240229",
                    max_tokens=4000,
                    temperature=0.7,
                    messages=[{"role": "user", "content": analysis_prompt}]
                )
                
                ai_analysis = response.content[0].text
            else:
                ai_analysis = "تحليل الذكاء الاصطناعي غير متاح"
            
            return {
                'file_info': {
                    'path': file_path,
                    'type': 'IFC',
                    'schema': ifc_file.schema,
                    'size': os.path.getsize(file_path)
                },
                'elements': elements,
                'dimensions': dimensions,
                'analysis': ai_analysis
            }
            
        except Exception as e:
            logging.error(f"خطأ في تحليل ملف BIM: {str(e)}")
            raise
        
    def _analyze_image_file(self, file_path):
        """تحليل ملف صورة"""
        # قراءة الصورة
        if file_path.lower().endswith('.pdf'):
            # TODO: Add PDF to image conversion
            pass
        else:
            image = cv2.imread(file_path)
            
        # استخراج النص
        text = pytesseract.image_to_string(image, lang='ara+eng')
        
        # تحليل الخطوط والأشكال
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        edges = cv2.Canny(gray, 50, 150)
        lines = cv2.HoughLinesP(edges, 1, np.pi/180, 100)
        
        # تحليل متقدم باستخدام Claude
        if self.claude_client:
            image_info = {
                'dimensions': image.shape,
                'extracted_text': text,
                'detected_lines': len(lines) if lines is not None else 0
            }
            
            analysis_prompt = f"""
            قم بتحليل معلومات الصورة الهندسية التالية وتقديم:
            1. وصف عام للصورة
            2. تحليل النص المستخرج
            3. تحليل الخطوط والأشكال
            4. تحديد نوع المشروع والتخصص
            5. توصيات وملاحظات
            
            معلومات الصورة:
            {json.dumps(image_info, indent=2, ensure_ascii=False)}
            """
            
            response = self.claude_client.messages.create(
                model="claude-3-sonnet-20240229",
                max_tokens=4000,
                temperature=0.7,
                messages=[{"role": "user", "content": analysis_prompt}]
            )
            
            ai_analysis = response.content[0].text
        else:
            ai_analysis = "تحليل الذكاء الاصطناعي غير متاح"
            
        return {
            'file_info': {
                'path': file_path,
                'type': os.path.splitext(file_path)[1],
                'size': os.path.getsize(file_path),
                'modified': datetime.fromtimestamp(os.path.getmtime(file_path)).strftime('%Y-%m-%d %H:%M:%S')
            },
            'dimensions': {
                'width': image.shape[1],
                'height': image.shape[0],
                'channels': image.shape[2]
            },
            'extracted_text': text,
            'analysis': {
                'detected_lines': len(lines) if lines is not None else 0
            },
            'ai_analysis': ai_analysis
        }