Search in sources :

Example 1 with AddMultiplePartsCommand

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);
    }
}
Also used : Human(org.concord.energy3d.model.Human) ReadOnlyVector3(com.ardor3d.math.type.ReadOnlyVector3) Vector3(com.ardor3d.math.Vector3) Tree(org.concord.energy3d.model.Tree) Foundation(org.concord.energy3d.model.Foundation) AddMultiplePartsCommand(org.concord.energy3d.undo.AddMultiplePartsCommand) HousePart(org.concord.energy3d.model.HousePart) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

Vector3 (com.ardor3d.math.Vector3)1 ReadOnlyVector3 (com.ardor3d.math.type.ReadOnlyVector3)1 ObjectInputStream (java.io.ObjectInputStream)1 Foundation (org.concord.energy3d.model.Foundation)1 HousePart (org.concord.energy3d.model.HousePart)1 Human (org.concord.energy3d.model.Human)1 Tree (org.concord.energy3d.model.Tree)1 AddMultiplePartsCommand (org.concord.energy3d.undo.AddMultiplePartsCommand)1