diff --git a/cadquery/cqgi.py b/cadquery/cqgi.py index 92fd860..cc0d087 100644 --- a/cadquery/cqgi.py +++ b/cadquery/cqgi.py @@ -95,10 +95,13 @@ class CQModel(object): self.set_param_values(build_parameters) collector = ScriptCallback() env = EnvironmentBuilder().with_real_builtins().with_cadquery_objects() \ - .add_entry("build_object", collector.build_object).build() + .add_entry("build_object", collector.build_object) \ + .add_entry("debug", collector.debug) \ + .build() c = compile(self.ast_tree, CQSCRIPT, 'exec') exec (c, env) + result.set_debug(collector.debugObjects ) if collector.has_results(): result.set_success_result(collector.outputObjects) else: @@ -139,6 +142,7 @@ class BuildResult(object): def __init__(self): self.buildTime = None self.results = [] + self.debugObjects = [] self.first_result = None self.success = False self.exception = None @@ -147,6 +151,9 @@ class BuildResult(object): self.exception = ex self.success = False + def set_debug(self, debugObjects): + self.debugObjects = debugObjects + def set_success_result(self, results): self.results = results self.first_result = self.results[0] @@ -270,9 +277,9 @@ class ScriptCallback(object): the build_object() method is exposed to CQ scripts, to allow them to return objects to the execution environment """ - def __init__(self): self.outputObjects = [] + self.debugObjects = [] def build_object(self, shape): """ @@ -281,6 +288,12 @@ class ScriptCallback(object): """ self.outputObjects.append(shape) + def debug(self,obj,args={}): + """ + Debug print/output an object, with optional arguments. + """ + self.debugObjects.append(DebugObject(obj,args)) + def describe_parameter(self,var, valid_values, short_desc): """ Not yet implemented: allows a script to document @@ -297,7 +310,15 @@ class ScriptCallback(object): def has_results(self): return len(self.outputObjects) > 0 - +class DebugObject(object): + """ + Represents a request to debug an object + Object is the type of object we want to debug + args are parameters for use during debuging ( for example, color, tranparency ) + """ + def __init__(self,object,args): + self.args = args + self.object = object class InvalidParameterError(Exception): """ diff --git a/tests/TestCQGI.py b/tests/TestCQGI.py index 35d8906..ae8c49d 100644 --- a/tests/TestCQGI.py +++ b/tests/TestCQGI.py @@ -23,6 +23,18 @@ TESTSCRIPT = textwrap.dedent( """ ) +TEST_DEBUG_SCRIPT = textwrap.dedent( + """ + height=2.0 + width=3.0 + (a,b) = (1.0,1.0) + foo="bar" + debug(foo, { "color": 'yellow' } ) + result = "%s|%s|%s|%s" % ( str(height) , str(width) , foo , str(a) ) + build_object(result) + debug(height ) + """ +) class TestCQGI(BaseTest): def test_parser(self): @@ -31,6 +43,16 @@ class TestCQGI(BaseTest): self.assertEquals(set(metadata.parameters.keys()), {'height', 'width', 'a', 'b', 'foo'}) + def test_build_with_debug(self): + model = cqgi.CQModel(TEST_DEBUG_SCRIPT) + result = model.build() + debugItems = result.debugObjects + self.assertTrue(len(debugItems) == 2) + self.assertTrue( debugItems[0].object == "bar" ) + self.assertTrue( debugItems[0].args == { "color":'yellow' } ) + self.assertTrue( debugItems[1].object == 2.0 ) + self.assertTrue( debugItems[1].args == {} ) + def test_build_with_empty_params(self): model = cqgi.CQModel(TESTSCRIPT) result = model.build()