home *** CD-ROM | disk | FTP | other *** search
/ Dream 55 / Amiga_Dream_55.iso / RISCOS / APPS / SCI / ELECTRON / AUTOPC.ZIP / !AutoPCB / Source / c / AutoPCB < prev    next >
Text File  |  1991-04-06  |  9KB  |  369 lines

  1. /*
  2. ** printed circuit board autorouter, Copyright (C) Randy Nevin 1989.
  3. **
  4. ** you may give this software to anyone, make as many copies as you like, and
  5. ** post it on public computer bulletin boards and file servers. you may not
  6. ** sell it or charge any fee for distribution (except for media and postage),
  7. ** remove this comment or the copyright notice from the code, or claim that
  8. ** you wrote this code or anything derived from it. you may modify the code as
  9. ** much as you want (please document clearly with comments, and maintain the
  10. ** coding style), but programs which are derived from this one are subject to
  11. ** the conditions stated here. i am providing this code so that people can
  12. ** learn from it, so if you distribute it, please include source code, not
  13. ** just executables. contact me to report bugs or suggest enhancements; i do
  14. ** not guarantee support, but i will make an effort in good faith to help you,
  15. ** and i want to act as a central clearing house for future versions. you
  16. ** should contact me before undertaking a significant development effort, to
  17. ** avoid reinventing the wheel. if you come up with an enhancement you
  18. ** consider particularly useful, i would appreciate being informed so that it
  19. ** can be incorporated in future versions. my address is: Randy Nevin,
  20. ** 1731 211th PL NE, Redmond, WA 98053. this code is available directly from
  21. ** the author; just send a floppy and a self-addressed floppy mailer with
  22. ** sufficient postage.
  23. **
  24. ** HISTORY
  25. ** (name                date            description)
  26. ** ----------------------------------------------------
  27. ** randy nevin          2/1/89          initial version
  28. ** randy nevin          2/4/89          made retrace table driven, instead of
  29. **                                      doubly-nested switch statements.
  30. ** randy nevin          2/4/89          changed dir from int to char (cut
  31. **                                      storage for it in half).
  32. ** randy nevin          2/8/89          changed SetQueue and ReSetQueue to
  33. **                                      give priority to goal nodes.
  34. ** randy nevin          2/11/89         don't output incremental search
  35. **                                      results if stdout is redirected.
  36. ** randy nevin          2/11/89         released version 1.00
  37. ** pete goodwin         17/3/91         adapted for Archimedes
  38. */
  39.  
  40. /* C routines */
  41.  
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <time.h>
  45. #include <string.h>
  46.  
  47. /* WIMP */
  48.  
  49. #include "os.h"
  50. #include "wimp.h"
  51. #include "wimpt.h"
  52. #include "win.h"
  53. #include "res.h"
  54. #include "resspr.h"
  55. #include "template.h"
  56. #include "dbox.h"
  57. #include "baricon.h"
  58. #include "event.h"
  59. #include "menu.h"
  60. #include "werr.h"
  61. #include "flex.h"
  62. #include "heap.h"
  63. #include "xferrecv.h"
  64. #include "alarm.h"
  65. #include "visdelay.h"
  66. #include "saveas.h"
  67.  
  68. /* Auto PCB stuff */
  69.  
  70. #include "cell.h"
  71. #include "coordmap.h"
  72. #include "draw.h"
  73.  
  74. /* DRAW file 1 thou of an inch */
  75. #define MIL  (180*256/1000)
  76.  
  77. #define BLACK 0x00000000
  78. #define WHITE 0x00000000
  79. #define TRANS 0xffffffff
  80. #define RED   0x0000ff00
  81. #define GREEN 0x00ff0000
  82. #define BLUE  0xff000000
  83.  
  84. extern int Nrows;
  85. extern int Ncols;
  86.  
  87. extern void InitBoard(void);
  88. extern long GetCell(int, int, int);
  89. extern void SetCell(int, int, int, long);
  90.  
  91. extern void PlotCell(int, int, int, int);
  92.  
  93. static int scale, colfact, rowfact, show_hole,
  94.            show_top, show_bottom, DP, DPlimit;
  95.  
  96. #define ICON_MENU      0
  97.  
  98. #define ICON_MENU_INFO 1
  99. #define ICON_MENU_QUIT 2
  100.  
  101. #define DBOX_CMD       0
  102.  
  103. #define PROCESS        0
  104. #define FILE_SOURCE    13
  105. #define TIME           16
  106. #define PLOT           17
  107. #define OPT_HOLE       14
  108. #define OPT_TOP        15
  109. #define OPT_BOTTOM     16
  110.  
  111. int justboard = 0; /* need all data structures, not just the board */
  112.  
  113. extern void Initialize( FILE * );
  114. extern BOOL Solve( void );
  115. extern void Report( FILE * );
  116.  
  117. dbox cmd_dbox;
  118. static menu icon_menu;
  119.  
  120. static BOOL solving;
  121. static long start, stop;
  122.  
  123. static FILE *fin;
  124.  
  125. void Nomem(void)
  126. {
  127.   werr(0, "Out of memory");
  128. }
  129.  
  130. void move(int x, int y)
  131. {
  132.   draw_path_move(x * MIL, y * MIL);
  133. }
  134.  
  135. void draw(int x, int y)
  136. {
  137.   draw_path_line(x * MIL, y * MIL);
  138. }
  139.  
  140. void circle(int x, int y, int r)
  141. {
  142.   draw_path_circle(x * MIL, y * MIL, r * MIL);
  143. }
  144.  
  145. static void process_display()
  146. {
  147.   long x;
  148.   int r, c, hole, top, bottom, mark;
  149.   double scale;
  150.   char buffer[13];
  151.  
  152.   hole   = dbox_getnumeric(cmd_dbox, OPT_HOLE);
  153.   top    = dbox_getnumeric(cmd_dbox, OPT_TOP);
  154.   bottom = dbox_getnumeric(cmd_dbox, OPT_BOTTOM);
  155.  
  156.   scale = 2.0;
  157.  
  158.   draw_file_header("Auto PCB    ");
  159.  
  160.   if (bottom)
  161.   {
  162.     draw_object_header(2, Ncols * 50 * MIL, Nrows * 50 * MIL);
  163.     draw_path_header(TRANS, GREEN);
  164.  
  165.     for (r = 0; r < Nrows; r++)
  166.       for (c = 0; c < Ncols; c++)
  167.       {
  168.         x = GetCell(r, c, BOTTOM);
  169.         if (x & HOLE)
  170.           PlotLine(x, c, r, BOTTOM);
  171.       }
  172.     draw_path_end();
  173.   }
  174.  
  175.   if (top)
  176.   {
  177.     draw_object_header(2, Ncols * 50 * MIL, Nrows * 50 * MIL);
  178.     draw_path_header(TRANS, RED);
  179.  
  180.     for (r = 0; r < Nrows; r++)
  181.       for (c = 0; c < Ncols; c++)
  182.       {
  183.         x = GetCell(r, c, TOP);
  184.         if (x & HOLE)
  185.           PlotLine(x, c, r, TOP);
  186.       }
  187.     draw_path_end();
  188.   }
  189.  
  190.   if (hole)
  191.   {
  192.     draw_object_header(2, Ncols * 50 * MIL, Nrows * 50 * MIL);
  193.     draw_path_header(TRANS, BLACK);
  194.  
  195.     for (r = 0; r < Nrows; r++)
  196.       for (c = 0; c < Ncols; c++)
  197.       {
  198.         x = GetCell(r, c, TOP);
  199.         if (x & HOLE)
  200.           circle(c * 50 + 25, r * 50 + 25, 15);
  201.       }
  202.     draw_path_end();
  203.   }
  204.  
  205.   draw_display(scale);
  206. }
  207.  
  208. static BOOL save_diag(char *name, void *handle)
  209. {
  210.   BOOL ok;
  211.   int id;
  212.   os_filestr file;
  213.  
  214.   id = (int)handle;
  215.  
  216.   visdelay_begin();
  217.  
  218.   file.action   = 10;
  219.   file.name     = name;
  220.   file.loadaddr = 0xaff;
  221.   file.start    = (int)diag;
  222.   file.end      = (int)&diag[diagp];
  223.  
  224.   ok = (wimpt_complain(os_file(&file)) == 0);
  225.  
  226.   visdelay_end();
  227.  
  228.   return ok;
  229. }
  230.  
  231. static void process_pcb()
  232. {
  233.   saveas(0xAFF, "DrawFile", 1000, save_diag, NULL, NULL, 0);
  234. }
  235.  
  236. static void icon_event(wimp_i icon)
  237. {
  238.   dbox_showstatic(cmd_dbox);
  239. }
  240.  
  241. static void menu_event(void *handle, char *hit)
  242. {
  243.   int id;
  244.  
  245.   id = (int)handle;
  246.  
  247.   if (id == ICON_MENU)
  248.     switch(hit[0])
  249.     {
  250.       case ICON_MENU_INFO:
  251.         {
  252.           dbox d;
  253.  
  254.           d = dbox_new("info");
  255.           dbox_show(d);
  256.           dbox_fillin(d);
  257.           dbox_dispose(&d);
  258.         }
  259.         break;
  260.  
  261.       case ICON_MENU_QUIT:
  262.         exit(0);
  263.     }
  264. }
  265.  
  266. static void dbox_event(dbox d, void *handle)
  267. {
  268.   int id;
  269.   dbox_field field;
  270.  
  271.   id = (int)handle;
  272.   field = dbox_get(d);
  273.   if (field == dbox_CLOSE)
  274.     dbox_hide(d);
  275.   if (id == DBOX_CMD && field == PROCESS && !solving)
  276.     process_pcb();
  277.   if (id == DBOX_CMD && field == PLOT && !solving)
  278.     process_display();
  279. }
  280.  
  281. static void start_solve(int at, void *handle)
  282. {
  283.   visdelay_begin();
  284.   if (solving = Solve())
  285.     alarm_set(alarm_timenow()+10, start_solve, 0);
  286.   else
  287.   {
  288.     stop = time(NULL) - start;
  289.     dbox_setnumeric(cmd_dbox, TIME, stop);
  290.     dbox_setfield(cmd_dbox, PROCESS, "Save route");
  291.   }
  292.   visdelay_end();
  293. }
  294.  
  295. static BOOL dbox_raw_event(dbox d, void *event, void *handle)
  296. {
  297.   int id;
  298.   wimp_eventstr *e;
  299.  
  300.   id = (int)handle;
  301.   e = (wimp_eventstr *)event;
  302.   switch(e->e)
  303.   {
  304.     case wimp_ESEND:
  305.     case wimp_ESENDWANTACK:
  306.       switch(e->data.msg.hdr.action)
  307.       {
  308.         case wimp_MDATALOAD:
  309.         case wimp_MDATAOPEN:
  310.           {
  311.             char *name;
  312.             int filetype;
  313.  
  314.             filetype = xferrecv_checkinsert(&name);
  315.             if (filetype != -1)
  316.             {
  317.               if (filetype == 0xfff)
  318.               {
  319.                 dbox_setfield(cmd_dbox, FILE_SOURCE, name);
  320.                 fin = fopen(name, "rb");
  321.                 if (fin != NULL)
  322.                 {
  323.                   xferrecv_insertfileok();
  324.                   Initialize(fin);
  325.                   start = time(NULL);
  326.                   dbox_setfield(cmd_dbox, PROCESS, "Processing");
  327.                   start_solve(0, 0);
  328.                 }
  329.               }
  330.               else
  331.                 werr(0, "can't load filetype &%x", filetype);              
  332.             }
  333.           }
  334.           return TRUE;
  335.       }
  336.     break;
  337.   }
  338.   return FALSE;
  339. }
  340.  
  341. int main (int argc, char *argv[] )
  342. {
  343.   wimpt_init("Auto PCB");
  344.   res_init("autopcb");
  345.   resspr_init();
  346.   template_init();
  347.   dbox_init();
  348.   flex_init();
  349.   heap_init(TRUE);
  350.   visdelay_init();
  351.  
  352.   baricon("!autopcb", (int)resspr_area(), icon_event);
  353.   icon_menu = menu_new("AutoPCB", ">Info,Quit");
  354.   event_attachmenu(win_ICONBAR, icon_menu, menu_event, (void *)ICON_MENU);
  355.  
  356.   cmd_dbox = dbox_new("command");
  357.   dbox_eventhandler(cmd_dbox, dbox_event, DBOX_CMD);
  358.   dbox_raw_eventhandler(cmd_dbox, dbox_raw_event, DBOX_CMD);
  359.  
  360.   dbox_setfield(cmd_dbox, FILE_SOURCE, "<Source file>");
  361.  
  362.   solving = TRUE;
  363.  
  364.   draw_init();
  365.  
  366.   while (TRUE)
  367.     event_process();
  368. }
  369.