def main(): source_dwg = ezdxf.readfile('CustomBlocks.dxf') target_dwg = ezdxf.new(source_dwg.dxfversion) importer = ezdxf.Importer(source_dwg, target_dwg) importer.import_blocks(query='CustomBlock1') importer.import_modelspace_entities() target_dwg.saveas("CustomBlocks_Import.dxf")
def import_and_save(dxf, target_dxf): try: source_dwg = ezdxf.readfile(dxf) except ezdxf.DXFStructureError: print('ФАЙЛ %s НЕ БЫЛ ДОБАВЛЕН!' % dxf) print('УДАЛИТЕ ЕГО И ПЕРЕКОНВЕРТИРУЙТЕ ЗАНОВО!') return importer = ezdxf.Importer(source_dwg, target_dxf) importer.import_all() target_dxf.save()
def test_block_import(filename, tmpdir): source_dwg = ezdxf.readfile(filename) target_dwg = ezdxf.new(source_dwg.dxfversion) importer = ezdxf.Importer(source_dwg, target_dwg) importer.import_blocks(query='CustomBlock1') importer.import_modelspace_entities() filename = str(tmpdir.join('custom_blocks_import.dxf')) try: target_dwg.saveas(filename) except ezdxf.DXFError as e: pytest.fail("DXFError: {0} for DXF version {1}".format( str(e), target_dwg.dxfversion)) assert os.path.exists(filename)
def joinDXF(boxid): target_drawing = ezdxf.new(dxfversion='AC1024') print target_drawing.dxfversion target_filename = outdir + "/boxmaze_" + str(boxid) + ".dxf" FILES = ["pieces.dxf", "maze.dxf", "boxmaze.dxf", "id_numbers.dxf"] for file in FILES: filename = dxfdir + file print "processing " + filename dxf = ezdxf.readfile(filename) importer = ezdxf.Importer(dxf, target_drawing) importer.import_all(table_conflict='discard', block_conflict='discard') target_drawing.saveas(target_filename)
def generate_dxf(download_folder, dxf_filename, form_web, version): """ This function generates a dxf file. """ try: if not get_errors_upload_topographical_file() and not errors_square( ) and (not errors_rectangle()): file_user = get_dxf_configuration(form_web) dwg = ezdxf.new(version) # Create the model space. msp = dwg.modelspace() if get_symbols(): source_drawing = ezdxf.readfile(get_symbols_dxf_file()) importer = ezdxf.Importer(source_drawing, dwg) importer.import_blocks() # Adding symbols to model. insert_symbols(msp, get_points(), file_user) # Creating required layers. create_layers(dwg, file_user) # Adding points to model. create_points(dwg, msp, get_points()) # Adding circles to model. if get_circles(): create_circles(msp, get_circles(), file_user) # Adding lines to model. if get_lines(): create_lines(msp, get_lines(), file_user) # Adding curves to model. if get_curves(): create_curves(msp, get_curves(), file_user) # Adding squares to model. if get_squares(): create_squares(msp, get_squares(), file_user) # Adding rectangles to model. if get_rectangles(): create_rectangles(msp, get_rectangles(), file_user) if dwg.saveas(download_folder + '/' + dxf_filename) is None: return True except (IOError, NameError) as e: return False
# if not present, AutoCAD would not open the file... my_line_types = [ ("DASHSPACE", "Dashspace - - - - - - - - - - - - - - - -", [10.0, 5.0, -5.0]), ("DOT", "Dot . . . . . . . . . . . . . . . . . . . ", [5.0, 0.0, -5.0]), ] for name, desc, pattern in my_line_types: if name not in dwg.linetypes: dwg.linetypes.new(name=name, dxfattribs={ 'description': desc, 'pattern': pattern }) # copy board from the original DXF and paste it into the new one importer = ezdxf.Importer(original_dwg, dwg) importer.import_blocks(query=block_label, conflict='discard') msp.add_blockref(block_label, (0, 0), dxfattribs={ 'xscale': 1., 'yscale': 1., 'rotation': -90. }) # print some info for debug block = dwg.blocks.get( block_label) # get all INSERT entities with entity.dxf.name == "Part12" print block.name for e in block: if e.dxftype() == 'LINE': print e.dxf.color, e.dxf.linetype, e.dxftype(), e.dxf.start, e.dxf.end
_a,_b = getLineDef(str_lo_x, str_lo_y, str_hi_x, str_hi_y) str_line_def.append([_a, _b]) if (debug and not (i/nStr)%2==0): msp.add_lwpolyline([(segm_def[0][0], _a*segm_def[0][0]+_b), (segm_def[-1][0], _a*segm_def[-1][0]+_b)], dxfattribs={'layer': strip_layer}) # -------------------------------------------------------------- # Place connectors, routing and some simple # rectangles to designate VFAT, OPTO and needed clearance # -------------------------------------------------------------- # import connector footprint conn_name = 'Hirose' conn_dwg = ezdxf.readfile("in/HIROSE_FX10A_140S_14_SV.dxf") conn_importer = ezdxf.Importer(conn_dwg, dwg) conn_importer.import_blocks(query=conn_name, conflict='discard') # draw rectangles around the connectors denoting the VFAT+Flex and a 5mm buffer zone vfat = dwg.blocks.new(name='VFAT') vfat.add_lwpolyline([(26,-6.5, trc_width, trc_width), (26,66.5, trc_width, trc_width), (-26,66.5, trc_width, trc_width), (-26,-6.5, trc_width, trc_width), (26,-6.5, trc_width, trc_width)]) _buff = 5 vfat.add_lwpolyline([(26+_buff,-6.5-_buff, trc_width, trc_width), (26+_buff,66.5+_buff, trc_width, trc_width), (-26-_buff,66.5+_buff, trc_width, trc_width), (-26-_buff,-6.5-_buff, trc_width, trc_width),
pass dxflist = list( filter(lambda checkdxffiles: checkdxffiles.lower().find('.dxf') != -1, checkdxffiles)) #if len(tklist) !=1 : if not os.path.exists("tk.dxf"): print(r"下缺少图框文件或者有多个图框文件,需要把图框复制到tk文件夹下") elif len(dxflist) == 0: print(r".\dxf 下缺少dxf文件,需要把dxf复制到DXF文件夹下") else: for foot, dirs, checkdxffiles in os.walk('.\\DXF\\'): pass tkdxffiles = list( filter(lambda checkdxffiles: checkdxffiles.lower().find('.dxf') != -1, checkdxffiles)) for file in tkdxffiles: target_drawing = ezdxf.readfile('.\\tk\\tk.dxf') source_drawing = ezdxf.readfile('.\\dxf\\' + file) print('正在处理文件:' + file) modelspace = target_drawing.modelspace() for e in modelspace: if e.dxftype() == 'TEXT': text = e.dxf.text a, p1, p2 = e.get_pos() importer = ezdxf.Importer(source_drawing, target_drawing) importer.import_all() target_drawing.saveas('.\\dwg\\' + file) print("处理完毕!")
# if not present, AutoCAD would not open the file... my_line_types = [ ("DASHSPACE", "Dashspace - - - - - - - - - - - - - - - -", [10.0, 5.0, -5.0]), ("DOT", "Dot . . . . . . . . . . . . . . . . . . . ", [5.0, 0.0, -5.0]), ] for name, desc, pattern in my_line_types: if name not in dwg.linetypes: dwg.linetypes.new(name=name, dxfattribs={ 'description': desc, 'pattern': pattern }) # copy board from the original DXF and paste it into the new one importer = ezdxf.Importer(original_dwg, dwg) importer.import_blocks(query=block_label, conflict='discard') board_rotation = 180. if (board != "ME0"): board_rotation = -90. msp.add_blockref(block_label, (0, 0), dxfattribs={ 'xscale': 1., 'yscale': 1., 'rotation': board_rotation }) if (board != "ME0"): chimney_dwg = ezdxf.readfile("in/" + board + "_chimney.dxf") importer = ezdxf.Importer(chimney_dwg, dwg) importer.import_blocks(query=chimney_label, conflict='discard')
def drawSlot(slot): debug = False board = "GE21_M1" if (slot < 5): board = "GE21_M4" block_label = "" if (board == "GE21_M1"): block_label = "BLOCK385" elif (board == "GE21_M4"): block_label = "BLOCK80" # handles for finding lines on the drawing active_area_color = 150 # blue, 50 - yellow, 82 - green, 222 - purple, 10 - red, 40 - orange chamber_cover_color = 1 segmentation_color = 3 nConnPerRow = 6 via_radius = 0.3 nStripsPerConn = 128 if (slot == 3 or slot == 8 or slot == 12): nStripsPerConn = nStripsPerConn / 2 gap = 0.2 if (slot == 2 or slot == 6): gap = 0.3 elif (slot == 7): gap = 0.4 # define segment by the highest point in Y segm_def = [] if (board == "GE21_M1"): segm_def = [[1365.5, 244.462221 + gap], [1561.0, 279.4608 + gap], [1756.5, 314.462022 + gap]] elif (board == "GE21_M4"): segm_def = [[2726.0, 488.029303 + gap], [2962.0, 530.28 + gap], [3198.0, 572.530342 + gap]] # adjust segments for slots with half strips if (slot == 3 or slot == 8 or slot == 9 or slot == 12): _a, _b = getLineDef(segm_def[0][0], segm_def[0][1], segm_def[-1][0], segm_def[-1][1]) _mid = (segm_def[0][0] + segm_def[1][0]) / 2 segm_def.insert(1, [_mid, _a * _mid + _b]) # don't draw the full drawing for each slot if (slot == 3 or slot == 4 or slot > 7): segm_def = segm_def[:-1] else: segm_def = segm_def[1:] nEtaSegm = len(segm_def) - 1 # specify where to place the vias in each segment via_row_x = [] if (slot == 1 or slot == 2): via_row_x = [segm_def[-1][0] - 80] elif (slot == 3 or slot == 8 or slot == 9): via_row_x = [segm_def[1][0] - 1, segm_def[1][0] + 1] elif (slot == 9): via_row_x = [segm_def[1][0] - 2, segm_def[1][0] + 2] elif (slot == 4 or slot == 10): via_row_x = [segm_def[0][0] + 10] elif (slot == 5 or slot == 6 or slot == 7): via_row_x = [segm_def[-1][0] - 80] elif (slot == 11): via_row_x = [(segm_def[0][0] + segm_def[1][0]) / 2] elif (slot == 12): via_row_x = [segm_def[1][0] - 25, segm_def[1][0] + 25] original_dwg = ezdxf.readfile("in/" + board + "_outline.dxf") dwg = ezdxf.new(dxfversion=original_dwg.dxfversion) msp = dwg.modelspace() dwg.layers.new(name='Strip gaps', dxfattribs={ 'linetype': 'Continuous', 'color': 160 }) dwg.layers.new(name='Traces', dxfattribs={ 'linetype': 'Continuous', 'color': 40 }) dwg.layers.new(name='Strips', dxfattribs={ 'linetype': 'DASHED', 'color': 222 }) dwg.layers.new(name='Vias', dxfattribs={ 'linetype': 'Continuous', 'color': 10 }) strip_block = dwg.blocks.new(name='SLOT' + str(slot)) # declaring linetypes that are not default, but were used in the original # if not present, AutoCAD would not open the file... my_line_types = [ ("DASHSPACE", "Dashspace - - - - - - - - - - - - - - - -", [10.0, 5.0, -5.0]), ("DOT", "Dot . . . . . . . . . . . . . . . . . . . ", [5.0, 0.0, -5.0]), ] for name, desc, pattern in my_line_types: if name not in dwg.linetypes: dwg.linetypes.new(name=name, dxfattribs={ 'description': desc, 'pattern': pattern }) # copy board from the original DXF and paste it into the new one importer = ezdxf.Importer(original_dwg, dwg) importer.import_blocks(query=block_label, conflict='discard') board_rotation = -90. msp.add_blockref(block_label, (0, 0), dxfattribs={ 'xscale': 1., 'yscale': 1., 'rotation': board_rotation }) # building strips me0_maxy = 0 if (board == 'ME0' ): # do not extrapolate strips all the way due to irregular shape _c, _d = getLineDef(segm_def[0][0], segm_def[0][1], segm_def[-3][0], segm_def[-3][1]) me0_maxy = _c * segm_def[-1][0] + _d gap_pts_lo, gap_pts_hi = [], [] gap_line_def = [] str_line_def = [] width_lo = segm_def[0][1] * 2 width_hi = segm_def[-1][1] * 2 if (board == 'ME0'): width_hi = me0_maxy * 2 str_width_lo = (width_lo - (nStripsPerConn * nConnPerRow + 1) * gap) / ( nStripsPerConn * nConnPerRow) str_width_hi = (width_hi - (nStripsPerConn * nConnPerRow + 1) * gap) / ( nStripsPerConn * nConnPerRow) for i in range(nStripsPerConn * nConnPerRow + 1): # gaps gap_pts_lo.append( (segm_def[0][0], i * (str_width_lo + gap) + gap / 2. - width_lo / 2., gap, gap)) # high end of gaps gap_pts_hi.append( (segm_def[-1][0], i * (str_width_hi + gap) + gap / 2. - width_hi / 2., gap, gap)) # also need the line definition for finding overlaps with vias later _a, _b = getLineDef(gap_pts_lo[-1][0], gap_pts_lo[-1][1], gap_pts_hi[-1][0], gap_pts_hi[-1][1]) gap_line_def.append([_a, _b]) # strips for i in range(nStripsPerConn * nConnPerRow): str_lo_x = segm_def[0][0] str_lo_y = i * (str_width_lo + gap) + gap + str_width_lo / 2. - width_lo / 2. str_hi_x = segm_def[-1][0] str_hi_y = i * (str_width_hi + gap) + gap + str_width_hi / 2. - width_hi / 2. _a, _b = getLineDef(str_lo_x, str_lo_y, str_hi_x, str_hi_y) str_line_def.append([_a, _b]) if (debug): strip_block.add_lwpolyline([(0, _b), (1500, _a * 1500 + _b)], dxfattribs={'layer': 'Strip gaps'}) # add via rows - saving center positions for later via_centers = [] for iseg in range(nEtaSegm): via_centers.append([]) # determine strip width at desired radius for lowest lying strip, here the via would be in the narrowest portion this_width = 2 * (str_line_def[-1][0] * via_row_x[iseg] + str_line_def[-1][1]) str_width = (this_width - (nStripsPerConn * nConnPerRow + 1) * gap) / ( nStripsPerConn * nConnPerRow * 1.) for istr in range(nStripsPerConn * nConnPerRow): # get via center via_x = via_row_x[iseg] via_y = 0 if (slot == 11): if (istr % 2 == 0): via_x += 25 else: via_x -= 25 if (str_width > 2 * (via_radius)): # single row via_y = via_x * str_line_def[istr][0] + str_line_def[istr][1] else: # strips too thin, zig-zag if (istr % 2 == 0): via_x = via_x - 1.25 * via_radius via_y = via_x * str_line_def[istr][0] + str_line_def[istr][ 1] else: via_x = via_x + 1.25 * via_radius via_y = via_x * str_line_def[istr][0] + str_line_def[istr][ 1] via_centers[-1].append((via_x, via_y)) # draw vias for reference if (istr >= (getSector(slot) - 1) * nStripsPerConn and istr < getSector(slot) * nStripsPerConn): strip_block.add_circle((via_x, via_y), via_radius, dxfattribs={'layer': 'Vias'}) # draw gaps for istr in range(nStripsPerConn * nConnPerRow): # list of points to define strip gap this_str = [gap_pts_lo[istr]] # loop over eta segments to find via overlaps for iseg in range(nEtaSegm): this_width = 2 * (str_line_def[-1][0] * via_row_x[iseg] + str_line_def[-1][1]) str_width = (this_width - (nStripsPerConn * nConnPerRow + 1) * gap) / ( nStripsPerConn * nConnPerRow * 1.) # overlaps if (str_width < (2 * via_radius)): # adding gap/2 to radius to account for thickness of gap via_eff_radius = via_radius + gap / 2. # intersection with last via _x2, _y2, _x3, _y3, _x4, _y4, _x5, _y5 = -1e10, -1e10, -1e10, -1e10, -1e10, -1e10, -1e10, -1e10 if (istr > 0): _x2, _y2, _x3, _y3 = getLineCircleIntercept( via_centers[iseg][istr - 1][0], via_centers[iseg][istr - 1][1], via_eff_radius, gap_line_def[istr][0], gap_line_def[istr][1]) dist = 0.5 * math.sqrt((_x3 - _x2) * (_x3 - _x2) + (_y3 - _y2) * (_y3 - _y2)) bulge1 = -(via_eff_radius - math.sqrt( via_eff_radius * via_eff_radius - dist * dist)) / dist # intersection with next via if istr < (nStripsPerConn * nConnPerRow - 1): _x4, _y4, _x5, _y5 = getLineCircleIntercept( via_centers[iseg][istr][0], via_centers[iseg][istr][1], via_eff_radius, gap_line_def[istr][0], gap_line_def[istr][1]) dist = 0.5 * math.sqrt((_x5 - _x4) * (_x5 - _x4) + (_y5 - _y4) * (_y5 - _y4)) bulge2 = (via_eff_radius - math.sqrt(via_eff_radius * via_eff_radius - dist * dist)) / dist # by definition of the intercept function _x2<_x3 and _x4<_x5 if (_x2 < _x4): if (istr > 0): this_str.append((_x2, _y2, gap, gap, bulge1)) this_str.append((_x3, _y3, gap, gap)) if (istr < nStripsPerConn * nConnPerRow - 1): this_str.append((_x4, _y4, gap, gap, bulge2)) this_str.append((_x5, _y5, gap, gap)) else: if (istr < nStripsPerConn * nConnPerRow - 1): this_str.append((_x4, _y4, gap, gap, bulge2)) this_str.append((_x5, _y5, gap, gap)) if (istr > 0): this_str.append((_x2, _y2, gap, gap, bulge1)) this_str.append((_x3, _y3, gap, gap)) # add last point and draw gap this_str.append(gap_pts_hi[istr]) if (istr >= (getSector(slot) - 1) * nStripsPerConn and istr < (getSector(slot) * nStripsPerConn + 1)): strip_block.add_lwpolyline(this_str, dxfattribs={'layer': 'Strip gaps'}) # gaps between eta segments for iseg in range(nEtaSegm + 1): _x = segm_def[iseg][0] _y1 = _x * gap_line_def[getSector(slot) * nStripsPerConn][0] + gap_line_def[ getSector(slot) * nStripsPerConn][1] _y2 = _x * gap_line_def[ (getSector(slot) - 1) * nStripsPerConn][0] + gap_line_def[ (getSector(slot) - 1) * nStripsPerConn][1] if (iseg == 0): strip_block.add_lwpolyline([(_x - gap / 2, _y1, gap, gap), (_x - gap / 2, _y2, gap, gap)], dxfattribs={'layer': 'Strip gaps'}) elif (iseg == nEtaSegm): strip_block.add_lwpolyline([(_x + gap / 2, _y1, gap, gap), (_x + gap / 2, _y2, gap, gap)], dxfattribs={'layer': 'Strip gaps'}) else: strip_block.add_lwpolyline([(_x, _y1, gap, gap), (_x, _y2, gap, gap)], dxfattribs={'layer': 'Strip gaps'}) # add strip block to drawing msp.add_blockref('SLOT' + str(slot), (0, 0), dxfattribs={ 'xscale': 1., 'yscale': 1., 'rotation': 0 }) outfile = "out/" + board outfile += "_s" + str(slot) + ".dxf" dwg.saveas(outfile) return outfile