pcb 4.1.1
An interactive printed circuit board layout editor.

renumber.c

Go to the documentation of this file.
00001 
00029 #include <stdio.h>
00030 #include <math.h>
00031 
00032 #include "config.h"
00033 #include "global.h"
00034 #include "data.h"
00035 #include "hid.h"
00036 #include "misc.h"
00037 #include "create.h"
00038 #include "rtree.h"
00039 #include "undo.h"
00040 #include "error.h"
00041 #include "change.h"
00042 
00043 
00044 /* %start-doc actions RenumberBlock
00045 
00046 The @code{RenumberBlocks()} action renumbers all selected refdesses on
00047 the pcb.
00048 
00049 Usage: RenumberBlock(oldnum,newnum)
00050 
00051 All selected elements are renumbered by adding (newnum-oldnum) to
00052 the existing number.
00053 
00054 To invoke it, use the command window, usually by typing ":".
00055 
00056 Example:
00057 
00058 @code{RenumberBlock(100,200)} will change R213 to R313.
00059 
00060 %end-doc */
00061 static int
00062 renumber_block (int argc, char **argv, Coord x, Coord y)
00063 {
00064   char num_buf[15];
00065   int old_base;
00066   int new_base;
00067 
00068   if (argc < 2)
00069   {
00070     Message("Usage: RenumberBlock oldnum newnum");
00071     return 1;
00072   }
00073 
00074   old_base = atoi (argv[0]);
00075   new_base = atoi (argv[1]);
00076 
00077   SET_FLAG (NAMEONPCBFLAG, PCB);
00078 
00079   ELEMENT_LOOP (PCB->Data);
00080   {
00081     char *refdes_split;
00082     char *cp;
00083     char *old_ref;
00084     char *new_ref;
00085     int num;
00086 
00087     if (!TEST_FLAG (SELECTEDFLAG, element)
00088         || EMPTY_STRING_P(element->Name[1].TextString))
00089       continue;
00090 
00091     old_ref = element->Name[1].TextString;
00092     for (refdes_split = cp = old_ref; *cp; cp++)
00093       if (!isdigit (*cp))
00094         refdes_split = cp+1;
00095 
00096     num = atoi (refdes_split);
00097     num += (new_base - old_base);
00098     sprintf(num_buf, "%d" ,num);
00099     new_ref = (char *) malloc (refdes_split - old_ref + strlen (num_buf) + 1);
00100     memcpy (new_ref, old_ref, refdes_split - old_ref);
00101     strcpy (new_ref + (refdes_split - old_ref), num_buf);
00102 
00103     AddObjectToChangeNameUndoList (ELEMENT_TYPE, NULL, NULL,
00104                                    element,
00105                                    NAMEONPCB_NAME (element));
00106 
00107     ChangeObjectName (ELEMENT_TYPE, element, NULL, NULL, new_ref);
00108   }
00109   END_LOOP;
00110   IncrementUndoSerialNumber ();
00111   return 0;
00112 }
00113 
00114 
00115 /* %start-doc actions RenumberBuffer
00116 
00117 The @code{RenumberBuffer()} action renumbers all selected refdesses in
00118 the paste buffer.
00119 
00120 Usage: RenumberBuffer(oldnum,newnum)
00121 
00122 All selected elements are renumbered by adding (newnum-oldnum) to
00123 the existing number.
00124 
00125 To invoke it, use the command window, usually by typing ":".
00126 
00127 Example:
00128 
00129 @code{RenumberBuffer(0,10)} will change R2 to R12.
00130 
00131 %end-doc */
00132 static int
00133 renumber_buffer (int argc, char **argv, Coord x, Coord y)
00134 {
00135   char num_buf[15];
00136   int old_base;
00137   int new_base;
00138 
00139   if (argc < 2)
00140   {
00141     Message("Usage: RenumberBuffer oldnum newnum");
00142     return 1;
00143   }
00144 
00145   old_base = atoi (argv[0]);
00146   new_base = atoi (argv[1]);
00147 
00148   SET_FLAG (NAMEONPCBFLAG, PCB);
00149 
00150   ELEMENT_LOOP (PASTEBUFFER->Data);
00151   {
00152     char *refdes_split;
00153     char *cp;
00154     char *old_ref;
00155     char *new_ref;
00156     int num;
00157 
00158     if (EMPTY_STRING_P(element->Name[1].TextString))
00159       continue;
00160 
00161     old_ref = element->Name[1].TextString;
00162     for (refdes_split=cp=old_ref; *cp; cp++)
00163       if (!isdigit(*cp))
00164         refdes_split = cp+1;
00165 
00166     num = atoi (refdes_split);
00167     num += (new_base - old_base);
00168     sprintf (num_buf, "%d" ,num);
00169     new_ref = (char *) malloc (refdes_split - old_ref + strlen (num_buf) + 1);
00170     memcpy (new_ref, old_ref, refdes_split - old_ref);
00171     strcpy (new_ref + (refdes_split - old_ref), num_buf);
00172 
00173     ChangeObjectName (ELEMENT_TYPE, element, NULL, NULL, new_ref);
00174   }
00175   END_LOOP;
00176   return 0;
00177 }
00178 
00179 static HID_Action renumber_block_action_list[] =
00180 {
00181   {"RenumberBlock", NULL, renumber_block, NULL, NULL},
00182   {"RenumberBuffer", NULL, renumber_buffer, NULL, NULL}
00183 };
00184 
00185 REGISTER_ACTIONS (renumber_block_action_list)
00186 
00187 void
00188 hid_renumber_init()
00189 {
00190   register_renumber_block_action_list();
00191 }