Skip to content

Instantly share code, notes, and snippets.

@aavogt
Last active April 29, 2024 23:12
Show Gist options
  • Save aavogt/ab8a5e35a8d67d00bc0e43e092e5b361 to your computer and use it in GitHub Desktop.
Save aavogt/ab8a5e35a8d67d00bc0e43e092e5b361 to your computer and use it in GitHub Desktop.
script to download/run yet-another-cad-viewer
#!/bin/bash
PWD0=$(pwd)
SCRIPT_DIR=$(dirname "$0")
writePY () {
cat <<'EOF' > $SCRIPT_DIR/yet-another-cad-viewer-master/example/object.py
# Optional: enable logging to see what's happening
import sys
import pdb
if len(sys.argv) < 3 or sys.argv[2] in ['--help', '-h']:
print("Usage: yacv [files]")
sys.exit(0)
import logging
import os
from build123d import * # Also works with cadquery objects!
logging.basicConfig(level=logging.DEBUG)
from yacv_server import show, export_all # Check out other exported methods for more features!
import subprocess
import pyinotify
files = []
for i in range(2, len(sys.argv)):
if os.path.isabs(sys.argv[i]):
files.append(sys.argv[i])
else:
files.append(sys.argv[1] + '/' + sys.argv[i])
ms = {}
def importShow(ev):
print(f"importShow({ev})")
for file in files:
if ev is None or (ev is not None and ev.pathname == file):
ms[file] = Mesher().read(file)
objs = []
names = []
mask = []
for n, m in ms.items():
mask.append( (n == getattr(ev, 'pathname', None)) * len(m))
objs = objs + m
if len(ms) == 1:
names.append(n)
else:
names = names + [ f"{n}{i}" for i in range(len(m)) ]
prefix = os.path.commonprefix(names)
names = [ n[len(prefix):] for n in names ]
if len(names) == 1:
names = os.path.basename(names[0])
if ev is None:
show(*objs, names=names)
else:
objs = [ objs[i] for i in range(len(objs)) if mask[i] ]
names = [ names[i] for i in range(len(names)) if mask[i] ]
show(*objs, names=names, auto_clear=False)
wm = pyinotify.WatchManager()
for file in files:
wm.add_watch(file, pyinotify.IN_MODIFY)
importShow(None)
notifier = pyinotify.Notifier(wm, importShow)
subprocess.run("xdg-open http://localhost:${YACV_PORT:-32323}", shell=True)
notifier.loop()
EOF
}
# if the yet-another-cad-viewer-master is missing, download it
if [ ! -d $SCRIPT_DIR/yet-another-cad-viewer-master ]; then
echo "Downloading yet-another-cad-viewer-master"
git clone https://github.com/yeicor-3d/yet-another-cad-viewer.git $SCRIPT_DIR/yet-another-cad-viewer-master
writePY
fi
# if this file is newer than the object.py, rewrite it
if [ $0 -nt $SCRIPT_DIR/yet-another-cad-viewer-master/example/object.py ]; then
writePY
fi
cd $SCRIPT_DIR/yet-another-cad-viewer-master/example
# if the venv is missing, create it
if [ ! -d venv ]; then
echo "Creating virtual environment"
python3 -m venv venv
. venv/bin/activate
pip install -r requirements.txt
pip install pyinotify
else
. venv/bin/activate
fi
python object.py $PWD0 $*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment