def get_dxf_entity(self, coords, layer): """Create the cell content as INSERT-entity with trailing ATTRIB-entities. coords -- tuple of border-coordinates : left, right, top, bottom layer -- layer, which should be used for dxf entities """ left, right, top, bottom = self.substract_margin(coords) style = self.style halign = style['halign'] valign = style['valign'] xpos = (left, float(left+right)/2., right)[halign] ypos = (bottom, float(bottom+top)/2., top)[valign-1] insert = Insert(blockname=self.blockdef['name'], insert=(xpos, ypos), xscale=style['xscale'], yscale=style['yscale'], rotation=style['rotation'], layer=layer) # process attribs for key, value in self.attribs.items(): try: attdef = self.blockdef.find_attdef(key) attrib = attdef.new_attrib(text=str(value)) insert.add(attrib, relative=True) except KeyError: pass # ignore none existing attdefs return insert
def _build(self): def set_tags(insert_entity): basepoint = self.blockdef['basepoint']['xyz'] for tag, text in self.attribs.items(): try: attdef = self.blockdef.find_attdef(tag) attrib = attdef.new_attrib(text=text) insert_entity.add(attrib, relative=True, block_basepoint=basepoint) except KeyError: # no attdef <tag> found pass insert = Insert(blockname=self.blockdef['name'], insert=self.insert, rotation=self.rotation, layer=self.layer, color=self.color, linetype=self.linetype) for key, value in [('xscale', self.xscale), ('yscale', self.yscale), ('zscale', self.zscale)]: if value != 1.: insert[key] = value set_tags(insert) return insert.__dxf__()
def get_dxf_entity(self, border_coords, layer): """ Create the cell content as INSERT-entity with trailing ATTRIB-Entities. :param border_coords: tuple of border-coordinates : left, right, top, bottom :param str layer: layer, which should be used for dxf entities """ left, right, top, bottom = self.get_workspace_coords(border_coords) style = self.style halign = style['halign'] valign = style['valign'] xpos = (left, float(left + right) / 2., right)[halign] ypos = (bottom, float(bottom + top) / 2., top)[valign - 1] insert = Insert(blockname=self.blockdef['name'], insert=(xpos, ypos), xscale=style['xscale'], yscale=style['yscale'], rotation=style['rotation'], layer=layer) # process attribs for key, value in self.attribs.items(): try: attdef = self.blockdef.find_attdef(key) attrib = attdef.new_attrib(text=str(value)) insert.add(attrib, relative=True) except KeyError: pass # ignore none existing ATTDEFs return insert
def test_add_attrib_absolute(self): block_ref = Insert(blockname='TestAttrib', insert=(5, 5), rotation=30) attrib = Attrib( insert=(1, 1), rotation=15, tag='TEST', text='attrib', ) block_ref.add(attrib, relative=False) inserted_attrib = block_ref.data[0] self.assertEqual(inserted_attrib['rotation'], 15.) self.assertEqual(inserted_attrib['insert']['xy'], [1., 1.])
def test_add_attrib_relative(self): # insert blockref with 45 degree rotation block_ref = Insert(blockname='TestAttrib', insert=(0, 0), rotation=45) attrib = Attrib( insert=(1, 1), rotation=45, # 45 degree relative to original block definition tag='TEST', text='attrib', ) block_ref.add(attrib, relative=True) # result rotation = 45 + 45 = 90 inserted_attrib = block_ref.data[0] self.assertEqual(inserted_attrib['rotation'], 90.) self.assertAlmostEqual(inserted_attrib['insert']['x'], 0, places=3) self.assertAlmostEqual(inserted_attrib['insert']['y'], 1.4142, places=3) # y = sqrt(2)
def tick(point, rotate=False): """ build the insert-entity for the tick block """ return Insert(insert=point, blockname=self.prop('tick'), rotation=self.angle + (180. if rotate else 0.), xscale=self.prop('tickfactor'), yscale=self.prop('tickfactor'), layer=self.prop('layer'))
def _draw_ticks(self): rotation = vector2angle(self.target_vector) rotation = degrees(rotation + pi) self.data.append( Insert(insert=self.target, blockname='DIMTICK_RADIUS', rotation=rotation, xscale=self.prop('tickfactor'), yscale=self.prop('tickfactor'), layer=self.prop('layer')))
def _draw_ticks(self): for vector, mirror in [(self.start_vector, False), (self.end_vector, self.prop('tick2x'))]: insert_point = vadd(self.center, vmul_scalar(vector, self.pos_radius)) rotation = vector2angle(vector) + pi / 2. rotation = degrees(rotation + (pi if mirror else 0.)) self.data.append( Insert(insert=insert_point, blockname=self.prop('tick'), rotation=rotation, xscale=self.prop('tickfactor'), yscale=self.prop('tickfactor'), layer=self.prop('layer')))
def test_insert_all_attribs(self): insert = Insert(attribs_follow=1, blockname='empty', xscale=1.0, yscale=2.0, zscale=3.0, rotation=30.0, columns=2, rows=7, colspacing=1.7, rowspacing=2.9) expected = " 0\nINSERT\n 8\n0\n 66\n1\n 2\nempty\n" \ " 10\n0.0\n 20\n0.0\n 30\n0.0\n" \ " 41\n1.0\n 42\n2.0\n 43\n3.0\n 50\n30.0\n" \ " 70\n2\n 71\n7\n 44\n1.7\n 45\n2.9\n" self.assertEqual(dxfstr(insert), expected)
def insert(blockname, insert=(0., 0.), **kwargs): """ Insert a new block-reference. :param str blockname: name of block definition :param insert: insert point (xy- or xyz-tuple), z-axis is 0 by default :param float xscale: x-scale factor, default=1. :param float yscale: y-scale factor, default=1. :param float zscale: z-scale factor, default=1. :param float rotation: rotation angle in degree, default=0. :param int columns: column count, default=1 :param int rows: row count, default=1 :param float colspacing: column spacing, default=0. :param float rowspacing: row spacing, default=0. """ return Insert(blockname=blockname, insert=insert, **kwargs)
def test_insert_simple(self): insert = Insert(blockname='empty') expected = " 0\nINSERT\n 8\n0\n 2\nempty\n 10\n0.0\n 20\n0.0\n 30\n0.0\n" self.assertEqual(dxfstr(insert), expected)