Example #1
0
    def test_to_code_empty(self):
        sphere   = ScadObject("sphere"  , [0.6]       , None, None)
        cuboid   = ScadObject("cube"    , [[1, 2, 3]] , None, None)

        # Empty SCAD object
        empty_comment = ScadObject(None, None, None, None)
        self.assertEqual(empty_comment.to_code(), "\n".join([
            ";",
        ]))

        # Empty SCAD object with comment
        empty_comment = ScadObject(None, None, None, None).comment("Empty")
        self.assertEqual(empty_comment.to_code(), "\n".join([
            "// Empty",
            ";",
        ]))

        # Empty SCAD object with children
        empty_children = ScadObject(None, None, None, [sphere, cuboid])
        self.assertEqual(empty_children.to_code(), "\n".join([
            "{",
            "    sphere(0.6);",
            "    cube([1, 2, 3]);",
            "}",
        ]))

        # Empty SCAD object with children and comment
        empty_children_comment = ScadObject(None, None, None, [sphere, cuboid]).comment("Empty")
        self.assertEqual(empty_children_comment.to_code(), "\n".join([
            "// Empty",
            "{",
            "    sphere(0.6);",
            "    cube([1, 2, 3]);",
            "}",
        ]))
Example #2
0
    def test_to_code(self):
        sphere   = ScadObject("sphere"  , [0.6]       , None, None)
        cuboid   = ScadObject("cube"    , [[1, 2, 3]] , None, None)
        cylinder = ScadObject("cylinder", [4], [("r", 0.5)], None)

        union      = ScadObject("union"     , None, None, [sphere, cylinder])
        difference = ScadObject("difference", None, None, [union, cuboid])

        chain1 = ScadObject("union", None, None, [cuboid]);
        chain2 = ScadObject("intersection", None, None, [chain1]);

        # Simple object
        self.assertEqual(sphere  .to_code(), "sphere(0.6);"       ) # Basic
        self.assertEqual(cuboid    .to_code(), "cube([1, 2, 3]);"   ) # List parameter
        self.assertEqual(cylinder.to_code(), "cylinder(4, r = 0.5);") # Multiple parameters

        # Nested CSG
        self.assertEqual(difference.to_code(), "\n".join([
            "difference() {",
            "    union() {",
            "        sphere(0.6);",
            "        cylinder(4, r = 0.5);",
            "    }",
            "    cube([1, 2, 3]);",
            "}"
        ]))

        # Different indents
        self.assertEqual(union.to_code(""), "\n".join([
            "union() {",
            "sphere(0.6);",
            "cylinder(4, r = 0.5);",
            "}"
        ])) # No indent
        self.assertEqual(union.to_code("", "    "), "\n".join([
            "    union() {",
            "    sphere(0.6);",
            "    cylinder(4, r = 0.5);",
            "    }"
        ])) # Top indent only
        self.assertEqual(union.to_code("  ", "    "), "\n".join([
            "    union() {",
            "      sphere(0.6);",
            "      cylinder(4, r = 0.5);",
            "    }"
        ])) # Indent and top indent

        # Inline
        self.assertEqual(union.to_code(inline = True), "union() { sphere(0.6); cylinder(4, r = 0.5); }")

        # With comments
        sphere_with_comment = ScadObject("sphere", [1], None, None, "A sphere!")
        union_with_comment  = ScadObject("union", None, None, [sphere_with_comment], "A union!\nIt can contain multiple objects.")
        self.assertEqual(sphere_with_comment.to_code(" ", "  "), "\n".join([
            "  // A sphere!",
            "  sphere(1);",
        ]))
        self.assertEqual(union_with_comment.to_code(" ", "  "), "\n".join([
            "  // A union!",
            "  // It can contain multiple objects.",
            "  union() {",
            "   // A sphere!",
            "   sphere(1);",
            "  }"
        ]))
        self.assertEqual(sphere_with_comment.to_code(inline = True), "sphere(1);")

        # Simplify
        self.assertEqual(chain2.to_code(inline = True                 ), "intersection() { union() { cube([1, 2, 3]); } }")
        self.assertEqual(chain2.to_code(inline = True, simplify = True), "intersection() union() cube([1, 2, 3]);")