# FikoYT FreeCAD API Entegrasyonu

FikoYT sunucusuna entegre edilmiş FreeCAD STEP dosya analiz API'si.

## Özellikler

- **STEP Dosya Analizi**: .stp ve .step dosyalarını FreeCAD ile analiz eder
- **Bounding Box Hesaplama**: Optimal ve axis-aligned bounding box bilgileri
- **Malzeme Hesaplamaları**: Hacim, yüzey alanı, ağırlık hesaplamaları
- **Çoklu Malzeme Desteği**: Alüminyum, çelik, bakır, titanyum vb.
- **Matrix of Inertia**: Eylemsizlik matrisi hesaplaması
- **FikoYTCtx Entegrasyonu**: Mevcut FikoYT altyapısı ile tam uyumlu

## Mimari

```
┌─────────────────┐
│   Client        │
└────────┬────────┘
         │ POST /uyar/api/freecad/analyze
         ▼
┌─────────────────────────────────┐
│   Go Fiber API                  │
│   (Port: 9000)                  │
│   - File upload handling        │
│   - FikoYTCtx middleware        │
│   - Request validation          │
└────────┬────────────────────────┘
         │ HTTP Proxy
         ▼
┌─────────────────────────────────┐
│   Python Flask API              │
│   (Port: 8081)                  │
│   - FreeCAD wrapper             │
│   - free.py integration         │
│   - JSON response               │
└─────────────────────────────────┘
```

## API Endpoint'leri

### 1. STEP Dosya Analizi

**Endpoint**: `POST /uyar/api/freecad/analyze`

**Request** (Multipart Form Data):
```
file: [STEP dosyası] (required)
material: aluminium | steel | copper | titanium | ... (optional, default: aluminium)
density: [özel yoğunluk kg/dm³] (optional)
precision: [ondalık hassasiyet] (optional, default: 4)
```

**Response** (FikoYTCevap formatı):
```json
{
  "durum": {
    "kodu": 200,
    "tamamlandi": true,
    "mesaj": "STEP dosyası başarıyla analiz edildi",
    "hata": false,
    "hatalar": []
  },
  "sonuc": {
    "file_name": "model.stp",
    "total_objects_analyzed": 5,
    "material": {
      "name": "Aluminium",
      "density_kg_dm3": 2.71,
      "symbol": "Al"
    },
    "total_weight": {
      "value": 1234.5678,
      "unit": "g",
      "kg": 1.2346
    },
    "objects": [
      {
        "object_name": "Parça-1",
        "bounding_box_optimal": {
          "x_length": 100.0,
          "y_length": 50.0,
          "z_length": 25.0,
          "unit": "mm"
        },
        "volume": {
          "value": 125000.0,
          "unit": "mm³"
        },
        "weight": {
          "value": 338.75,
          "unit": "g",
          "kg": 0.3388,
          "material": "Aluminium"
        }
      }
    ]
  }
}
```

### 2. Malzeme Listesi

**Endpoint**: `GET /uyar/api/freecad/materials`

**Response**:
```json
{
  "durum": {
    "kodu": 200,
    "tamamlandi": true,
    "mesaj": "Malzeme listesi başarıyla alındı",
    "hata": false
  },
  "sonuc": {
    "materials": [
      {
        "key": "aluminium",
        "name": "Aluminium",
        "symbol": "Al",
        "density_kg_dm3": 2.71
      },
      {
        "key": "steel",
        "name": "Steel",
        "symbol": "Fe",
        "density_kg_dm3": 7.85
      }
    ]
  }
}
```

### 3. Health Check

**Endpoint**: `GET /uyar/api/freecad/health`

**Response**:
```json
{
  "durum": {
    "kodu": 200,
    "tamamlandi": true,
    "mesaj": "FreeCAD API çalışıyor",
    "hata": false
  },
  "sonuc": {
    "status": "ok",
    "service": "FreeCAD API Server",
    "version": "1.0.0"
  }
}
```

## Kullanım Örnekleri

### cURL ile STEP Analizi

```bash
# Basit kullanım (varsayılan malzeme: alüminyum)
curl -X POST http://localhost:9000/uyar/api/freecad/analyze \
  -F "file=@model.stp"

# Farklı malzeme ile
curl -X POST http://localhost:9000/uyar/api/freecad/analyze \
  -F "file=@model.stp" \
  -F "material=steel"

# Özel yoğunluk ile
curl -X POST http://localhost:9000/uyar/api/freecad/analyze \
  -F "file=@model.stp" \
  -F "density=7.85"

# Hassasiyet ayarı ile
curl -X POST http://localhost:9000/uyar/api/freecad/analyze \
  -F "file=@model.stp" \
  -F "precision=2"
```

### JavaScript/Fetch ile

```javascript
const formData = new FormData();
formData.append('file', stepFile);
formData.append('material', 'aluminium');

const response = await fetch('http://localhost:9000/uyar/api/freecad/analyze', {
  method: 'POST',
  body: formData
});

const result = await response.json();
console.log(result.sonuc);
```

### Python ile

```python
import requests

files = {'file': open('model.stp', 'rb')}
data = {'material': 'steel'}

response = requests.post(
    'http://localhost:9000/uyar/api/freecad/analyze',
    files=files,
    data=data
)

result = response.json()
print(result['sonuc'])
```

## Docker Kurulum ve Çalıştırma

### 1. Build

```bash
cd docker/fikoyt-sunucu
docker-compose build
```

### 2. Başlat

```bash
docker-compose up -d
```

### 3. Logları İzle

```bash
# Tüm loglar
docker-compose logs -f

# Sadece Go API
docker-compose logs -f fikoyt-sunucu | grep "Go"

# Sadece FreeCAD Python API
docker-compose logs -f fikoyt-sunucu | grep "FreeCAD"
```

### 4. Durdur

```bash
docker-compose down
```

## Mevcut Malzemeler

| Key | İsim | Sembol | Yoğunluk (kg/dm³) |
|-----|------|--------|-------------------|
| aluminium | Aluminium | Al | 2.71 |
| steel | Steel | Fe | 7.85 |
| copper | Copper | Cu | 8.96 |
| titanium | Titanium | Ti | 4.51 |
| plastic_pla | PLA Plastic | PLA | 1.25 |
| plastic_abs | ABS Plastic | ABS | 1.05 |
| wood | Wood (Oak) | Wood | 0.75 |
| glass | Glass | Glass | 2.50 |
| bronze | Bronze | Bronze | 8.80 |
| brass | Brass | Brass | 8.50 |

## Dosya Yapısı

```
FikoYT.Yeni.Cag/
├── Python/
│   ├── free.py                      # FreeCAD analiz motoru
│   └── freecad_api_server.py        # Flask API wrapper
│
├── merkez.go-server.com/
│   └── apps/uyar_dokum/uyar/api_v1/
│       └── freecad/
│           └── freecad_handler.go   # Go API handler
│
└── docker/fikoyt-sunucu/
    ├── docker-compose.yml           # Docker compose config
    └── merkez.go-server.com/
        └── Dockerfile               # FreeCAD + Python + Go image
```

## Environment Variables

```bash
# Go API
FREECAD_API_URL=http://localhost:8081  # Python API URL

# Python API
FREECAD_API_PORT=8081                  # Flask port
FLASK_DEBUG=False                      # Debug modu
```

## Hata Kodları

| Kod | Açıklama |
|-----|----------|
| 200 | Başarılı |
| 400 | Geçersiz dosya veya parametre |
| 500 | Sunucu hatası (FreeCAD analiz hatası) |
| 503 | FreeCAD API'ye bağlanılamadı |

## Performans

- **Ortalama Analiz Süresi**: 2-5 saniye (dosya boyutuna göre)
- **Maksimum Dosya Boyutu**: 500MB
- **Timeout**: 120 saniye
- **Desteklenen Format**: .stp, .step

## Geliştirme Notları

### Python API'yi Lokal Test Etme

```bash
cd Python
python3 freecad_api_server.py
```

### Go API'yi Lokal Test Etme

```bash
cd merkez.go-server.com
export FREECAD_API_URL=http://localhost:8081
go run main.go
```

## Troubleshooting

### FreeCAD API'ye bağlanılamıyor

1. Python API'nin çalıştığını kontrol edin:
```bash
curl http://localhost:8081/health
```

2. Docker container'ı kontrol edin:
```bash
docker-compose ps
docker-compose logs fikoyt-sunucu
```

### FreeCAD analiz hatası

1. STEP dosyasının geçerli olduğundan emin olun
2. Dosya boyutunu kontrol edin (max 500MB)
3. FreeCAD loglarını inceleyin:
```bash
docker-compose logs fikoyt-sunucu | grep "FreeCAD"
```

## Lisans

FikoYT © 2025
