|
2 | 2 | Classes for OpenSCAD builtins
|
3 | 3 | """
|
4 | 4 | from .solidpython import OpenSCADObject
|
| 5 | +from .solidpython import IncludedOpenSCADObject |
5 | 6 |
|
6 | 7 | class polygon(OpenSCADObject):
|
7 | 8 | '''
|
@@ -533,3 +534,73 @@ def __init__(self, n):
|
533 | 534 | class assign(OpenSCADObject):
|
534 | 535 | def __init__(self):
|
535 | 536 | OpenSCADObject.__init__(self, 'assign', {})
|
| 537 | + |
| 538 | +# ================================ |
| 539 | +# = Modifier Convenience Methods = |
| 540 | +# ================================ |
| 541 | +def debug(openscad_obj): |
| 542 | + openscad_obj.set_modifier("#") |
| 543 | + return openscad_obj |
| 544 | + |
| 545 | + |
| 546 | +def background(openscad_obj): |
| 547 | + openscad_obj.set_modifier("%") |
| 548 | + return openscad_obj |
| 549 | + |
| 550 | + |
| 551 | +def root(openscad_obj): |
| 552 | + openscad_obj.set_modifier("!") |
| 553 | + return openscad_obj |
| 554 | + |
| 555 | + |
| 556 | +def disable(openscad_obj): |
| 557 | + openscad_obj.set_modifier("*") |
| 558 | + return openscad_obj |
| 559 | + |
| 560 | + |
| 561 | +# =============== |
| 562 | +# = Including OpenSCAD code = |
| 563 | +# =============== |
| 564 | + |
| 565 | +# use() & include() mimic OpenSCAD's use/include mechanics. |
| 566 | +# -- use() makes methods in scad_file_path.scad available to |
| 567 | +# be called. |
| 568 | +# --include() makes those methods available AND executes all code in |
| 569 | +# scad_file_path.scad, which may have side effects. |
| 570 | +# Unless you have a specific need, call use(). |
| 571 | +def use(scad_file_path, use_not_include=True): |
| 572 | + ''' |
| 573 | + Opens scad_file_path, parses it for all usable calls, |
| 574 | + and adds them to caller's namespace. |
| 575 | + ''' |
| 576 | + # These functions in solidpython are used here and only here; don't pollute |
| 577 | + # the global namespace with them |
| 578 | + from .solidpython import extract_callable_signatures |
| 579 | + from .solidpython import new_openscad_class_str |
| 580 | + from .solidpython import calling_module |
| 581 | + |
| 582 | + try: |
| 583 | + with open(scad_file_path) as module: |
| 584 | + contents = module.read() |
| 585 | + except Exception as e: |
| 586 | + raise Exception("Failed to import SCAD module '%(scad_file_path)s' " |
| 587 | + "with error: %(e)s " % vars()) |
| 588 | + |
| 589 | + # Once we have a list of all callables and arguments, dynamically |
| 590 | + # add OpenSCADObject subclasses for all callables to the calling module's |
| 591 | + # namespace. |
| 592 | + symbols_dicts = extract_callable_signatures(scad_file_path) |
| 593 | + |
| 594 | + for sd in symbols_dicts: |
| 595 | + class_str = new_openscad_class_str(sd['name'], sd['args'], sd['kwargs'], |
| 596 | + scad_file_path, use_not_include) |
| 597 | + # If this is called from 'include', we have to look deeper in the stack |
| 598 | + # to find the right module to add the new class to. |
| 599 | + stack_depth = 2 if use_not_include else 3 |
| 600 | + exec(class_str, calling_module(stack_depth).__dict__) |
| 601 | + |
| 602 | + return True |
| 603 | + |
| 604 | + |
| 605 | +def include(scad_file_path): |
| 606 | + return use(scad_file_path, use_not_include=False) |
0 commit comments