1
0
mirror of https://github.com/nophead/NopSCADlib.git synced 2025-07-31 20:50:15 +02:00

Added pose_stl() module because pose() only works for assembly views.

This commit is contained in:
Chris Palmer
2025-04-09 23:29:00 +01:00
parent 6a58d37f77
commit 33ac3820b0
4 changed files with 26 additions and 7 deletions

View File

@@ -7847,6 +7847,8 @@ The `pose()` module allows assembly views in the readme to be posed differently
* 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. * 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. * Two `pose()` modules can be chained to allow different poses for exploded and assembled views.
The `pose_stl()` module allows an STL child to be posed for its rendered image used in the readme for the project.
[utils/core/bom.scad](utils/core/bom.scad) Implementation. [utils/core/bom.scad](utils/core/bom.scad) Implementation.
[tests/BOM.scad](tests/BOM.scad) Code for this example. [tests/BOM.scad](tests/BOM.scad) Code for this example.
@@ -7870,8 +7872,9 @@ The `pose()` module allows assembly views in the readme to be posed differently
| `no_explode()` | Prevent children being exploded | | `no_explode()` | Prevent children being exploded |
| `no_pose()` | Force children not to be posed even if parent is | | `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 | | `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, 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(a = [55, 0, 25], t = [0, 0, 0], exploded = undef, d = undef)` | Pose an 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_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_stl(a = [70, 0, 315], t = [0, 0, 0], d = 500)` | Pose an STL for its render, `a`, `t`, & `d` are camera parameters. |
| `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. | | `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 `<name>_stl` to make it | | `stl(name)` | Name an stl that will appear on the BOM, there needs to a module named `<name>_stl` to make it |
| `stl_colour(colour = pp1_colour, alpha = 1)` | Colour an stl where it is placed in an assembly. `alpha` can be used to make it appear transparent. | | `stl_colour(colour = pp1_colour, alpha = 1)` | Colour an stl where it is placed in an assembly. `alpha` can be used to make it appear transparent. |

View File

@@ -69,7 +69,10 @@ class Part:
self.count = 1 self.count = 1
for arg in args: for arg in args:
arg = arg.replace('true', 'True').replace('false', 'False').replace('undef', 'None') arg = arg.replace('true', 'True').replace('false', 'False').replace('undef', 'None')
exec('self.' + arg) try:
exec('self.' + arg)
except:
print(arg)
def data(self): def data(self):
return self.__dict__ return self.__dict__
@@ -107,7 +110,7 @@ class BOM:
match = re.match(r'^(.*?\.stl|.*?\.dxf|.*?\.svg)\((.*)\)$', s) #look for name.stl(...), name.dxf(...) or name.svg(...) match = re.match(r'^(.*?\.stl|.*?\.dxf|.*?\.svg)\((.*)\)$', s) #look for name.stl(...), name.dxf(...) or name.svg(...)
if match: if match:
s = match.group(1) s = match.group(1)
args = [match.group(2)] args = match.group(2).split('|')
if s[-4:] == ".stl": if s[-4:] == ".stl":
parts = self.printed parts = self.printed
else: else:

View File

@@ -61,11 +61,14 @@ def render(target, type):
things = { 'stl' : 'printed', 'dxf' : 'routed', 'svg' : 'routed' }[type] things = { 'stl' : 'printed', 'dxf' : 'routed', 'svg' : 'routed' }[type]
colours = {} colours = {}
cameras = {}
for ass in flat_bom: for ass in flat_bom:
for part in ass[things]: for part in ass[things]:
obj = ass[things][part] obj = ass[things][part]
if "colour" in obj: if "colour" in obj:
colours[part] = obj["colour"] colours[part] = obj["colour"]
if "camera" in obj:
cameras[part] = obj["camera"]
# #
# Remove unused png files # Remove unused png files
# #
@@ -87,17 +90,20 @@ def render(target, type):
png_maker_name = tmp_dir + "/png.scad" png_maker_name = tmp_dir + "/png.scad"
pp1 = [0, 146/255, 0] pp1 = [0, 146/255, 0]
colour = pp1 colour = pp1
camera = "0,0,0,70,0,315,500"
if part in cameras:
camera = cameras[part]
if part in colours: if part in colours:
colour = colours[part] colour = colours[part]
if not '[' in colour: if not '[' in colour:
colour = '"' + colour + '"' colour = '"' + colour + '"'
with open(png_maker_name, "w") as f: with open(png_maker_name, "w") as f:
f.write('color(%s) import("%s");\n' % (colour, reltmp(part_file, target))) f.write('color(%s) import("%s");\n' % (colour, reltmp(part_file, target)))
cam = "--camera=0,0,0,70,0,315,500" if type == 'stl' else "--camera=0,0,0,0,0,0,500" cam = "--camera=" + camera if type == 'stl' else "--camera=0,0,0,0,0,0,500"
render = "--preview" if type == 'stl' or colour != pp1 else "--render" render = "--preview" if type == 'stl' or colour != pp1 else "--render"
tmp_name = tmp_dir + '/' + part[:-4] + '.png' tmp_name = tmp_dir + '/' + part[:-4] + '.png'
dummy_deps_name = tmp_dir + '/tmp.deps' # work around for OpenSCAD issue #3879 dummy_deps_name = tmp_dir + '/tmp.deps' # work around for OpenSCAD issue #3879
openscad.run("-o", tmp_name, png_maker_name, colour_scheme, "--projection=p", image_size, cam, render, "-D$pose=1", "-D$explode=0", "--autocenter", "--viewall", "-d", dummy_deps_name) openscad.run("-o", tmp_name, png_maker_name, colour_scheme, "--projection=p", image_size, cam, render, "--autocenter", "--viewall", "-d", dummy_deps_name)
do_cmd(("magick "+ tmp_name + " -trim -resize 280x280 -background %s -gravity Center -extent 280x280 -bordercolor %s -border 10 %s" do_cmd(("magick "+ tmp_name + " -trim -resize 280x280 -background %s -gravity Center -extent 280x280 -bordercolor %s -border 10 %s"
% (background, background, tmp_name)).split()) % (background, background, tmp_name)).split())
update_image(tmp_name, png_name) update_image(tmp_name, png_name)

View File

@@ -41,6 +41,8 @@
//! * If the `d` parameter is set to specify the camera distance then the normal `viewall` and `autocenter` options are suppressed allowing a small section to be zoomed in to fill the view. //! * If the `d` parameter is set to specify the camera distance then the normal `viewall` and `autocenter` options are suppressed 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. //! * 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. //! * Two `pose()` modules can be chained to allow different poses for exploded and assembled views.
//!
//! The `pose_stl()` module allows an STL child to be posed for its rendered image used in the readme for the project.
// //
function bom_mode(n = 1) = (is_undef($bom) ? 0 : $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 bom_mode(n = 1) = (is_undef($bom) ? 0 : $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) ? is_undef($explode) ? 0 : $explode : 0; //! Returns the value of `$explode` if it is defined, else `0` function exploded() = is_undef($exploded_parent) ? is_undef($explode) ? 0 : $explode : 0; //! Returns the value of `$explode` if it is defined, else `0`
@@ -73,7 +75,7 @@ module explode(d, explode_children = false, offset = [0,0,0], show_line = true)
module no_pose() //! Force children not to be posed even if parent is module no_pose() //! Force children not to be posed even if parent is
let($posed = true, $zoomed = undef) children(); let($posed = true, $zoomed = undef) children();
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. module pose(a = [55, 0, 25], t = [0, 0, 0], exploded = undef, d = undef) //! Pose an 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) let($zoomed = is_undef(d)
? is_undef($zoomed) ? is_undef($zoomed)
? undef ? undef
@@ -136,10 +138,15 @@ module stl_colour(colour = pp1_colour, alpha = 1) { //! Colour an stl where it i
children(); children();
} }
module pose_stl(a = [70, 0, 315], t = [0, 0, 0], d = 500) //! Pose an STL for its render, `a`, `t`, & `d` are camera parameters.
let($stl_camera = str(t.x, ",", t.y, ",", t.z, ",", a.x, ",", a.y, ",", a.z, ",", d))
children();
module stl(name) { //! Name an stl that will appear on the BOM, there needs to a module named `<name>_stl` to make it module stl(name) { //! Name an stl that will appear on the BOM, there needs to a module named `<name>_stl` to make it
if(bom_mode() && is_undef($in_stl)) { if(bom_mode() && is_undef($in_stl)) {
colour = is_undef($stl_colour) ? pp1_colour : $stl_colour; colour = is_undef($stl_colour) ? pp1_colour : $stl_colour;
echo(str("~", name, ".stl(colour='", colour, "')")); camera = is_undef($stl_camera) ? "0,0,0,70,0,315,500" : $stl_camera;
echo(str("~", name, ".stl(colour='", colour, "'| camera='", camera, "')" ));
} }
if($children) if($children)
if(is_undef($pose)) if(is_undef($pose))