def test_object_link_change_id(self): g = Group(id=999) self.assertEqual(g['id'], 999) use = Use(g) # change 'id' after assigning to <Use> object g['id'] = 'newid' self.assertEqual(use.tostring(), '<use xlink:href="#newid" />')
def draw_axes(drawing, g, L=0.1, stroke_width=0.01, klass="axes"): ID = f"axes-{L}-{stroke_width}" for element in drawing.defs.elements: if element.attribs.get("id", None) == ID: break else: template = drawing.g(id=ID) line = drawing.line(start=(0, 0), end=(L, 0), stroke_width=stroke_width, stroke="red") template.add(line) line = drawing.line(start=(0, 0), end=(0, L), stroke_width=stroke_width, stroke="green") template.add(line) drawing.defs.add(template) g2 = drawing.g() g2.attribs["class"] = klass use = Use(f"#{ID}") g2.add(use) # # line = drawing.line(start=(0, 0), end=(L, 0), stroke_width=stroke_width, stroke="red") # g2.add(line) # # line = drawing.line(start=(0, 0), end=(0, L), stroke_width=stroke_width, stroke="green") # g2.add(line) g.add(g2)
def UseInch(obj, xy): def trans_inch(*n): pt_per_inch = 72 return tuple([nn * pt_per_inch for nn in n]) return Use( obj, trans_inch(*xy), )
def draw_svg(self, drawing, g): T = 0.562 / 0.585 S = 1.0 rect = drawing.rect(insert=(-S / 2, -S / 2), size=(S, S), fill="#222", stroke="none") g.add(rect) if self.fn: # texture = get_jpeg_bytes(self.fn) with open(self.fn, "rb") as _: texture = _.read() if b"git-lfs" in texture: msg = f"The file {self.fn} is a Git LFS pointer. Repo not checked out correctly." raise Exception(msg) # print(f'drawing defs {drawing.defs}') ID = f"texture-{self.kind}" for img in drawing.defs.elements: if img.attribs.get("id", None) == ID: break else: href = data_encoded_for_src(texture, mime_from_fn(self.fn)) img = drawing.image( href=href, size=(T, T), insert=(-T / 2, -T / 2), style= "transform: rotate(90deg) scaleX(-1) rotate(-90deg) ", ) img.attribs["class"] = "tile-textures" img.attribs["id"] = ID drawing.defs.add(img) use = Use(f"#{ID}") g.add(use) # # if draw_directions_lanes: # if self.kind != 'floor': # start = (-0.5, -0.25) # end = (+0, -0.25) # line = drawing.line(start=start, end=end, stroke='blue', stroke_width='0.01') # g.add(line) draw_axes(drawing, g) draw_children(drawing, self, g)
def encode(self, bits_data: List[int]): tattoo_bit_height = int(len(bits_data) / 8) x, y = 0, 0 xmin, xmax = 0, 0 ymin, ymax = 0, 0 tattoo = Group(id="tattoo") for j in range(tattoo_bit_height): for i in range(8): if x > xmax: xmax = x if y > ymax: ymax = y bit = bits_data.pop() if bit == 0: tattoo.add(Use("#zero", (x, y))) elif bit == 1: tattoo.add(Use("#one", (x, y))) else: raise RuntimeError() x, y = self.shift_bit(x, y) x, y = self.shift_byte(j, y) xmax += self.poly.inner_circle_radius * self.dimens.bit_radius * 2 ymax += self.poly.outer_circle_radius * self.dimens.bit_radius * 2 tattoo_width = xmax - xmin tattoo_height = ymax - ymin scaled_width_mm = 65 scale_factor = scaled_width_mm * 3.78 / tattoo_width scaled_height_mm = tattoo_height * scale_factor / 3.78 tattoo.scale(scale_factor) tattoo.translate( 3.78 * (210 - scaled_width_mm) / 2, 3.78 * (297 - scaled_height_mm) / 2 ) self.dwg.add(tattoo) self.dwg.save(pretty=True)
def test_object_link_auto_id(self): AutoID(999) # set next id to 999 g = Group() use = Use(g) self.assertEqual(use.tostring(), '<use xlink:href="#id999" />')
def test_object_link(self): g = Group(id='test') use = Use(g) self.assertEqual(use.tostring(), '<use xlink:href="#test" />')
def test_constructor2(self): use = Use('#an_id', insert=(10, 20), size=(100, 200)) self.assertEqual( use.tostring(), '<use height="200" width="100" x="10" xlink:href="#an_id" y="20" />' )
def test_constructor(self): use = Use('#an_id', x=10, y=20, width=100, height=200) self.assertEqual( use.tostring(), '<use height="200" width="100" x="10" xlink:href="#an_id" y="20" />' )
def test_constructor2(self): use = Use('#an_id', insert=(10, 20), size=(100, 200)) self.assertEqual(use.tostring(), '<use height="200" width="100" x="10" xlink:href="#an_id" y="20" />')
def test_constructor(self): use = Use('#an_id', x=10, y=20, width=100, height=200) self.assertEqual(use.tostring(), '<use height="200" width="100" x="10" xlink:href="#an_id" y="20" />')
def _define_one_symbol(self): dot = Symbol(id="dot") dot.add(Circle(center=(0, 0), r=self.dimens.stroke_width * 0.4, fill="black", stroke="none")) self.dwg.defs.add(dot) one = Symbol(id="one", class_="bit-1", stroke_width=1, stroke="black") one.add(Circle(center=(0, 0), r=self.dimens.bit_radius, fill="none", stroke="none")) empty_face_symbol = Symbol(id="empty_face") light_face_symbol = Symbol(id="light_face") dense_face_symbol = Symbol(id="dense_face") points = ["{} {}".format( self.dimens.bit_radius * math.sin(2 * math.pi * v / self.poly.num_vertices), self.dimens.bit_radius * math.cos(2 * math.pi * v / self.poly.num_vertices) ) for v in range(0, 3)] data = ['M', "0 0 L", *points, 'Z'] path = Path(data, fill="none", stroke_linejoin="bevel") empty_face_symbol.add(path) light_face_symbol.add(path) dense_face_symbol.add(path) x0 = 0 x1 = self.dimens.bit_radius * math.sin(2 * math.pi * 2 / self.poly.num_vertices) y0 = self.dimens.bit_radius * math.cos(2 * math.pi * 2 / self.poly.num_vertices) y1 = self.poly.outer_circle_radius * self.dimens.bit_radius w = x1 - x0 h = y1 - y0 h_rect = y1 num_dense = 200 num_light = int(num_dense / 2) points = np.apply_along_axis( lambda p: np.array([p[0] * w, p[1] * h_rect + p[0] * y0]), 1, np.clip( i4_sobol_generate(2, num_dense) + np.random.rand(num_dense, 2) / h, 0, 1 )) for p in points[:num_light]: light_face_symbol.add(Use(dot.get_iri(), (p[0], p[1]))) dense_face_symbol.add(Use(dot.get_iri(), (p[0], p[1]))) for p in points[num_light:]: dense_face_symbol.add(Use(dot.get_iri(), (p[0], p[1]))) self.dwg.defs.add(empty_face_symbol) self.dwg.defs.add(light_face_symbol) self.dwg.defs.add(dense_face_symbol) f1 = Use(empty_face_symbol.get_iri()) one.add(f1) f2 = Use(dense_face_symbol.get_iri()) f2.rotate(120) one.add(f2) f3 = Use(light_face_symbol.get_iri()) f3.rotate(240) one.add(f3) self.dwg.defs.add(one)
def draw_svg(self, drawing, g): ID = str(id(self)) for element in drawing.defs.elements: if element.attribs.get("id", None) == ID: break else: template = drawing.g(id=ID) L, W = self.length, self.width W = 0.11 Lb = 0.11 Lf = 0.08 d = 0.10 d = 0.12 wheel_width = 0.027 wheel_radius = 0.032 rx_robot = 0.02 rx_wheel = 0.005 fancy = True if fancy: rect = drawing.rect( insert=(-wheel_radius, -d * 0.5 - wheel_width / 2), size=(+wheel_radius * 2, wheel_width), fill="black", stroke_width="0.01", stroke="black", rx=rx_wheel, ) rect.width = "0.1em" template.add(rect) rect = drawing.rect( insert=(-wheel_radius, +d * 0.5 - wheel_width / 2), size=(+wheel_radius * 2, wheel_width), fill="black", stroke_width="0.01", stroke="black", rx=rx_wheel, ) rect.width = "0.1em" template.add(rect) rect = drawing.rect( insert=(-Lb, -W * 0.5), size=(Lb + Lf, W), fill=self.color, stroke_width="0.01", stroke="black", rx=rx_robot, ) rect.width = "0.1em" template.add(rect) else: rect = drawing.rect( insert=(-Lb, -W * 0.5), size=(Lb + Lf, W), fill=self.color, # style='opacity:0.4', stroke_width="0.01", stroke="black", # rx=rx_robot, ) template.add(rect) drawing.defs.add(template) use = Use(f"#{ID}") g.add(use) draw_axes(drawing, g)