Example #1
0
    def set_scale(self, ctx, page_w, page_h):

        """
        Automatically sets a scale factor for the straight drawing.

        Arguments:
        ctx -- a Pycairo context
        page_w, page_h -- width and height of drawing area
        """

        pipelen = self.length
        fld = self.diameters["fo"]
        flr = self.p_rad["fo"]

        # Calculate radius dimension line lengths, these vary based
        # on font size, and need to be considered for scaling along
        # the y-axis. These dimensions will be upscaled during the
        # final drawing, and will be independent of the scale factor
        # calculated.

        rad_values = []
        for comp in ["co", "ci", "lo", "li"]:
            rad_values.append(str(int(round(self.diameters[comp]))))
        rdm = get_largest_text_height(ctx, rad_values, self.text["dims"], True)
        self.dim_line_length = rdm
        rdm = self.dim_line_length * 4

        # Calculate length dimension width, this can vary based both
        # on font size and on the length itself, and needs to be
        # considered for scaling along the x-axis. This dimension will
        # be upscaled during the final drawing, and will be independent
        # of the scale factor calculated.

        len_values = []
        len_values.append(str(int(round(self.length))))
        ldm = get_largest_text_width(ctx, len_values, self.text["dims"], True)
        self.len_dim_line_length = ldm
        ldm *= (self.len_dim_line_length_offset_m +
                self.len_dim_line_length_width_m)

        # Calculate x and y scale factors

        x_scale = (page_w - ldm) / fld
        y_scale = (page_h - rdm) / (pipelen + flr)

        # Scale based on the smallest factor

        self.scale = min(x_scale, y_scale)
        ctx.scale(self.scale, self.scale)

        # Set the origin based on calculated scale

        page_w /= self.scale
        bend_w = fld + ldm / self.scale
        x_origin = (page_w - bend_w) / 2 + flr

        page_h /= self.scale
        bend_h = pipelen + flr + rdm / self.scale
        y_origin = page_h - (page_h - bend_h) / 2 - flr

        ctx.translate(x_origin, y_origin)

        # Upscale line lengths

        self.len_dim_line_length /= self.scale

        # Call superclass function

        Pipe.set_scale(self, ctx, page_w, page_h)
Example #2
0
    def set_scale(self, ctx, page_w, page_h):

        """
        Automatically sets a scale factor for the bend drawing.

        Arguments:
        ctx -- a Pycairo context
        page_w, page_h -- width and height of drawing area
        """

        b_arc = self.bend_arc
        cri = self.radii["inner"]["co"]
        cro = self.radii["outer"]["co"]
        bfr = self.radii["outer"]["fo"]
        flr = self.p_rad["fo"]

        # Calculate segment dimension line lengths

        dim_values = []
        dpt = self.dos_dim_dp
        for dim in ["cex", "cin", "lex", "lin"]:
            dim_values.append(str(round(self.segdims[dim].value, dpt)))
        ddm = get_largest_text_width(ctx, dim_values, self.text["dims"], True)
        self.dos_dim_line_length = ddm

        # Calculate radius dimension line lengths, these vary based
        # on font size, and need to be considered for scaling along
        # the axes. These dimensions will be upscaled during the
        # final drawing, and will be independent of the scale factor
        # calculated.

        rad_values = []
        for comp in ["co", "ci", "lo", "li"]:
            rad_values.append(str(int(round(self.diameters[comp]))))
        rdm = get_largest_text_width(ctx, rad_values, self.text["dims"], True)
        self.dim_line_length = rdm
        rdm = self.dim_line_length * 4

        # Calculate x scale factor

        # dos_s not yet implemented. Under some circumstances the segment
        # dimension lines on the drawing can extend past the right edge
        # of the drawing extent currently calculated here. The option to
        # suppress the dimension lines and relegate them to a box is
        # available, so problem is minor. Scaling depends upon whether
        # casing is one-piece of segments, whether the option to show
        # the segment dimensions was shown, and so on, so is more
        # complicated than the options shown here. Approach should be
        # the same, calculate the width including the segment dimension
        # lines under the selected options, and then scale based on that
        # if it turns out to be the largest of the calculated width. Then
        # set the x origin accordingly.

        #n      = ns//2 + (ns%2)
        #dx     = ptoc(sa*n,ddm*1.5).x
        #pt     = self.pc_pts["out"]["co"][n]
        #dos_w  = pt.x

        rad_w = bfr
        ang_w = rad_w - cos(b_arc) * cri
        dm_w = cos(pi / 2 - b_arc) * rdm

        #dos_s  = (page_w - dx) / dos_w

        rad_s = page_w / bfr
        ang_s = (page_w - dm_w) / ang_w
        x_scale = min(rad_s, ang_s)

        # Calculate y scale factor

        rad_h = sin(b_arc) * bfr + flr
        ang_h = sin(b_arc) * cro + flr
        dm_h = sin(pi / 2 - b_arc) * rdm
        y_scale = min(page_h / rad_h, (page_h - dm_h) / ang_h)

        # Set scale based on smallest factor

        self.scale = min(x_scale, y_scale)
        ctx.scale(self.scale, self.scale)

        # Set bend origin based on calculated scale

        page_w /= self.scale
        bend_w = max(rad_w, ang_w + dm_w / self.scale)
        x_origin = page_w - (page_w - bend_w) / 2 - bfr

        page_h /= self.scale
        bend_h = max(rad_h, ang_h + dm_h / self.scale)
        y_origin = page_h - (page_h - bend_h) / 2 - flr

        ctx.translate(x_origin, y_origin)

        # Scale lines

        self.dos_dim_line_length /= self.scale

        # Call superclass function

        Pipe.set_scale(self, ctx, page_w, page_h)