Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.00 KB | None | 0 0
  1. from csv import reader
  2. from array import array
  3. from shapely.geometry import Polygon
  4. from shapely.geometry import Point
  5. from shapely.ops import cascaded_union
  6. import locale
  7. import re
  8. import sys
  9. import getopt
  10.  
  11. coordinates_mcu = {}
  12. coordinates_mem = {}
  13. connect = {}
  14. decimal_point = locale.localeconv()['decimal_point']
  15. route_table = []
  16. jumps_list = []
  17. solution = ''
  18. jumps_count = 0
  19. current_line = 0
  20.  
  21. def serve_route_table(newpoly):
  22. global route_table
  23. first_route = None
  24. first_poly = None
  25. #Serving new poly to route table, concatenating routes according to polys interlaces
  26. for route in route_table:
  27. for poly in route:
  28. if (poly["layer"] == newpoly["layer"] and poly["poly"].intersects(newpoly["poly"])):
  29. poly["poly"] = cascaded_union( [poly["poly"], newpoly["poly"]] )
  30. if first_route == None:
  31. first_route = route
  32. first_poly = poly
  33. else:
  34. first_poly["poly"] = cascaded_union( [first_poly["poly"], poly["poly"]] )
  35. route.remove( poly )
  36. first_route += route
  37. route_table.remove( route )
  38.  
  39. if first_route == None:
  40. route_table.append( [ newpoly, ] )
  41.  
  42. def serve_route_table_jump():
  43. global jumps_list
  44. first_route = None
  45.  
  46. for jump in jumps_list:
  47. for route in route_table:
  48. for poly in route:
  49. if (poly["poly"].contains( jump )):
  50. if first_route == None:
  51. first_route = route
  52. else:
  53. first_route += route
  54. route_table.remove( route )
  55.  
  56.  
  57. def parse_poly(command, solution):
  58.  
  59. global current_line
  60.  
  61. poly_object = {}
  62.  
  63. try:
  64. #Get number of points and layer
  65. edges_count = re.findall( "(?<=POLY)[^,]+(?=,)", command )
  66. if len(edges_count) != 1:
  67. print "Syntax error while getting points count in POLY, line #%d" % current_line
  68. exit()
  69.  
  70. layer_number = re.findall( "(?<=,)[^\n\r]+(?=)", command )
  71. if len(layer_number) != 1:
  72. print "Syntax error while getting layer in POLY, line #%d" % current_line
  73. exit()
  74. poly_object["layer"] = int(layer_number[0])
  75.  
  76. if poly_object["layer"] > 10 or poly_object["layer"] < 1 :
  77. print "Error on line %d - layer number should be in range [1, 10], found %d" % (current_line,
  78. poly_object["layer"])
  79. exit()
  80.  
  81. points_list = []
  82. #Get all points
  83. for iter in range(0, int(edges_count[0])):
  84. points_line = solution.readline()
  85. current_line += 1
  86. points = points_line.split(';')
  87. if len(points) != 2:
  88. print "Syntax error extracting POLY point, line #%d" % current_line
  89. exit()
  90. points_list.append( (float(points[0].replace(',', decimal_point)),
  91. float(points[1].replace(',', decimal_point))) )
  92.  
  93. except:
  94. print "Syntax error, line #%d" % current_line
  95. exit()
  96.  
  97. #Create shapely polygon
  98. poly_object["poly"] = Polygon( points_list )
  99.  
  100. serve_route_table( poly_object )
  101.  
  102.  
  103.  
  104. def main():
  105.  
  106. global current_line
  107. global route_table
  108. global jumps_count
  109. global jumps_list
  110.  
  111. inputfile = sys.argv[1]
  112.  
  113. #Load connect & coordinates files
  114. with open('coordinates.csv', 'rU') as fcoords:
  115. csvreader = reader( fcoords, delimiter=';' )
  116. for row in csvreader:
  117. if (int(row[1]) == 1):
  118. coordinates_mcu[int(row[0])] = Point(
  119. float(row[2].replace(',', decimal_point)),
  120. float(row[3].replace(',', decimal_point)) )
  121. else:
  122. coordinates_mem[int(row[0])] = Point(
  123. float(row[2].replace(',', decimal_point)),
  124. float(row[3].replace(',', decimal_point)) )
  125.  
  126. with open('connect.csv', 'rU') as fcoords:
  127. csvreader = reader( fcoords, delimiter=';' )
  128. for row in csvreader:
  129. connect[int(row[0])] = int(row[1])
  130.  
  131. #Load the solution file, create route table
  132. solution = open(inputfile, 'rU')
  133. while True:
  134.  
  135. current_line += 1
  136. command = solution.readline()
  137.  
  138. if command.startswith( 'POLY' ):
  139. parse_poly(command, solution)
  140. elif command.startswith( 'JUMP' ):
  141. jumps_count += 1
  142. try:
  143. point_x = re.findall("(?<=JUMP)[^;]+(?=;)", command)
  144. if len(point_x) != 1:
  145. print "Syntax error extracting JUMP point X coordinate, line #%d" % current_line
  146. exit()
  147. point_x = float(point_x[0].replace(',', decimal_point))
  148. point_y = re.findall("(?<=;)[^;]+(?=)", command)
  149. if len(point_y) != 1:
  150. print "Syntax error extracting JUMP point Y coordinate, line #%d" % current_line
  151. exit()
  152. point_y = float(point_y[0].replace(',', decimal_point))
  153. jumps_list.append( Point( point_x, point_y ) )
  154. except:
  155. print "Syntax error, line #%d" % current_line
  156. exit()
  157.  
  158. if command == '':
  159. break
  160.  
  161. serve_route_table_jump()
  162.  
  163. correct_connections = {}
  164.  
  165. #OK, checking connections are correct
  166. for pin_mcu, point_mcu in coordinates_mcu.iteritems():
  167. for route in route_table:
  168. for poly in route:
  169. if poly["poly"].intersects(point_mcu.buffer(0.1)) and poly["layer"] == 1:
  170. for pin_mem, point_mem in coordinates_mem.iteritems():
  171. if poly["poly"].intersects(point_mem.buffer(0.1)) and connect[pin_mcu] != pin_mem:
  172. print "MCU pin %d and Memory pin %d are connected, though shouldn't" % (pin_mcu, pin_mem)
  173. elif poly["poly"].contains(point_mem):
  174. correct_connections[pin_mcu] = pin_mem
  175. break
  176.  
  177. #Printing points that are not connected, but should be
  178. for pin_mcu in range(1, 41):
  179. if not(correct_connections.has_key( pin_mcu )):
  180. print "MCU pin %d is NOT connected with Memory pin %d, though should" % (pin_mcu, connect[pin_mcu])
  181.  
  182. #Printing estimation of electrical length
  183. el_length = 0.0
  184. for route in route_table:
  185. for poly in route:
  186. el_length += poly["poly"].length / 2.0
  187.  
  188. print "S = %d" % el_length
  189. print "Vias %d" % len(jumps_list)
  190.  
  191. #calculate layers used
  192. layers = 1
  193. for route in route_table:
  194. for poly in route:
  195. if poly["layer"] > layers:
  196. layers = poly["layer"]
  197.  
  198. print "Layers used %d" % layers
  199.  
  200. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement