Re: use private elements of the classes HomePane and HomeController for made a plugin to export the furniture in separated obj

by chicosRV » Fri Jun 22, 2012 5:04 pm

this is what i have done

i modified the original code of the methods export to obj(it originally exported everything in one obj)

but what i really need is to made that function in a plugin

when a took the code for made the pluggin there were a lot of element that get selected like error

the plugin class only have the home and the userPreferences


this is the code

class Home Controller

Code: Select all

 public void exportToOBJ() {
     final String objName = getView().showExportToOBJDialog(this.home.getName());    
     final String dir = reduceToRoot(objName);
     HomePane.dir = dir;
    
    //Count Furniture//
    
    countFurniture();
    writeXMLEden3D("ss");
    
      
      if (objName != null) {
        
        // Export 3D view in a threaded task
        Callable<Void> exportToObjTask = new Callable<Void>() {
              public Void call() throws RecorderException {
                getView().exportToOBJ(HomePane.dir);
                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());
      }      
     
  }





class HomePane

Code: Select all

public static void exportHomeToFile(Home home, String objFile, String header) throws RecorderException {
      OBJWriter writer = null;
      boolean exportInterrupted = false;

      String[] names = new String[HomePane.counter];
      names[0] = "Room";
      int c=1;
      for(HomePieceOfFurniture piece : home.getFurniture()) {
        if(piece.isVisible()) {
          names[c] = piece.getName();
          c++;
        }

      }



      for(int j=0 ; j < HomePane.counter; j++ ) {
        try {
          HomePane.objName = names[j];
          writer = new OBJWriter(objFile+HomePane.objName+".obj", header, -1);

          List<Selectable> emptySelection = Collections.emptyList();

          home.setSelectedItems(emptySelection);



          if (HomePane.objName.equals("Room"))
          {


            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 (HomePane.objName.equals("Room"))
          {
            i = 0;

            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()) {

              // 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;

          if (!HomePane.objName.equals("Room"))
          {

            //Write 3D furniture
            for (HomePieceOfFurniture piece : home.getFurniture()) {
              if (piece.isVisible() && HomePane.objName.equals(piece.getName())) {

                // 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);
            }
          }
        }  



      } 
    }