Example #1
0
def draw_faces(faces, **kwargs):
    """Draw faces as individual meshes and optionally set individual name, color, and layer properties.

    Parameters
    ----------
    faces : list[dict]
        A list of face dictionaries.
        See Notes, for more information about the structure of the dict.

    Returns
    -------
    list[System.Guid]

    Notes
    -----
    A face dict has the following schema:

    .. code-block:: python

        Schema({
            'points': And(len, lambda x: all(len(y) == 3 for y in x)),
            Optional('name', default=''): str,
            Optional('color', default=None): And(lambda x: len(x) == 3, all(0 <= y <= 255 for y in x)),
            Optional('vertexcolors', default=None): And(len, lambda x: all(0 <= y <= 255 for y in x)),
        })

    """
    guids = []
    for face in iter(faces):
        points = face['points'][:]
        name = face.get('name')
        color = face.get('color')
        vertexcolors = face.get('vertexcolors')

        v = len(points)

        if v < 3:
            continue
        elif v == 3:
            mfaces = [[0, 1, 2, 2]]
        elif v == 4:
            mfaces = [[0, 1, 2, 3]]
        else:
            mfaces = [list(range(v))]

        guid = draw_mesh(points, mfaces, color=color, name=name, clear=False, redraw=False, layer=None)

        if vertexcolors:
            try:
                compas_rhino.set_mesh_vertex_colors(guid, vertexcolors)
            except Exception:
                pass

        guids.append(guid)

    return guids
Example #2
0
def draw_mesh_as_faces(mesh,
                       layer=None,
                       clear_layer=False,
                       facecolor=None,
                       redraw=True):

    guids = compas_rhino.get_objects(
        name='{0}.*'.format(mesh.attributes['name']))
    compas_rhino.delete_objects(guids)

    if clear_layer:
        if not layer:
            compas_rhino.clear_current_layer()
        else:
            compas_rhino.clear_layer(layer)

    facecolor = facecolor or {}

    meshes = []

    for fkey in mesh.faces():
        vertices = mesh.face_coordinates(fkey)
        faces = [range(len(vertices))]
        color = facecolor.get(fkey, (255, 255, 255))
        guid = compas_rhino.xdraw_mesh(vertices,
                                       faces,
                                       None,
                                       '{0}.face.{1}'.format(
                                           mesh.attributes['name'], fkey),
                                       layer=layer,
                                       clear=False,
                                       redraw=False)
        compas_rhino.set_mesh_vertex_colors(
            guid, [color for i in range(len(vertices))])
        meshes.append(guid)

    if layer:
        previous = rs.CurrentLayer(layer)

    guid = rs.JoinMeshes(meshes, delete_input=True)

    if layer:
        rs.CurrentLayer(previous)

    rs.ObjectName(guid, '{0}'.format(mesh.attributes['name']))

    rs.EnableRedraw()
    rs.Redraw()
Example #3
0
def draw_faces(faces, **kwargs):
    guids = []
    for face in iter(faces):
        points = face['points'][:]
        name = face.get('name')
        color = face.get('color')
        vertexcolors = face.get('vertexcolors')

        v = len(points)

        if v < 3:
            continue
        if v == 3:
            mfaces = [[0, 1, 2, 2]]
        elif v == 4:
            mfaces = [[0, 1, 2, 3]]
        else:
            mfaces = _face_to_max_quad(points, range(v))
            if vertexcolors:
                r, g, b = [
                    sum(component) / v for component in zip(*vertexcolors)
                ]
                r = int(min(max(0, r), 255))
                g = int(min(max(0, g), 255))
                b = int(min(max(0, b), 255))
                vertexcolors.append((r, g, b))

        guid = draw_mesh(points,
                         mfaces,
                         color=color,
                         name=name,
                         clear=False,
                         redraw=False,
                         layer=None)

        if vertexcolors:
            try:
                compas_rhino.set_mesh_vertex_colors(guid, vertexcolors)
            except Exception:
                pass

        guids.append(guid)

    return guids
Example #4
0
    def color_interfaces(self, mode=0):
        """Color the interfaces with shades of blue and red according to the forces at the corners.

        Parameters
        ----------
        mode : int, optional
            Mode to switch between normal forces(0) and frictions(1)
            Default is 0.

        Notes
        -----
        * Currently only normal forces are taken into account.
        * Gradients should go from blue to red over white.
        * White marks the neutral line (the axis of rotational equilibrium).
        * ...

        Examples
        --------
        .. code-block:: python

            pass

        """
        if mode == 0:
            # redraw the faces with a discretisation that makes sense for the neutral axis
            # color the drawn meshes
            for u, v, attr in self.assembly.edges(True):
                if not attr['interface_forces']:
                    continue

                name = "{}.interface.{}-{}".format(self.assembly.name, u, v)
                guids = compas_rhino.get_objects(name=name)
                if not guids:
                    continue

                guid = guids[0]

                forces = [
                    f['c_np'] - f['c_nn'] for f in attr['interface_forces']
                ]

                fmin = min(forces)
                fmax = max(forces)
                fmax = max(abs(fmin), fmax)

                colors = []

                p = len(attr['interface_points'])

                for i in range(p):
                    f = forces[i]

                    if f > 0.0:
                        color = i_to_blue(f / fmax)
                    elif f < 0.0:
                        color = i_to_red(-f / fmax)
                    else:
                        color = (255, 255, 255)

                    colors.append(color)

                # this is obviously not correct
                # just a visual reminder
                if p > 4:
                    colors.append((255, 255, 255))

                compas_rhino.set_mesh_vertex_colors(guid, colors)

        elif mode == 1:
            for u, v, attr in self.assembly.edges(True):
                if attr['interface_forces'] is None:
                    continue

                name = "{}.interface.{}-{}".format(self.assembly.name, u, v)
                guids = compas_rhino.get_objects(name=name)
                if not guids:
                    continue

                guid = guids[0]

                iu, iv = attr['interface_uvw'][0], attr['interface_uvw'][1]

                forces = [
                    length_vector(
                        add_vectors(scale_vector(iu, f['c_u']),
                                    scale_vector(iv, f['c_v'])))
                    for f in attr['interface_forces']
                ]

                fmin = min(forces)
                fmax = max(forces)
                fmax = max(abs(fmin), fmax)
                fmax = max(fmax, self.defaults['range.friction'])

                colors = []

                p = len(attr['interface_points'])

                for i in range(p):
                    f = forces[i]

                    if f > 0.0:
                        color = i_to_red(f / fmax)
                    else:
                        color = (255, 255, 255)

                    colors.append(color)

                # this is obviously not correct
                # just a visual reminder
                if p > 4:
                    colors.append((255, 255, 255))

                compas_rhino.set_mesh_vertex_colors(guid, colors)

        else:
            print("please choose mode 0 or 1")
Example #5
0
def draw_faces(faces, **kwargs):
    """Draw faces as individual meshes and optionally set individual name, color, and layer properties.

    Parameters
    ----------
    faces : list of dict
        A list of face dictionaries.

    Returns
    -------
    list of GUID

    Notes
    -----
    A face dict has the following schema:

    .. code-block:: python

        Schema({
            'points': And(len, lambda x: all(len(y) == 3 for y in x)),
            Optional('name', default=''): str,
            Optional('color', default=None): And(lambda x: len(x) == 3, all(0 <= y <= 255 for y in x)),
            Optional('vertexcolors', default=None): And(len, lambda x: all(0 <= y <= 255 for y in x)),
        })

    """
    guids = []
    for face in iter(faces):
        points = face['points'][:]
        name = face.get('name')
        color = face.get('color')
        vertexcolors = face.get('vertexcolors')

        v = len(points)

        if v < 3:
            continue
        if v == 3:
            mfaces = [[0, 1, 2, 2]]
        elif v == 4:
            mfaces = [[0, 1, 2, 3]]
        else:
            mfaces = _face_to_max_quad(points, range(v))
            if vertexcolors:
                r, g, b = [
                    sum(component) / v for component in zip(*vertexcolors)
                ]
                r = int(min(max(0, r), 255))
                g = int(min(max(0, g), 255))
                b = int(min(max(0, b), 255))
                vertexcolors.append((r, g, b))

        guid = draw_mesh(points,
                         mfaces,
                         color=color,
                         name=name,
                         clear=False,
                         redraw=False,
                         layer=None)

        if vertexcolors:
            try:
                compas_rhino.set_mesh_vertex_colors(guid, vertexcolors)
            except Exception:
                pass

        guids.append(guid)

    return guids