Newer
Older
#!/usr/bin/env python
import numpy as np
import ezdxf
import random
class dxf_editor:
def __init__(self,path,dxf_file):
self.dwg=ezdxf.readfile(path+dxf_file)
self.msp=self.dwg.modelspace()
self.dxf_name='silhouette_ele.dxf'
self.img_name='silouette_ele.png'
self.create_layer('Cut',5)
self.create_layer('Circuit',1)
self.create_layer('Label',3)
self.create_layer('Fold',4)
self.create_layer('Pin_temp',6)
self.layer_rearrange() ## reagrrange cut and fold lines to corresponding layers
self.find_pin()
isolength=2.6 #mm
self.draw_on_pin(isolength)
self.dwg.saveas(self.dxf_name)
# os.system("inkscape -f silhouette_ele.dxf -e silouette_ele.png")
# self.img=cv2.imread(path+self.img_name)
# print(self.img)
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
def create_layer(self,layer_name,color):
if not layer_name in self.dwg.layers:
self.dwg.layers.new(name=layer_name,dxfattribs={'color':color})
def layer_rearrange(self):
# put fold lines to the new layer 'Fold'
# put cut lines to the new layer 'Cut
for e in self.msp.query('LINE'):
if e.dxf.color!=5:
e.dxf.layer='Fold'
else:
e.dxf.layer='Cut'
def find_pin(self):
tolerance=0.05 #mm
pincutsize=1 #mm
pin_edge_arr=np.array([[0,0],[0,0]]).reshape(1,2,2)
self.center_arr=np.array([[0,0]])
for e in self.msp.query('LINE[layer=="Cut"]'):
length= sqrt((e.dxf.start[0]-e.dxf.end[0])**2+(e.dxf.start[1]-e.dxf.end[1])**2)
if length > pincutsize-tolerance and length < pincutsize + tolerance:
e.dxf.layer='Pin_temp'
if e.dxf.start[1]==e.dxf.end[1]: ##this line is horizontal
pin_edge=np.array([e.dxf.start,e.dxf.end])[:,:2]
pin_edge_arr=np.concatenate((pin_edge_arr,pin_edge.reshape(1,2,2)),axis=0)
pin_edge_arr=pin_edge_arr[1:]
# print(pin_edge_arr)
for i in range(len(pin_edge_arr)):
for e in np.delete(pin_edge_arr,i,axis=0):
if pin_edge_arr[i][0,1]-e[0,1]==1.0:
center_x=pin_edge_arr[i][0,0]-0.5
center_y=pin_edge_arr[i][0,1]-0.5
center=np.array([center_x,center_y]).reshape(1,2)
self.center_arr=np.append(self.center_arr,center,axis=0)
self.center_arr=np.unique(self.center_arr,axis=0)
self.center_arr=self.center_arr[1:]
def draw_on_pin(self,fulllength): #isolation
iso=self.dwg.blocks.new(name='ISO_BLK')
isolength=fulllength/2
iso.add_line((-isolength,isolength),(isolength,isolength),dxfattribs={'linetype':'DASHDOT'})
iso.add_line((-isolength,-isolength),(isolength,-isolength),dxfattribs={'linetype':'DASHDOT'})
iso.add_line((-isolength,-isolength),(-isolength,isolength),dxfattribs={'linetype':'DASHDOT'})
iso.add_line((isolength,-isolength),(isolength,isolength),dxfattribs={'linetype':'DASHDOT'})
iso.add_line((-isolength,-trace_w/2),(-(isolength+5),-trace_w/2),dxfattribs={'linetype':'DASHDOT'})
iso.add_line((-isolength,trace_w/2),(-(isolength+5),trace_w/2),dxfattribs={'linetype':'DASHDOT'})
for center_point in self.center_arr:
self.msp.add_blockref('ISO_BLK',center_point,dxfattribs={'layer':'Circuit'})
# def img_color_convert(self):
# for i in range(self.img.shape[0]):
# for j in
# if e.dxf.start[1]==e.dxf.end[1]: ##this line is horizontal
# midpoint_x=min(e.dxf.start[0],e.dxf.end[0])+length/2
# elif e.dxf.start[0]==e.dxf.end[0]: #This line is vertical
# midpoint_y=min(e.dxf.start[1],e.dxf.end[1])+length/2
# # print(midpoint)
# self.midpoint_arr=np.concatenate((self.midpoint_arr,np.array([midpoint])),axis=0)
# self.midpoint_arr=self.midpoint_arr[1:]
# self.midpoint_arr=np.unique(self.midpoint_arr,axis=0)
# print(self.midpoint_arr)
def main():
path='/home/jingyan/Documents/summer_intern_lemur/roco_electrical/'
dxf_file='graph-silhouette.dxf'
edit=dxf_editor(path,dxf_file)
if __name__ == '__main__':
main()
# path='/home/jingyan/Documents/summer_intern_lemur/roco_electrical/'
# dxf_file='graph-silhouette.dxf'
# dwg=ezdxf.readfile(path+dxf_file)
# # iterate over all entities in model space
# def print_entity(e):
# print("LINE on layer: %s\n" % e.dxf.layer)
# print("start point:",e.dxf.start)
# print("end point:" , e.dxf.end)
# print("color:", e.dxf.color)
# msp = dwg.modelspace()
# # for e in msp:
# # if e.dxftype() == 'LINE':
# # print_entity(e)
# i=0
# # entity query for all LINE entities in model space
# for e in msp.query('LINE[color==1]'):
# i+=1
# print_entity(e)
# # print(i)
# # # for z in msp.query('CIRCLE'):
# # # print(z)
# if not 'Isolation' in dwg.layers:
# dwg.layers.new(name='Isolation',dxfattribs={'linetype':'DASHED','color':2})
# msp.add_line((0,0),(50,50),dxfattribs={'layer':'Isolation'})
# def get_random_point():
# """Creates random x, y coordinates."""
# x = random.randint(-100, 100)
# y = random.randint(-100, 100)
# return x, y
# # Create a block with the name 'FLAG'
# flag = dwg.blocks.new(name='FLAG')
# # Add DXF entities to the block 'FLAG'.
# # The default base point (= insertion point) of the block is (0, 0).
# # flag.add_polyline2d([(0, 0), (0, 5), (4, 3), (0, 3)]) # the flag as 2D polyline
# flag.add_circle((0, 0), .4, dxfattribs={'color': 5}) # mark the base point with a circle