Re: How to export to .obj all furniture seperately.

by Mitsaki » Fri Jan 20, 2012 5:08 pm

So, what I finally did and it worked is:

I first added in the HomePane.java

Code: Select all

 // Used in the export
  public static int m = 0;
  public static int o = 0;
  public static String a;

  public static String objFile
;


Afterwards, I changed the HomeController.java and especially exporttoObj

Code: Select all

 public void exportToOBJ() {
   
    // Added from this point...
    // In the beginning k=0, l=0, m=0
    // Count how many pieces of furniture are placed in the house during the first control
    //-------------------------------COUNT THE FURNITURE------------------------------------
    HomePane.m = 0;
    HomePane.objFile = "";
    for (HomePieceOfFurniture piece : home.getFurniture()) {
       if (piece.isVisible()) {
              // In the end m shows how many furniture there are
              HomePane.m++;
       }
    }
    
    // I add one, so that I can store "Room" in it.
    HomePane.m = HomePane.m + 1;
    System.out.printf (HomePane.m + "\n");
    //------------------------------------SAVE THE ARRAY------------------------------------
    // A table used to save the names of the furniture with the "m" that we counted before
    String [] temp = new String[HomePane.m + 1];
    temp[0] = "Room";
    
    HomePane.o = 1;
    for (HomePieceOfFurniture piece : home.getFurniture()) {
       if (piece.isVisible()) {
           temp[HomePane.o] = piece.getName();
           //System.out.printf (HomePane.o + "\n");
           // System.out.printf(temp[HomePane.o] + "\n");
           HomePane.o++;
       }
    }
    
    String [] Furniture = new String[HomePane.m];
    int z;
    for (z = 0; z < HomePane.m ; z++)
    {
      Furniture[z] = temp[z];
    }
    
    // l is used for the array Furniture[m] and it will be used again
    HomePane.o = 0;
    
    // For the room
    //-----------------------------------CALL THE FUNCTION----------------------------------
    for (HomePane.o = 0; HomePane.o < HomePane.m ; HomePane.o++)
    {
      // System.out.printf (HomePane.o + "\n");
      // System.out.printf(Furniture[HomePane.o] + "\n");
      HomePane.a = Furniture[HomePane.o];
      HomePane.objFile = Furniture[HomePane.o];
      // System.out.printf(objFile + "\n");
      // Use a clone of home to ignore selection
      final String objName = getView().showExportToOBJDialog(this.home.getName());
      if (objName != null) {
        // Export 3D view in a threaded task
        Callable<Void> exportToObjTask = new Callable<Void>() {
              public Void call() throws RecorderException {
                getView().exportToOBJ(objName);
                return null;
              }
            };
        ThreadedTaskController.ExceptionHandler exceptionHandler = 
            new ThreadedTaskController.ExceptionHandler() {
              public void handleException(Exception ex) {
                if (!(ex instanceof InterruptedRecorderException)) {
                  if (ex instanceof RecorderException) {
                    String message = preferences.getLocalizedString(
                        HomeController.class, "exportToOBJError", objName);
                    getView().showError(message);
                  } else {
                    ex.printStackTrace();
                  }
                }
              }
            };
        new ThreadedTaskController(exportToObjTask, 
            this.preferences.getLocalizedString(HomeController.class, "exportToOBJMessage"), exceptionHandler, 
            this.preferences, this.viewFactory).executeTask(getView());
       }
     }
    
   }


and then I changed the HomePane.java and especially private static class OBJExporter

Code: Select all

  private static class OBJExporter {
    public static void exportHomeToFile(Home home, String objFile, String header) throws RecorderException {
      OBJWriter writer = null;
      boolean exportInterrupted = false;
      try {
        writer = new OBJWriter(objFile, header, -1);
        // System.out.printf(objFile + "\n");
        if (objFile.equals("./My_Home/Room.obj"))
        {
          System.out.printf(objFile + "\n");
        List<Selectable> emptySelection = Collections.emptyList();
        home.setSelectedItems(emptySelection);
        if (home.getWalls().size() > 0) {
          // Create a not alive new ground to be able to explore its coordinates without setting capabilities
          Rectangle2D homeBounds = getExportedHomeBounds(home);
          Ground3D groundNode = new Ground3D(home, 
              (float)homeBounds.getX(), (float)homeBounds.getY(), 
              (float)homeBounds.getWidth(), (float)homeBounds.getHeight(), true);
          writer.writeNode(groundNode, "ground");
          }
        }
        
        // Write 3D walls 
        int i;
        
        
        // The first time, we want to export the only the walls and the room
        if (objFile.equals("./My_Home/Room.obj"))
        {
          i = 0;
          System.out.printf("Bhka sto walls\n"); 
          System.out.printf(objFile + "\n");
          for (Wall wall : home.getWalls()) {
            // Create a not alive new wall to be able to explore its coordinates without setting capabilities 
            Wall3D wallNode = new Wall3D(wall, home, true, true);
            writer.writeNode(wallNode, "wall_" + ++i);
            }
        
          // Write 3D rooms 
          i = 0;
          for (Room room : home.getRooms()) {
          System.out.printf("Bhka sto Room\n");
          // Create a not alive new room to be able to explore its coordinates without setting capabilities 
          Room3D roomNode = new Room3D(room, home, false, true, true);
          writer.writeNode(roomNode, "room_" + ++i);
          }
        }
        
        i = 0;
        System.out.printf(objFile + "\n");
        if (!objFile.equals("./My_Home/Room.obj"))
        {
          System.out.printf("Bhka sto Furniture1\n");
        //Write 3D furniture 
        for (HomePieceOfFurniture piece : home.getFurniture()) {
          if (piece.isVisible() && objFile.equals("." + File.separator + "My_Home" + File.separator + piece.getName() + ".obj")) {
            System.out.printf("Bhka sto Furniture2\n");
            // System.out.printf(objFile + "\n");
            // Create a not alive new piece to be able to explore its coordinates without setting capabilities
            // I set as home = null
            HomePieceOfFurniture3D pieceNode = new HomePieceOfFurniture3D(piece, home, true, true);
            writer.writeNode(pieceNode);
            }
          }
        }
      } catch (InterruptedIOException ex) {
        exportInterrupted = true;
        throw new InterruptedRecorderException("Export to " + objFile + " interrupted");
      } catch (IOException ex) {
        throw new RecorderException("Couldn't export to OBJ in " + objFile, ex);
      } finally {
        if (writer != null) {
          
          try {
            writer.close();
            // Delete the file if exporting is interrupted
            if (exportInterrupted) {
              new File(objFile).delete();
            }
          } catch (IOException ex) {
            throw new RecorderException("Couldn't export to OBJ in " + objFile, ex);
          }
        }
      }   
    }


That's more or less it.