use of org.concord.energy3d.undo.AddMultiplePartsCommand in project energy3d by concord-consortium.
the class Scene method importFile.
public void importFile(final URL url) throws Exception {
if (PrintController.getInstance().isPrintPreview()) {
MainPanel.getInstance().getPreviewButton().setSelected(false);
while (!PrintController.getInstance().isFinished()) {
Thread.yield();
}
}
if (url != null) {
long max = -1;
for (final HousePart x : Scene.getInstance().parts) {
if (x.getId() > max) {
max = x.getId();
}
}
if (max < 0) {
max = 0;
}
System.out.print("Opening..." + url + "...");
final ObjectInputStream in = new ObjectInputStream(url.openStream());
final Scene instance = (Scene) in.readObject();
in.close();
// instance.cleanup();
instance.upgradeSceneToNewVersion();
if (url != null) {
final AddMultiplePartsCommand cmd = new AddMultiplePartsCommand(new ArrayList<HousePart>(instance.getParts()), url);
double cx = 0;
double cy = 0;
int count = 0;
for (final HousePart p : instance.getParts()) {
p.setId(max + p.getId());
Scene.getInstance().parts.add(p);
originalHouseRoot.attachChild(p.getRoot());
if (p instanceof Foundation || p instanceof Tree || p instanceof Human) {
final Vector3 c = p.getAbsCenter();
cx += c.getX();
cy += c.getY();
count++;
}
}
final Vector3 position = SceneManager.getInstance().getPickedLocationOnLand();
if (position != null) {
final Vector3 shift = position.subtractLocal(count == 0 ? new Vector3(0, 0, 0) : new Vector3(cx / count, cy / count, 0));
for (final HousePart p : instance.getParts()) {
if (p instanceof Foundation || p instanceof Tree || p instanceof Human) {
for (int i = 0; i < p.getPoints().size(); i++) {
p.getPoints().get(i).addLocal(shift);
}
}
}
}
redrawAll = true;
SceneManager.getInstance().getUndoManager().addEdit(cmd);
}
root.updateWorldBound(true);
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
MainPanel.getInstance().getEnergyButton().setSelected(false);
}
});
setEdited(true);
} else {
JOptionPane.showMessageDialog(MainFrame.getInstance(), "URL doesn't exist.", "Error", JOptionPane.ERROR_MESSAGE);
}
}
Aggregations