Compare commits

...

2 Commits

Author SHA1 Message Date
27a4cf5626 add three.js example WebGL renderer 2024-02-03 03:04:24 +00:00
c4d90efedb cq_toplevel: add a --export-svg option 2024-02-03 03:00:21 +00:00
2 changed files with 80 additions and 0 deletions

46
build/doc/index.html Normal file
View File

@ -0,0 +1,46 @@
<html>
<head>
<!-- based on three.js example here: <https://jsfiddle.net/2nyxkmco/> -->
<script type="importmap">
{
"imports": {
"three": "https://unpkg.com/three/build/three.module.js"
}
}
</script>
<head>
<body>
<script type="javascript">
import * as THREE from 'three';
const width = window.innerWidth, height = window.innerHeight;
// init
const camera = new THREE.PerspectiveCamera( 70, width / height, 0.01, 10 );
camera.position.z = 1;
const scene = new THREE.Scene();
const geometry = new THREE.BoxGeometry( 0.2, 0.2, 0.2 );
const material = new THREE.MeshNormalMaterial();
const mesh = new THREE.Mesh( geometry, material );
scene.add( mesh );
const renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setSize( width, height );
renderer.setAnimationLoop( animation );
document.body.appendChild( renderer.domElement );
// animation
function animation( time ) {
mesh.rotation.x = time / 2000;
mesh.rotation.y = time / 1000;
renderer.render( scene, camera );
}
</body>
</html>

View File

@ -22,6 +22,28 @@ import ldtek_battery
logger = logging.getLogger(__name__)
def svg_export_options(view: str):
proj = (1, 1, 1)
if view == "front":
proj = (0.00, -0.05, 0.10)
elif view == "back":
proj = (0.00, -0.05, -0.10)
elif view == "right":
proj = (-0.10, 0.00, 0.00)
return dict(
width = 1024,
height = 1024,
marginLeft = 100,
marginTop = 10,
showAxes = False,
# projectionDir controls both the angle and the distance from the camera to the model
projectionDir = proj,
strokeWidth = 0.25,
strokeColor = (255, 0, 0),
hiddenColor = (0, 0, 255),
showHidden = True,
)
def model():
render_phone = os.environ.get("CASE_RENDER_PHONE", "") not in ("", "0")
phone = pinephone.PinePhone()
@ -35,6 +57,7 @@ def main():
parser = argparse.ArgumentParser(description="toplevel cadquery interface")
parser.add_argument("--render-phone", action="store_true", help="include the phone model itself in the stl; useful to confirm fit visually before printing")
parser.add_argument("--export-stl")
parser.add_argument("--export-svg")
parser.add_argument("--editor", action="store_true", help="view in cq-editor")
args = parser.parse_args()
@ -42,11 +65,22 @@ def main():
if args.render_phone:
os.environ["CASE_RENDER_PHONE"] = "1"
logger.info("computing model ...")
model_ = model()
if args.export_stl:
logger.info("exporting stl to %s", args.export_stl)
cq.exporters.export(model_, args.export_stl)
if args.export_svg:
view = None
if "front" in args.export_svg:
view = "front"
elif "back" in args.export_svg:
view = "back"
elif "right" in args.export_svg:
view = "right"
logger.info("exporting svg to %s (view: %s)", args.export_svg, str(view))
cq.exporters.export(model_, args.export_svg, opt=svg_export_options(view))
if args.editor:
logger.info("launching cq-editor")
subprocess.check_call(["cq-editor", __file__])