Skip to content

Commit

Permalink
add volume check to b1scad test
Browse files Browse the repository at this point in the history
  • Loading branch information
bat52 committed Dec 25, 2024
1 parent 752c1da commit 30a4ee9
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/b1scad/scad/model02.scad
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
translate([5, 6, 7]) {
sphere(2);
sphere(20);
}
10 changes: 8 additions & 2 deletions src/b1scad/scad2py.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ def scad2py(infname: str, execute_en: bool = True):

# generate output file
output_path = file_replace_extension(infname, ".py")

# generate module name
fname = os.path.basename(output_path)
basefname,_ = os.path.splitext(fname)
modelname = snake2camel(basefname)
Expand All @@ -176,12 +178,16 @@ def scad2py(infname: str, execute_en: bool = True):
# Generate main python code
pyshape = generator.generate(infname)

# generate python file code
# generate python file code
generator.write_template(retshape=pyshape, model=modelname)
print(f"Generated Python code saved to {output_path}")

if execute_en:
os.system(f'python3 -m {output_path} -odoff')
cmdstr = f'python3 {output_path} -odoff'
print(cmdstr)
os.system(cmdstr)

return output_path, modelname

# Example usage:
if __name__ == "__main__":
Expand Down
29 changes: 25 additions & 4 deletions src/b1scad/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,37 @@
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), "../"))
from b1scad.scad2py import scad2py
from pylele.conversion.scad2stl import scad2stl
from pylele.api.solid import stl_report_metrics, volume_match_reference

def stl_compare_volume(refstl, outstl):
refrpt = stl_report_metrics(refstl)
outrpt = stl_report_metrics(outstl)

assert volume_match_reference(
volume=outrpt['volume'],
reference=refrpt['volume'],
), f"reference file: {refstl}\nout file: {outstl}\nreference volume: {refrpt['volume']}, volume: {outrpt['volume']}"

pass

class B1scadTestMethods(unittest.TestCase):
"""Pylele Test Class"""
def test_all_scad(self):
scaddir = os.path.join(os.path.abspath(os.path.dirname(__file__)),"scad")
for idx in range(3):
fname = f"model{idx:02}.scad"
fullfile = os.path.join(scaddir,fname)
print(fullfile)
scad2py(fullfile)
fname = f"model{idx:02}"
scadfname = f"{fname}.scad"
fullscadfile = os.path.join(scaddir,scadfname)
print(fullscadfile)

# generate reference .stl file
refstl = scad2stl(fullscadfile)

# generate output .py and .stl file
outpy, modelname = scad2py(fullscadfile, execute_en=True)
outstl = os.path.join("./build",modelname,f'{modelname}.stl')
stl_compare_volume(refstl, outstl)

def test_main():
""" Launch all tests """
Expand Down
31 changes: 17 additions & 14 deletions src/pylele/api/solid.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ def export_dict2text(outpath, fname, dictdata, fmt='.txt') -> str:

assert os.path.isfile(out_fname)


def volume_match_reference(
volume: float,
reference: float,
Expand All @@ -135,6 +134,21 @@ def volume_match_reference(

return False

def stl_report_metrics(out_fname: str) -> dict:
assert os.path.isfile(out_fname), f"File {out_fname} does not exist!!!"
mesh = trimesh.load_mesh(out_fname)
# assert mesh.is_watertight
print(f"mesh_volume: {mesh.volume}")
print(f"mesh.convex_hull.volume: {mesh.convex_hull.volume}")
print(f"mesh.bounding_box: {mesh.bounding_box.extents}")
# mesh.show() does not work
rpt = {}
rpt["volume"] = mesh.volume
rpt["convex_hull_volume"] = mesh.convex_hull.volume
rpt["bounding_box_x"] = mesh.bounding_box.extents[0]
rpt["bounding_box_y"] = mesh.bounding_box.extents[1]
rpt["bounding_box_z"] = mesh.bounding_box.extents[2]
return rpt

def stl_check_volume(
out_fname: str,
Expand All @@ -145,20 +159,9 @@ def stl_check_volume(
"""Check the volume of an .stl mesh against a reference value"""
rpt = {}
if check_en:
assert os.path.isfile(out_fname), f"File {out_fname} does not exist!!!"
mesh = trimesh.load_mesh(out_fname)
# assert mesh.is_watertight
print(f"mesh_volume: {mesh.volume}")
print(f"mesh.convex_hull.volume: {mesh.convex_hull.volume}")
print(f"mesh.bounding_box: {mesh.bounding_box.extents}")
# mesh.show() does not work
rpt["volume"] = mesh.volume
rpt["convex_hull_volume"] = mesh.convex_hull.volume
rpt["bounding_box_x"] = mesh.bounding_box.extents[0]
rpt["bounding_box_y"] = mesh.bounding_box.extents[1]
rpt["bounding_box_z"] = mesh.bounding_box.extents[2]
rpt = stl_report_metrics(out_fname)
rpt['pass'] = volume_match_reference(
volume=mesh.volume,
volume=rpt['volume'],
reference=reference_volume,
tolerance=reference_volume_tolerance/100
)
Expand Down
2 changes: 1 addition & 1 deletion src/test.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env bash

SCRIPT_DIR=$(dirname "$(realpath "${BASH_SOURCE[0]}")")
time python3 $SCRIPT_DIR/b1scad/test.py $@
time python3 $SCRIPT_DIR/b1scad/test.py
time python3 $SCRIPT_DIR/pylele/test.py $@

0 comments on commit 30a4ee9

Please sign in to comment.