def test_path_TepidQuadratic(self): path = Path("M 10 5 Q 15 30 25 15 T 50 40") pe = PathElement() pe.path = path ibb = (10, 50), (5, 40) self.assert_bounding_box_is_equal(pe, *ibb)
def add_frame(self, name, box, style, radius=0): """ name -- The name of the new frame object. box -- The boundary box of the node. style -- The style used to draw the path. radius -- The corner radius of the frame. returns a new frame node. """ r = min( [radius, (abs(box[1] - box[0]) / 2), (abs(box[3] - box[2]) / 2)]) if radius > 0: d = ' '.join( str(x) for x in [ 'M', box[0], (box[2] + r), 'A', r, r, '0 0 1', ( box[0] + r), box[2], 'L', (box[1] - r), box[2], 'A', r, r, '0 0 1', box[1], (box[2] + r), 'L', box[1], ( box[3] - r), 'A', r, r, '0 0 1', (box[1] - r), box[3], 'L', (box[0] + r), box[3], 'A', r, r, '0 0 1', box[0], (box[3] - r), 'Z' ]) else: d = ' '.join( str(x) for x in [ 'M', box[0], box[2], 'L', box[1], box[2], 'L', box[1], box[3], 'L', box[0], box[3], 'Z' ]) elem = PathElement() elem.style = style elem.label = name elem.path = d return elem
def test_path_combined_1(self): path = Path("M 0 0 C 11 14 33 3 85 98 H 84 V 91 L 13 78 C 26 83 65 24 94 77") # path = Path("M 0 0 C 11 14 33 3 85 98") pe = PathElement() pe.path = path ibb = self.get_inkscape_bounding_box(pe) self.assert_bounding_box_is_equal(pe, *ibb, disable_inkscape_check=True)
def generateLine(self, x1, y1, x2, y2, strokeWidth, stroke, name): line = PathElement() line.path = 'M {} {} L {} {}'.format(x1, y1, x2, y2) line.style = { 'stroke': stroke, 'stroke-width': strokeWidth, 'fill': 'none' } line.label = name return line
def generateSectorColoredPart(): nSectorPlusOne = nSector + 1 pathPattern = 'm{} {} v {} h {} v {} h {} v {} h {} v {} Z' coloredPart = PathElement() coloredPart.style = { 'stroke': stroke, 'stroke-width': strokeWidth, 'fill': self.sectorColors[nSector] } coloredPart.label = 'colored-part-{}'.format(nSectorPlusOne) coloredPart.path = pathPattern.format( (nColumns / 2 - 2) * xBoxSize, initialY - yBoxSize, -initialY + yBoxSize, xBoxSize, yBoxSize * (nBoxesPerColumn - 5), xBoxSize, yBoxSize, -xBoxSize, yBoxSize * 3) return coloredPart
def generateOutline(): pathPattern = 'm{} {} v {} l {} {} h {} l {} {} v {} Z' outline = PathElement() outline.style = { 'stroke': stroke, 'stroke-width': strokeWidth, 'fill': fillSecure } outline.label = 'outline-{}'.format(nSector + 1) outline.path = pathPattern.format(initialX, initialY, -initialY + yBoxSize, obliqueDistance, -yBoxSize, topDistance, obliqueDistance, yBoxSize, yBoxSize * (nBoxesPerColumn - 1)) return outline
def generateSectorCenterPart(): nSectorPlusOne = nSector + 1 pathPattern = 'm{} {} L {} {} L {} {}' centerColoredPart = PathElement() fill = ('none', self.sectorColors[nSector])[not self.useLaser and self.generateCenter] centerColoredPart.style = { 'stroke': stroke, 'stroke-width': strokeWidth, 'fill': fill } centerColoredPart.label = 'colored-part-{}'.format(nSectorPlusOne) cornerSize = initialX + obliqueDistance centerColoredPart.path = pathPattern.format( cornerSize, 0, 0, -distanceFromBoardCenter, -cornerSize, 0) return centerColoredPart
def test_random_path_1(self): import random from inkex.paths import Line, Vert, Horz, Curve, Move, Arc, Quadratic, TepidQuadratic, Smooth, ZoneClose klasses = (Line, Vert, Horz, Curve, Move, Quadratic) # , ZoneClose, Arc def random_segment(klass): args = [random.randint(1, 100) for _ in range(klass.nargs)] if klass is Arc: args[2] = 0 # random.randint(0, 1) args[3] = 0 # random.randint(0, 1) args[4] = 0 # random.randint(0, 1) return klass(*args) random.seed(2128506) # random.seed(datetime.now()) n_trials = 10 n_elements = 15 for i in range(n_trials): path = Path() path.append(Move(0, 0)) for j in range(n_elements): k = random.choice(klasses) path.append(random_segment(k)) if k is Curve: while random.randint(0, 1) == 1: path.append(random_segment(Smooth)) if k is Quadratic: while random.randint(0, 1) == 1: path.append(random_segment(TepidQuadratic)) pe = PathElement() pe.path = path ibb = self.get_inkscape_bounding_box(pe) self.assert_bounding_box_is_equal(pe, *ibb, disable_inkscape_check=True)
def test_path_TepidQuadratic_2(self): path = Path("M 10 5 Q 15 30 25 15 T 50 40 T 15 20") pe = PathElement() pe.path = path ibb = (10, 10 + 43.462), (5, 56) self.assert_bounding_box_is_equal(pe, *ibb)
def test_path_Move(self): path = Path("M 10 20") pe = PathElement() pe.path = path self.assert_bounding_box_is_equal(pe, (10, 10),( 20, 20))
def test_path_Arc_long_sweep_off(self): path = Path("M 10 20 A 10 20 0 1 0 20 15") path_element = PathElement() path_element.path = path self.assert_bounding_box_is_equal(path_element, (7.078, 20 + 7.078), (15.0, 15.0 + 39.127))
def generate(self): teeth = self.options.teeth pitch = self.svg.unittouu(str(self.options.pitch) + self.options.unit) angle = self.options.angle # Angle of tangent to tooth at circular pitch wrt radial line. centerdiameter = self.svg.unittouu( str(self.options.centerdiameter) + self.options.unit) # print >>sys.stderr, "Teeth: %s\n" % teeth two_pi = 2.0 * pi # Pitch (circular pitch): Length of the arc from one tooth to the next) # Pitch diameter: Diameter of pitch circle. pitch_diameter = float(teeth) * pitch / pi pitch_radius = pitch_diameter / 2.0 # Base Circle base_diameter = pitch_diameter * cos(radians(angle)) base_radius = base_diameter / 2.0 # Diametrial pitch: Number of teeth per unit length. pitch_diametrial = float(teeth) / pitch_diameter # Addendum: Radial distance from pitch circle to outside circle. addendum = 1.0 / pitch_diametrial # Outer Circle outer_radius = pitch_radius + addendum outer_diameter = outer_radius * 2.0 # Tooth thickness: Tooth width along pitch circle. tooth = (pi * pitch_diameter) / (2.0 * float(teeth)) # Undercut? undercut = (2.0 / (sin(radians(angle))**2)) needs_undercut = teeth < undercut # Clearance: Radial distance between top of tooth on one gear to bottom of gap on another. clearance = 0.0 # Dedendum: Radial distance from pitch circle to root diameter. dedendum = addendum + clearance # Root diameter: Diameter of bottom of tooth spaces. root_radius = pitch_radius - dedendum root_diameter = root_radius * 2.0 half_thick_angle = two_pi / (4.0 * float(teeth)) pitch_to_base_angle = involute_intersect_angle(base_radius, pitch_radius) pitch_to_outer_angle = involute_intersect_angle( base_radius, outer_radius) - pitch_to_base_angle centers = [(x * two_pi / float(teeth)) for x in range(teeth)] points = [] for c in centers: # Angles pitch1 = c - half_thick_angle base1 = pitch1 - pitch_to_base_angle outer1 = pitch1 + pitch_to_outer_angle pitch2 = c + half_thick_angle base2 = pitch2 + pitch_to_base_angle outer2 = pitch2 - pitch_to_outer_angle # Points b1 = point_on_circle(base_radius, base1) p1 = point_on_circle(pitch_radius, pitch1) o1 = point_on_circle(outer_radius, outer1) b2 = point_on_circle(base_radius, base2) p2 = point_on_circle(pitch_radius, pitch2) o2 = point_on_circle(outer_radius, outer2) if root_radius > base_radius: pitch_to_root_angle = pitch_to_base_angle - involute_intersect_angle( base_radius, root_radius) root1 = pitch1 - pitch_to_root_angle root2 = pitch2 + pitch_to_root_angle r1 = point_on_circle(root_radius, root1) r2 = point_on_circle(root_radius, root2) p_tmp = [r1, p1, o1, o2, p2, r2] else: r1 = point_on_circle(root_radius, base1) r2 = point_on_circle(root_radius, base2) p_tmp = [r1, b1, p1, o1, o2, p2, b2, r2] points.extend(p_tmp) path = points_to_svgd(points) # Create SVG Path for gear style = { 'stroke': '#000000', 'fill': 'none', 'stroke-width': str(self.svg.unittouu('1px')) } gear = PathElement() gear.style = style gear.path = path yield gear if centerdiameter > 0.0: arc = PathElement.arc((0, 0), centerdiameter / 2) arc.style = style yield arc
def test_path_vert(self): path = Path("M 15 30 v 20") pe = PathElement() pe.path = path self.assert_bounding_box_is_equal(pe, (15, 15), (30, 50))
def test_path_Curve(self): path = Path("M10 10 C 20 20, 40 20, 50 10") pe = PathElement() pe.path = path self.assert_bounding_box_is_equal(pe, (10, 50), (10, 17.5))
def test_path_Zone(self): path = Path("M 15 30 Z") pe = PathElement() pe.path = path self.assert_bounding_box_is_equal(pe, (15, 15), (30, 30))
def test_path_horz(self): path = Path("M 15 30 h 20") pe = PathElement() pe.path = path self.assert_bounding_box_is_equal(pe, (15, 35), (30, 30))
def test_path_line(self): path = Path("M 15 30 l 10 20") pe = PathElement() pe.path = path self.assert_bounding_box_is_equal(pe, (15, 25), (30, 50))
def test_path_Line(self): path = Path("M 15 30 L 10 20") pe = PathElement() pe.path = path self.assert_bounding_box_is_equal(pe, (10, 15),( 20, 30))
def test_path_Arc_short_sweep_on(self): path = Path("M 10 20 A 10 20 0 0 1 20 15") path_element = PathElement() path_element.path = path self.assert_bounding_box_is_equal(path_element, (10, 20), (14.127, 14.127 + 5.873))
def test_path_Arc_long_sweep_on_axis_x_25(self): path = Path("M 10 20 A 10 20 25 1 1 20 15") path_element = PathElement() path_element.path = path self.assert_bounding_box_is_equal(path_element, (4.723, 4.723 + 24.786), (-17.149, -17.149 + 37.149))
def test_path_Arc_long_sweep_on(self): path = Path("M 10 20 A 10 20 0 1 1 20 15") path_element = PathElement() path_element.path = path self.assert_bounding_box_is_equal(path_element, (2.922, 2.922 + 20), (-19.127, -19.127 + 39.127))