Print at Dec 20, 2025, 11:52:00 AM

Posted by Mitsaki at Aug 20, 2011, 7:38:49 AM
Re: How to run a new class in SH3D source
package Blender;
/*
* Read_File_and_Save.java Aug 11, 2011
*
* Creation of a class that reads a text file and saves it in an array
* Afterwards, it creates a SH3D project which is according to user's preferences.
*/

/**
* @author Dimitra Micha
*/

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

//import com.eteks.sweethome3d.io.DefaultUserPreferences;
import com.eteks.sweethome3d.model.CollectionEvent;
import com.eteks.sweethome3d.model.CollectionListener;
import com.eteks.sweethome3d.model.Home;
import com.eteks.sweethome3d.model.Room;
import com.eteks.sweethome3d.model.TextureImage;
//import com.eteks.sweethome3d.model.UserPreferences;
import com.eteks.sweethome3d.model.Wall;


public class FileRead {

/** Basic external class used to create a room and import furniture in it according to user's preferences.
* @throws Exception
*
*
*/
public static void main(String args[])
{

// Maximum elements of the array:108 (200 obligatory, 88 optional)
String[] str = new String[108];

try {
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("/Users/dimitramicha/Desktop/SweetHome3D1.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
int i=0;
while ((strLine = br.readLine()) != null)
{
// Print the content on the console
//System.out.println (strLine);
//System.out.println(i);
str = strLine;
System.out.println(str);
i++;
// Check each line - Don't forget to make them comments afterwards!!!!!
}

//Close the input stream
in.close();
}
catch (Exception e)
{
//Catch exception if any
System.err.println("Error: " + e.getMessage());
}

String Shape_of_Room = str[2];
String Shiny_Matt = str[3];
String Right_Wall_Color = str[5];
String Right_Wall_Texture = str[6];
String Left_Wall_Color = str[7];
String Left_Wall_Texture = str[8];
String North_Wall_Color = str[9];
String North_Wall_Texture = str[10];
String South_Wall_Color = str[11];
String South_Wall_Texture = str[12];
String Ceiling_Display = str[13];
String Ceiling_Texture = str[14];
String Floor_Display = str[15];
String Floor_Texture = str[16];

// Save in another array the list of furniture. The maximum of this list is 91
// (108 values in total - 17 already mentioned above)

int j;
for (j=0;j<91;j++) {
String List_of_Furniture = (String) str[j+17];
}
//Check the Shape_of_Room
//Else - If Statement

if (Shape_of_Room == "Parallelogram") {

// Create a home and a wall listener that updates lists when notified
Home Parallelogram = new Home();

final List<Wall> addedWalls = new ArrayList<Wall>();
final List<Wall> deletedWalls = new ArrayList<Wall>();
final List<Wall> updatedWalls = new ArrayList<Wall>();
final PropertyChangeListener wallChangeListener = new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent ev) {
updatedWalls.add((Wall)ev.getSource());
}
};

Parallelogram.addWallsListener(new CollectionListener<Wall> () {
public void collectionChanged(CollectionEvent<Wall> ev) {
switch (ev.getType()) {
case ADD :
addedWalls.add(ev.getItem());
ev.getItem().addPropertyChangeListener(wallChangeListener);
break;
case DELETE :
deletedWalls.add(ev.getItem());
ev.getItem().removePropertyChangeListener(wallChangeListener);
break;
}
}
});

//Create a Room
Room Parallelogram_Room = new Room(null);
Parallelogram_Room.addPoint(0, 0);
Parallelogram_Room.addPoint(0, 400);
Parallelogram_Room.addPoint(625, 400);
Parallelogram_Room.addPoint(625, 400);
Parallelogram_Room.addPoint(0, 0);

Parallelogram.addRoom(Parallelogram_Room);

//Creation Walls
Wall Left_Wall = new Wall(0, 0, 0, 400, (float) 7.62);
Wall North_Wall = new Wall(0, 400, 625, 400, (float) 7.62);
Wall Right_Wall = new Wall(625, 400, 625 ,0 , (float) 7.62);
Wall South_Wall = new Wall(625, 0, 0, 0, (float) 7.62);

// Add them to home
Parallelogram.addWall(Left_Wall);
Parallelogram.addWall(North_Wall);
Parallelogram.addWall(Right_Wall);
Parallelogram.addWall(South_Wall);

// Set as User Preferences the default User Preferences
//UserPreferences preferences = new DefaultUserPreferences();

// Set as Texture image the first texture that is available

//TextureImage firstTexture = preferences.getTexturesCatalog().getCategories().get(0).getTexture(0);

// Join end point of one wall to start point of the other wall (4 times - 4 walls)
Left_Wall.setWallAtEnd(North_Wall);
North_Wall.setWallAtEnd(Right_Wall);
Right_Wall.setWallAtEnd(South_Wall);
South_Wall.setWallAtEnd(Left_Wall);

if (Floor_Display == "true") {
Parallelogram_Room.setFloorVisible(true);
}

if (Shiny_Matt == "Matt") {
Left_Wall.setRightSideShininess(0);
North_Wall.setRightSideShininess(0);
Right_Wall.setRightSideShininess(0);
South_Wall.setRightSideShininess(0);
}
else if (Shiny_Matt == "Shiny") {

Left_Wall.setRightSideShininess(1);
North_Wall.setRightSideShininess(1);
Right_Wall.setRightSideShininess(1);
South_Wall.setRightSideShininess(1);
}

// http://cloford.com/resources/colours/500col.htm Color(int rgb)
// Create a string array which will be used to check the color of each wall
// Create an array of integers which includes the rgb numbers of the colors

String[] color = {"Grey","White","Red","Green","Yellow","Blue"};
int[] rgb = {13882323,16119285,6974207,10210715,9170175,15641692};

int k;

for(k=0;k<6;k++)
{
if (Left_Wall_Color == color[k]) {

Left_Wall.setRightSideColor(rgb[k]);
}
if (North_Wall_Color == color[k]) {

North_Wall.setRightSideColor(rgb[k]);
}
if (Right_Wall_Color == color[k]) {

Right_Wall.setRightSideColor(rgb[k]);
}
if (South_Wall_Color == color[k]) {

South_Wall.setRightSideColor(rgb[k]);
}

};
}
(it continues with the same code, else -if state but different objects are created)
}