1 /*******************************************************************************
\r
2 * Copyright (c) 2007- VTT Technical Research Centre of Finland.
\r
3 * All rights reserved. This program and the accompanying materials
\r
4 * are made available under the terms of the Eclipse Public License v1.0
\r
5 * which accompanies this distribution, and is available at
\r
6 * http://www.eclipse.org/legal/epl-v10.html
\r
9 * VTT Technical Research Centre of Finland - initial API and implementation
\r
10 *******************************************************************************/
\r
11 package org.simantics.proconf.g3d.shapeeditor.tools;
\r
13 import java.util.ArrayList;
\r
14 import java.util.Collection;
\r
15 import java.util.Iterator;
\r
16 import java.util.List;
\r
17 import java.util.TreeMap;
\r
18 import java.util.Map.Entry;
\r
20 import javax.vecmath.AxisAngle4d;
\r
21 import javax.vecmath.Point3d;
\r
23 import org.eclipse.jface.action.Action;
\r
24 import org.eclipse.jface.action.IMenuManager;
\r
25 import org.eclipse.jface.action.IToolBarManager;
\r
26 import org.eclipse.jface.action.MenuManager;
\r
27 import org.eclipse.jface.resource.ImageDescriptor;
\r
28 import org.eclipse.swt.SWT;
\r
29 import org.eclipse.swt.layout.FormAttachment;
\r
30 import org.eclipse.swt.layout.FormData;
\r
31 import org.eclipse.swt.layout.FormLayout;
\r
32 import org.eclipse.swt.layout.GridData;
\r
33 import org.eclipse.swt.layout.GridLayout;
\r
34 import org.eclipse.swt.widgets.Composite;
\r
35 import org.eclipse.swt.widgets.Text;
\r
36 import org.eclipse.ui.ISharedImages;
\r
37 import org.eclipse.ui.PlatformUI;
\r
38 import org.simantics.db.Graph;
\r
39 import org.simantics.db.GraphRequestAdapter;
\r
40 import org.simantics.db.GraphRequestStatus;
\r
41 import org.simantics.db.Resource;
\r
42 import org.simantics.layer0.stubs.Property;
\r
43 import org.simantics.layer0.utils.EntityFactory;
\r
44 import org.simantics.layer0.utils.IEntity;
\r
45 import org.simantics.layer0.utils.instantiation.Instance;
\r
46 import org.simantics.layer0.utils.instantiation.InstanceFactory;
\r
47 import org.simantics.proconf.g3d.actions.ContextAction;
\r
48 import org.simantics.proconf.g3d.actions.FocusAction;
\r
49 import org.simantics.proconf.g3d.actions.RemoveAction;
\r
50 import org.simantics.proconf.g3d.actions.RotateAction;
\r
51 import org.simantics.proconf.g3d.actions.TranslateAction;
\r
52 import org.simantics.proconf.g3d.actions.WriteAction;
\r
53 import org.simantics.proconf.g3d.base.EditorContribution;
\r
54 import org.simantics.proconf.g3d.base.G3DAPI;
\r
55 import org.simantics.proconf.g3d.base.G3DTools;
\r
56 import org.simantics.proconf.g3d.base.ThreeDimensionalEditorBase;
\r
57 import org.simantics.proconf.g3d.common.StructuredResourceSelection;
\r
58 import org.simantics.proconf.g3d.csg.stubs.BooleanOperation;
\r
59 import org.simantics.proconf.g3d.csg.stubs.CSGModel;
\r
60 import org.simantics.proconf.g3d.csg.stubs.CSGShape;
\r
61 import org.simantics.proconf.g3d.csg.stubs.Difference;
\r
62 import org.simantics.proconf.g3d.csg.stubs.Intersection;
\r
63 import org.simantics.proconf.g3d.csg.stubs.Primitive;
\r
64 import org.simantics.proconf.g3d.csg.stubs.Union;
\r
65 import org.simantics.proconf.g3d.scenegraph.IGraphicsNode;
\r
66 import org.simantics.proconf.g3d.shapeeditor.Activator;
\r
67 import org.simantics.proconf.g3d.shapeeditor.ShapeEditorResources;
\r
68 import org.simantics.proconf.g3d.shapeeditor.views.ShapeEditorBase;
\r
69 import org.simantics.proconf.g3d.stubs.G3DNode;
\r
70 import org.simantics.proconf.g3d.stubs.Shape;
\r
71 import org.simantics.utils.ui.ErrorLogger;
\r
73 public class CSGModellingContribution implements EditorContribution {
\r
75 private static ImageDescriptor INTERSECTION_IMAGE = Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID,"icons/intersection.png");
\r
76 private static ImageDescriptor UNION_IMAGE = Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/union.png");
\r
77 private static ImageDescriptor DIFFERENCE_IMAGE = Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID,"icons/difference.png");
\r
79 private ShapeEditorBase parent;
\r
81 private List<ContextAction> actions = new ArrayList<ContextAction>();
\r
83 private List<Action> addActions = new ArrayList<Action>();
\r
84 private ContextAction unionAction;
\r
85 private ContextAction differenceAction;
\r
86 private ContextAction intersectionAction;
\r
87 private ContextAction splitAction;
\r
88 private ContextAction linkAction;
\r
89 private ContextAction unlinkAction;
\r
90 private ContextAction translateAction;
\r
91 private ContextAction rotateAction;
\r
92 private ContextAction removeAction;
\r
94 private Composite infoComposite;
\r
95 private Text infoText;
\r
98 public CSGModellingContribution(ThreeDimensionalEditorBase parent) {
\r
99 this.parent = (ShapeEditorBase)parent;
\r
103 public String getName() {
\r
104 return "Shape Editing";
\r
108 public void initialize(Graph graph) {
\r
109 makeActions(graph);
\r
113 public void createControl(Composite parent) {
\r
114 FormLayout flayout = new FormLayout();
\r
115 parent.setLayout(flayout);
\r
116 infoComposite = new Composite(parent, SWT.BORDER);
\r
117 FormData data = new FormData();
\r
118 data.top = new FormAttachment(0, 0);
\r
119 data.left = new FormAttachment(0, 0);
\r
120 data.right = new FormAttachment(100, 0);
\r
121 data.bottom = new FormAttachment(infoComposite, 0, SWT.TOP);
\r
122 this.parent.getRenderingComposite().setLayoutData(data);
\r
123 data = new FormData();
\r
124 data.left = new FormAttachment(0, 0);
\r
125 data.right = new FormAttachment(100, 0);
\r
126 data.bottom = new FormAttachment(100, 0);
\r
128 infoComposite.setLayoutData(data);
\r
129 GridLayout layout = new GridLayout(1,false);
\r
130 layout.marginWidth = 1;
\r
131 layout.marginHeight = 1;
\r
132 infoComposite.setLayout(layout);
\r
133 infoText = new Text(infoComposite, SWT.NONE);
\r
134 GridData gdata = new GridData();
\r
135 gdata.grabExcessHorizontalSpace = true;
\r
136 gdata.horizontalAlignment = SWT.FILL;
\r
137 infoText.setLayoutData(gdata);
\r
141 public void disposeControl() {
\r
142 infoComposite.dispose();
\r
146 public void fillContextMenu(Graph graph, IMenuManager manager, StructuredResourceSelection selection) {
\r
148 if (selection.isEmpty()) {
\r
149 MenuManager addMenu = new MenuManager("Add", "add");
\r
150 addMenu.setRemoveAllWhenShown(false);
\r
151 for (Action a : addActions) {
\r
154 manager.add(addMenu);
\r
159 protected void makeActions(Graph graph) {
\r
160 actions.add(translateAction = new TranslateAction(parent) {
\r
162 public void setInfoText(String text) {
\r
163 infoText.setText(text);
\r
166 actions.add(rotateAction = new RotateAction(parent){
\r
168 public void setInfoText(String text) {
\r
169 infoText.setText(text);
\r
172 actions.add(removeAction = new RemoveAction(parent));
\r
173 actions.add(new FocusAction(parent));
\r
176 IEntity primitive = EntityFactory.create(graph,ShapeEditorResources.csgResource.Primitive);
\r
178 Collection<IEntity> primitives = primitive.getRelatedObjects(graph.getBuiltins().SupertypeOf);
\r
180 TreeMap<String, Resource> sorter = new TreeMap<String, Resource>();
\r
181 for (IEntity p : primitives) {
\r
182 String key = p.getName();
\r
183 if (key.equals(""))
\r
184 key = "ERROR (" + p.getURI() + ")";
\r
185 sorter.put(key, p.getResource());
\r
188 for (Entry<String, Resource> e : sorter.entrySet()) {
\r
189 final String name = e.getKey();
\r
190 final Resource r = e.getValue();
\r
191 Action a = new Action() {
\r
193 public void run() {
\r
194 parent.getSession().asyncWrite(new GraphRequestAdapter() {
\r
196 public GraphRequestStatus perform(Graph g) throws Exception {
\r
198 Instance ins = InstanceFactory.getInstanceOfType(g,r);
\r
199 Resource instance = ins.instantiate(g);
\r
200 Shape shape = new Shape(g,instance);
\r
201 res = shape.getResource();
\r
203 CSGModel model = new CSGModel(g,parent.getModelResource());
\r
204 model.addStatement(ShapeEditorResources.g3dResource.HasChild, shape.getResource());
\r
205 return GraphRequestStatus.transactionComplete();
\r
209 public void handleException(Throwable e) {
\r
210 super.handleException(e);
\r
211 ErrorLogger.defaultLogError("Adding " + name + " failed.", e);
\r
215 public void requestCompleted(GraphRequestStatus status) {
\r
216 parent.getRenderingComposite().getDisplay().asyncExec(new Runnable(){
\r
217 public void run() {
\r
218 parent.getSelectionAdapter().updateSelection(new StructuredResourceSelection(res));
\r
226 a.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
\r
231 unionAction = new WriteAction(parent,false) {
\r
235 public boolean usable(Graph graph,List<Resource> resources) {
\r
236 if (resources.size() >= 2)
\r
241 public GraphRequestStatus doChanges(Graph graph) {
\r
242 List<IGraphicsNode> list = parent.getSelectionAdapter().getSelectedObjects();
\r
243 if (list.size() < 2) {
\r
244 showMessage("Union works between two objects");
\r
245 return GraphRequestStatus.transactionCancel();
\r
247 Resource r1 = list.get(0).getResource();
\r
248 CSGShape shape1 = new CSGShape(graph,r1);
\r
250 G3DNode parent = shape1.getParent();
\r
251 if (parent == null) {
\r
252 showMessage("Primary shape has no parent, don't know what to do");
\r
253 return GraphRequestStatus.transactionCancel();
\r
255 BooleanOperation op = Union.createDefault(graph).toBooleanOperation(); //FIXME : stubcast
\r
256 r = op.getResource();
\r
257 if (createBooleanOp(op, parent, shape1, list))
\r
258 return GraphRequestStatus.transactionComplete();
\r
260 return GraphRequestStatus.transactionCancel();
\r
264 public void afterChanges(GraphRequestStatus status) {
\r
265 parent.getRenderingComposite().getDisplay().asyncExec(new Runnable(){
\r
266 public void run() {
\r
267 parent.getSelectionAdapter().updateSelection(new StructuredResourceSelection(r));
\r
272 unionAction.setText("Union");
\r
273 unionAction.setToolTipText("Union");
\r
274 unionAction.setImageDescriptor(UNION_IMAGE);
\r
276 differenceAction = new WriteAction(parent,false) {
\r
280 public boolean usable(Graph graph,List<Resource> resources) {
\r
281 if (resources.size() >= 2)
\r
286 public GraphRequestStatus doChanges(Graph graph) {
\r
287 List<IGraphicsNode> list = parent.getSelectionAdapter().getSelectedObjects();
\r
288 if (list.size() < 2) {
\r
289 showMessage("Difference works between two objects");
\r
290 return GraphRequestStatus.transactionCancel();
\r
292 CSGShape shape1 = new CSGShape(graph,list.get(0).getResource());
\r
293 G3DNode parent = shape1.getParent();
\r
294 if (parent == null) {
\r
295 showMessage("Primary shape has no parent, don't know what to do");
\r
296 return GraphRequestStatus.transactionCancel();
\r
298 BooleanOperation op = Difference.createDefault(graph).toBooleanOperation(); //FIXME : stubcast
\r
299 r = op.getResource();
\r
300 if (createBooleanOp(op, parent, shape1, list))
\r
301 return GraphRequestStatus.transactionComplete();
\r
303 return GraphRequestStatus.transactionCancel();
\r
307 public void afterChanges(GraphRequestStatus status) {
\r
308 parent.getRenderingComposite().getDisplay().asyncExec(new Runnable(){
\r
309 public void run() {
\r
310 parent.getSelectionAdapter().updateSelection(new StructuredResourceSelection(r));
\r
315 differenceAction.setText("Difference");
\r
316 differenceAction.setToolTipText("Difference");
\r
317 differenceAction.setImageDescriptor(DIFFERENCE_IMAGE);
\r
319 intersectionAction = new WriteAction(parent,false) {
\r
322 public boolean usable(Graph graph,List<Resource> resources) {
\r
323 if (resources.size() >= 2)
\r
328 public GraphRequestStatus doChanges(Graph graph) {
\r
329 List<IGraphicsNode> list = parent.getSelectionAdapter()
\r
330 .getSelectedObjects();
\r
331 if (list.size() < 2) {
\r
332 showMessage("Intersection works between two objects");
\r
333 return GraphRequestStatus.transactionCancel();
\r
335 CSGShape shape1 = new CSGShape(graph,list.get(0).getResource());
\r
336 G3DNode parent = shape1.getParent();
\r
337 if (parent == null) {
\r
338 showMessage("Primary shape has no parent, don't know what to do");
\r
339 return GraphRequestStatus.transactionCancel();
\r
341 BooleanOperation op = Intersection.createDefault(graph).toBooleanOperation(); //FIXME : stubcast
\r
342 r = op.getResource();
\r
343 if (createBooleanOp(op, parent, shape1, list))
\r
344 return GraphRequestStatus.transactionComplete();
\r
346 return GraphRequestStatus.transactionCancel();
\r
350 public void afterChanges(GraphRequestStatus status) {
\r
351 parent.getRenderingComposite().getDisplay().asyncExec(new Runnable(){
\r
352 public void run() {
\r
353 parent.getSelectionAdapter().updateSelection(new StructuredResourceSelection(r));
\r
359 intersectionAction.setText("Intersection");
\r
360 intersectionAction.setToolTipText("Intersection");
\r
361 intersectionAction.setImageDescriptor(INTERSECTION_IMAGE);
\r
363 splitAction = new WriteAction(parent,false) {
\r
366 public boolean usable(Graph graph,List<Resource> resources) {
\r
367 if (resources.size() == 1) {
\r
368 Resource r = resources.iterator().next();
\r
369 IEntity t = EntityFactory.create(graph,r);
\r
370 if (t.isInstanceOf(ShapeEditorResources.csgResource.BooleanOperation)) {
\r
380 public GraphRequestStatus doChanges(Graph graph) {
\r
381 List<IGraphicsNode> list = parent.getSelectionAdapter().getSelectedObjects();
\r
382 if (list.size() != 1) {
\r
383 showMessage("Split requires one object");
\r
384 return GraphRequestStatus.transactionCancel();
\r
386 Resource deletedResource = list.get(0).getResource();
\r
387 IEntity deletedEntity = EntityFactory.create(graph,deletedResource);
\r
388 if (!deletedEntity.isInstanceOf(ShapeEditorResources.csgResource.BooleanOperation)) {
\r
389 showMessage("Split requires boolean operation");
\r
390 return GraphRequestStatus.transactionCancel();
\r
392 Collection<IEntity> parents = deletedEntity.getRelatedObjects(ShapeEditorResources.g3dResource.HasParent);
\r
393 if (parents.size() != 1) {
\r
394 showMessage("Shape has " + parents.size()
\r
395 + " parents, don't know what to do");
\r
396 return GraphRequestStatus.transactionCancel();
\r
398 IEntity parent = parents.iterator().next();
\r
399 // find all shapes and their positions and orientations relative to world coordinates
\r
400 BooleanOperation op = new BooleanOperation(deletedEntity);
\r
401 CSGShape shape1 = op.getMainShape();
\r
402 Point3d shape1WorldPos = G3DTools.getPoint(shape1.getWorldPosition());
\r
403 AxisAngle4d shape1WorldRot = G3DTools.getOrientation(shape1.getWorldOrientation());
\r
404 //System.out.println(shape1WorldPos + " " + shape1WorldRot);
\r
405 Collection<CSGShape> shape2s = op.getSecondaryShape();
\r
407 ArrayList<Point3d> shape2WorldPos = new ArrayList<Point3d>();
\r
408 ArrayList<AxisAngle4d> shape2WorldRot = new ArrayList<AxisAngle4d>();
\r
409 for (CSGShape shape : shape2s) {
\r
410 Point3d pos = G3DTools.getPoint(shape.getWorldPosition());
\r
411 AxisAngle4d rot = G3DTools.getOrientation(shape.getWorldOrientation());
\r
412 shape2WorldPos.add(pos);
\r
413 shape2WorldRot.add(rot);
\r
414 //System.out.println(pos + " " + rot);
\r
417 // removed boolean operation is either connected to model or another boolean operation.
\r
418 CSGModel m = new CSGModel(graph,CSGModellingContribution.this.parent.getModelResource());
\r
419 if (parent.getResource().equals(CSGModellingContribution.this.parent.getModelResource())) {
\r
420 // if deleted boolean operation is connected to the model,
\r
421 // all its children are added to the model
\r
423 m.removeStatement(ShapeEditorResources.g3dResource.HasChild, op);
\r
424 m.addStatement(ShapeEditorResources.g3dResource.HasChild, shape1);
\r
425 for (CSGShape shape2 : shape2s) {
\r
426 m.addStatement(ShapeEditorResources.g3dResource.HasChild, shape2);
\r
430 // deleted boolean operation is connected to another boolean
\r
432 // if the deleted boolean operation is primary child, we'll
\r
433 // must replace it with deleted boolean operations
\r
435 if (!parent.isInstanceOf(ShapeEditorResources.csgResource.BooleanOperation)) {
\r
436 ErrorLogger.defaultLogError("Parent shape is not a boolean operation nor model ?!", null);
\r
437 return GraphRequestStatus.transactionCancel();
\r
439 BooleanOperation parentOp = new BooleanOperation(parent);
\r
440 // we'll have to list all secondary shapes in parent boolean
\r
441 // op so that we can find the correct relatio
\r
442 Collection<IEntity> parentShape2s = parentOp.getRelatedObjects(ShapeEditorResources.csgResource.HasSecondaryShape);
\r
444 if (parentOp.getMainShape().getResource().equals(deletedResource)) {
\r
445 // split boolean operation is the primary child in the
\r
446 // parent boolean operation
\r
447 parent.removeStatement(ShapeEditorResources.csgResource.HasMainShape,deletedResource);
\r
448 // graph.commitChanges(ShapeEditorView.this);
\r
449 parent.addStatement(ShapeEditorResources.csgResource.HasMainShape, shape1);
\r
450 // graph.commitChanges(ShapeEditorView.this);
\r
451 for (CSGShape shape2 : shape2s) {
\r
452 m.addStatement(ShapeEditorResources.g3dResource.HasChild, shape2);
\r
454 } else if (contains(parentShape2s, deletedResource)) {
\r
455 // split boolean operation is one of the secondary
\r
456 // shapes in the parent boolean operation
\r
457 parent.removeStatement(ShapeEditorResources.csgResource.HasSecondaryShape,deletedResource);
\r
458 // graph.commitChanges(ShapeEditorView.this);
\r
459 parent.addStatement(ShapeEditorResources.csgResource.HasSecondaryShape,shape1);
\r
460 // graph.commitChanges(ShapeEditorView.this);
\r
462 // model.getConsistOfShapeSet().add(shape2);
\r
463 for (CSGShape shape2 : shape2s) {
\r
464 m.addStatement(ShapeEditorResources.g3dResource.HasChild, shape2);
\r
467 ErrorLogger.defaultLogError("Parent shape is not a boolean operation nor model ?!", null);
\r
468 return GraphRequestStatus.transactionCancel();
\r
471 deletedEntity.removeStatement(ShapeEditorResources.csgResource.HasMainShape, shape1);
\r
472 for (CSGShape shape2 : shape2s)
\r
473 deletedEntity.removeStatement(ShapeEditorResources.csgResource.HasSecondaryShape, shape2);
\r
475 //System.out.println("Setting original transformations");
\r
476 //G3DTools.setTuple3(shape1.getWorldPosition(), shape1WorldPos);
\r
477 //G3DTools.setOrientation(shape1.getWorldOrientation(), shape1WorldRot);
\r
478 G3DAPI.setWorldTransformation(shape1, shape1WorldPos,shape1WorldRot);
\r
481 for (CSGShape shape : shape2s) {
\r
482 G3DAPI.setWorldTransformation(shape, shape2WorldPos.get(i),shape2WorldRot.get(i));
\r
483 //G3DTools.setTuple3(shape.getWorldPosition(), shape2WorldPos.get(i));
\r
484 //G3DTools.setOrientation(shape.getWorldOrientation(),shape2WorldRot.get(i));
\r
487 return GraphRequestStatus.transactionComplete();
\r
491 splitAction.setText("Split");
\r
492 splitAction.setToolTipText("Split");
\r
493 splitAction.setImageDescriptor(PlatformUI.getWorkbench()
\r
494 .getSharedImages().getImageDescriptor(
\r
495 ISharedImages.IMG_OBJS_INFO_TSK));
\r
497 linkAction = new WriteAction(parent,false) {
\r
500 public boolean usable(Graph graph,List<Resource> resources) {
\r
501 if (resources.size() == 2) {
\r
502 Iterator<Resource> i = resources.iterator();
\r
503 Shape s1 = new Shape(graph,i.next());
\r
504 Shape s2 = new Shape(graph,i.next());
\r
505 if (s1.getRelatedObjects(ShapeEditorResources.g3dResource.GeometryDefinitionOf).size() == 0 &&
\r
506 s2.getRelatedObjects(ShapeEditorResources.g3dResource.GeometryDefinitionOf).size() == 0)
\r
513 public GraphRequestStatus doChanges(Graph graph) {
\r
514 List<IGraphicsNode> list = parent.getSelectionAdapter().getSelectedObjects();
\r
515 if (list.size() != 2) {
\r
516 showMessage("Link works between two objects");
\r
517 return GraphRequestStatus.transactionCancel();
\r
519 Resource r1 = list.get(0).getResource();
\r
520 Resource r2 = list.get(1).getResource();
\r
522 CSGShape shape1 = new CSGShape(graph,r1);
\r
523 CSGShape shape2 = new CSGShape(graph,r2);
\r
525 Point3d p = G3DTools.getPoint(shape2.getWorldPosition());
\r
526 AxisAngle4d aa = G3DTools.getOrientation(shape2.getWorldOrientation());
\r
527 //System.out.println(p + " " + aa);
\r
528 shape2.removeRelatedStatements(ShapeEditorResources.g3dResource.HasParent);
\r
529 r = shape2.getResource();
\r
530 //System.out.println("Link remove commit");
\r
531 //graph.commitChanges(ShapeEditorView.this);
\r
532 //shape1.getChild().add(shape2.toG3DNode()); //FIXME : stubcast
\r
533 shape1.addStatement(ShapeEditorResources.g3dResource.HasChild, shape2);
\r
534 // FIXME : this is needed
\r
535 //System.out.println("Link add commit");
\r
536 //graph.commitChanges(ShapeEditorView.this);
\r
537 //G3DTools.setTuple3(shape2.getWorldPosition(), p);
\r
538 //G3DTools.setOrientation(shape2.getWorldOrientation(), aa);
\r
539 G3DAPI.setWorldTransformation(shape2, p, aa);
\r
540 return GraphRequestStatus.transactionComplete();
\r
544 public void afterChanges(GraphRequestStatus status) {
\r
545 parent.getRenderingComposite().getDisplay().asyncExec(new Runnable(){
\r
546 public void run() {
\r
547 parent.getSelectionAdapter().updateSelection(new StructuredResourceSelection(r));
\r
552 linkAction.setText("Link");
\r
553 linkAction.setToolTipText("Link");
\r
554 linkAction.setImageDescriptor(Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/link.png"));
\r
556 unlinkAction = new WriteAction(parent,false) {
\r
559 public boolean usable(Graph graph,List<Resource> resources) {
\r
560 if (resources.size() == 1) {
\r
561 Iterator<Resource> i = resources.iterator();
\r
562 Shape s1 = new Shape(graph,i.next());
\r
563 return (s1.getRelatedObjects(ShapeEditorResources.g3dResource.GeometryDefinitionOf).size() == 0
\r
564 && s1.getParent() != null && !s1.getParent()
\r
565 .getResource().equals(CSGModellingContribution.this.parent.getModelResource()));
\r
571 public GraphRequestStatus doChanges(Graph graph) {
\r
572 List<IGraphicsNode> list = parent.getSelectionAdapter().getSelectedObjects();
\r
573 if (list.size() != 1) {
\r
574 showMessage("Unlink works with one object");
\r
575 return GraphRequestStatus.transactionCancel();
\r
577 Resource r1 = list.get(0).getResource();
\r
579 CSGShape shape1 = new CSGShape(graph,r1);
\r
580 CSGModel m = new CSGModel(graph,CSGModellingContribution.this.parent.getModelResource());
\r
581 Point3d p = G3DTools.getPoint(shape1.getWorldPosition());
\r
582 AxisAngle4d aa = G3DTools.getOrientation(shape1.getWorldOrientation());
\r
583 shape1.removeRelatedStatements(ShapeEditorResources.g3dResource.HasParent);
\r
584 //graph.commitChanges(ShapeEditorView.this);
\r
585 m.addStatement(ShapeEditorResources.g3dResource.HasChild, shape1);
\r
586 // FIXME : this is needed
\r
587 //graph.commitChanges(ShapeEditorView.this);
\r
588 //G3DTools.setTuple3(shape1.getWorldPosition(), p);
\r
589 //G3DTools.setOrientation(shape1.getWorldOrientation(), aa);
\r
590 G3DAPI.setWorldTransformation(shape1, p, aa);
\r
591 r = shape1.getResource();
\r
592 return GraphRequestStatus.transactionComplete();
\r
596 public void afterChanges(GraphRequestStatus status) {
\r
597 parent.getRenderingComposite().getDisplay().asyncExec(new Runnable(){
\r
598 public void run() {
\r
599 parent.getSelectionAdapter().updateSelection(new StructuredResourceSelection(r));
\r
604 unlinkAction.setText("Unlink");
\r
605 unlinkAction.setToolTipText("Unlink");
\r
606 unlinkAction.setImageDescriptor(Activator.imageDescriptorFromPlugin(
\r
607 Activator.PLUGIN_ID, "icons/unlink.png"));
\r
609 actions.add(unionAction);
\r
610 actions.add(intersectionAction);
\r
611 actions.add(differenceAction);
\r
612 actions.add(splitAction);
\r
613 actions.add(linkAction);
\r
614 actions.add(unlinkAction);
\r
617 boolean contains(ArrayList<Resource> parentShape2sIds, Resource id) {
\r
618 for (int i = 0; i < parentShape2sIds.size(); i++) {
\r
619 if (parentShape2sIds.get(i).equals(id))
\r
626 boolean contains(Collection<IEntity> parentShape2sIds, Resource id) {
\r
627 for (IEntity e : parentShape2sIds) {
\r
628 if (e.getResource().equals(id))
\r
635 private boolean createBooleanOp(BooleanOperation op, G3DNode parent,
\r
636 CSGShape shape1, List<IGraphicsNode> list) {
\r
638 resetShape(op.toShape()); //FIXME : stubcast
\r
639 // new boolean operation is added to the first shape's parent
\r
640 // the parent is the model
\r
642 Point3d refPos = G3DTools.getPoint(shape1.getLocalPosition());
\r
643 G3DTools.setTuple3(op.getLocalPosition(), refPos);
\r
645 G3DTools.addTuple3(shape1.getLocalPosition(), refPos);
\r
646 op.removeRelatedStatements(ShapeEditorResources.csgResource.HasMainShape);
\r
647 op.addStatement(ShapeEditorResources.csgResource.HasMainShape, shape1);
\r
649 if (!replaceShape(parent, shape1.toShape(), op.toShape())) { //FIXME : stubcast
\r
652 //model.getConsistOfSet().remove(shape1);
\r
653 for (int i = 1; i < list.size(); i++) {
\r
654 CSGShape shape2 = new CSGShape(op.getGraph(),list.get(i).getResource());
\r
655 G3DTools.addTuple3(shape2.getLocalPosition(), refPos);
\r
656 G3DNode shape2parent = shape2.getParent();
\r
657 if (shape2parent != null) {
\r
658 // we'll must link before removing or shape will be deleted
\r
659 //op.addStatement(ShapeEditorResources.csgResource.HasSecondaryShape,shape2);
\r
660 //if (!replaceShape(shape2parent, shape2, null)) {
\r
661 // op.removeStatement(ShapeEditorResources.csgResource.HasSecondaryShape, shape2);
\r
662 // // shape couldn't be removed so we'll remove the link
\r
665 if (replaceShape(shape2parent, shape2.toShape(), null)) { //FIXME : stubcast
\r
666 op.addStatement(ShapeEditorResources.csgResource.HasSecondaryShape,shape2);
\r
670 // Commit is not needed because this is final call in all transactions
\r
671 //graph.commitChanges(ShapeEditorView.this);
\r
676 * Replaces or removes parent from shape
\r
677 * @param parent parent containing shape
\r
678 * @param removed the shape
\r
679 * @param added the replacing shape or null
\r
680 * @return true if replacing or removing was successful
\r
681 * @throws TransactionException
\r
683 private boolean replaceShape(G3DNode parent, Shape removed, Shape added) {
\r
684 assert (parent != null);
\r
685 assert (removed != null);
\r
688 //CSGModel m = CSGModelFactory.create(parent.getGraph(),model);
\r
689 if (parent.getResource().equals(this.parent.getModelResource())) {
\r
690 // parent is model (rootnode).
\r
691 // shape is connected to it with "Has Child" relation
\r
692 parent.removeStatement(ShapeEditorResources.g3dResource.HasChild, removed);
\r
693 if (added != null) {
\r
694 parent.addStatement(ShapeEditorResources.g3dResource.HasChild, added);
\r
697 // the first shape's parent is boolean operation
\r
698 // so we must know if its connected as a primary shape or as a secondary shape
\r
699 if (!parent.isInstanceOf(ShapeEditorResources.csgResource.BooleanOperation)) {
\r
700 ErrorLogger.defaultLogError("Parent shape is not a boolean operation nor model ?!",null);
\r
703 BooleanOperation parentOp = new BooleanOperation(parent);
\r
704 // listing all secondary shapes in the parent boolean operation
\r
705 Collection<CSGShape> parentShape2s = parentOp.getSecondaryShape();
\r
706 ArrayList<Resource> parentShape2sIds = new ArrayList<Resource>();
\r
707 for (CSGShape shape2 : parentShape2s)
\r
708 parentShape2sIds.add(shape2.getResource());
\r
709 if (parentOp.getMainShape().getResource().equals(removed.getResource())) {
\r
710 if (added == null) {
\r
713 parent.removeStatement(ShapeEditorResources.csgResource.HasMainShape, removed);
\r
714 parent.addStatement(ShapeEditorResources.csgResource.HasMainShape, added);
\r
716 } else if (contains(parentShape2sIds, removed.getResource())) {
\r
718 parent.removeStatement(ShapeEditorResources.csgResource.HasSecondaryShape, removed);
\r
719 parent.addStatement(ShapeEditorResources.csgResource.HasSecondaryShape, added);
\r
722 ErrorLogger.defaultLogError("Parent shape is not a boolean operation nor model ?!",null);
\r
723 //coreTC.cancelTransaction();
\r
731 * Resets shape to identity rotation and zero translation
\r
735 private void resetShape(Shape shape) {
\r
736 G3DTools.resetTransformation(shape);
\r
737 Graph graph = shape.getGraph();
\r
738 if (shape.isInstanceOf(ShapeEditorResources.csgResource.Primitive)) {
\r
739 Primitive prim = new Primitive(shape);
\r
740 Collection<Property> c = prim.getSizingProperty();
\r
742 ErrorLogger.getDefault().logWarning("Shape does not contain sizing properties.", null);
\r
744 for (Property p : c) {
\r
745 if (p.isInstanceOf(graph.getBuiltins().Double)) {
\r
746 graph.setScalarDouble(p.getResource(), 1.0);
\r
747 } else if (p.isInstanceOf(graph.getBuiltins().Integer)) {
\r
748 graph.setScalarInteger(p.getResource(), 1);
\r
750 ErrorLogger.getDefault().logWarning("Cannot handle sizing property " + p.getName() , null);
\r
757 protected void showMessage(String s) {
\r
758 parent.showMessage(s);
\r
762 public Collection<ContextAction> getActions() {
\r
768 public void fillLocalToolBar(IToolBarManager manager) {
\r
773 public void fillLocalPullDown(IMenuManager manager) {
\r
778 public void dispose() {
\r
783 public void run() {
\r