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 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 writeVectorDXF(nodes, VMin, VMax, eps, scale, fname): colMono = 7 arrow = [(1.0,0.0),(0.6,-0.1),(0.6,0.1),(1.0,0.0)] arrowline = [(0.0,0.0),(0.6,0.0)] dwg = dxf.drawing(fname) # create block vectorsymbol = dxf.block(name='vector') vectorsymbol.add( dxf.polyline(arrow, color=0) ) vectorsymbol.add( dxf.polyline(arrowline, color=0) ) # add block definition to the drawing dwg.blocks.add(vectorsymbol) nOfVectors = 0 for nID in nodes: x = nodes[nID][0] y = nodes[nID][1] u = nodes[nID][2] v = nodes[nID][3] velocity = (u**2.0 + v**2.0)**(1.0/2.0) phi = math.atan2(v,u)*180.0/math.pi # write block, if value is inside a given band blockScale = scale * velocity if velocity <= VMax and velocity >= VMin: if velocity < eps and velocity > -eps: continue else: dwg.add(dxf.insert2(blockdef=vectorsymbol, insert= (x, y), xscale=blockScale, yscale=blockScale, layer='0', color = colMono, rotation = phi)) nOfVectors += 1 dwg.save() return nOfVectors
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], 'YPOS': "y = %.3f" % point[1] } randomscale = 0.5 + random.random() * 2.0 dwg.add( dxf.insert2(blockdef=flag, insert=point, attribs=values, xscale=randomscale, yscale=randomscale, layer='FLAGS', rotation=-15)) number += 1 dwg.save() print("drawing '%s' created.\n" % filename)
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], 'YPOS': "y = %.3f" % point[1] } randomscale = 0.5 + random.random() * 2.0 dwg.add(dxf.insert2(blockdef=flag, insert=point, attribs=values, xscale=randomscale, yscale=randomscale, layer='FLAGS', rotation=-15)) number += 1 dwg.save() print("drawing '%s' created.\n" % filename)
def render(self): data = self.data point_layers = {} # create the drawing and add layers to it drawing = dxf.drawing() drawing.add_layer(self.filter_layer_name('objects')) drawing.add_layer(self.filter_layer_name('marker')) drawing.add_layer(self.filter_layer_name('Mesh')) # Add layers from the doncodes for l in set([ self.doncodes["doncodes"][lx]["CAD LAYERS"] for lx in self.doncodes["doncodes"] ]): if l: drawing.add_layer(self.filter_layer_name(l)) # might need custom layers for major/minor contours have_contour_names = [] for x in ["major", "minor"]: if self.settings["job"].get("contours") and self.settings["job"][ "contours"][x] and self.settings["job"]["contours"][x].get( "layer"): drawing.add_layer( self.filter_layer_name( "Contour - " + str(self.settings["job"]["contours"][x].get("layer")))) have_contour_names.append(x) if len(have_contour_names) < 2: # drawing.add_layer(self.filter_layer_name('Contour')) drawing.add_layer(self.filter_layer_name('Contour Splines')) drawing.add_layer(self.filter_layer_name('Contour Splines Flat')) # add the separated codes/shots/rls for x in ["Shots", "Rls", "Codes"]: for y in ["", "_NC", "_NL"]: drawing.add_layer(self.filter_layer_name(x + y)) #drawing.header['$ANGBASE'] = self.bm["marker_angle"] #drawing.header['$UCSORG'] = (0, 0, 0) #drawing.header['$UCSXDIR'] = (1, 0, 0) #drawing.header['$UCSYDIR'] = (0, 1, 0) # set 3D Point #drawing.header['$EXTMIN'] = (0, 0, -10) #drawing.header['$EXTMAX'] = (1000, 1000, 100) # add the mesh if self.settings["options"]["threedMesh"] == "1": self.render_mesh(drawing) #add stringlines to string_lines layer for line in data["stringlines"]: if len(data["stringlines"][line]) > 1: # variable to store stringline colour, thickness etc. to be passed to polyline stringline_detail = {} first_point = data["stringlines"][line][0] ms_layer = first_point["doncode"].get("MS Properties") layer_codes = self.doncodes["ms_layers"].get(ms_layer) # make sure we have stringline properties in our doncodes array (often blank) if layer_codes: # print data["stringlines"][line][0]["code"], ms_layer # print first_point["code"], layer_codes # if we have a special colour if layer_codes.get("Col"): stringline_detail["color"] = int(layer_codes["Col"]) # if we have a line weight set if layer_codes.get("Line Weight") and int( layer_codes.get("Line Weight")): stringline_detail["thickness"] = int( layer_codes.get("Line Weight")) # if we have a line style TODO: map these to their actual line styles if layer_codes.get("Line Style") and int( layer_codes.get("Line Style")): stringline_detail["linetype"] = int( layer_codes.get("Line Style")) polyline = dxf.polyline(layer=self.filter_layer_name( first_point["doncode"].get("CAD LAYERS")), **stringline_detail) polyline.add_vertices([ self.convert_point(p["point"]) for p in data["stringlines"][line] ]) drawing.add(polyline) # add the regular (original) contour line if self.settings["options"]["origContour"] == "1": self.render_contour(drawing, data["contour_lines"], lambda p: self.convert_point( p)) # Line was previously commented. # add splines contour line if self.settings["options"]["splinesContour"] == "1": # splines version self.render_contour(drawing, data["contour_splines"], lambda p: self.convert_point(p), " Splines") # flat spline version self.render_contour( drawing, data["contour_splines"], lambda p: self.flatten_point(self.convert_point(p)), " Splines Flat") # loop through the points with 2d objects on them putting Xrefs on the ones that need it #print data["objects_2d"] for o in data["objects_2d"]: # //bush.position.set(objects_2d[o][0],objects_2d[o][1]+0.3,objects_2d[o][2]).multiplyScalar(psu_renderer.SCALE); # psu_renderer.generate_obj(bush.clone()); drawing.add_xref(self.settings["global"].get("export", {}).get( "xref_path", "") + o["code"] + ".dwg", insert=self.convert_point(o["point"]), layer=self.filter_layer_name( o["doncode"].get("CAD LAYERS"))) # find and add trees to plot and tree schedule # create the table for tree information tree_table = dxf.table(insert=self.convert_point( [self.bounding_box[0][1], 0, self.bounding_box[2][0] - 20]), nrows=len(data["trees"]) + 2, ncols=4) tree_table.set_col_width(1, 4) tree_table.set_col_width(2, 3) tree_table.set_col_width(3, 5) # table cell style ctext = tree_table.new_cell_style('ctext', textcolor=7, textheight=0.5, halign=dxfwrite.CENTER, valign=dxfwrite.MIDDLE) # table border style border = tree_table.new_border_style(color=6, priority=51) ctext.set_border_style(border, right=False) # table header hcell = tree_table.text_cell(0, 0, "SCHEDULE OF TREES") hcell.span = (1, 4) tree_table.text_cell(1, 1, "DIAMETER", style='ctext') tree_table.text_cell(1, 2, "HEIGHT", style='ctext') tree_table.text_cell(1, 3, "TYPE", style='ctext') for t in range(len(data["trees"])): tree = data["trees"][t] diameter = tree["tree_details"].get("diameter", None) height = tree["tree_details"].get("height", None) spread = tree["tree_details"].get("spread", None) note = tree["tree_details"].get("note", None) blockname = "tree_" + str(t + 1) layer = self.filter_layer_name( self.doncodes["doncodes"][tree["code"]].get("CAD LAYERS")) if diameter or spread: tree_block = dxf.block(name=blockname) # trunk if diameter: tree_block.add( dxf.circle(float(diameter) / 2.0, [0, 0], layer=layer)) # leaves if spread: tree_block.add( dxf.circle(float(spread) / 2.0, [0, 0], layer=layer)) drawing.blocks.add(tree_block) drawing.add( dxf.insert2(blockdef=tree_block, insert=self.convert_point(tree["position"]), layer=layer)) # table # table position is left of bounding box tree_table.text_cell(t + 2, 0, "T" + str(t + 1), style='ctext') tree_table.text_cell(t + 2, 1, diameter and diameter or "", style='ctext') tree_table.text_cell(t + 2, 2, (height and height + "m" or ""), style='ctext') tree_table.text_cell(t + 2, 3, note and note.upper() or "", style='ctext') drawing.add(tree_table) if not self.bm.get("fake"): drawing.add( dxf.point(self.convert_point(self.bm["position"]), layer=self.filter_layer_name( self.doncodes["doncodes"][self.bm["code"]].get( "CAD LAYERS")))) drawing.add( dxf.text("BM", self.convert_point(self.bm["position"]), height=0.2, layer=self.filter_layer_name( self.doncodes["doncodes"][self.bm["code"]].get( "CAD LAYERS")))) # loop through the points adding labels to the layer seen = [] for p in data["p"]: # uniquely identify this piece of text at this position to prevent duplicates text_id = tuple( [p["original_code"], p.get("day_id", "")] + p["position"]) # figure out if we are NL or layer_postfix = "" for l in ["NL", "NC"]: if l in p["extensions"]["function"]: layer_postfix = "_" + l # if this isn't a duplicate and isn't a tree if not text_id in seen or p["code"] == "TR": blockname = 'point_' + str(p["point_id"]) + ( p.get("day_id") and "-" + p["day_id"] or "") point_block = dxf.block(name=blockname) if p.get("force_point_layer"): layer_code = p["force_point_layer"]["code"] else: layer_code = p["code"] layer = self.filter_layer_name( self.doncodes["doncodes"][layer_code].get("CAD LAYERS")) # create a block to unite the various elements of the point # for trees we just want to draw their ref if p["code"] == "TR": point_block.add( dxf.text( "T" + str([t["point_id"] for t in data["trees"]].index(p["point_id"]) + 1), [0.2, 0.2], height=0.2, layer="Codes" + layer_postfix)) else: # these codes don't have the original_code text printed on the plot if not p["code"] in ["RC", "LC"]: # everything else gets original code printed point_block.add( dxf.text(p["original_code"] + " (" + str(p["line_number"]) + ")", (0, 0), alignpoint=(0, -0.2), height=0.05, rotation=-90, layer="Codes" + layer_postfix, valign=MIDDLE)) if not "NL" in p["extensions"]["function"]: point_block.add( dxf.text("%.2f" % (p["position"][1]), (0, 0), alignpoint=(0.12, -0.12), height=0.3, rotation=-45, layer="Rls" + layer_postfix, valign=MIDDLE)) point_block.add( dxf.point([0, 0], layer="Shots" + layer_postfix)) drawing.blocks.add(point_block) drawing.add( dxf.insert2(blockdef=point_block, insert=self.convert_point(p["position"]), layer=layer)) # print p["doncode"].get("Draw Cross Hair on Point") if self.doncodes["doncodes"][p["code"]].get( 'Draw Cross Hair on Point', 'n').lower() in ["1", "y", "yes", "true", "x"]: drawing.add_xref(self.settings["global"].get( "export", {}).get("xref_path", "") + "X.dwg", insert=self.convert_point(p["position"]), layer="Shots" + layer_postfix) seen.append(text_id) # save the drawing to the binary blob and then return the DXF code # binary blob to pretend to be a file and store our saved DXF blob = StringIO() drawing.save_to_fileobj(blob) return blob.getvalue()
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()