Пример #1
0
def draw_polylines(polylines):
    """Draw polylines.

    Parameters
    ----------
    polylines : list of dict
        The polyline definitions.

    Returns
    -------
    list of :class:`Rhino.Geometry.Polyline`

    Notes
    -----
    .. code-block:: python

        Schema({
            'points': lambda x: all(len(y) == 3 for y in x)
        })

    """
    rg_polylines = []
    for p in iter(polylines):
        points = p['points']
        poly = Polyline([Point3d(*xyz) for xyz in points])
        poly.DeleteShortSegments(TOL)
        rg_polylines.append(poly)
    return rg_polylines
Пример #2
0
    def geometry_at_position_contour(self, position, as_crv=False):
        """
        Gets the contour polyline at a given position by making a polyline
        from all nodes which share the specified 'position' attribute.

        Parameters
        ----------
        position : hashable
            The index / identifier of the position

        as_crv : bool, optional
            If ``True``, will return a PolylineCurve instead of a Polyline.

            Defaults to ``False``.

        Returns
        -------
        contour : :obj:`Rhino.Geometry.Polyline`
            The contour as a Polyline if ``as_crv`` is ``False``.
        contour : :obj:`Rhino.Geometry.PolylineCurve`
            The contour as a PolylineCurve if ``as_crv`` is ``True``.
        """

        points = [n[1]["geo"] for n in self.nodes_on_position(position, True)]
        Contour = RhinoPolyline(points)
        if as_crv:
            Contour = Contour.ToPolylineCurve()
        return Contour
Пример #3
0
def xdraw_polylines(polylines):
    """Draw polylines.
    """
    rg_polylines = []
    for p in iter(polylines):
        points = p['points']
        poly = Polyline([Point3d(*xyz) for xyz in points])
        poly.DeleteShortSegments(TOL)
        rg_polylines.append(poly)
    return rg_polylines
Пример #4
0
def xdraw_polylines(polylines, **kwargs):
    """Draw polylines, and optionally set individual name, color, arrow, and
    layer properties.
    """
    guids = []
    for p in iter(polylines):
        points = p['points']
        name = p.get('name', '')
        color = p.get('color')
        arrow = p.get('arrow')
        layer = p.get('layer')
        poly = Polyline([Point3d(*xyz) for xyz in points])
        poly.DeleteShortSegments(TOL)
        guid = add_polyline(poly)
        if not guid:
            continue
        obj = find_object(guid)
        if not obj:
            continue
        attr = obj.Attributes
        if color:
            attr.ObjectColor = FromArgb(*color)
            attr.ColorSource = ColorFromObject
        else:
            attr.ColorSource = ColorFromLayer
        if arrow == 'end':
            attr.ObjectDecoration = EndArrowhead
        if arrow == 'start':
            attr.ObjectDecoration = StartArrowhead
        if layer and find_layer_by_fullpath:
            index = find_layer_by_fullpath(layer, True)
            if index >= 0:
                attr.LayerIndex = index
        attr.Name = name
        obj.CommitChanges()
        guids.append(guid)
    return guids
Пример #5
0
def draw_polylines(polylines, **kwargs):
    """Draw polylines, and optionally set individual name, color, arrow, and
    layer properties.

    Parameters
    ----------
    labels : list of dict
        A list of polyline dictionaries.

    Returns
    -------
    list of GUID

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

    .. code-block:: python

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

    """
    guids = []
    for p in iter(polylines):
        points = p['points']
        name = p.get('name', '')
        color = p.get('color')
        arrow = p.get('arrow')
        layer = p.get('layer')
        poly = Polyline([Point3d(*xyz) for xyz in points])
        poly.DeleteShortSegments(TOL)
        guid = add_polyline(poly)
        if not guid:
            continue
        obj = find_object(guid)
        if not obj:
            continue
        attr = obj.Attributes
        if color:
            attr.ObjectColor = FromArgb(*color)
            attr.ColorSource = ColorFromObject
        else:
            attr.ColorSource = ColorFromLayer
        if arrow == 'end':
            attr.ObjectDecoration = EndArrowhead
        if arrow == 'start':
            attr.ObjectDecoration = StartArrowhead
        if layer and find_layer_by_fullpath:
            index = find_layer_by_fullpath(layer, True)
            if index >= 0:
                attr.LayerIndex = index
        attr.Name = name
        obj.CommitChanges()
        guids.append(guid)
    return guids
Пример #6
0
def break_polyline(polyline, break_angle, as_crv=False):
    """
    Breaks a polyline at kinks based on a specified angle. Will move the seam
    of closed polylines to the first kink discovered.

    Parameters
    ----------
    polyline : :obj:`Rhino.Geometry.Polyline`
        Polyline to break apart at angles.

    break_angle : float
        The angle at which to break apart the polyline (in radians).

    as_crv : bool, optional
        If ``True``, will return a :obj:`Rhino.Geometry.PolylineCurve` object.

        Defaults to ``False``.

    Returns
    -------
    polyline_segments : list of :obj:`Rhino.Geometry.Polyline`
        A list of the broken segments as Polylines if ``as_crv`` is
        ``False``.
    polyline_segments: list of :obj:`Rhino.Geometry.PolylineCurve`
        A list of the broken segments as PolylineCurves if ``as_crv`` is
        ``True``.

    """

    # get all the polyline segments
    segments = deque(polyline.GetSegments())

    # check if polyline in closed
    if polyline.IsClosed:
        closedSeamAtKink = False
    else:
        closedSeamAtKink = True

    # initialize containers
    plcs = []
    pl = RhinoPolyline()

    # process all segments
    while len(segments) > 0:
        # if there is only one segment left, add the endpoint to the new pl
        if len(segments) == 1:
            ln = segments.popleft()
            pl.Add(ln.To)
            plcs.append(pl)
            break

        # get unitized directions of this and next segment
        thisdir = segments[0].Direction
        nextdir = segments[1].Direction
        thisdir.Unitize()
        nextdir.Unitize()

        # compute angle
        vdp = thisdir * nextdir
        angle = cos(vdp / (thisdir.Length * nextdir.Length))
        angle = RhinoVector3d.VectorAngle(thisdir, nextdir)

        # check angles and execute breaks
        if angle >= break_angle:
            if not closedSeamAtKink:
                segments.rotate(-1)
                pl.Add(segments.popleft().From)
                closedSeamAtKink = True
            elif closedSeamAtKink:
                ln = segments.popleft()
                pl.Add(ln.From)
                pl.Add(ln.To)
                plcs.append(pl)
                pl = RhinoPolyline()
        else:
            if not closedSeamAtKink:
                segments.rotate(-1)
            else:
                pl.Add(segments.popleft().From)

    if as_crv:
        return [pline.ToPolylineCurve() for pline in plcs]
    else:
        return plcs