File size: 5,363 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

import cv2
import numpy as np
from PIL import Image
import pytesseract
import logging
from datetime import datetime

class EngineeringDrawingAnalyzer:
    """محلل الرسومات الهندسية المتقدم"""
    
    def __init__(self, claude_client=None):
        self.claude_client = claude_client
        self.supported_formats = ['.dwg', '.dxf', '.pdf', '.jpg', '.jpeg', '.png']
        
    def analyze_drawing(self, file_path):
        """تحليل الرسم الهندسي"""
        try:
            # قراءة الصورة
            image = cv2.imread(file_path)
            
            # تحليل الخطوط والأشكال
            lines, shapes = self._analyze_shapes(image)
            
            # استخراج النص والأبعاد
            text, dimensions = self._extract_text_and_dimensions(image)
            
            # تحليل متقدم باستخدام Claude
            if self.claude_client:
                drawing_info = {
                    'lines': lines,
                    'shapes': shapes,
                    'text': text,
                    '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)
                },
                'analysis': {
                    'lines': lines,
                    'shapes': shapes,
                    'text': text,
                    'dimensions': dimensions,
                    'ai_analysis': ai_analysis
                }
            }
            
        except Exception as e:
            logging.error(f"خطأ في تحليل الرسم: {str(e)}")
            raise
            
    def _analyze_shapes(self, image):
        """تحليل الخطوط والأشكال"""
        # تحويل الصورة إلى تدرج رمادي
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        
        # كشف الحواف
        edges = cv2.Canny(gray, 50, 150)
        
        # كشف الخطوط
        lines = cv2.HoughLinesP(edges, 1, np.pi/180, 100)
        
        # كشف الأشكال
        contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        
        shapes = []
        for contour in contours:
            approx = cv2.approxPolyDP(contour, 0.04 * cv2.arcLength(contour, True), True)
            if len(approx) == 3:
                shapes.append('triangle')
            elif len(approx) == 4:
                shapes.append('rectangle')
            elif len(approx) == 5:
                shapes.append('pentagon')
            elif len(approx) > 5:
                shapes.append('circle')
                
        return {
            'line_count': len(lines) if lines is not None else 0,
            'line_angles': self._analyze_line_angles(lines) if lines is not None else [],
            'line_lengths': self._analyze_line_lengths(lines) if lines is not None else []
        }, {
            'shape_count': len(shapes),
            'shape_types': dict(Counter(shapes))
        }
        
    def _extract_text_and_dimensions(self, image):
        """استخراج النص والأبعاد"""
        # استخراج النص
        text = pytesseract.image_to_string(image, lang='ara+eng')
        
        # تحليل الأبعاد
        dimensions = self._analyze_dimensions_from_text(text)
        
        return text, dimensions
        
    def _analyze_dimensions_from_text(self, text):
        """تحليل الأبعاد من النص"""
        import re
        
        # نمط للبحث عن الأبعاد
        dimension_pattern = r'(\d+(?:\.\d+)?)\s*(م|متر|سم|مم|m|cm|mm)'
        
        # البحث عن جميع الأبعاد
        dimensions = re.findall(dimension_pattern, text)
        
        # تحويل الأبعاد إلى قائمة منظمة
        processed_dimensions = []
        for value, unit in dimensions:
            processed_dimensions.append({
                'value': float(value),
                'unit': unit
            })
            
        return processed_dimensions