-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcweep-dev.py
More file actions
85 lines (74 loc) · 2.52 KB
/
Copy pathcweep-dev.py
File metadata and controls
85 lines (74 loc) · 2.52 KB
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
import os
import shutil
import subprocess
import sys
import threading
import webbrowser
from pathlib import Path
from ocp_vscode.standalone import Viewer
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer
viewer = Viewer({"port": 3939, "host": "127.0.0.1"})
viewer_thread = threading.Thread(target=viewer.start, daemon=True)
viewer_thread.start()
webbrowser.open("http://127.0.0.1:3939")
cwd = Path(__file__).parent
script = cwd / "cweep.py"
pcb_file = cwd / "cweep.kicad_pcb"
step_file = cwd / "case" / "cweep.step"
sys.argv = [script, "--preview"]
ctx = {"__file__": str(script)}
# We generate a model of the PCB if we can, which is helpful for validating case fitment during
# development
if shutil.which("kicad-cli") and not (
step_file.exists() and step_file.stat().st_mtime > pcb_file.stat().st_mtime
):
subprocess.run(
[
"kicad-cli",
"pcb",
"export",
"step",
"--force",
"--fuse-shapes",
# Although including this soldermask would make the percieved thickness of the board
# more accurate, the thin faces kicad exports for the silkscreen make the viewer bug out
# when clipping the model, so I don't actually incldue them except for validating that
# the placement of the models is correct
# "--include-soldermask",
"--subst-models",
"--output",
str(step_file),
str(pcb_file),
]
)
class H(FileSystemEventHandler):
def __init__(self):
self.last_modified = {}
def on_modified(self, event):
if event is not None and event.src_path not in (str(script), str(pcb_file)):
return
watched_file = event.src_path if event else str(script)
current_mtime = os.path.getmtime(watched_file)
if self.last_modified.get(watched_file) == current_mtime:
return
self.last_modified[watched_file] = current_mtime
print("\033[2J\033[H") # Clear terminal
filename = Path(watched_file).name if event else "cweep.py"
print(f"{filename} modified, reloading...")
try:
exec(open(script).read(), ctx)
except Exception as e:
sys.excepthook(type(e), e, e.__traceback__)
h = H()
obs = Observer()
obs.schedule(h, str(cwd), recursive=False)
obs.start()
print("watching cweep.py")
h.on_modified(None)
try:
obs.join()
except KeyboardInterrupt:
print("\nShutting down...")
obs.stop()
obs.join()