Skip to content

Commit

Permalink
publish STL files on a temporary static web site as a github action
Browse files Browse the repository at this point in the history
  • Loading branch information
drayde committed Feb 26, 2024
1 parent 3362584 commit 7605838
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 1 deletion.
68 changes: 68 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# SPDX-FileCopyrightText: 2023 Robin Vobruba <hoijui.quaero@gmail.com>
# SPDX-FileCopyrightText: 2024 Andreas Kahler <mail@andreaskahler.com>
#
# SPDX-License-Identifier: Unlicense

name: Deploy Static STLs

on:
push:
branches: [ smart_doc, ak_smart_doc ]

# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
contents: read
pages: write
id-token: write

# Allow one concurrent deployment
concurrency:
group: "pages"
cancel-in-progress: true

jobs:
build:
name: Compile
runs-on: ubuntu-latest
steps:
- name: "Check out the repo"
uses: actions/checkout@v3
with:
submodules: true

- name: Fix checkout ownership
run: |
# HACK Workaround for bug:
# https://github.com/actions/runner/issues/2033
mkdir -p /home/runner/work/_temp/_github_home
printf "[safe]\n\tdirectory = /github/workspace" \
> /home/runner/work/_temp/_github_home/.gitconfig
- uses: actions/setup-python@v4
with:
python-version: '3.10'

- name: Install requirements
run: |
pip install -r doc_generator\requirements.txt
- name: Generate STL
run: |
python doc_generator/generate_static.py
- name: Upload artifact
uses: actions/upload-pages-artifact@v1
with:
path: "doc_generator/doc_generator/build/static/"

# Deployment job
deploy:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
needs: build
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v1
141 changes: 141 additions & 0 deletions doc_generator/generate_static.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
"""
Generate a bunch of STL files
to show case the possiblilties of the nimble cadquery code
To be replaced later with code that builds complete bundles
of STL files and documentation for custom configurations
"""
import os
import sys
from pathlib import Path

from orchestration import OrchestrationRunner
import cadquery as cq
from cadquery import cqgi

# List of generated files (description, filename)
generated_files = []


def generate():

print("Starting generate()")
runner = OrchestrationRunner("static", "static")

print("Setting up build environment")
runner.setup(create_only_build_dir=True)

generate_leg(294, 3.6, runner._build_dir / "beam-21holes.stl")
generated_files.append(("Rack leg with 21 holes", "beam-21holes.stl"))
generate_leg(168, 3.6, runner._build_dir / "beam-12holes.stl")
generated_files.append(("Rack leg with 12 holes", "beam-12holes.stl"))
generate_leg(294, 5.8, runner._build_dir / "beam-M6-21holes.stl")
generated_files.append(("Rack leg with 21 holes (M6 version)", "beam-M6-21holes.stl"))
generate_leg(168, 5.8, runner._build_dir / "beam-M6-12holes.stl")
generated_files.append(("Rack leg with 12 holes (M6 version)", "beam-M6-12holes.stl"))

for (t, c) in [
("stuff", 3),
("stuff-thin", 3),
("nuc", 3),
("nuc", 4),
("usw-flex", 3),
("usw-flex-mini", 2),
("anker-powerport5", 2),
("anker-a2123", 2),
("anker-atom3slim", 2),
("hdd35", 2),
("dual-ssd", 2),
("raspi", 2)]:
filename = f"shelf_6in_{c}u_{t}.stl"
out_file = runner._build_dir / (filename)
print(f"Creating {t} shelf, saving to {out_file}")
generate_shelf(t, c, out_file)
generated_files.append((f"6 inch shelf, type {t}, height {c} units", filename))

# create index.html with links to all generated files
index_file = runner._build_dir / "index.html"
with open(index_file, "w") as f:
f.write("<html>")
f.write("""
<head>
<title>Nimble generated files</title>
<style>
body { font-family: Arial, sans-serif; }
</style>
</head>
<body>
<p>
This is temporary output of the new "smart doc" Nimble feature.
</p>
<p>
In the future it will create complete bundles
of STL files and documentation for custom configurations<br><br>
</p>
<p>
For more details on this project see
<a href="https://github.com/Wakoma/nimble">https://github.com/Wakoma/nimble</a>
</p>
""")

f.write("<h3>Generated files:</h3>")
f.write("<ul>")
for (desc, filename) in generated_files:
f.write(f"<li><a href='{filename}'>{desc}</a></li>")
f.write("</ul>")
f.write("</body></html>")

print("Finished generate()")


def generate_leg(length, mounting_holes_dia, out_file):
params = {'length': length, 'mounting_holes_dia': mounting_holes_dia}
run_cqgi_model_script_and_save("rack_leg.py", params, out_file)


def generate_shelf(shelf_type, hole_count, out_file):
params = {'shelf_type': shelf_type, 'hole_count': hole_count}
run_cqgi_model_script_and_save("tray_6in.py", params, out_file)


def run_cqgi_model_script_and_save(file_name, params, out_file):
"""
Handle executing the model script via CQGI, save result
"""
# get path to this script file
module_folder = Path(os.path.abspath(__file__)).parent
module_folder.resolve()

# remember python path to restore later
python_path = sys.path

# Add the path to the cadquery script to the python path
cq_path = module_folder / "mechanical" / "components" / "cadquery"
sys.path.append(str(cq_path))

# Read and execute the cadquery script
user_script = ""
with open(cq_path / file_name) as f:
user_script = f.read()

# Build the object with the customized parameters and get it ready to export
build_result = cqgi.parse(user_script).build(build_parameters=params)

# restore python path
sys.path = python_path

if build_result.success:
res = build_result.results[0].shape

if not os.path.exists(out_file):
cq.exporters.export(res, str(out_file))
else:
print(f"Failed to build {file_name}")
print(build_result.exception)



if __name__ == "__main__":
generate()
6 changes: 5 additions & 1 deletion doc_generator/orchestration.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def __init__(self, config, config_hash, force_rebuild=True) -> None:
self._config_hash = config_hash
self._force_rebuild = force_rebuild

def setup(self):
def setup(self, create_only_build_dir=False):
"""
Set up build environment.
"""
Expand Down Expand Up @@ -78,6 +78,10 @@ def setup(self):
with open(self._build_dir / build_dir_identification_file, 'w') as f:
f.write(
"The contents of this directory are generated by the NIMBLE build system. Do not edit manually.")

if create_only_build_dir:
return

os.mkdir(self._outputdir_gitbuilding)
os.mkdir(self._outputdir_stl)
os.mkdir(self._outputdir_svg)
Expand Down

0 comments on commit 7605838

Please sign in to comment.