cq_toplevel: memoize the model, to speed up the case where invoked in a way that we dont need to compute it
This commit is contained in:
@@ -44,7 +44,8 @@ def svg_export_options(view: str):
|
|||||||
showHidden = True,
|
showHidden = True,
|
||||||
)
|
)
|
||||||
|
|
||||||
def model():
|
def _model():
|
||||||
|
logger.info("computing model ...")
|
||||||
render_phone = os.environ.get("CASE_RENDER_PHONE", "") not in ("", "0")
|
render_phone = os.environ.get("CASE_RENDER_PHONE", "") not in ("", "0")
|
||||||
render_phone_only = os.environ.get("CASE_RENDER_PHONE_ONLY", "") not in ("", "0")
|
render_phone_only = os.environ.get("CASE_RENDER_PHONE_ONLY", "") not in ("", "0")
|
||||||
|
|
||||||
@@ -56,6 +57,14 @@ def model():
|
|||||||
battery = ldtek_battery.LdtekBattery()
|
battery = ldtek_battery.LdtekBattery()
|
||||||
return case.case(phone, battery=battery, render_phone=render_phone)
|
return case.case(phone, battery=battery, render_phone=render_phone)
|
||||||
|
|
||||||
|
_computedModel = None
|
||||||
|
def model():
|
||||||
|
""" memoized wrapper around `_model` """
|
||||||
|
global _computedModel
|
||||||
|
if _computedModel is None:
|
||||||
|
_computedModel = _model()
|
||||||
|
return _computedModel
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
logging.basicConfig()
|
logging.basicConfig()
|
||||||
logging.getLogger().setLevel(logging.INFO)
|
logging.getLogger().setLevel(logging.INFO)
|
||||||
@@ -76,12 +85,9 @@ def main():
|
|||||||
if args.render_phone_only:
|
if args.render_phone_only:
|
||||||
os.environ["CASE_RENDER_PHONE_ONLY"] = "1"
|
os.environ["CASE_RENDER_PHONE_ONLY"] = "1"
|
||||||
|
|
||||||
logger.info("computing model ...")
|
|
||||||
model_ = model()
|
|
||||||
|
|
||||||
if args.export_stl:
|
if args.export_stl:
|
||||||
logger.info("exporting stl to %s", args.export_stl)
|
logger.info("exporting stl to %s", args.export_stl)
|
||||||
cq.exporters.export(model_, args.export_stl)
|
cq.exporters.export(model(), args.export_stl)
|
||||||
|
|
||||||
if args.export_svg:
|
if args.export_svg:
|
||||||
view = None
|
view = None
|
||||||
@@ -92,14 +98,14 @@ def main():
|
|||||||
elif "right" in args.export_svg:
|
elif "right" in args.export_svg:
|
||||||
view = "right"
|
view = "right"
|
||||||
logger.info("exporting svg to %s (view: %s)", args.export_svg, str(view))
|
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))
|
cq.exporters.export(model(), args.export_svg, opt=svg_export_options(view))
|
||||||
|
|
||||||
if args.export_vtk:
|
if args.export_vtk:
|
||||||
vtk_file = args.export_vtk
|
vtk_file = args.export_vtk
|
||||||
js_var, _ext = os.path.splitext(os.path.basename(vtk_file))
|
js_var, _ext = os.path.splitext(os.path.basename(vtk_file))
|
||||||
js_file = f'{vtk_file}.js'
|
js_file = f'{vtk_file}.js'
|
||||||
logger.info("exporting VTK (for web rendering) to %s", vtk_file)
|
logger.info("exporting VTK (for web rendering) to %s", vtk_file)
|
||||||
cq.exporters.export(model_, vtk_file, cq.exporters.ExportTypes.VTP)
|
cq.exporters.export(model(), vtk_file, cq.exporters.ExportTypes.VTP)
|
||||||
|
|
||||||
logger.info("wrapping VTK data in a javascript variable (var %s) in %s", js_var, js_file)
|
logger.info("wrapping VTK data in a javascript variable (var %s) in %s", js_var, js_file)
|
||||||
vtk_data = open(vtk_file).read()
|
vtk_data = open(vtk_file).read()
|
||||||
@@ -116,4 +122,6 @@ def main():
|
|||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
else:
|
else:
|
||||||
|
# this `result` var should be picked up by cadquery, in case we were imported by it.
|
||||||
|
# note that we don't actually get here until the user presses the `>` render button.
|
||||||
result = model()
|
result = model()
|
||||||
|
Reference in New Issue
Block a user