Пример #1
0
def filletWire(aWire, r, chamfer=False):
    """Fillet each angle of a wire with r as radius.

    If chamfer is true, a `chamfer` is made instead, and `r` is the
    size of the chamfer.
    """
    edges = aWire.Edges
    edges = Part.__sortEdges__(edges)
    filEdges = [edges[0]]

    for i in range(len(edges) - 1):
        result = fillet([filEdges[-1], edges[i + 1]], r, chamfer)
        if len(result) > 2:
            filEdges[-1:] = result[0:3]
        else:
            filEdges[-1:] = result[0:2]

    if isReallyClosed(aWire):
        result = fillet([filEdges[-1], filEdges[0]], r, chamfer)
        if len(result) > 2:
            filEdges[-1:] = result[0:2]
            filEdges[0] = result[2]

    return Part.Wire(filEdges)
Пример #2
0
def offsetWire(wire, dvec, bind=False, occ=False,
               widthList=None, offsetMode=None, alignList=[],
               normal=None, basewireOffset=0):
    """Offset the wire along the given vector.

    Parameters
    ----------
    wire as a list of edges (use the list directly),
    or previously as a wire or a face (Draft Wire with MakeFace True
    or False supported).

    The vector will be applied at the first vertex of the wire. If bind
    is True (and the shape is open), the original wire and the offsetted one
    are bound by 2 edges, forming a face.

    If widthList is provided (values only, not lengths - i.e. no unit),
    each value will be used to offset each corresponding edge in the wire.

    The 1st value overrides 'dvec' for 1st segment of wire;
    if a value is zero, value of 'widthList[0]' will follow;
    if widthList[0]' == 0, but dvec still provided, dvec will be followed

    offsetMode="BasewireMode" or None

    If alignList is provided,
    each value will be used to offset each corresponding edge
    in the wire with corresponding index.

    'basewireOffset' corresponds to 'offset' in ArchWall which offset
    the basewire before creating the wall outline

    OffsetWire() is now aware of width and align per edge
    Primarily for use with ArchWall based on Sketch object

    To Do
    -----
    `dvec` vector to offset is now derived (and can be ignored)
    in this function if widthList and alignList are provided
    - 'dvec' to be obsolete in future?
    """
    if isinstance(wire, Part.Wire) or isinstance(wire, Part.Face):
        # Seems has repeatedly sortEdges, remark out here
        # edges = Part.__sortEdges__(wire.Edges)
        edges = wire.Edges
    elif isinstance(wire, list):
        if isinstance(wire[0], Part.Edge):
            edges = wire.copy()
            # How to avoid __sortEdges__ again?
            # Make getNormal directly tackle edges?
            wire = Part.Wire(Part.__sortEdges__(edges))
    else:
        print("Either Part.Wire or Part.Edges should be provided, "
              "returning None")
        return None

    # For sketch with a number of wires, getNormal() may result
    # in different direction for each wire.
    # The 'normal' parameter, if provided e.g. by ArchWall,
    # allows normal over different wires e.g. in a Sketch be consistent
    # (over different calls of this function)
    if normal:
        norm = normal
    else:
        norm = getNormal(wire)  # norm = Vector(0, 0, 1)

    closed = isReallyClosed(wire)
    nedges = []
    if occ:
        length = abs(dvec.Length)
        if not length:
            return None

        if wire.Wires:
            wire = wire.Wires[0]
        else:
            wire = Part.Wire(edges)

        try:
            off = wire.makeOffset(length)
        except Part.OCCError:
            return None
        else:
            return off

    # vec of first edge depends on its geometry
    e = edges[0]

    # Make a copy of alignList - to avoid changes in this function
    # become starting input of next call of this function?
    # https://www.dataquest.io/blog/tutorial-functions-modify-lists-dictionaries-python/
    # alignListC = alignList.copy()  # Only Python 3
    alignListC = list(alignList)  # Python 2 and 3

    # Check the direction / offset of starting edge
    firstDir = None
    try:
        if alignListC[0] == 'Left':
            firstDir = 1
            firstAlign = 'Left'
        elif alignListC[0] == 'Right':
            firstDir = -1
            firstAlign = 'Right'
        elif alignListC[0] == 'Center':
            firstDir = 1
            firstAlign = 'Center'
    except IndexError:
        # Should no longer happen for ArchWall
        # as aligns are 'filled in' by ArchWall
        pass

    # If not provided by alignListC checked above, check the direction
    # of offset in dvec (not 'align').

    # TODO Should check if dvec is provided or not
    # ('legacy/backward-compatible' mode)
    if not firstDir:
        # need to test against Part.Circle, not Part.ArcOfCircle
        if isinstance(e.Curve, Part.Circle):
            v0 = e.Vertexes[0].Point.sub(e.Curve.Center)
        else:
            v0 = vec(e).cross(norm)
        # check against dvec provided for the offset direction
        # would not know if dvec is vector of width (Left/Right Align)
        # or width/2 (Center Align)
        dvec0 = DraftVecUtils.scaleTo(v0, dvec.Length)
        if DraftVecUtils.equals(dvec0, dvec):
            # "Left Offset" (Left Align or 'left offset' in Centre Align)
            firstDir = 1
            firstAlign = 'Left'
            alignListC.append('Left')
        elif DraftVecUtils.equals(dvec0, dvec.negative()):
            # "Right Offset" (Right Align or 'right offset' in Centre Align)
            firstDir = -1
            firstAlign = 'Right'
            alignListC.append('Right')
        else:
            print(" something wrong with firstDir ")
            firstAlign = 'Left'
            alignListC.append('Left')

    for i in range(len(edges)):
        # make a copy so it do not reverse the self.baseWires edges
        # pointed to by _Wall.getExtrusionData()?
        curredge = edges[i].copy()

        # record first edge's Orientation, Dir, Align and set Delta
        if i == 0:
            # TODO Could be edge.Orientation in fact
            # "Forward" or "Reversed"
            firstOrientation = curredge.Vertexes[0].Orientation
            curOrientation = firstOrientation
            curDir = firstDir
            curAlign = firstAlign
            delta = dvec

        # record current edge's Orientation, and set Delta
        if i != 0:  # else:
            # TODO Should also calculate 1st edge direction above
            if isinstance(curredge.Curve, Part.Circle):
                delta = curredge.Vertexes[0].Point.sub(curredge.Curve.Center)
            else:
                delta = vec(curredge).cross(norm)
            # TODO Could be edge.Orientation in fact
            curOrientation = curredge.Vertexes[0].Orientation

        # Consider individual edge width
        if widthList:  # ArchWall should now always provide widthList
            try:
                if widthList[i] > 0:
                    delta = DraftVecUtils.scaleTo(delta, widthList[i])
                elif dvec:
                    delta = DraftVecUtils.scaleTo(delta, dvec.Length)
                else:
                    # just hardcoded default value as ArchWall would provide
                    # if dvec is not provided either
                    delta = DraftVecUtils.scaleTo(delta, 200)
            except Part.OCCError:
                if dvec:
                    delta = DraftVecUtils.scaleTo(delta, dvec.Length)
                else:
                    # just hardcoded default value as ArchWall would provide
                    # if dvec is not provided either
                    delta = DraftVecUtils.scaleTo(delta, 200)
        else:
            delta = DraftVecUtils.scaleTo(delta, dvec.Length)

        # Consider individual edge Align direction
        # - ArchWall should now always provide alignList
        if i == 0:
            if alignListC[0] == 'Center':
                delta = DraftVecUtils.scaleTo(delta, delta.Length/2)
            # No need to do anything for 'Left' and 'Right' as original dvec
            # have set both the direction and amount of offset correct
            # elif alignListC[i] == 'Left':  #elif alignListC[i] == 'Right':
        if i != 0:
            try:
                if alignListC[i] == 'Left':
                    curDir = 1
                    curAlign = 'Left'
                elif alignListC[i] == 'Right':
                    curDir = -1
                    curAlign = 'Right'
                    delta = delta.negative()
                elif alignListC[i] == 'Center':
                    curDir = 1
                    curAlign = 'Center'
                    delta = DraftVecUtils.scaleTo(delta, delta.Length/2)
            except IndexError:
                curDir = firstDir
                curAlign = firstAlign
                if firstAlign == 'Right':
                    delta = delta.negative()
                elif firstAlign == 'Center':
                    delta = DraftVecUtils.scaleTo(delta, delta.Length/2)

        # Consider whether generating the 'offset wire' or the 'base wire'
        if offsetMode is None:
            # Consider if curOrientation and/or curDir match their
            # firstOrientation/firstDir - to determine whether
            # and how to offset the current edge

            # This is a xor
            if (curOrientation == firstOrientation) != (curDir == firstDir):
                if curAlign in ['Left', 'Right']:
                    # ArchWall has an Offset properties for user to offset
                    # the basewire before creating the base profile of wall
                    # (not applicable to 'Center' align)
                    if basewireOffset:
                        delta = DraftVecUtils.scaleTo(delta, basewireOffset)
                        nedge = offset(curredge,delta,trim=True)
                    else:
                        nedge = curredge
                elif curAlign == 'Center':
                    delta = delta.negative()
                    nedge = offset(curredge, delta, trim=True)
            else:
                # if curAlign in ['Left', 'Right']:
                # elif curAlign == 'Center': # Both conditions same result.
                # ArchWall has an Offset properties for user to offset
                # the basewire before creating the base profile of wall
                # (not applicable to 'Center' align)
                if basewireOffset:
                    if curAlign in ['Left', 'Right']:
                        delta = DraftVecUtils.scaleTo(delta,
                                                      delta.Length + basewireOffset)
                    #else: # elif curAlign == 'Center': #pass # no need to add basewireOffset
                nedge = offset(curredge, delta, trim=True)

            # TODO arc always in counter-clockwise directinon
            # ... ( not necessarily 'reversed')
            if curOrientation == "Reversed":
                # need to test against Part.Circle, not Part.ArcOfCircle
                if not isinstance(curredge.Curve, Part.Circle):
                    # if not arc/circle, assume straight line, reverse it
                    nedge = Part.Edge(nedge.Vertexes[1], nedge.Vertexes[0])
                else:
                    # if arc/circle
                    # Part.ArcOfCircle(edge.Curve,
                    #                  edge.FirstParameter, edge.LastParameter,
                    #                  edge.Curve.Axis.z > 0)
                    midParameter = nedge.FirstParameter + (nedge.LastParameter - nedge.FirstParameter)/2
                    midOfArc = nedge.valueAt(midParameter)
                    nedge = Part.ArcOfCircle(nedge.Vertexes[1].Point,
                                             midOfArc,
                                             nedge.Vertexes[0].Point).toShape()
                    # TODO any better solution than to calculate midpoint
                    # of arc to reverse?

        elif offsetMode in ["BasewireMode"]:
            if (not (curOrientation == firstOrientation)
                    != (curDir == firstDir)):
                if curAlign in ['Left', 'Right']:
                    # ArchWall has an Offset properties for user to offset
                    # the basewire before creating the base profile of wall
                    # (not applicable to 'Center' align)
                    if basewireOffset:
                        delta = DraftVecUtils.scaleTo(delta, basewireOffset)
                        nedge = offset(curredge, delta, trim=True)
                    else:
                        nedge = curredge
                elif curAlign == 'Center':
                    delta = delta.negative()
                    nedge = offset(curredge, delta, trim=True)
            else:
                if curAlign in ['Left', 'Right']:
                    # ArchWall has an Offset properties for user to offset
                    # the basewire before creating the base profile of wall
                    # (not applicable to 'Center' align)
                    if basewireOffset:
                        delta = DraftVecUtils.scaleTo(delta,
                                                      delta.Length + basewireOffset)
                    nedge = offset(curredge, delta, trim=True)

                elif curAlign == 'Center':
                    nedge = offset(curredge, delta, trim=True)
            if curOrientation == "Reversed":
                # need to test against Part.Circle, not Part.ArcOfCircle
                if not isinstance(curredge.Curve, Part.Circle):
                    # if not arc/circle, assume straight line, reverse it
                    nedge = Part.Edge(nedge.Vertexes[1], nedge.Vertexes[0])
                else:
                    # if arc/circle
                    # Part.ArcOfCircle(edge.Curve,
                    #                  edge.FirstParameter,
                    #                  edge.LastParameter,
                    #                  edge.Curve.Axis.z > 0)
                    midParameter = nedge.FirstParameter + (nedge.LastParameter - nedge.FirstParameter)/2
                    midOfArc = nedge.valueAt(midParameter)
                    nedge = Part.ArcOfCircle(nedge.Vertexes[1].Point,
                                             midOfArc,
                                             nedge.Vertexes[0].Point).toShape()
                    # TODO any better solution than to calculate midpoint
                    # of arc to reverse?
        else:
            print(" something wrong ")
            return None
        if not nedge:
            return None

        nedges.append(nedge)

    if len(edges) > 1:
        nedges = connect(nedges, closed)
    else:
        nedges = Part.Wire(nedges[0])

    if bind and not closed:
        e1 = Part.LineSegment(edges[0].Vertexes[0].Point,
                              nedges[0].Vertexes[0].Point).toShape()
        e2 = Part.LineSegment(edges[-1].Vertexes[-1].Point,
                              nedges[-1].Vertexes[-1].Point).toShape()
        alledges = edges.extend(nedges)
        alledges = alledges.extend([e1, e2])
        w = Part.Wire(alledges)
        return w
    else:
        return nedges