Example #1
0
def short_rotate(f, t):
    f, t = vector3(evalcache.unlazy_if_need(f)), vector3(
        evalcache.unlazy_if_need(t))

    f = f / np.linalg.norm(f)
    t = t / np.linalg.norm(t)

    if np.linalg.norm(f - t) < 1e-5:
        return nulltrans()

    axis = np.cross(f, t)
    dot_product = np.dot(f, t)
    angle = np.arccos(dot_product)

    return rotate(axis, angle)
Example #2
0
    def __new__(cls, *args, info=None):
        args = [evalcache.unlazy_if_need(a) for a in args]

        if len(args) == 0:
            input_array = (0, 0, 0)

        elif isinstance(args[0], TopoDS_Vertex):
            pnt = BRep_Tool.Pnt(args[0])
            input_array = (pnt.X(), pnt.Y(), pnt.Z())

        elif isinstance(args[0], (gp_Pnt, gp_Dir, gp_Vec, gp_XYZ)):
            input_array = (args[0].X(), args[0].Y(), args[0].Z())
        else:
            try:
                _ = args[0][0]
                input_array = args[0]
            except:
                input_array = args

        if len(input_array) == 1:
            input_array = ((input_array[0], 0, 0))
        elif len(input_array) == 2:
            input_array = ((input_array[0], input_array[1], 0))
        elif len(input_array) == 3:
            input_array = ((input_array[0], input_array[1], input_array[2]))

        obj = numpy.asarray(input_array).view(cls)
        obj.info = info
        return obj
Example #3
0
def lexsort(a):
    a = evalcache.unlazy_if_need(a)

    def comparator(a, b):
        return 1 if a > b else -1

    return sorted(a, key=cmp_to_key(comparator))
Example #4
0
def vector3(*args):
    args = [evalcache.unlazy_if_need(a) for a in args]
    if len(args) == 1:
        if args[0] is None:
            return vector3(0, 0, 0)
        return vector3(*args[0])
    return pyservoce.vector3(*args)
Example #5
0
def color(*arg):
    args = [evalcache.unlazy_if_need(a) for a in arg]

    if isinstance(args[0], pyservoce.color):
        return args[0]

    return pyservoce.color(*args)
Example #6
0
 def add_shape(self, shp, color=pyservoce.default_color):
     shp = evalcache.unlazy_if_need(shp)
     controller = pyservoce.interactive_object(shp)
     controller.set_color(pyservoce.color(color))
     self.dispobjects.append(controller)
     self.shapes_holder.append(shp)
     return controller
Example #7
0
    def __init__(self,
                 parts=[],
                 parent=None,
                 shape=None,
                 name=None,
                 location=pyservoce.libservoce.nulltrans()):
        self.parent = parent
        self.location = evalcache.unlazy_if_need(location)
        self.global_location = self.location
        self.name = name
        self.color = None
        self.dispobjects = []
        self.scene = None

        self.shapes_holder = []

        self.views = set()
        self.childs = set()

        if parent is not None:
            parent.add_child(self)

        if shape is not None:
            print(
                "zencad.assemble.unit: `shape` option is deprecated. use `parts` option instead"
            )
            self.shape = shape
            self.add_shape(self.shape)

        for obj in parts:
            self.add(obj)
Example #8
0
def _sew_wire(lst, sort=True):
    lst = evalcache.unlazy_if_need(lst)
    lst = _wires_to_edges(lst)

    if sort:
        lst = _sort_wires(lst)

    return pyservoce.make_wire(lst)
Example #9
0
def _sew_wire(lst, sort=True):
    from zencad.geom.wire import _make_wire
    lst = evalcache.unlazy_if_need(lst)
    lst = _wires_to_edges(lst)

    if sort:
        lst = _sort_wires(lst)

    return _make_wire(lst)
Example #10
0
File: svg.py Project: mirmik/zencad
def shape_to_svg(fpath, shape, color, mapping):
    shape = evalcache.unlazy_if_need(shape)
    color = color_convert(color)
    size, off = box_size(shape, mapping)
    writer = SvgWriter(fpath=fpath, off=off, size=size)

    writer.begin()
    writer.push_shape(shape, color=color)

    writer.save()
Example #11
0
File: svg.py Project: mirmik/zencad
def shape_to_svg_string(shape, color, mapping):
    shape = evalcache.unlazy_if_need(shape)
    color = color_convert(color)
    size, off = box_size(shape, mapping)
    writer = SvgWriter(size=size, off=off)

    writer.begin()
    writer.push_shape(shape, color=color)

    return writer.dwg.tostring()
Example #12
0
def lexsort(a):
    a = evalcache.unlazy_if_need(a)

    def comparator(a, b):
        return 1 if a > b else -1

    a = [
        zencad.util.point3(round(f.x, 4), round(f.y, 4), round(f.z, 4))
        for f in a
    ]
    return sorted(a, key=cmp_to_key(comparator))
Example #13
0
    def add(self, obj, color=default_color()):
        from zencad.interactive.displayable import Displayable

        obj = evalcache.unlazy_if_need(obj)

        if isinstance(obj, Displayable):
            obj.bind_to_scene(self)
            iobj = obj
        else:
            iobj = create_interactive_object(obj, color)
            self.add(iobj)

        return iobj
Example #14
0
File: svg.py Project: mirmik/zencad
def box_size(shape, mapping):
    shape = evalcache.unlazy_if_need(shape)
    box = shape.bbox()

    if mapping:
        off = (box.xmin, -box.ymax)
    else:
        off = (0, 0)

    return (
        str(box.xmax - box.xmin),
        str(box.ymax - box.ymin),
    ), off
Example #15
0
def _sort_wires(lst):
    lst = evalcache.unlazy_if_need(lst)
    size = len(lst)

    res = [lst[0]]
    strt = lst[0].endpoints()[0]
    fini = lst[0].endpoints()[1]
    del lst[0]

    stubiter = 0
    while len(res) != size:
        for i, l in enumerate(lst):
            l_strt = l.endpoints()[0]
            l_fini = l.endpoints()[1]

            # TODO: Fix point3 equality in servoce library and change equalities to early methods.
            if numpy.linalg.norm(strt - l_strt) < 1e-5:
                strt = l_fini
                del lst[i]
                res.insert(0, l)
                break

            elif numpy.linalg.norm(strt - l_fini) < 1e-5:
                strt = l_strt
                del lst[i]
                res.insert(0, l)
                break

            elif numpy.linalg.norm(fini - l_strt) < 1e-5:
                fini = l_fini
                del lst[i]
                res.append(l)
                break

            elif numpy.linalg.norm(fini - l_fini) < 1e-5:
                fini = l_strt
                del lst[i]
                res.append(l)
                break

        else:
            stubiter += 1

        if stubiter >= 3:
            raise Exception("sew:sorting: Failed to wires sorting")

    return res
Example #16
0
    def bind_scene(self, scene, color=pyservoce.default_color, deep=False):
        for d in self.dispobjects:
            scene.viewer.display(d)
            self.views.add(ShapeView(d))

        if self.shape is not None:
            if self.color is not None:
                color = self.color

            shape_view = ShapeView(
                scene.add(evalcache.unlazy_if_need(self.shape), color))
            scene.viewer.display(shape_view.sctrl)
            self.views.add(shape_view)

        self.apply_view_location(deep=False)

        if deep:
            for c in self.childs:
                c.bind_scene(scene, color=color, deep=True)
Example #17
0
    def __init__(self,
                 parent=None,
                 shape=None,
                 name=None,
                 location=pyservoce.libservoce.nulltrans()):
        self.parent = parent
        self.shape = shape
        self.location = evalcache.unlazy_if_need(location)
        self.global_location = self.location
        self.name = name
        self.color = None
        self.dispobjects = []
        self.shapes_holder = []

        self.views = set()
        self.childs = set()

        if parent is not None:
            parent.add_child(self)
Example #18
0
File: svg.py Project: mirmik/zencad
    def read_path(self, el):
        d = el["d"]

        fill_opacity = None
        fill = None

        if "fill" in el:
            fill = el["fill"]
        if "fill_opacity" in el:
            #fill_opacity = el["fill_opacity"]
            pass

        tokens = d.split()
        self.wb = None
        self.wires = []
        self.iter = iter(tokens)

        while 1:
            try:
                cmd = next(self.iter)
            except Exception:
                self.read_path_final_wb()
                break

            if cmd == "M":
                self.read_path_M()
            elif cmd == "A":
                self.read_path_A()
            elif cmd == "L":
                self.read_path_L()
            elif cmd == "Z":
                self.read_path_Z()
            else:
                raise Exception("svgreader:path:undefined_command", cmd)

        self.wires = evalcache.unlazy_if_need(self.wires)
        if fill is not None:
            return _fill(self.wires)

        else:
            return _union(self.wires)
Example #19
0
 def bar(*args, **kwargs):
     args = [evalcache.unlazy_if_need(a) for a in args]
     kwargs = {k: evalcache.unlazy_if_need(v) for k, v in kwargs.items()}
     return foo(*args, **kwargs)
Example #20
0
    def relocate(self, location, deep=False, view=True):
        self.location = evalcache.unlazy_if_need(location)
        self.location_update(deep=deep, view=False)

        if view:
            self._apply_view_location(deep=deep)
Example #21
0
    def __init__(self, obj, color=None):
        if color == None:
            color = zencad.default_color

        super().__init__(evalcache.unlazy_if_need(obj), pyservoce.color(color))
        self.object = obj