taxpon / openpyscad

Python library to generate OpenSCAD source code. This library provides intuitive interface when you handle 3D data.
https://pypi.org/project/openpyscad/
MIT License
120 stars 21 forks source link

linear_extrude does not work for Difference, Union & Intersection #16

Open aweltsch opened 5 years ago

aweltsch commented 5 years ago

Hi thank you for creating this library.

I discovered a bug in the logic related to linear extrusion. Trying to execute the following code results in an exception. I am using version 0.3.2

from openpyscad import Square
x = Square([2, 2])
y = Square([1, 1])
diff = x - y
diff.linear_extrude(height=1)

This results in an exception (which seems to be related to #5)

Traceback (most recent call last):
  File "openpyscad-test.py", line 5, in <module>
    diff.linear_extrude(height=1)
  File "/mnt/extspace/andre/devel/git/keyboard_designs/venv/lib/python3.7/site-packages/openpyscad/base.py", line 308, in linear_extrude
    return Linear_Extrude(*args, **kwargs).append(self)
  File "/mnt/extspace/andre/devel/git/keyboard_designs/venv/lib/python3.7/site-packages/openpyscad/base.py", line 184, in append
    self._validate_append(obj)
  File "/mnt/extspace/andre/devel/git/keyboard_designs/venv/lib/python3.7/site-packages/openpyscad/transformations.py", line 59, in _validate_append
    raise TypeError('Appended object must be a instance of Shape2dObject or Transformation.')
TypeError: Appended object must be a instance of Shape2dObject or Transformation.

I would expect that the library can generate openscad output like this:

linear_extrude(height=1) {
    difference() {
        square([2, 2]);
        square([1, 1]);
    };
};
taxpon commented 5 years ago

@aweltsch Thank you for your feedback. I'll check the bug.

NuclearManD commented 3 years ago

Should this also work for rotate_extrude? I'm new to OpenSCAD, only ever played with linear_extrude. I forked this repo and it looks like the fix would be very simple, going to create a PR. I am guessing that rotate_extrude has the same problem as linear_extrude.


class Linear_Extrude(_Transformation):
    def _validate_append(self, obj):
        from .shapes_2d import Shape2dObject
        if not isinstance(obj, (Shape2dObject, Transformation)):
            raise TypeError('Appended object must be a instance of Shape2dObject or Transformation.')

class Rotate_Extrude(_Transformation):
    def _validate_append(self, obj):
        from .shapes_2d import Shape2dObject
        if not isinstance(obj, (Shape2dObject, Transformation)):
            raise TypeError('Appended object must be a instance of Shape2dObject.')

As you can see, there is a check that is run to make sure it's the right instance, but this happens for rotate_extrude too.