File size: 8,690 Bytes
5154c4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
253
254
255
256
257
258
259
260
import itertools
import math
import os
from pathlib import Path
import ezdxf

OUTPUT_ROOT = Path(os.environ.get("OUTPUT_ROOT", "/home/user/Desktop"))


GUI_BYPASS_FORBIDDEN_EXTENSIONS = {
    ".py", ".pyw", ".ipynb", ".sh", ".bash", ".zsh", ".bat", ".cmd",
    ".ps1", ".psm1", ".psd1", ".vbs", ".js", ".mjs", ".ts", ".rb",
    ".lua", ".tcl", ".ahk", ".scr",
}
GUI_BYPASS_ALLOWED_FILENAMES = {"eval.py"}
GUI_BYPASS_OUTPUT_TOKENS = (
    "result.ifc", "result.pdf", "result.osm", "workflow.osw", "summary.txt",
    "report.csv", "result.csv",
    ".dxf", ".dwg", ".step", ".stp", ".fcstd", ".scad", ".stl", ".obj",
    ".db", ".rst", ".rth", ".wbpj", ".odb", ".cae", ".inp",
    ".nc", ".gcode", ".slb", ".ipt", ".sldprt", ".sldasm",
    "autocad_result", "apdl_", "wb_",
)
GUI_BYPASS_COMMAND_TOKENS = (
    "python", "python3", "py ", "powershell", "pwsh", "cmd.exe", "cmd /c",
    "bash", " sh ", "zsh", "node", "ruby", "perl",
    "ifcopenshell", "openstudio", "energyplus",
    "blender --background", "revitbatchprocessor",
    "ansys", "mapdl", "fluent", "abaqus", "cae noGUI",
    "freecad", "freecadcmd", "openscad", "librecad",
    "ezdxf", "cadquery", "accoreconsole", "autolisp",
    "solidworks", "solvespace",
)


def _read_text_safe(path):
    try:
        return path.read_text(encoding="utf-8", errors="ignore")
    except Exception:
        return ""


def _desktop_script_artifacts(root):
    if not root.exists() or not root.is_dir():
        return True
    try:
        candidates = list(root.iterdir())
        for directory in list(candidates):
            if directory.is_dir() and directory.name not in {"__pycache__", "_runtime"}:
                try:
                    candidates.extend(directory.iterdir())
                except Exception:
                    pass
        for path in candidates:
            if not path.is_file():
                continue
            if path.name in GUI_BYPASS_ALLOWED_FILENAMES:
                continue
            if path.suffix.lower() in GUI_BYPASS_FORBIDDEN_EXTENSIONS:
                return True
    except Exception:
        return True
    return False


def _history_paths(root):
    home = Path.home()
    return [
        home / ".bash_history",
        home / ".zsh_history",
        home / ".python_history",
        home / ".local/share/fish/fish_history",
        home / "AppData/Roaming/Microsoft/Windows/PowerShell/PSReadLine/ConsoleHost_history.txt",
        home / "AppData/Roaming/Microsoft/Windows/PowerShell/PSReadLine/Visual Studio Code Host_history.txt",
        root / ".bash_history",
        root / ".zsh_history",
    ]


def _history_contains_bypass(root):
    for path in _history_paths(root):
        if not path.is_file():
            continue
        text = _read_text_safe(path).lower()
        if not text:
            continue
        for raw_line in text.splitlines():
            line = raw_line.strip()
            if not line or "eval.py" in line:
                continue
            touches_output = any(token in line for token in GUI_BYPASS_OUTPUT_TOKENS)
            runs_command = any(token in line for token in GUI_BYPASS_COMMAND_TOKENS)
            writes_file = any(
                token in line for token in (">", "tee ", "cat ", "set-content", "out-file", "new-item")
            )
            if touches_output and (runs_command or writes_file):
                return True
            if ("/desktop/" in line or "\\desktop\\" in line) and any(
                ext in line for ext in GUI_BYPASS_FORBIDDEN_EXTENSIONS
            ) and runs_command:
                return True
    return False


def check_no_gui_bypass(root):
    root = Path(root)
    if _desktop_script_artifacts(root):
        return False
    if _history_contains_bypass(root):
        return False
    return True

TARGET = "final_ss_gui_06.dxf"
TOL = 0.75


def _close(a, b, tol=TOL):
    return abs(float(a) - float(b)) <= tol


def _point_close(a, b, tol=TOL):
    return _close(a[0], b[0], tol) and _close(a[1], b[1], tol)


def _dist(a, b):
    return math.hypot(float(a[0]) - float(b[0]), float(a[1]) - float(b[1]))


def _dedupe(points):
    unique = []
    for point in points:
        if not any(_point_close(point, prev) for prev in unique):
            unique.append(point)
    return unique


def _segment_points(doc):
    points = []
    for entity in doc.modelspace():
        if entity.dxftype() == "LINE":
            start, end = entity.dxf.start, entity.dxf.end
            points.extend([(float(start.x), float(start.y)), (float(end.x), float(end.y))])
        elif entity.dxftype() == "LWPOLYLINE":
            points.extend((float(p[0]), float(p[1])) for p in entity.get_points("xy"))
    return _dedupe(points)


def _outline_segments(doc):
    segments = []
    for entity in doc.modelspace():
        if entity.dxftype() == "LINE":
            start, end = entity.dxf.start, entity.dxf.end
            segments.append(((float(start.x), float(start.y)), (float(end.x), float(end.y))))
        elif entity.dxftype() == "LWPOLYLINE":
            points = [(float(p[0]), float(p[1])) for p in entity.get_points("xy")]
            segments.extend(zip(points, points[1:]))
            if entity.closed and len(points) > 2:
                segments.append((points[-1], points[0]))
    return segments


def _edge_present(segments, start, end, tol=0.15):
    return any(
        (_point_close(a, start, tol) and _point_close(b, end, tol))
        or (_point_close(a, end, tol) and _point_close(b, start, tol))
        for a, b in segments
    )


def _hex_edges_present(doc, points, center):
    ordered = sorted(points, key=lambda p: math.atan2(p[1] - center[1], p[0] - center[0]))
    segments = _outline_segments(doc)
    return all(
        _edge_present(segments, ordered[index], ordered[(index + 1) % 6])
        for index in range(6)
    )


def _regular_hex_center(points):
    if len(points) != 6:
        return None
    center = (sum(p[0] for p in points) / 6, sum(p[1] for p in points) / 6)
    expected_top_y = 40 * math.sin(math.radians(60))
    if not all(_close(_dist(p, center), 40, 1.0) for p in points):
        return None
    rel = sorted((round(p[0] - center[0], 2), round(p[1] - center[1], 2)) for p in points)
    has_left_right = any(_point_close((dx, dy), (-40, 0), 1.0) for dx, dy in rel) and any(_point_close((dx, dy), (40, 0), 1.0) for dx, dy in rel)
    has_top = sum(1 for dx, dy in rel if _close(dy, expected_top_y, 1.0)) == 2
    has_bottom = sum(1 for dx, dy in rel if _close(dy, -expected_top_y, 1.0)) == 2
    if has_left_right and has_top and has_bottom:
        return center
    return None


def _hex_center(doc):
    for entity in doc.modelspace():
        if entity.dxftype() != "LWPOLYLINE":
            continue
        pts = _dedupe([(float(p[0]), float(p[1])) for p in entity.get_points("xy")])
        center = _regular_hex_center(pts)
        if center is not None and _hex_edges_present(doc, pts, center):
            return center
    points = _segment_points(doc)
    for candidate in itertools.combinations(points, 6):
        center = _regular_hex_center(candidate)
        if center is not None and _hex_edges_present(doc, candidate, center):
            return center
    return None


def _has_circle(doc, center, radius):
    for entity in doc.modelspace():
        if entity.dxftype() != "CIRCLE":
            continue
        c = entity.dxf.center
        if _point_close((c.x, c.y), center) and _close(entity.dxf.radius, radius):
            return True
    return False


def evaluate():
    if not check_no_gui_bypass(Path(os.environ.get("OUTPUT_ROOT", "/home/user/Desktop"))):
        return False
    path = OUTPUT_ROOT / TARGET
    if not path.exists() or path.stat().st_size <= 0:
        return False
    doc = ezdxf.readfile(path)
    if int(doc.header.get("$INSUNITS", 0)) != 4:
        return False
    center = _hex_center(doc)
    if center is None:
        return False
    return (
        _has_circle(doc, center, 10)
        and _has_circle(doc, (center[0] - 28, center[1]), 3)
        and _has_circle(doc, (center[0] + 28, center[1]), 3)
        and all(
            _has_circle(
                doc,
                (
                    center[0] + 25 * math.cos(math.radians(angle)),
                    center[1] + 25 * math.sin(math.radians(angle)),
                ),
                2,
            )
            for angle in [0, 60, 120, 180, 240, 300]
        )
    )


if __name__ == "__main__":
    try:
        if not check_no_gui_bypass(OUTPUT_ROOT):
            ok = False
        else:
            ok = evaluate()
    except Exception:
        ok = False
    print("True" if ok else "False")