3:stl export(processing) part2

3D word print





3d_word_stl.pde
  1. //controlP5
  2. import controlP5.*;
  3. ControlP5 controlP5;
  4. Textfield tfUserName;
  5. String txts = "";
  6. int sliderA;


  7. // Import
  8. import javax.swing.*;

  9. // Unlekker
  10. import unlekker.data.*;
  11. import unlekker.geom.*;
  12. public STL stl;
  13. public FaceList poly;

  14. // File IO
  15. String getFile; 
  16. String setFile;
  17. String [] txtarray = null;
  18. String txt = null;

  19. // Field 
  20. int FIELD_SIZE = 50;
  21. int FIELD_STEP =  10;

  22. //import peasy.org.apache.commons.math.*;
  23. //import peasy.*;
  24. //import peasy.org.apache.commons.math.geometry.*;
  25. PFont ff;
  26. PImage ww;
  27. ArrayList positions = new ArrayList();
  28. //PeasyCam cam;
  29. String sentence;

  30. public void setup()
  31. {
  32.   size(400, 250, P3D);
  33.   //cam = new PeasyCam(this, 80);
  34.   //cam.setRotations( -0.73, -0.27, 0.30); 
  35.   ff = loadFont("HelveticaNeue-UltraLight-48.vlw");
  36.   sentence = "";
  37.   ww = crImage();
  38.   scan();
  39.   colorMode(RGB, 255);
  40.   frameRate(60);
  41.   fill(0, 0);
  42.   
  43.   controlP5 = new ControlP5(this);
  44.   controlP5.addButton("savefile", 1, 180, 150, 40, 10);
  45.   controlP5.addSlider("sliderA", -10, 10,0, 100, 100, 200, 10);
  46.   
  47.   smooth();
  48. }

  49. public void draw()
  50. {
  51.   background(0);
  52.   lights();
  53.    fill(255);
  54.   text("press any one word → slide bar → save file", 80, 200); // 表示するテキスト, x座標, y座標
  55.   pushMatrix();
  56.   translate(-200, -8, 0);  
  57.     // STL書き出し
  58.   if (setFile != null) {
  59.     beginRaw("unlekker.data.STL", setFile+".stl");
  60.     println("start :"+setFile);
  61.   }
  62.   for(int i = 0; i < positions.size()-1; i++)
  63.   {
  64.     PVector ps = (PVector) positions.get(i);
  65.     translate(ps.x+200, ps.y+50, 0);
  66.     noStroke();
  67.     fill(200);
  68.     box(1, 1, sliderA);
  69.     translate(-(ps.x+200), -(ps.y+50), 0);
  70.   }
  71.     // STL書き出し終了
  72.   if (setFile !=null) {
  73.     endRaw();   
  74.     setFile = null;
  75.     println("end");
  76.   }
  77.   popMatrix();

  78.   controlP5.draw();
  79. }

  80. PImage crImage()
  81. {
  82.   PGraphics pg = createGraphics(400,20,JAVA2D);
  83.   pg.beginDraw();
  84.   pg.background(255);
  85.   pg.fill(250);
  86.   pg.textAlign(CENTER);
  87.   pg.textFont(ff, 16);
  88.   pg.text(sentence, 0, 0, 400, 20);
  89.   pg.endDraw();
  90.   PImage w = createImage(400,20,RGB);
  91.   copy(pg, 0, 0, 400, 20, 0, 0, 400, 20);
  92.   return w;
  93. }

  94. public void scan()
  95. {
  96.   positions.clear(); 
  97.   for(int x = 0; x < ww.width; x++) {
  98.     for(int y = 0; y < ww.height; y++) {
  99.       if(get(x,y) != -65794){
  100.         positions.add(new PVector(x,y,0));     
  101.       }
  102.     }
  103.    }
  104. }

  105. public void keyPressed() {
  106.   if(textWidth(sentence) <= 210){
  107.      sentence += key;
  108.      crImage();
  109.      scan();
  110.      } 
  111.      else
  112.      sentence = "";
  113. }

  114. public void savefile(int theValue) {
  115.   setFile = setFileName();
  116. }

  117. public void sliderA(int v) {
  118.   sliderA = v;
  119. }


file_io.pde
  1. //ファイルを取り込むファンクション 
  2. void fileLoader() {
  3.   //選択ファイルパスのドット以降の文字列を取得
  4.   String ext = getFile.substring(getFile.lastIndexOf('.') + 1);
  5.   //その文字列を小文字にする
  6.   ext.toLowerCase();
  7.   //文字列末尾がstlであれば 
  8.   if (ext.equals("stl")) {
  9.     println(ext);
  10.     stl=new STL(this, getFile);
  11.     // Get polygon data
  12.     poly=stl.getPolyData();
  13.     poly.normalize(FIELD_SIZE*2); // normalize object to 400 radius
  14.     poly.calcBounds();
  15.     poly.center(); // center it around world origin
  16.   }

  17.   //選択ファイルパスを空に戻す
  18.   getFile = null;
  19. }



  20. //ファイル選択画面、選択ファイルパス取得の処理 
  21. String getFileName() {
  22.   //処理タイミングの設定 
  23.   SwingUtilities.invokeLater(new Runnable() { 
  24.     public void run() {
  25.       try {
  26.         //ファイル選択画面表示 
  27.         JFileChooser fc = new JFileChooser(); 
  28.         int returnVal = fc.showOpenDialog(null);
  29.         //「開く」ボタンが押された場合
  30.         if (returnVal == JFileChooser.APPROVE_OPTION) {
  31.           //選択ファイル取得 
  32.           File file = fc.getSelectedFile();
  33.           //選択ファイルのパス取得 
  34.           getFile = file.getPath();
  35.         }
  36.       }
  37.       //上記以外の場合 
  38.       catch (Exception e) {
  39.         //エラー出力 
  40.         e.printStackTrace();
  41.       }
  42.     }
  43.   } 
  44.   );
  45.   //選択ファイルパス取得
  46.   return getFile;
  47. }


  48. //ファイル選択画面、選択ファイルパス取得の処理 
  49. String setFileName() {
  50.   //処理タイミングの設定 
  51.   SwingUtilities.invokeLater(new Runnable() { 
  52.     public void run() {
  53.       try {
  54.         //ファイル選択画面表示 
  55.         JFileChooser fc = new JFileChooser(); 
  56.         int returnVal = fc.showSaveDialog(null);
  57.         //「開く」ボタンが押された場合
  58.         if (returnVal == JFileChooser.APPROVE_OPTION) {
  59.           //選択ファイル取得 
  60.           File file = fc.getSelectedFile();
  61.           //選択ファイルのパス取得 
  62.           setFile = file.getPath();
  63.         }
  64.       }
  65.       //上記以外の場合 
  66.       catch (Exception e) {
  67.         //エラー出力 
  68.         e.printStackTrace();
  69.       }
  70.     }
  71.   } 
  72.   );
  73.   //選択ファイルパス取得
  74.   return setFile;
  75. }



0 コメント:

コメントを投稿

 

Instagram Photostream

Twitter Updates