1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.g3d.csg.actions;
14 import java.util.Collection;
15 import java.util.HashMap;
16 import java.util.Iterator;
19 import javax.vecmath.Quat4d;
20 import javax.vecmath.Vector3d;
22 import org.eclipse.jface.action.Action;
23 import org.simantics.g3d.csg.scenegraph2.CSGparentNode;
24 import org.simantics.g3d.csg.scenegraph2.CSGrootNode;
25 import org.simantics.g3d.csg.scenegraph2.ICSGnode;
26 import org.simantics.utils.ui.ExceptionUtils;
28 public class AddBooleanOpAction2 extends Action {
30 Class<? extends CSGparentNode> booleanClass;
31 Collection<ICSGnode> nodes;
33 public AddBooleanOpAction2(CSGrootNode root, Class<? extends CSGparentNode> booleanClass, Collection<ICSGnode> nodes) {
35 String name = booleanClass.getSimpleName();
36 if (name.endsWith("Node"))
37 name = name.substring(0,name.length()-4);
39 this.booleanClass = booleanClass;
42 if (nodes.size() != 2)
44 for (ICSGnode node : nodes) {
45 if (!node.getParent().equals(root))
53 CSGparentNode booleanNode = booleanClass.newInstance();
54 Map<ICSGnode,Vector3d> positions = new HashMap<ICSGnode, Vector3d>();
55 Map<ICSGnode,Quat4d> orientations = new HashMap<ICSGnode, Quat4d>();
56 for (ICSGnode node : nodes) {
57 positions.put(node, node.getWorldPosition());
58 orientations.put(node, node.getWorldOrientation());
59 //root.remChild(node);
62 Iterator<ICSGnode> iter = nodes.iterator();
63 booleanNode.addPrimaryChild(iter.next());
64 booleanNode.addSecondaryChild(iter.next());
66 String name = root.getUniqueName(booleanNode.getClass().getSimpleName());
67 booleanNode.setName(name);
69 root.addChild(booleanNode);
70 for (ICSGnode node : nodes) {
71 node.setWorldPosition(positions.get(node));
72 node.setWorldOrientation(orientations.get(node));
74 root.getNodeMap().commit("Add Boolean Op");
75 } catch (Exception e) {
76 ExceptionUtils.logAndShowError("Cannot create boolean operation.", e);