def save_traced_scene(self,dxf_file): """ write geometry and traced rays to a DXF file for your viewing pleasure. It's no fun with more than a few 100 rays though. >1000 rays look like sonic the sea urchin.""" drawing = dxf.drawing(dxf_file) drawing.add_layer('Rays', color=3) drawing.add_layer('Geometry', color=5) print "Writing results as DXF file." for res in self.results: rays_origin = res[0] rays_dest = res[1] #draw rays to dxf for (r0,rd) in zip(rays_origin,rays_dest): drawing.add(dxf.face3d([r0[0:3], rd[0:3], rd[0:3]], layer="Rays")) #draw facets (m_v0,m_v1,m_v2)=self.geometry for (t0,t1,t2) in zip(m_v0,m_v1,m_v2): drawing.add(dxf.face3d([t0[0:3], t1[0:3], t2[0:3]], layer="Geometry")) drawing.save()
def export_sections_dxf(self, graph, filename): with self.connect() as con: cur = con.cursor() cur.execute(""" with hole_idx as ( select s.id as section_id, h.id as hole_id from _albion.named_section as s join _albion.hole as h on s.geom && h.geom and st_intersects(s.geom, st_startpoint(h.geom)) ) select st_collectionhomogenize(st_collect(ef.geom)) from albion.all_edge as e join hole_idx as hs on hs.hole_id = e.start_ join hole_idx as he on he.hole_id = e.end_ and he.section_id = hs.section_id join albion.edge_face as ef on ef.start_ = e.start_ and ef.end_ = e.end_ and not st_isempty(ef.geom) where ef.graph_id='{}' """.format(graph)) drawing = dxf.drawing(filename) m = wkb.loads(bytes.fromhex(cur.fetchone()[0])) for p in m: r = p.exterior.coords drawing.add( dxf.face3d( [tuple(r[0]), tuple(r[1]), tuple(r[2])], flags=1)) drawing.save()
def export_elementary_volume_dxf(self, graph_id, cell_ids, outdir, closed_only=False): with self.connect() as con: cur = con.cursor() cur.execute(""" select cell_id, row_number() over(partition by cell_id order by closed desc), geom, closed from ( select cell_id, triangulation as geom, albion.is_closed_volume(triangulation) as closed from albion.volume where cell_id in ({}) and graph_id='{}' ) as t """.format(','.join(["'{}'".format(c) for c in cell_ids]), graph_id)) for cell_id, i, wkb_geom, closed in cur.fetchall(): geom = wkb.loads(bytes.fromhex(wkb_geom)) if closed_only and not closed: continue filename = '{}_{}_{}_{}.dxf'.format( cell_id, graph_id, "closed" if closed else "opened", i) path = os.path.join(outdir, filename) drawing = dxf.drawing(path) for p in geom: r = p.exterior.coords drawing.add( dxf.face3d([tuple(r[0]), tuple(r[1]), tuple(r[2])], flags=1)) drawing.save()
def write_dxf(self,dxf_file): """ writes geometry to a file by using the dxfwrite libs.""" drawing = dxf.drawing(dxf_file) drawing.add_layer('0', color=2) for tri in self.triangles: drawing.add(dxf.face3d([self.vertices[tri[0]][0:3], self.vertices[tri[1]][0:3], self.vertices[tri[2]][0:3]], layer="0")) #drawing.add(dxf.text('Test', insert=(0, 0.2), layer='TEXTLAYER')) drawing.save()
def writeDxf(self, nodeDict, drawing, layerName): numNodes = len(self.nodeIds) if (numNodes == 2): coordsA = nodeDict[self.nodeIds[0]].coords coordsB = nodeDict[self.nodeIds[1]].coords drawing.add( DXFEngine.line((coordsA[0], coordsA[1], coordsA[2]), (coordsB[0], coordsB[1], coordsB[2]), color=0, layer=layerName)) elif (numNodes == 3): coordsA = nodeDict[self.nodeIds[0]].coords coordsB = nodeDict[self.nodeIds[1]].coords coordsC = nodeDict[self.nodeIds[2]].coords drawing.add( DXFEngine.face3d([(coordsA[0], coordsA[1], coordsA[2]), (coordsB[0], coordsB[1], coordsB[2]), (coordsC[0], coordsC[1], coordsC[2])], color=0, layer=layerName)) elif (numNodes == 4): coordsA = nodeDict[self.nodeIds[0]].coords coordsB = nodeDict[self.nodeIds[1]].coords coordsC = nodeDict[self.nodeIds[2]].coords coordsD = nodeDict[self.nodeIds[3]].coords drawing.add( DXFEngine.face3d([(coordsA[0], coordsA[1], coordsA[2]), (coordsB[0], coordsB[1], coordsB[2]), (coordsC[0], coordsC[1], coordsC[2]), (coordsD[0], coordsD[1], coordsD[2])], color=0, layer=layerName)) else: for i in range(0, numNodes - 1): coordsA = nodeDict[self.nodeIds[i]].coords coordsB = nodeDict[self.nodeIds[i + 1]].coords drawing.add( DXFEngine.line((coordsA[0], coordsA[1], coordsA[2]), (coordsB[0], coordsB[1], coordsB[2]), color=0, layer=layerName))
def save_dxf(self,dxf_file): drawing = dxf.drawing(dxf_file) drawing.add_layer('Rays', color=3) #drawing.add_layer('Geometry', color=5) print "Writing ryas to DXF file." for (r0,rd) in zip(self.rays_origin,self.rays_origin+self.rays_dir): #drawing.add(dxf.line(start=r0[0:3], end=rd[0:3],layer="Rays",thickness=1, linetype="SOLID")) drawing.add(dxf.face3d([r0[0:3], rd[0:3], rd[0:3]], layer="Rays")) drawing.save()
def save_traced_scene(self, dxf_file): """ write geometry and traced rays to a DXF file for your viewing pleasure. It's no fun with more than a few 100 rays though. >1000 rays look like sonic the sea urchin.""" drawing = dxf.drawing(dxf_file) drawing.add_layer('Rays', color=3) drawing.add_layer('Geometry', color=5) print "Writing results as DXF file." for res in self.results: rays_origin = res[0] rays_dest = res[1] #draw rays to dxf for (r0, rd) in zip(rays_origin, rays_dest): drawing.add( dxf.face3d([r0[0:3], rd[0:3], rd[0:3]], layer="Rays")) #draw facets (m_v0, m_v1, m_v2) = self.geometry for (t0, t1, t2) in zip(m_v0, m_v1, m_v2): drawing.add( dxf.face3d([t0[0:3], t1[0:3], t2[0:3]], layer="Geometry")) drawing.save()
def writeMeshDXF(fname, nodes, mesh, type): dwg = dxf.drawing(fname) for eID in mesh: p1 = (nodes[mesh[eID][0]][0], nodes[mesh[eID][0]][1], nodes[mesh[eID][0]][2]) p2 = (nodes[mesh[eID][1]][0], nodes[mesh[eID][1]][1], nodes[mesh[eID][1]][2]) p3 = (nodes[mesh[eID][2]][0], nodes[mesh[eID][2]][1], nodes[mesh[eID][2]][2]) if type == 1: dwg.add(dxf.face3d((p1, p2, p3))) elif type == 2: dwg.add(dxf.polyline((p1, p2, p3, p1))) dwg.save()
def export_dxf(self, graph_id, filename): with self.connect() as con: cur = con.cursor() cur.execute(""" select albion.volume_union(st_collectionhomogenize(st_collect(triangulation))) from albion.volume where graph_id='{}' and albion.is_closed_volume(triangulation) and albion.volume_of_geom(triangulation) > 1 """.format(graph_id)) drawing = dxf.drawing(filename) m = wkb.loads(bytes.fromhex(cur.fetchone()[0])) for p in m: r = p.exterior.coords drawing.add( dxf.face3d( [tuple(r[0]), tuple(r[1]), tuple(r[2])], flags=1)) drawing.save()
def cblock(bname,x,y,z,lx,ly,lz): '''This function takes the block name that the faces will be added to, an xyz of the cube itself, and an xyz for location''' # create 3dfaces for cube face1 = dxf.face3d([(lx,ly,lz),(x+lx,ly,lz),(x+lx,y+ly,lz),(lx,y+ly,lz)]) face2 = dxf.face3d([(lx,ly,lz),(x+lx,ly,lz),(x+lx,ly,z+lz),(lx,ly,z+lz)]) face3 = dxf.face3d([(lx,ly,lz),(lx,ly,z+lz),(lx,y+ly,z+lz),(lx,y+ly,lz)]) face4 = dxf.face3d([(x+lx,ly,lz),(x+lx,ly,z+lz),(x+lx,y+ly,z+lz),(x+lx,y+ly,lz)]) face5 = dxf.face3d([(lx,y+ly,z+lz),(lx,ly,z+lz),(x+lx,ly,z+lz),(x+lx,y+ly,z+lz)]) face6 = dxf.face3d([(lx,y+ly,lz),(x+lx,y+ly,lz),(x+lx,y+ly,z+lz),(lx,y+ly,z+lz)]) # add 3dface to block bname.add(face1) bname.add(face2) bname.add(face3) bname.add(face4) bname.add(face5) bname.add(face6)
def main(filein, fileout, width, height, gap): d = dxfgrabber.read(filein) def flipxy(p): return (p[1], p[0]) layers = {} for r in d.entities: if not r.layer in layers: layers[r.layer] = [] layers[r.layer].append(map(flipxy, r.points)) polys_to_cut = [] for (_, ps) in layers.iteritems(): polys_to_cut += map(lambda p: geo.extend_poly(gap, p, False), ps) h = height / 2.0 w = width / 2.0 gndplane = Polygon([(h, w), (-h, w), (-h, -w), (h, -w)]) for p in polys_to_cut: gndplane = gndplane - Polygon(p) # Polygon.triStrip() returns a list of tristrips which need to be # turned into quads, and the list needs to be flattened. layers["GROUND"] = [ quad for quads in map( geo.tristrip_to_quads, gndplane.triStrip()) for quad in quads] drawing = DXFEngine.drawing(fileout) for (l, qs) in layers.iteritems(): for q in qs: drawing.add(DXFEngine.face3d(map(flipxy, q), layer=l)) drawing.save()
def export_elementary_volume_dxf(self, graph_id, cell_id, outdir, closed): with self.connect() as con: closed_sql = "" if not closed: closed_sql = "not" cur = con.cursor() cur.execute(""" select geom from albion.dynamic_volume where cell_id='{}' and graph_id='{}' and {} albion.is_closed_volume(geom) """.format(cell_id, graph_id, closed_sql)) status = "opened" if closed: status = "closed" i = 0 for wkb_geom in cur.fetchall(): geom = wkb.loads(bytes.fromhex(wkb_geom[0])) filename = '{}_{}_{}_{}.dxf'.format(cell_id, graph_id, status, i) path = os.path.join(outdir, filename) drawing = dxf.drawing(path) for p in geom: r = p.exterior.coords drawing.add( dxf.face3d([tuple(r[0]), tuple(r[1]), tuple(r[2])], flags=1)) drawing.save() i += 1
def room_to_3d_solids(fp, room_id): # prepare the array to return solids_arr = [] # compute the oriented boundary boundary_list = fp.compute_room_oriented_boundary(room_id) # iterate over each boundary, making walls for bi in range(len(boundary_list)): # iterate over the edges of this boundary for ei in range(len(boundary_list[bi])): # get the indices of the two verts of this wall v1 = boundary_list[bi][ei-1] v2 = boundary_list[bi][ei] # prepare the next wall as a solid points = [( fp.verts[v1][0], \ fp.verts[v1][1], \ fp.room_min_z[room_id] ), \ ( fp.verts[v1][0], \ fp.verts[v1][1], \ fp.room_max_z[room_id] ), \ ( fp.verts[v2][0], \ fp.verts[v2][1], \ fp.room_max_z[room_id] ), \ ( fp.verts[v2][0], \ fp.verts[v2][1], \ fp.room_min_z[room_id] )] wall = dxf.face3d(points) solids_arr.append(wall) # return the final list return solids_arr
def cblock(bname,x,y,z,lx,ly,lz): # create a block board1 = dxf.block(name=bname) drawing.blocks.add(board1) # create 3dfaces for cube face1 = dxf.face3d([(0,0,0),(x,0,0),(x,y,0),(0,y,0)]) face2 = dxf.face3d([(0,0,0),(x,0,0),(x,0,z),(0,0,z)]) face3 = dxf.face3d([(0,0,0),(0,0,z),(0,y,z),(0,y,0)]) face4 = dxf.face3d([(x,0,0),(x,0,z),(x,y,z),(x,y,0)]) face5 = dxf.face3d([(0,y,z),(0,0,z),(x,0,z),(x,y,z)]) face6 = dxf.face3d([(0,y,0),(x,y,0),(x,y,z),(0,y,z)]) # add 3dface to block board1.add(face1) board1.add(face2) board1.add(face3) board1.add(face4) board1.add(face5) board1.add(face6) # insert the block-create ref, then add to drawing blockref = dxf.insert(blockname=bname, insert=(lx,ly,lz)) drawing.add(blockref)
def render_mesh(self, drawing): for t in self.data.get("triangles"): f = dxf.face3d( [self.convert_point(t.get(x)) for x in ["a", "b", "c"]], layer="Mesh") drawing.add(f)
cur.execute(""" insert into albion.section(id, triangulation, graph_id, grid_id) select _albion.unique_id()::varchar, st_collectionhomogenize(st_collect(albion.triangulate_edge(ceil_, wall_))), graph_id, grid_id from albion.edge where graph_id='{}' group by graph_id, grid_id """.format(sys.argv[2])) cur.execute(""" select st_collectionhomogenize(st_collect(triangulation)) from albion.section where graph_id='{}' """.format(sys.argv[2])) drawing = dxf.drawing(sys.argv[3] + '.dxf') m = wkb.loads(bytes.fromhex(cur.fetchone()[0])) for p in m: r = p.exterior.coords drawing.add(dxf.face3d([tuple(r[0]), tuple(r[1]), tuple(r[2])], flags=1)) drawing.save() cur.execute(""" select albion.to_obj(st_collectionhomogenize(st_collect(triangulation))) from albion.section where graph_id='{}' """.format(sys.argv[2])) open(sys.argv[3] + '.obj', 'w').write(cur.fetchone()[0])
#!/usr/bin/python from dxfwrite import DXFEngine as dxf # set up the basic drawing drawing = dxf.drawing('basic.dxf') # create a block board1 = dxf.block(name="board") drawing.blocks.add(board1) # create 3dfaces for cube face1 = dxf.face3d([(0,0,0),(100,0,0),(100,100,0),(0,100,0)]) face2 = dxf.face3d([(0,0,0),(100,0,0),(100,0,100),(0,0,100)]) face3 = dxf.face3d([(0,0,0),(0,0,100),(0,100,100),(0,100,0)]) face4 = dxf.face3d([(100,0,0),(100,0,100),(100,100,100),(100,100,0)]) face5 = dxf.face3d([(0,0,100),(100,0,100),(100,100,100),(0,100,100)]) face6 = dxf.face3d([(0,100,0),(100,100,0),(100,100,100),(0,100,100)]) # add 3dface to block board1.add(face1) board1.add(face2) board1.add(face3) board1.add(face4) board1.add(face5) board1.add(face6) # insert the block-create ref, then add to drawing blockref = dxf.insert(blockname='board', insert=(0,0,0)) drawing.add(blockref)
def to_face3d(self): from dxfwrite import DXFEngine as dxf return dxf.face3d(points=self.points, **self.kwargs)
drawing.add(dxf.circle(center=(5, 0), radius=5)) # add an ARC-entity drawing.add(dxf.arc(center=(5, 0), radius=4, startangle=30, endangle=150)) #add a POINT-entity drawing.add(dxf.point(point=(1, 1))) # add a SOLID-entity with 4 points drawing.add(dxf.solid([(0, 0), (1, 0), (1, 1), (0, 1)], color=2)) # add a SOLID-entity with 3 points drawing.add(dxf.solid([(0, 1), (1, 1), (1, 2)], color=3)) # add a 3DFACE-entity drawing.add(dxf.face3d([(5, 5), (6, 5), (6, 6), (5, 6)], color=3)) # add a Trace-entity drawing.add(dxf.trace([(7, 5), (8, 5), (8, 6), (7, 6)], color=4)) # add a TEXT-entity drawing.add(dxf.text("Manfred")) # add a TEXT-entity with more properties drawing.add( dxf.text( text="mozman", style="ISOCPEUR", height=0.7, oblique=15, color=5,
drawing.add(dxf.circle(center=(5,0), radius=5)) # add an ARC-entity drawing.add(dxf.arc(center=(5,0), radius=4, startangle=30, endangle=150)) #add a POINT-entity drawing.add(dxf.point(point=(1,1))) # add a SOLID-entity with 4 points drawing.add(dxf.solid([(0,0), (1,0), (1,1), (0,1)], color=2)) # add a SOLID-entity with 3 points drawing.add(dxf.solid([(0,1), (1,1), (1,2)], color=3)) # add a 3DFACE-entity drawing.add(dxf.face3d([(5,5), (6,5), (6,6), (5,6)], color=3)) # add a Trace-entity drawing.add(dxf.trace([(7,5), (8,5), (8,6), (7,6)], color=4)) # add a TEXT-entity drawing.add(dxf.text("Manfred")) # add a TEXT-entity with more properties drawing.add(dxf.text( text="mozman", style="ISOCPEUR", height=0.7, oblique=15, color=5, insert=(0,5),