def test_insert2(self): block = dxf.block('B1') att1 = dxf.attdef('TAG1', (1.0, 0.0), height=0.35) att2 = dxf.attdef('TAG2', (1.0, 0.5), height=0.35) block.add(att1) block.add(att2) attribs = {'TAG1': 'TextForTAG1', 'TAG2': 'TextForTAG2'} blockref = dxf.insert2(block, insert=(0, 0), attribs=attribs) result = dxfstr(blockref) self.assertTrue('TAG1' in result) self.assertTrue('TAG2' in result) self.assertTrue('TextForTAG1' in result) self.assertTrue('TextForTAG2' in result)
def writeDXF(self): fname = self.ui.lineEditOutputDXF.text() file = open(fname, 'w') rad = 0.25 scale = 1.0 col = 7 dec = self.ui.spinBoxDecimal.value() dwg = dxf.drawing(fname) # create block scalarsymbol = dxf.block(name='symbol') scalarsymbol.add( dxf.circle(radius=rad, color=0) ) # define some attributes scalarsymbol.add( dxf.attdef(insert=(1.25, -1.25), tag='VAL1', height=1.25, color=0) ) # add block definition to the drawing dwg.blocks.add(scalarsymbol) for nID in self.points: x = self.points[nID][0] y = self.points[nID][1] val1 = self.points[nID][2] values = {'VAL1': "%.{0}f".format(dec) % val1} dwg.add(dxf.insert2(blockdef=scalarsymbol, insert=(x, y), attribs=values, xscale=scale, yscale=scale, layer='0', color = col)) dwg.save()
def get_mat_symbol(): p1 = 0.5 p2 = 0.25 points = [(p1, p2), (p2, p1), (-p2, p1), (-p1, p2), (-p1, -p2), (-p2, -p1), (p2, -p1), (p1, -p2)] polygon = dxf.polyline(points, color=2) polygon.close() attdef = dxf.attdef(text='0', tag='num', height=0.7, color=1, halign=dxfwrite.CENTER, valign=dxfwrite.MIDDLE ) symbolblock = dxf.block('matsymbol') symbolblock.add(polygon) symbolblock.add(attdef) dwg.blocks.add(symbolblock) return symbolblock
def get_mat_symbol(): p1 = 0.5 p2 = 0.25 points = [(p1, p2), (p2, p1), (-p2, p1), (-p1, p2), (-p1, -p2), (-p2, -p1), (p2, -p1), (p1, -p2)] polygon = dxf.polyline(points, color=2) polygon.close() attdef = dxf.attdef(text='0', tag='num', height=0.7, color=1, halign=dxfwrite.CENTER, valign=dxfwrite.MIDDLE) symbolblock = dxf.block('matsymbol') symbolblock.add(polygon) symbolblock.add(attdef) dwg.blocks.add(symbolblock) return symbolblock
sample_coords = [get_random_point() for x in range(50)] flag_symbol = [(0, 0), (0, 5), (4, 3), (0, 3)] filename = 'flags.dxf' dwg = dxf.drawing(filename) dwg.add_layer('FLAGS') # first create a block flag = dxf.block(name='flag') # add dxf entities to the block (the flag) # use basepoint = (x, y) define an other basepoint than (0, 0) flag.add(dxf.polyline(flag_symbol)) flag.add(dxf.circle(radius=.4, color=2)) # define some attributes flag.add(dxf.attdef(insert=(0.5, -0.5), tag='NAME', height=0.5, color=3)) flag.add(dxf.attdef(insert=(0.5, -1.0), tag='XPOS', height=0.25, color=4)) flag.add(dxf.attdef(insert=(0.5, -1.5), tag='YPOS', height=0.25, color=4)) # add block definition to the drawing dwg.blocks.add(flag) number = 1 for point in sample_coords: # now insert flag symbols at coordinate 'point' # insert2 needs the block definition object as parameter 'blockdef' # see http://packages.python.org/dxfwrite/entities/insert2.html # fill attribtes by creating a dict(), keystr is the 'tag' name of the # attribute values = { 'NAME': "P(%d)" % number, 'XPOS': "x = %.3f" % point[0],
sample_coords = [get_random_point() for x in range(50)] flag_symbol = [(0,0), (0, 5), (4, 3), (0, 3)] filename = 'flags.dxf' dwg = dxf.drawing(filename) dwg.add_layer('FLAGS') # first create a block flag = dxf.block(name='flag') # add dxf entities to the block (the flag) # use basepoint = (x, y) define an other basepoint than (0, 0) flag.add( dxf.polyline(flag_symbol) ) flag.add( dxf.circle(radius=.4, color=2) ) # define some attributes flag.add( dxf.attdef(insert=(0.5, -0.5), tag='NAME', height=0.5, color=3) ) flag.add( dxf.attdef(insert=(0.5, -1.0), tag='XPOS', height=0.25, color=4) ) flag.add( dxf.attdef(insert=(0.5, -1.5), tag='YPOS', height=0.25, color=4) ) # add block definition to the drawing dwg.blocks.add(flag) number = 1 for point in sample_coords: # now insert flag symbols at coordinate 'point' # insert2 needs the block definition object as parameter 'blockdef' # see http://packages.python.org/dxfwrite/entities/insert2.html # fill attribtes by creating a dict(), keystr is the 'tag' name of the # attribute values = { 'NAME': "P(%d)" % number, 'XPOS': "x = %.3f" % point[0],
oblique=15, color=5, insert=(0, 5), rotation=30, )) # create BLOCK-entity block = dxf.block(name='Rechteck') # add DXF-entities to the block block.add(dxflist_example(0, 0, 1, 1)) # create an ATTDEF-entity, can be use to crate new ATTRIBS with following # default values, see attdef.new_attrib() call below attdef = dxf.attdef( insert=(.2, .2), rotation=30, height=0.25, text='test', prompt='test eingeben:', # only important for interactive CAD systems tag='TEST') # add attdef to the block definition block.add(attdef) # add the block to the BLOCKS section drawing.blocks.add(block) # insert the block references for x in range(1, 10): block_ref = dxf.insert(blockname='Rechteck', insert=(x * 2, 10), rotation=x * 6, xscale=x, yscale=x)
oblique=15, color=5, insert=(0,5), rotation=30, )) # create BLOCK-entity block = dxf.block(name='Rechteck') # add DXF-entities to the block block.add(dxflist_example(0, 0, 1, 1)) # create an ATTDEF-entity, can be use to crate new ATTRIBS with following # default values, see attdef.new_attrib() call below attdef = dxf.attdef( insert=(.2, .2), rotation = 30, height=0.25, text='test', prompt='test eingeben:', # only important for interactive CAD systems tag='TEST' ) # add attdef to the block definition block.add(attdef) # add the block to the BLOCKS section drawing.blocks.add(block) # insert the block references for x in range(1, 10): block_ref = dxf.insert( blockname='Rechteck', insert=(x*2,10), rotation=x*6, xscale=x,
def writeScalarDXF(nodes, SMin, SMax, eps, scale, symbol, useMono, fname): colMono = 7 colPos = 1 colNeg = 3 hLine = [(-0.75,0.0), (0.75, 0.0)] vLine = [(0.0,-0.75), (0.0,0.75)] rad = 0.375 dwg = dxf.drawing(fname) # create block scalarsymbol = dxf.block(name='symbol') if symbol != 0 and symbol != 3: scalarsymbol.add( dxf.polyline(hLine, color=0) ) scalarsymbol.add( dxf.polyline(vLine, color=0) ) if symbol != 1 and symbol != 3: scalarsymbol.add( dxf.circle(radius=rad, color=0) ) # define some attributes scalarsymbol.add( dxf.attdef(insert=(0.5, 0.5), tag='VAL1', height=1.0, color=0) ) scalarsymbol.add( dxf.attdef(insert=(0.5, -1.5), tag='VAL2', height=1.0, color=0) ) # add block definition to the drawing dwg.blocks.add(scalarsymbol) nOfScalars = 0 for nID in nodes: x = nodes[nID][0] y = nodes[nID][1] val1 = nodes[nID][2] val2 = nodes[nID][3] values = {} if val2 is None: values = {'VAL1': "%.2f" % val1, 'VAL2': ""} else: if val2 < eps and val2 > -eps: values = {'VAL1': "%.2f" % val1, 'VAL2': ""} else: values = {'VAL1': "%.2f" % val1, 'VAL2': "%.2f" % val2} # define color col = 0 if useMono is True: col = colMono else: if val1 >= 0: col = colPos else: col = colNeg if val1 >= SMin and val1 <= SMax: if val1 < eps and val1 > -eps: continue else: dwg.add(dxf.insert2(blockdef=scalarsymbol, insert=(x, y), attribs=values, xscale=scale, yscale=scale, layer='0', color = col)) nOfScalars += 1 dwg.save() return nOfScalars
def writeCSDXF(fname, nameCS, nodeIDsCS, nodesCS, valuesCS, decFlow, scale, prefix, suffix): from dxfwrite.const import TOP, BOTTOM, LEFT, CENTER, RIGHT dwg = dxf.drawing(fname) # create block for symbol on left side of CS arrowLeft = [(0.0,0.25), (1.0, 0.25), (0.5, 1.1160), (0.0,0.25)] symbolLeft = dxf.block(name='symbolLeft') symbolLeft.add( dxf.solid(arrowLeft, color=0) ) symbolLeft.add( dxf.attdef(insert=(0, -0.25), tag='CS', height=1.0, color=0, halign=LEFT, valign=TOP )) # create block for symbol on right side of CS arrowRight = [(0.0,0.25), (-1.0, 0.25), (-0.5, 1.1160), (0.0,0.25)] symbolRight = dxf.block(name='symbolRight') symbolRight.add( dxf.solid(arrowRight, color=0) ) symbolRight.add( dxf.attdef(insert=(0, -0.25), tag='CS', height=1.0, color=0, halign=RIGHT, valign=TOP )) # create block for symbol on center of CS symbolCenter = dxf.block(name='symbolCenter') symbolCenter.add( dxf.attdef(insert=(0.0, 0.03), tag='MAX', height=0.75, color=0, halign=CENTER, valign=BOTTOM) ) symbolCenter.add( dxf.attdef(insert=(0.0, -0.25), tag='MIN', height=0.75, color=0, halign=CENTER, valign=TOP) ) # add block definitions to the drawing dwg.blocks.add(symbolLeft) dwg.blocks.add(symbolCenter) dwg.blocks.add(symbolRight) for csID in nodeIDsCS: x1 = nodesCS[nodeIDsCS[csID][0]][0] x2 = nodesCS[nodeIDsCS[csID][1]][0] y1 = nodesCS[nodeIDsCS[csID][0]][1] y2 = nodesCS[nodeIDsCS[csID][1]][1] p1 = (x1, y1) p2 = (x2, y2) dx = x2 - x1 dy = y2 - y1 phi = math.atan2(dy,dx)*180.0/math.pi pm = ((x1+x2)/2.0, (y1+y2)/2.0) valMin = valuesCS[csID][0] valMax = valuesCS[csID][1] dwg.add(dxf.polyline((p1, p2))) values = {'CS': "{0}".format(nameCS[csID])} dwg.add(dxf.insert2(blockdef=symbolLeft, insert=p1, attribs=values, xscale=scale, yscale=scale, layer='0', rotation = phi)) dwg.add(dxf.insert2(blockdef=symbolRight, insert=p2, attribs=values, xscale=scale, yscale=scale, layer='0', rotation = phi)) valuesCenter = {'MIN': "{0}%.{1}f{2} (min)".format(prefix, decFlow, suffix) % valMin, 'MAX': "{0}%.{1}f{2} (max)".format(prefix, decFlow, suffix) % valMax} dwg.add(dxf.insert2(blockdef=symbolCenter, insert=pm, attribs=valuesCenter, xscale=scale, yscale=scale, layer='0', rotation = phi)) dwg.save()