def test_context_manager(filename): with pytest.raises(ValueError): with r12writer(filename) as dxf: dxf.add_line((0, 0), (17, 23)) raise ValueError() dwg = ezdxf.readfile(filename) entities = list(dwg.modelspace()) assert len(entities) == 1 assert entities[0].dxftype() == 'LINE'
def saveDXF(self, outFileNameDXF): try: with r12writer(outFileNameDXF) as dxf: for i, circle in enumerate(self.circles): pointsNum = int(self.outputPointsNum.get()) centre = circle[0] radius = circle[1] x = centre[0] x1 = x + radius x2 = x - radius y = centre[1] z = centre[2] arc = 2 * pi / pointsNum # Draw the circle if self.outputDXFCircle.get(): dxf.add_circle(centre, radius=radius, layer="Circle"+str(i)) # Draw the diameter line if self.outputDXFDiameter.get(): dxf.add_line((x1, y, z), (x2, y, z), layer="Circle"+str(i)) # Draw the diameter label if self.outputDXFLabel.get(): diameter = radius * 2.0 # polylabel gives the radius of the circle, we want the diameter lineCentre = [(x2-x1)/2.0 + x1, y + 0.2, z] # Centre of the line with a slight offset dxf.add_text(f"{diameter:.2f}", lineCentre, align="CENTER", layer="Circle"+str(i)) # Draw the points approximating circle if self.outputDXFPoints.get(): # For each circle calculate outputPointsNum number of points around it for j in range(pointsNum): angle = arc * j currX = x + radius*cos(angle) currY = y + radius*sin(angle) dxf.add_point((currX, currY, z), layer="Circle"+str(i)) # Draw the polylines approximating circle if self.outputDXFPolyLines.get(): # For each circle calculate outputPointsNum number of points around it points = [(x+radius*cos(arc*j), y+radius*sin(arc*j), z) for j in range(pointsNum)] points.append(points[0]) dxf.add_polyline(points, layer="Circle"+str(i)) except OSError: messagebox.showerror(title="Error", message=f"Could not write to output file: {outFileNameDXF}") return 1 return 0
def test_write_r12(filename): with r12writer(filename) as dxf: dxf.add_line((0, 0), (17, 23)) dxf.add_arc((0, 0), radius=3, start=0, end=175) dxf.add_solid([(0, 0), (1, 0), (0, 1), (1, 1)]) dxf.add_point((1.5, 1.5)) dxf.add_polyline([(5, 5), (7, 3), (7, 6)]) # 2d polyline dxf.add_polyline([(4, 3, 2), (8, 5, 0), (2, 4, 9)]) # 3d polyline dxf.add_text("test the text entity", align="MIDDLE_CENTER") for i in range(CIRCLE_COUNT): dxf.add_circle((MAX_X_COORD*random(), MAX_Y_COORD*random()), radius=2) assert os.path.exists(filename)
# Copyright (c) 2018 Manfred Moitzi # License: MIT License from random import random from ezdxf.r12writer import r12writer MAX_X_COORD = 1000.0 MAX_Y_COORD = 1000.0 CIRCLE_COUNT = 10000 with r12writer("quick_and_dirty_dxf_r12.dxf") as dxf: dxf.add_line((0, 0), (17, 23)) dxf.add_circle((0, 0), radius=2) dxf.add_arc((0, 0), radius=3, start=0, end=175) dxf.add_solid([(0, 0), (1, 0), (0, 1), (1, 1)]) dxf.add_point((1.5, 1.5)) dxf.add_polyline([(5, 5), (7, 3), (7, 6)]) # 2d polyline dxf.add_polyline([(4, 3, 2), (8, 5, 0), (2, 4, 9)]) # 3d polyline dxf.add_text("test the text entity", align="MIDDLE_CENTER") with r12writer("many_circles.dxf") as dxf: for i in range(CIRCLE_COUNT): dxf.add_circle((MAX_X_COORD * random(), MAX_Y_COORD * random()), radius=2) LINETYPES = [ 'CONTINUOUS', 'CENTER', 'CENTERX2', 'CENTER2', 'DASHED', 'DASHEDX2',
def main(in_path, out_path): fixed = fixed_locations(ezdxf.readfile(in_path)) with r12writer(out_path) as dxf: for point in fixed: dxf.add_point(point)
circ_i = (rb + thk / 2) * 2 * pi #inner circumference becomes the inner arc length circ_o = (rt + thk / 2) * 2 * pi #outer circumference becomes the outer arc length ang_flat.append( (circ_o - circ_i) / dr) #the face width does not change from flat arc to bent cone r_i_flat.append( circ_i / ang_flat[-1] ) #flat arc radius can be calculated from the included angle and the arc length r_o_flat.append(circ_o / ang_flat[-1]) #same for outer #-----------now lets make some useful outputs! #code to write a dxf flat pattern cut file with r12writer("arcs.dxf") as dxf: r_prev = 0 #initialize a helper variable to remember the radius of the next largest ring left_end = 0 #initialize a helper variable to remember the leftmost point of the series of arcs in the layout for ri, ro, a in zip( r_i_flat, r_o_flat, ang_flat ): #again, this is stepping through the rings from largest to smallest #first we do a little math to space the rings nicely together in the flat pattern hx = ro * sin( a / 2 ) #the height of the corner where this ring comes closest to the next largest ring on the flat pattern if r_prev == 0: #don't try to take the square root of a negative number! xx = 0 else: #a horizontal distance from the previous arc's center to the outer corner overlap point x_prev = sqrt(r_prev * r_prev - hx * hx) #the new horizontal position of this arc's center
def worker(): # read json + reply print 'hi.....inside Python' data = request.get_json(force=True) result = '' print type(data) result = json.dumps(data) #print result result = json.loads(result) print type(result) #check whether the feature data is a linestring or point type numFeats = len(result['features']) if (numFeats > 0): geometryType = result['features'][0]['geometry']['type'] print geometryType, type(geometryType) #if type is linestring, run the linestring code; if (str(geometryType) == 'LineString'): print "hello" AttribInfo = result['features'][0]['properties'] Attribs = AttribInfo.keys() #print AttribInfo #line information/coordinates only, copied from writeLinestring.py with r12writer("write_Lines.dxf") as dxf: #where is this file being written? for i in range(numFeats): a_linefeature = result['features'][i]['geometry'][ 'coordinates'] dxf.add_polyline(a_linefeature) strDict = result['features'][i]['properties'] strDict = json.dumps(strDict) strDict = strDict.encode('ascii', 'replace') dxf.add_text(strDict[1:-1], insert=(a_linefeature[0][0], a_linefeature[0][1])) #dwg = ezdxf.readfile('write_Lines.dxf') dwg = io.open('write_Lines.dxf', mode='rt') outputText = dwg.read() elif (str(geometryType) == 'Point'): #copied from writeDXF_StrInput.py dwg = ezdxf.new('R2010') flag = dwg.blocks.new(name='FLAG') msp = dwg.modelspace() flag.add_point((0, 0), dxfattribs={'color': 2}) AttribInfo = result['features'][0]['properties'] Attribs = AttribInfo.keys() #add attribute definitions x = 0.5 y = -1 for i in Attribs: property = i flag.add_attdef(str(property), (x, y), { 'height': 0.5, 'color': 3 }) y = y - 1.5 # Now I can populate the drawing object and create the dxf file # get each coordinate # get the attribute information and store with each point points = [] x = 0.5 for i in range(numFeats): a_point = result['features'][i]['geometry']['coordinates'] att_info_dict = result['features'][i]['properties'] points.append( a_point ) #a list of lists, each coordinate is of type float msp.add_auto_blockref('FLAG', a_point, att_info_dict) dwg.saveas('write_point_att1.dxf') dwgT = io.open('write_point_att1.dxf', mode='rt') outputText = dwgT.read() else: outputText = '' output = outputText return output
#coding:utf-8 # Author: mozman # Purpose: test writing r12writer - reading ezdxf # Created: 31.04.2016 # Copyright (C) 2016, Manfred Moitzi # License: MIT License from random import random from ezdxf.r12writer import r12writer FILE = "r12writer.dxf" MAX_X_COORD = 1000.0 MAX_Y_COORD = 1000.0 CIRCLE_COUNT = 999 print("-"*20) with r12writer(FILE) as dxf: dxf.add_line((0, 0), (17, 23)) dxf.add_arc((0, 0), radius=3, start=0, end=175) dxf.add_solid([(0, 0), (1, 0), (0, 1), (1, 1)]) dxf.add_point((1.5, 1.5)) dxf.add_polyline([(5, 5), (7, 3), (7, 6)]) # 2d polyline dxf.add_polyline([(4, 3, 2), (8, 5, 0), (2, 4, 9)]) # 3d polyline dxf.add_text("test the text entity", align="MIDDLE_CENTER") for i in range(CIRCLE_COUNT): dxf.add_circle((MAX_X_COORD*random(), MAX_Y_COORD*random()), radius=2) print("r12writer writes {} circles.".format(CIRCLE_COUNT)) import ezdxf dwg = ezdxf.readfile(FILE)