From 7c2df8d36d214a8bf8ebdcd39da293209bc175ab Mon Sep 17 00:00:00 2001 From: Chris Palmer Date: Sat, 20 Feb 2021 19:28:52 +0000 Subject: [PATCH] The pose module can now specify the camera distance supressing viewall and autocentre. --- readme.md | 9 ++++++++- scripts/bom.py | 2 ++ scripts/views.py | 4 +++- utils/core/bom.scad | 47 +++++++++++++++++++++++++++++++-------------- 4 files changed, 46 insertions(+), 16 deletions(-) diff --git a/readme.md b/readme.md index 9aba91f..f403062 100644 --- a/readme.md +++ b/readme.md @@ -6066,6 +6066,13 @@ If the code to make an STL or DXF is made a child of the `stl()` or `dxf()` modu it with code. This can speed up the generation of the build instructions greatly but isn't compatible with STLs that include support structures. +The `pose()` module allows assembly views in the readme to be posed differently to the default view in the GUI: + +* Setting the `exploded` parameter to `true` allows just the exploded version to be posed and setting to `false` allows just the assembled view to be posed, the default is both. +* If the `d` parameter is set to specify the camera distance then the normal `viewall` and `autocenter` options are supressed allowing a small section to be zoomed in to fill the view. +* To get the parameter values make the GUI window square, pose the view with the mouse and then copy the viewport parameters from the Edit menu and paste them into the pose invocation. +* Two `pose()` modules can be chained to allow different poses for exploded and assembled views. + [utils/core/bom.scad](utils/core/bom.scad) Implementation. [tests/BOM.scad](tests/BOM.scad) Code for this example. @@ -6089,7 +6096,7 @@ This can speed up the generation of the build instructions greatly but isn't com | `no_explode()` | Prevent children being exploded | | `no_pose()` | Force children not to be posed even if parent is | | `not_on_bom(on = false)` | Specify the following child parts are not on the BOM, for example when they are on a PCB that comes assembled | -| `pose(a = [55, 0, 25], t = [0, 0, 0], exploded = undef)` | Pose an STL or assembly for rendering to png by specifying rotation `a` and translation `t`, `exploded = true for` just the exploded view or `false` for unexploded only. | +| `pose(a = [55, 0, 25], t = [0, 0, 0], exploded = undef, d = undef)` | Pose an STL or assembly for rendering to png by specifying rotation `a`, translation `t` and optionally `d`, `exploded = true for` just the exploded view or `false` for unexploded only. | | `pose_hflip(exploded = undef)` | Pose an STL or assembly for rendering to png by flipping around the Y axis, `exploded = true for` just the exploded view or `false` for unexploded only. | | `pose_vflip(exploded = undef)` | Pose an STL or assembly for rendering to png by flipping around the X axis, `exploded = true for` just the exploded view or `false` for unexploded only. | | `stl(name)` | Name an stl that will appear on the BOM, there needs to a module named `_stl` to make it | diff --git a/scripts/bom.py b/scripts/bom.py index db5ff23..69419c4 100755 --- a/scripts/bom.py +++ b/scripts/bom.py @@ -61,6 +61,7 @@ class BOM: self.name = name self.big = None self.ngb = False + self.zoomed = 0 self.count = 1 self.vitamins = {} self.printed = {} @@ -75,6 +76,7 @@ class BOM: "name" : self.name, "big" : self.big, "ngb" : self.ngb, + "zoomed" : self.zoomed, "count" : self.count, "assemblies" : assemblies, "vitamins" : {v : self.vitamins[v].data() for v in self.vitamins}, diff --git a/scripts/views.py b/scripts/views.py index c5dbaf7..3e1e1d8 100755 --- a/scripts/views.py +++ b/scripts/views.py @@ -185,6 +185,7 @@ def views(target, do_assemblies = None): # for ass in flat_bom: if ass["name"] == real_name: + zoomed = ass['zoomed'] if not "blurb" in ass: ass["blurb"] = blurb.scrape_module_blurb(lines[:line_no]) break @@ -219,7 +220,8 @@ def views(target, do_assemblies = None): t = time.time() target_def = ['-D$target="%s"' % target] if target else [] cwd_def = ['-D$cwd="%s"' % os.getcwd().replace('\\', '/')] - openscad.run_list(options.list() + target_def + cwd_def + ["-D$pose=1", "-D$explode=%d" % explode, colour_scheme, "--projection=p", "--imgsize=4096,4096", "--autocenter", "--viewall", "-d", dname, "-o", tmp_name, png_maker_name]); + view_def = ['--viewall', '--autocenter'] if not (zoomed & (1 << explode)) else ['--camera=0,0,0,55,0,25,140'] + openscad.run_list(options.list() + target_def + cwd_def + view_def +["-D$pose=1", "-D$explode=%d" % explode, colour_scheme, "--projection=p", "--imgsize=4096,4096", "-d", dname, "-o", tmp_name, png_maker_name]); times.add_time(png_name, t) do_cmd(["magick", tmp_name, "-trim", "-resize", "1004x1004", "-bordercolor", background, "-border", "10", tmp_name]) update_image(tmp_name, png_name) diff --git a/utils/core/bom.scad b/utils/core/bom.scad index cafa638..8f3f315 100644 --- a/utils/core/bom.scad +++ b/utils/core/bom.scad @@ -34,14 +34,19 @@ //! If the code to make an STL or DXF is made a child of the `stl()` or `dxf()` module then the STL or DXF will be used in the assembly views generated by `views.py` instead of generating //! it with code. //! This can speed up the generation of the build instructions greatly but isn't compatible with STLs that include support structures. +//! +//! The `pose()` module allows assembly views in the readme to be posed differently to the default view in the GUI: +//! +//! * Setting the `exploded` parameter to `true` allows just the exploded version to be posed and setting to `false` allows just the assembled view to be posed, the default is both. +//! * If the `d` parameter is set to specify the camera distance then the normal `viewall` and `autocenter` options are supressed allowing a small section to be zoomed in to fill the view. +//! * To get the parameter values make the GUI window square, pose the view with the mouse and then copy the viewport parameters from the Edit menu and paste them into the pose invocation. +//! * Two `pose()` modules can be chained to allow different poses for exploded and assembled views. // function bom_mode(n = 1) = $_bom >= n && (is_undef($on_bom) || $on_bom); //! Current BOM mode, 0 = none, 1 = printed and routed parts and assemblies, 2 includes vitamins as well function exploded() = is_undef($exploded_parent) ? $exploded : 0; //! Returns the value of `$exploded` if it is defined, else `0` function show_supports() = !$preview || exploded(); //! True if printed support material should be shown module no_explode() let($exploded_parent = true) children(); //! Prevent children being exploded -module no_pose() let($posed = true) children(); //! Force children not to be posed even if parent is - module explode(d, explode_children = false, offset = [0,0,0]) { //! Explode children by specified Z distance or vector `d`, option to explode grand children v = is_list(d) ? d : [0, 0, d]; o = is_list(offset) ? offset : [0, 0, offset]; @@ -62,17 +67,29 @@ module explode(d, explode_children = false, offset = [0,0,0]) { //! Explode children(); } -module pose(a = [55, 0, 25], t = [0, 0, 0], exploded = undef) //! Pose an STL or assembly for rendering to png by specifying rotation `a` and translation `t`, `exploded = true for` just the exploded view or `false` for unexploded only. - if(is_undef($pose) || !is_undef($posed) || (!is_undef(exploded) && exploded != !!exploded())) - children(); - else - let($posed = true) // only pose the top level - rotate([55, 0, 25]) - rotate([-a.x, 0, 0]) - rotate([0, -a.y, 0]) - rotate([0, 0, -a.z]) - translate(-t) - children(); +module no_pose() let($posed = true, $zoomed = undef) children(); //! Force children not to be posed even if parent is + +module pose(a = [55, 0, 25], t = [0, 0, 0], exploded = undef, d = undef) //! Pose an STL or assembly for rendering to png by specifying rotation `a`, translation `t` and optionally `d`, `exploded = true for` just the exploded view or `false` for unexploded only. + let($zoomed = is_undef(d) + ? is_undef($zoomed) + ? undef + : $zoomed + : is_undef(exploded) + ? 3 + : exploded + ? 2 + : 1) + if(is_undef($pose) || !is_undef($posed) || (!is_undef(exploded) && exploded != !!exploded())) + children(); + else + let($posed = true) // only pose the top level + rotate([55, 0, 25]) + translate_z(is_undef(d) ? 0 : 140 - d) + rotate([-a.x, 0, 0]) + rotate([0, -a.y, 0]) + rotate([0, 0, -a.z]) + translate(-t) + children(); module pose_hflip(exploded = undef) //! Pose an STL or assembly for rendering to png by flipping around the Y axis, `exploded = true for` just the exploded view or `false` for unexploded only. if(is_undef($pose) || !is_undef($posed) || (!is_undef(exploded) && exploded != !!exploded())) @@ -93,7 +110,9 @@ module pose_vflip(exploded = undef) //! Pose an STL or assembly for render module assembly(name, big = undef, ngb = false) { //! Name an assembly that will appear on the BOM, there needs to a module named `_assembly` to make it. `big` can force big or small assembly diagrams. if(bom_mode()) { - args = is_undef(big) && !ngb ? "" : str("(big=", big, ", ngb=", ngb, ")"); + zoom = is_undef($zoomed) ? 0 : $zoomed; + arglist = str(arg(big, undef, "big"), arg(ngb, false, "ngb"), arg(zoom, 0, "zoomed")); + args = len(arglist) ? str("(", slice(arglist, 2), ")") : ""; echo(str("~", name, "_assembly", args, "{")); } no_pose()