File size: 2,780 Bytes
fa16dd4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
parse_design_files.py — Extract high-fidelity structured data from OpenRocket (.ork)
and RockSim (.rkt) files using the rocketsmith API.

Output
------
    source/designs/files/json/ork/{id}.json
    source/designs/files/json/rkt/{id}.json
"""

import json
import logging
import sys
from pathlib import Path
from rocketsmith.openrocket.components import inspect_rocket_file
from rocketsmith.openrocket.utils import get_openrocket_path

# ---------------------------------------------------------------------------
# Logging
# ---------------------------------------------------------------------------

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s %(levelname)s %(message)s",
    handlers=[logging.StreamHandler(sys.stdout)],
)
log = logging.getLogger(__name__)

# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------

def main():
    try:
        jar_path = get_openrocket_path()
        log.info(f"Using OpenRocket JAR at: {jar_path}")
    except FileNotFoundError as e:
        log.error(f"Could not find OpenRocket JAR: {e}")
        sys.exit(1)

    root_dir = Path("source/designs/files")
    dst_dir = root_dir / "json"
    dst_dir.mkdir(parents=True, exist_ok=True)
    
    formats = ['ork', 'rkt']
    
    for fmt in formats:
        src_dir = root_dir / fmt
        
        if not src_dir.exists():
            log.warning(f"Source directory {src_dir} does not exist. Skipping.")
            continue
            
        files = sorted(list(src_dir.glob(f"*.{fmt}")))
        total = len(files)
        log.info(f"Processing {total} {fmt} files...")
        
        ok = failed = skipped = 0
        
        for i, f in enumerate(files):
            # Using same filename pattern as binary files: {id}.json
            dst_file = dst_dir / f"{f.stem}.json"
            
            if dst_file.exists():
                skipped += 1
                continue

            try:
                result = inspect_rocket_file(f, jar_path)
                # Add the file_extension field
                result["file_extension"] = fmt
                
                with dst_file.open('w', encoding='utf-8') as out:
                    json.dump(result, out, indent=2, ensure_ascii=False)
                ok += 1
            except Exception as e:
                log.error(f"Failed to parse {f.name}: {e}")
                failed += 1
                
            if (i + 1) % 50 == 0 or (i + 1) == total:
                log.info(f"Progress ({fmt}): {i+1}/{total} — ok={ok} skipped={skipped} failed={failed}")

    log.info("Finished processing all design files.")

if __name__ == "__main__":
    main()