Advertisement
AntonioVillanueva

KICAD PCB PARSER OLD FRENCH TO NEW VERSION

Oct 12th, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.59 KB | None | 0 0
  1. //Kicad from old PCB french kicad 4022 (2013) to NEW 4.0.7 Stable Release
  2. // Antonio Villanueva Segura
  3. #include <iostream>
  4. #include <fstream>
  5. #include <string.h>
  6. #include <stdlib.h> //for system
  7.  
  8. using namespace std;
  9. //OLD FRENCH KICAD
  10. string sust []={
  11. "Dessous.Adhes",//0 B.Adhes
  12. "Dessus.Adhes",//1 F.Adhes
  13. "Dessous.Pate",//2 B.Paste
  14. "Dessus.Pate",//3 F.Paste
  15. "Dessous.SilkS",//4 B.SilkS
  16. "Dessus.SilkS",//5 F.SilkS
  17. "Dessous.Masque",//6 B.Mask
  18. "Dessus.Masque",//7 F.Mask
  19. "Dessin.User",//8 Dwgs.User
  20. "Contours.Ci",//9 Margin.....
  21. "Dessous",//10 F.Cu
  22. "Dessus"//11 B.Cu
  23. };
  24.  
  25. //New KICAD
  26. string repl[]={
  27. "B.Adhes",//0
  28. "F.Adhes",//1
  29. "B.Paste",//2
  30. "F.Paste",//3
  31. "B.SilkS",//4
  32. "F.SilkS",//5
  33. "B.Mask",//6
  34. "F.Mask",//7
  35. "Dwgs.User",//8
  36. "Edge.Cuts",//9
  37. "F.Cu",//10
  38. "B.Cu",//11
  39. "Margin",//12
  40. "B.CrtYd",//13
  41. "F.CrtYd",//14
  42. "B.Fab",//15
  43. "F.Fab",//16
  44. };
  45.  
  46. int main(int argc, char *argv[]){
  47.     FILE *pipe;//pipe for read dir *.kicad_pcb
  48.     char buf[256]{0};//for read *.kicad_pcb file name
  49.     string line;
  50.     double pos(0);
  51.     size_t it(0);
  52.  
  53.     cout <<" Kicad PCB converter .From old  french 4022  to  4.0.7  stable "<<endl
  54.          <<" By default, looks for *.kicad_pcb extensions ,but we can indicate "
  55.     <<" the name as argument \n p.e (./kicad name.pcb )"<<endl<<endl;;
  56.    
  57.     if (argc ==1){//Not Params ...then search for ...ls *.kicad_pcb
  58.         if ((pipe=popen  ( "ls *.kicad_pcb","r")) <0 ){//Open system ls *.kicad_pcb
  59.          cout<<"Error Open PIPE"<<endl;return 0;//Error OPEN PIPE
  60.          }
  61.          else {
  62.                 fgets(buf,256,pipe);//retrieve filename from *.kicad_pcb
  63.                 if (strlen(buf)<1 ){cout<<"Error input File";return 0;}
  64.                 buf[strlen (buf)-1]=0;
  65.                 pclose (pipe);
  66.          }
  67.      }
  68.     else {
  69.     memcpy (buf,argv[1],strlen (argv[1]));
  70.     }
  71.  
  72.     ifstream read (string (buf),std::ifstream::in);
  73.     ofstream write (string (buf) + "_NEW");
  74.    
  75.     cout <<"Imput PCB KICAD File = "<< buf <<endl;//Print graphic imput debug
  76.  
  77.     if (read.is_open())
  78.     {
  79.         while ( getline (read,line) )
  80.         {
  81.             for (it=0;it < (sizeof (sust) /sizeof (sust[0]) );it++){
  82.                              
  83.                 //If find pattern , replace it
  84.                 if ( (pos=line.find(sust [it]))< string::npos ){//Find !
  85.                     line.replace(pos,sust[it].length(),repl[it] );//Replace
  86.                 }
  87.             }      
  88.             //cout <<line<<endl;//Graphic ctrl.
  89.             write<<line<<endl;//Out text
  90.             line.clear();
  91.         }
  92.    
  93.         //Close
  94.         read.close();
  95.         write.close();    
  96.        
  97.         //Rename files
  98.         string move ("mv "+string (buf)+" "+string (buf)+"_OLD");
  99.         system (move.c_str());
  100.        
  101.         move ="mv "+string (buf)+"_NEW"+" "+string (buf);
  102.         system (move.c_str());
  103.   }
  104.   else cout << "Unable to open file";
  105.   return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement