@@ -401,44 +401,16 @@ def open_file(self):
401401
402402 for key , val in circuit_data .items ():
403403 if "chip" in key :
404- x , y = val ["XY" ]
405- model_chip = [
406- (
407- self .board .sketcher .draw_chip ,
408- 1 ,
409- {
410- ** val ,
411- "matrix" : self .board .sketcher .matrix ,
412- },
413- )
414- ]
415- self .board .sketcher .circuit (x , y , model = model_chip )
404+ self .load_chip (val )
416405
417406 elif "wire" in key :
418- model_wire = [
419- (
420- self .board .sketcher .draw_wire ,
421- 1 ,
422- {
423- ** val ,
424- "matrix" : self .board .sketcher .matrix ,
425- },
426- )
427- ]
428- self .board .sketcher .circuit (x_o , y_o , model = model_wire )
407+ self .load_wire (val )
408+
429409 elif "io" in key :
430- model_io = [
431- (
432- self .board .sketcher .draw_pin_io ,
433- 1 ,
434- {
435- ** val ,
436- "matrix" : self .board .sketcher .matrix ,
437- },
438- )
439- ]
440- self .board .sketcher .circuit (x_o , y_o , model = model_io )
410+ self .load_io (val )
411+
441412 else :
413+
442414 print (f"Unspecified component: { key } " )
443415 messagebox .showinfo ("Open File" , f"Circuit loaded from { file_path } " )
444416 except Exception as e :
@@ -448,6 +420,62 @@ def open_file(self):
448420 else :
449421 print ("Open file cancelled." )
450422
423+ def load_chip (self , chip_data ):
424+ """Load a chip from the given chip_data."""
425+ x , y = chip_data ["XY" ]
426+ model_chip = [
427+ (
428+ self .board .sketcher .draw_chip ,
429+ 1 ,
430+ {
431+ ** chip_data ,
432+ "matrix" : self .board .sketcher .matrix ,
433+ },
434+ )
435+ ]
436+ self .board .sketcher .circuit (x , y , model = model_chip )
437+ new_chip_id = self .current_dict_circuit ["last_id" ]
438+
439+ (_ , _ ), (column , line ) = self .board .sketcher .find_nearest_grid (x , y , matrix = self .board .sketcher .matrix )
440+ occupied_holes = []
441+ for i in range (chip_data ["pinCount" ] // 2 ):
442+ # Top row (line 7 or 21)
443+ hole_id_top = f"{ column + i } ,{ line } "
444+ # Bottom row (line 6 or 20)
445+ hole_id_bottom = f"{ column + i } ,{ line + 1 } "
446+ occupied_holes .extend ([hole_id_top , hole_id_bottom ])
447+ self .current_dict_circuit [new_chip_id ]["occupied_holes" ] = occupied_holes
448+
449+ def load_wire (self , wire_data ):
450+ """Load a wire from the given wire_data."""
451+ x_o , y_o = self .board .sketcher .id_origins ["xyOrigin" ]
452+ model_wire = [
453+ (
454+ self .board .sketcher .draw_wire ,
455+ 1 ,
456+ {
457+ ** wire_data ,
458+ "matrix" : self .board .sketcher .matrix ,
459+ },
460+ )
461+ ]
462+ self .board .sketcher .circuit (x_o , y_o , model = model_wire )
463+
464+ def load_io (self , io_data ):
465+ """Load an input/output component from the given io_data."""
466+ x_o , y_o = self .board .sketcher .id_origins ["xyOrigin" ]
467+ model_io = [
468+ (
469+ self .board .sketcher .draw_pin_io ,
470+ 1 ,
471+ {
472+ ** io_data ,
473+ "matrix" : self .board .sketcher .matrix ,
474+ },
475+ )
476+ ]
477+ self .board .sketcher .circuit (x_o , y_o , model = model_io )
478+
451479 def save_file (self ):
452480 """Handler for the 'Save' menu item."""
453481 print ("Save File" )
0 commit comments