package org.simantics.g3d.csg.actions; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import javax.vecmath.Quat4d; import javax.vecmath.Vector3d; import org.eclipse.jface.action.Action; import org.simantics.g3d.csg.scenegraph2.CSGparentNode; import org.simantics.g3d.csg.scenegraph2.CSGrootNode; import org.simantics.g3d.csg.scenegraph2.ICSGnode; import org.simantics.utils.ui.ExceptionUtils; public class AddBooleanOpAction2 extends Action { CSGrootNode root; Class booleanClass; Collection nodes; public AddBooleanOpAction2(CSGrootNode root, Class booleanClass, Collection nodes) { super(); String name = booleanClass.getSimpleName(); if (name.endsWith("Node")) name = name.substring(0,name.length()-4); setText(name); this.booleanClass = booleanClass; this.nodes = nodes; this.root = root; if (nodes.size() != 2) setEnabled(false); for (ICSGnode node : nodes) { if (!node.getParent().equals(root)) setEnabled(false); } } @Override public void run() { try { CSGparentNode booleanNode = booleanClass.newInstance(); Map positions = new HashMap(); Map orientations = new HashMap(); for (ICSGnode node : nodes) { positions.put(node, node.getWorldPosition()); orientations.put(node, node.getWorldOrientation()); //root.remChild(node); node.deattach(); } Iterator iter = nodes.iterator(); booleanNode.addPrimaryChild(iter.next()); booleanNode.addSecondaryChild(iter.next()); String name = root.getUniqueName(booleanNode.getClass().getSimpleName()); booleanNode.setName(name); root.addChild(booleanNode); for (ICSGnode node : nodes) { node.setWorldPosition(positions.get(node)); node.setWorldOrientation(orientations.get(node)); } root.getNodeMap().commit(); } catch (Exception e) { ExceptionUtils.logAndShowError("Cannot create boolean operation.", e); } } }