confused High priority How to export to .obj all furniture seperately.

This topic has been viewed 5711 times and has 4 replies
Thread status: Active
Questions about extending features and developing plug-ins [img]http://www.sweethome3d.com/images/flags/en_small.gif[/img]
Post Reply New Topic
Mitsaki
Posts: 62
Joined: Tue Jul 26, 2011 2:27 pm

How to export to .obj all furniture seperately.

Post by Mitsaki »

Hello,

what I am trying to do now (you can also see my previous question) is to export first the home with the walls and then one by one all the furniture. So, what I have changed is the script HomePane.java, first by setting 4 public variables.

Code: Select all

// Used in the export
  public static int k = 0;
  public static int m = 0;
  public static int l = 0;
  public static String a;
and afterwards I have changed the following

Code: Select all

/**
* Exports the objects of the 3D view to the given OBJ file.
*/
public void exportToOBJ(String objFile) throws RecorderException {
System.out.printf("Exporttoobj\n");
String header = this.preferences != null
? this.preferences.getLocalizedString(HomePane.class,"exportToOBJ.header", new Date()): "";

// 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
objFile = "";
for (HomePieceOfFurniture piece : home.getFurniture()) {
if (piece.isVisible()) {
// In the end m shows how many furniture there are
m++;
}
}

// A table used to save the names of the furniture with the "m" that we counted before
String [] Furniture = new String[m]; 

a = "";
for (HomePieceOfFurniture piece : home.getFurniture()) {
if (piece.isVisible()) {
// "a" is used to save the name of the furniture piece
a = piece.getName();
Furniture[l] = a;
//System.out.printf(Furniture[m]);
l++;
}
}
// l is used for the array Furniture[m]
l = 0;

//for(k=0;k<l+1;k++)
//{
// Use a clone of home to ignore selection
// For the room
if(k==0)
{
objFile = "room";
//System.out.printf(objFile + "\n");
OBJExporter.exportHomeToFile(home.clone(), objFile, header);
}
if(k>0 && k<=m)
{
for (HomePieceOfFurniture piece : home.getFurniture()) {
if (piece.isVisible()) {
// "a" is used to save the name of the furniture piece
String a = piece.getName();
Furniture[l] = a;
objFile = Furniture[l];
OBJExporter.exportHomeToFile(home.clone(), objFile, header);
//System.out.printf(Furniture[m]);
l++;
}
//}
}
}
}

Code: Select all

/**
* Export to OBJ in a separate class to be able to run HomePane without Java 3D classes.
*/
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);

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");
}
int i = 0;
// The first time, we want to export the only the walls and the room
if (k==0)
{
System.out.printf("Walls\n");


// Write 3D walls 

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);
}
}
System.out.println(m);
i = 0;
if (k>0 && k<=m)
{
//Write 3D furniture 
for (HomePieceOfFurniture piece : home.getFurniture()) {
if (piece.isVisible() && a == piece.getName()) {
System.out.print(k + "\n");
System.out.printf("Furniture\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, null, true, true);
writer.writeNode(pieceNode);
k++;
}
}
}

if (k==0)
{
// Write 3D rooms 
i = 0;
for (Room room : home.getRooms()) {
System.out.printf("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);
k++;
}
}
} 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);
}
}
} 
}
The result is that everything is exported, I mean all the furniture and the room seperately, but the saved file has kind: Document, according to info. Furthermore, I can open it with txt.

Could give me a hint for what I am doing wrong?
Mitsaki
Posts: 62
Joined: Tue Jul 26, 2011 2:27 pm

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

Post by Mitsaki »

No need to try to answer this question. Tomorrow, I will try to explain what I did finally:)
Mitsaki
Posts: 62
Joined: Tue Jul 26, 2011 2:27 pm

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

Post by Mitsaki »

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.
chicosRV
Posts: 14
Joined: Fri May 25, 2012 4:37 pm

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

Post by chicosRV »

hello I used the last code you posted and yes, everything is exported seperately but all the .obj are in blank, only with a header and i am confused about it, are you sure this code work allright???, please i need a solution.
Mitsaki
Posts: 62
Joined: Tue Jul 26, 2011 2:27 pm

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

Post by Mitsaki »

Well, maybe you should check it again, because I use these furniture to import them in a program called blender and all of them appear perfectly.
Post Reply New Topic

Who is online

Users browsing this forum: No registered users and 2 guests