Skip to content
Snippets Groups Projects
analyze_dxf.py 5.22 KiB
Newer Older
#!/usr/bin/env python

import numpy as np
import ezdxf 
import random
from math import sqrt

class dxf_editor:
    def __init__(self,path,dxf_file):
        self.dwg=ezdxf.readfile(path+dxf_file)
        self.msp=self.dwg.modelspace()
        
        
        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('silhouette_ele.dxf')
    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'})
                    
        for center_point in self.center_arr:    
            self.msp.add_blockref('ISO_BLK',center_point,dxfattribs={'layer':'Circuit'})





        #         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
# msp.add_blockref('FLAG',(40,40))
# dwg.save()