1 package org.simantics.proconf.g3d.shapeeditor.tools;
\r
3 import java.util.ArrayList;
\r
4 import java.util.Collection;
\r
5 import java.util.Iterator;
\r
6 import java.util.List;
\r
7 import java.util.TreeMap;
\r
8 import java.util.Map.Entry;
\r
10 import javax.vecmath.AxisAngle4d;
\r
11 import javax.vecmath.Point3d;
\r
13 import org.eclipse.jface.action.Action;
\r
14 import org.eclipse.jface.action.IMenuManager;
\r
15 import org.eclipse.jface.action.IToolBarManager;
\r
16 import org.eclipse.jface.action.MenuManager;
\r
17 import org.eclipse.jface.resource.ImageDescriptor;
\r
18 import org.eclipse.swt.SWT;
\r
19 import org.eclipse.swt.layout.FormAttachment;
\r
20 import org.eclipse.swt.layout.FormData;
\r
21 import org.eclipse.swt.layout.FormLayout;
\r
22 import org.eclipse.swt.layout.GridData;
\r
23 import org.eclipse.swt.layout.GridLayout;
\r
24 import org.eclipse.swt.widgets.Composite;
\r
25 import org.eclipse.swt.widgets.Text;
\r
26 import org.eclipse.ui.ISharedImages;
\r
27 import org.eclipse.ui.PlatformUI;
\r
28 import org.simantics.db.Graph;
\r
29 import org.simantics.db.GraphRequestAdapter;
\r
30 import org.simantics.db.GraphRequestStatus;
\r
31 import org.simantics.db.Resource;
\r
32 import org.simantics.layer0.stubs.Property;
\r
33 import org.simantics.layer0.utils.EntityFactory;
\r
34 import org.simantics.layer0.utils.IEntity;
\r
35 import org.simantics.layer0.utils.instantiation.Instance;
\r
36 import org.simantics.layer0.utils.instantiation.InstanceFactory;
\r
37 import org.simantics.proconf.g3d.actions.ContextAction;
\r
38 import org.simantics.proconf.g3d.actions.FocusAction;
\r
39 import org.simantics.proconf.g3d.actions.RemoveAction;
\r
40 import org.simantics.proconf.g3d.actions.RotateAction;
\r
41 import org.simantics.proconf.g3d.actions.TranslateAction;
\r
42 import org.simantics.proconf.g3d.actions.WriteAction;
\r
43 import org.simantics.proconf.g3d.base.EditorContribution;
\r
44 import org.simantics.proconf.g3d.base.G3DAPI;
\r
45 import org.simantics.proconf.g3d.base.G3DTools;
\r
46 import org.simantics.proconf.g3d.base.ThreeDimensionalEditorBase;
\r
47 import org.simantics.proconf.g3d.common.StructuredResourceSelection;
\r
48 import org.simantics.proconf.g3d.csg.stubs.BooleanOperation;
\r
49 import org.simantics.proconf.g3d.csg.stubs.CSGModel;
\r
50 import org.simantics.proconf.g3d.csg.stubs.CSGShape;
\r
51 import org.simantics.proconf.g3d.csg.stubs.Difference;
\r
52 import org.simantics.proconf.g3d.csg.stubs.Intersection;
\r
53 import org.simantics.proconf.g3d.csg.stubs.Primitive;
\r
54 import org.simantics.proconf.g3d.csg.stubs.Union;
\r
55 import org.simantics.proconf.g3d.scenegraph.IGraphicsNode;
\r
56 import org.simantics.proconf.g3d.shapeeditor.Activator;
\r
57 import org.simantics.proconf.g3d.shapeeditor.ShapeEditorResources;
\r
58 import org.simantics.proconf.g3d.shapeeditor.views.ShapeEditorBase;
\r
59 import org.simantics.proconf.g3d.stubs.G3DNode;
\r
60 import org.simantics.proconf.g3d.stubs.Shape;
\r
61 import org.simantics.utils.ui.ErrorLogger;
\r
63 public class CSGModellingContribution implements EditorContribution {
\r
65 private static ImageDescriptor INTERSECTION_IMAGE = Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID,"icons/intersection.png");
\r
66 private static ImageDescriptor UNION_IMAGE = Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/union.png");
\r
67 private static ImageDescriptor DIFFERENCE_IMAGE = Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID,"icons/difference.png");
\r
69 private ShapeEditorBase parent;
\r
71 private List<ContextAction> actions = new ArrayList<ContextAction>();
\r
73 private List<Action> addActions = new ArrayList<Action>();
\r
74 private ContextAction unionAction;
\r
75 private ContextAction differenceAction;
\r
76 private ContextAction intersectionAction;
\r
77 private ContextAction splitAction;
\r
78 private ContextAction linkAction;
\r
79 private ContextAction unlinkAction;
\r
80 private ContextAction translateAction;
\r
81 private ContextAction rotateAction;
\r
82 private ContextAction removeAction;
\r
84 private Composite infoComposite;
\r
85 private Text infoText;
\r
88 public CSGModellingContribution(ThreeDimensionalEditorBase parent) {
\r
89 this.parent = (ShapeEditorBase)parent;
\r
93 public String getName() {
\r
94 return "Shape Editing";
\r
98 public void initialize(Graph graph) {
\r
103 public void createControl(Composite parent) {
\r
104 FormLayout flayout = new FormLayout();
\r
105 parent.setLayout(flayout);
\r
106 infoComposite = new Composite(parent, SWT.BORDER);
\r
107 FormData data = new FormData();
\r
108 data.top = new FormAttachment(0, 0);
\r
109 data.left = new FormAttachment(0, 0);
\r
110 data.right = new FormAttachment(100, 0);
\r
111 data.bottom = new FormAttachment(infoComposite, 0, SWT.TOP);
\r
112 this.parent.getRenderingComposite().setLayoutData(data);
\r
113 data = new FormData();
\r
114 data.left = new FormAttachment(0, 0);
\r
115 data.right = new FormAttachment(100, 0);
\r
116 data.bottom = new FormAttachment(100, 0);
\r
118 infoComposite.setLayoutData(data);
\r
119 GridLayout layout = new GridLayout(1,false);
\r
120 layout.marginWidth = 1;
\r
121 layout.marginHeight = 1;
\r
122 infoComposite.setLayout(layout);
\r
123 infoText = new Text(infoComposite, SWT.NONE);
\r
124 GridData gdata = new GridData();
\r
125 gdata.grabExcessHorizontalSpace = true;
\r
126 gdata.horizontalAlignment = SWT.FILL;
\r
127 infoText.setLayoutData(gdata);
\r
131 public void disposeControl() {
\r
132 infoComposite.dispose();
\r
136 public void fillContextMenu(Graph graph, IMenuManager manager, StructuredResourceSelection selection) {
\r
138 if (selection.isEmpty()) {
\r
139 MenuManager addMenu = new MenuManager("Add", "add");
\r
140 addMenu.setRemoveAllWhenShown(false);
\r
141 for (Action a : addActions) {
\r
144 manager.add(addMenu);
\r
149 protected void makeActions(Graph graph) {
\r
150 actions.add(translateAction = new TranslateAction(parent) {
\r
152 public void setInfoText(String text) {
\r
153 infoText.setText(text);
\r
156 actions.add(rotateAction = new RotateAction(parent){
\r
158 public void setInfoText(String text) {
\r
159 infoText.setText(text);
\r
162 actions.add(removeAction = new RemoveAction(parent));
\r
163 actions.add(new FocusAction(parent));
\r
166 IEntity primitive = EntityFactory.create(graph,ShapeEditorResources.csgResource.Primitive);
\r
168 Collection<IEntity> primitives = primitive.getRelatedObjects(graph.getBuiltins().SupertypeOf);
\r
170 TreeMap<String, Resource> sorter = new TreeMap<String, Resource>();
\r
171 for (IEntity p : primitives) {
\r
172 String key = p.getName();
\r
173 if (key.equals(""))
\r
174 key = "ERROR (" + p.getURI() + ")";
\r
175 sorter.put(key, p.getResource());
\r
178 for (Entry<String, Resource> e : sorter.entrySet()) {
\r
179 final String name = e.getKey();
\r
180 final Resource r = e.getValue();
\r
181 Action a = new Action() {
\r
183 public void run() {
\r
184 parent.getSession().asyncWrite(new GraphRequestAdapter() {
\r
186 public GraphRequestStatus perform(Graph g) throws Exception {
\r
188 Instance ins = InstanceFactory.getInstanceOfType(g,r);
\r
189 Resource instance = ins.instantiate(g);
\r
190 Shape shape = new Shape(g,instance);
\r
191 res = shape.getResource();
\r
193 CSGModel model = new CSGModel(g,parent.getModelResource());
\r
194 model.addStatement(ShapeEditorResources.g3dResource.HasChild, shape.getResource());
\r
195 return GraphRequestStatus.transactionComplete();
\r
199 public void handleException(Throwable e) {
\r
200 super.handleException(e);
\r
201 ErrorLogger.defaultLogError("Adding " + name + " failed.", e);
\r
205 public void requestCompleted(GraphRequestStatus status) {
\r
206 parent.getRenderingComposite().getDisplay().asyncExec(new Runnable(){
\r
207 public void run() {
\r
208 parent.getSelectionAdapter().updateSelection(new StructuredResourceSelection(res));
\r
216 a.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
\r
221 unionAction = new WriteAction(parent,false) {
\r
225 public boolean usable(Graph graph,List<Resource> resources) {
\r
226 if (resources.size() >= 2)
\r
231 public GraphRequestStatus doChanges(Graph graph) {
\r
232 List<IGraphicsNode> list = parent.getSelectionAdapter().getSelectedObjects();
\r
233 if (list.size() < 2) {
\r
234 showMessage("Union works between two objects");
\r
235 return GraphRequestStatus.transactionCancel();
\r
237 Resource r1 = list.get(0).getResource();
\r
238 CSGShape shape1 = new CSGShape(graph,r1);
\r
240 G3DNode parent = shape1.getParent();
\r
241 if (parent == null) {
\r
242 showMessage("Primary shape has no parent, don't know what to do");
\r
243 return GraphRequestStatus.transactionCancel();
\r
245 BooleanOperation op = Union.createDefault(graph).toBooleanOperation(); //FIXME : stubcast
\r
246 r = op.getResource();
\r
247 if (createBooleanOp(op, parent, shape1, list))
\r
248 return GraphRequestStatus.transactionComplete();
\r
250 return GraphRequestStatus.transactionCancel();
\r
254 public void afterChanges(GraphRequestStatus status) {
\r
255 parent.getRenderingComposite().getDisplay().asyncExec(new Runnable(){
\r
256 public void run() {
\r
257 parent.getSelectionAdapter().updateSelection(new StructuredResourceSelection(r));
\r
262 unionAction.setText("Union");
\r
263 unionAction.setToolTipText("Union");
\r
264 unionAction.setImageDescriptor(UNION_IMAGE);
\r
266 differenceAction = new WriteAction(parent,false) {
\r
270 public boolean usable(Graph graph,List<Resource> resources) {
\r
271 if (resources.size() >= 2)
\r
276 public GraphRequestStatus doChanges(Graph graph) {
\r
277 List<IGraphicsNode> list = parent.getSelectionAdapter().getSelectedObjects();
\r
278 if (list.size() < 2) {
\r
279 showMessage("Difference works between two objects");
\r
280 return GraphRequestStatus.transactionCancel();
\r
282 CSGShape shape1 = new CSGShape(graph,list.get(0).getResource());
\r
283 G3DNode parent = shape1.getParent();
\r
284 if (parent == null) {
\r
285 showMessage("Primary shape has no parent, don't know what to do");
\r
286 return GraphRequestStatus.transactionCancel();
\r
288 BooleanOperation op = Difference.createDefault(graph).toBooleanOperation(); //FIXME : stubcast
\r
289 r = op.getResource();
\r
290 if (createBooleanOp(op, parent, shape1, list))
\r
291 return GraphRequestStatus.transactionComplete();
\r
293 return GraphRequestStatus.transactionCancel();
\r
297 public void afterChanges(GraphRequestStatus status) {
\r
298 parent.getRenderingComposite().getDisplay().asyncExec(new Runnable(){
\r
299 public void run() {
\r
300 parent.getSelectionAdapter().updateSelection(new StructuredResourceSelection(r));
\r
305 differenceAction.setText("Difference");
\r
306 differenceAction.setToolTipText("Difference");
\r
307 differenceAction.setImageDescriptor(DIFFERENCE_IMAGE);
\r
309 intersectionAction = new WriteAction(parent,false) {
\r
312 public boolean usable(Graph graph,List<Resource> resources) {
\r
313 if (resources.size() >= 2)
\r
318 public GraphRequestStatus doChanges(Graph graph) {
\r
319 List<IGraphicsNode> list = parent.getSelectionAdapter()
\r
320 .getSelectedObjects();
\r
321 if (list.size() < 2) {
\r
322 showMessage("Intersection works between two objects");
\r
323 return GraphRequestStatus.transactionCancel();
\r
325 CSGShape shape1 = new CSGShape(graph,list.get(0).getResource());
\r
326 G3DNode parent = shape1.getParent();
\r
327 if (parent == null) {
\r
328 showMessage("Primary shape has no parent, don't know what to do");
\r
329 return GraphRequestStatus.transactionCancel();
\r
331 BooleanOperation op = Intersection.createDefault(graph).toBooleanOperation(); //FIXME : stubcast
\r
332 r = op.getResource();
\r
333 if (createBooleanOp(op, parent, shape1, list))
\r
334 return GraphRequestStatus.transactionComplete();
\r
336 return GraphRequestStatus.transactionCancel();
\r
340 public void afterChanges(GraphRequestStatus status) {
\r
341 parent.getRenderingComposite().getDisplay().asyncExec(new Runnable(){
\r
342 public void run() {
\r
343 parent.getSelectionAdapter().updateSelection(new StructuredResourceSelection(r));
\r
349 intersectionAction.setText("Intersection");
\r
350 intersectionAction.setToolTipText("Intersection");
\r
351 intersectionAction.setImageDescriptor(INTERSECTION_IMAGE);
\r
353 splitAction = new WriteAction(parent,false) {
\r
356 public boolean usable(Graph graph,List<Resource> resources) {
\r
357 if (resources.size() == 1) {
\r
358 Resource r = resources.iterator().next();
\r
359 IEntity t = EntityFactory.create(graph,r);
\r
360 if (t.isInstanceOf(ShapeEditorResources.csgResource.BooleanOperation)) {
\r
370 public GraphRequestStatus doChanges(Graph graph) {
\r
371 List<IGraphicsNode> list = parent.getSelectionAdapter().getSelectedObjects();
\r
372 if (list.size() != 1) {
\r
373 showMessage("Split requires one object");
\r
374 return GraphRequestStatus.transactionCancel();
\r
376 Resource deletedResource = list.get(0).getResource();
\r
377 IEntity deletedEntity = EntityFactory.create(graph,deletedResource);
\r
378 if (!deletedEntity.isInstanceOf(ShapeEditorResources.csgResource.BooleanOperation)) {
\r
379 showMessage("Split requires boolean operation");
\r
380 return GraphRequestStatus.transactionCancel();
\r
382 Collection<IEntity> parents = deletedEntity.getRelatedObjects(ShapeEditorResources.g3dResource.HasParent);
\r
383 if (parents.size() != 1) {
\r
384 showMessage("Shape has " + parents.size()
\r
385 + " parents, don't know what to do");
\r
386 return GraphRequestStatus.transactionCancel();
\r
388 IEntity parent = parents.iterator().next();
\r
389 // find all shapes and their positions and orientations relative to world coordinates
\r
390 BooleanOperation op = new BooleanOperation(deletedEntity);
\r
391 CSGShape shape1 = op.getMainShape();
\r
392 Point3d shape1WorldPos = G3DTools.getPoint(shape1.getWorldPosition());
\r
393 AxisAngle4d shape1WorldRot = G3DTools.getOrientation(shape1.getWorldOrientation());
\r
394 //System.out.println(shape1WorldPos + " " + shape1WorldRot);
\r
395 Collection<CSGShape> shape2s = op.getSecondaryShape();
\r
397 ArrayList<Point3d> shape2WorldPos = new ArrayList<Point3d>();
\r
398 ArrayList<AxisAngle4d> shape2WorldRot = new ArrayList<AxisAngle4d>();
\r
399 for (CSGShape shape : shape2s) {
\r
400 Point3d pos = G3DTools.getPoint(shape.getWorldPosition());
\r
401 AxisAngle4d rot = G3DTools.getOrientation(shape.getWorldOrientation());
\r
402 shape2WorldPos.add(pos);
\r
403 shape2WorldRot.add(rot);
\r
404 //System.out.println(pos + " " + rot);
\r
407 // removed boolean operation is either connected to model or another boolean operation.
\r
408 CSGModel m = new CSGModel(graph,CSGModellingContribution.this.parent.getModelResource());
\r
409 if (parent.getResource().equals(CSGModellingContribution.this.parent.getModelResource())) {
\r
410 // if deleted boolean operation is connected to the model,
\r
411 // all its children are added to the model
\r
413 m.removeStatement(ShapeEditorResources.g3dResource.HasChild, op);
\r
414 m.addStatement(ShapeEditorResources.g3dResource.HasChild, shape1);
\r
415 for (CSGShape shape2 : shape2s) {
\r
416 m.addStatement(ShapeEditorResources.g3dResource.HasChild, shape2);
\r
420 // deleted boolean operation is connected to another boolean
\r
422 // if the deleted boolean operation is primary child, we'll
\r
423 // must replace it with deleted boolean operations
\r
425 if (!parent.isInstanceOf(ShapeEditorResources.csgResource.BooleanOperation)) {
\r
426 ErrorLogger.defaultLogError("Parent shape is not a boolean operation nor model ?!", null);
\r
427 return GraphRequestStatus.transactionCancel();
\r
429 BooleanOperation parentOp = new BooleanOperation(parent);
\r
430 // we'll have to list all secondary shapes in parent boolean
\r
431 // op so that we can find the correct relatio
\r
432 Collection<IEntity> parentShape2s = parentOp.getRelatedObjects(ShapeEditorResources.csgResource.HasSecondaryShape);
\r
434 if (parentOp.getMainShape().getResource().equals(deletedResource)) {
\r
435 // split boolean operation is the primary child in the
\r
436 // parent boolean operation
\r
437 parent.removeStatement(ShapeEditorResources.csgResource.HasMainShape,deletedResource);
\r
438 // graph.commitChanges(ShapeEditorView.this);
\r
439 parent.addStatement(ShapeEditorResources.csgResource.HasMainShape, shape1);
\r
440 // graph.commitChanges(ShapeEditorView.this);
\r
441 for (CSGShape shape2 : shape2s) {
\r
442 m.addStatement(ShapeEditorResources.g3dResource.HasChild, shape2);
\r
444 } else if (contains(parentShape2s, deletedResource)) {
\r
445 // split boolean operation is one of the secondary
\r
446 // shapes in the parent boolean operation
\r
447 parent.removeStatement(ShapeEditorResources.csgResource.HasSecondaryShape,deletedResource);
\r
448 // graph.commitChanges(ShapeEditorView.this);
\r
449 parent.addStatement(ShapeEditorResources.csgResource.HasSecondaryShape,shape1);
\r
450 // graph.commitChanges(ShapeEditorView.this);
\r
452 // model.getConsistOfShapeSet().add(shape2);
\r
453 for (CSGShape shape2 : shape2s) {
\r
454 m.addStatement(ShapeEditorResources.g3dResource.HasChild, shape2);
\r
457 ErrorLogger.defaultLogError("Parent shape is not a boolean operation nor model ?!", null);
\r
458 return GraphRequestStatus.transactionCancel();
\r
461 deletedEntity.removeStatement(ShapeEditorResources.csgResource.HasMainShape, shape1);
\r
462 for (CSGShape shape2 : shape2s)
\r
463 deletedEntity.removeStatement(ShapeEditorResources.csgResource.HasSecondaryShape, shape2);
\r
465 //System.out.println("Setting original transformations");
\r
466 //G3DTools.setTuple3(shape1.getWorldPosition(), shape1WorldPos);
\r
467 //G3DTools.setOrientation(shape1.getWorldOrientation(), shape1WorldRot);
\r
468 G3DAPI.setWorldTransformation(shape1, shape1WorldPos,shape1WorldRot);
\r
471 for (CSGShape shape : shape2s) {
\r
472 G3DAPI.setWorldTransformation(shape, shape2WorldPos.get(i),shape2WorldRot.get(i));
\r
473 //G3DTools.setTuple3(shape.getWorldPosition(), shape2WorldPos.get(i));
\r
474 //G3DTools.setOrientation(shape.getWorldOrientation(),shape2WorldRot.get(i));
\r
477 return GraphRequestStatus.transactionComplete();
\r
481 splitAction.setText("Split");
\r
482 splitAction.setToolTipText("Split");
\r
483 splitAction.setImageDescriptor(PlatformUI.getWorkbench()
\r
484 .getSharedImages().getImageDescriptor(
\r
485 ISharedImages.IMG_OBJS_INFO_TSK));
\r
487 linkAction = new WriteAction(parent,false) {
\r
490 public boolean usable(Graph graph,List<Resource> resources) {
\r
491 if (resources.size() == 2) {
\r
492 Iterator<Resource> i = resources.iterator();
\r
493 Shape s1 = new Shape(graph,i.next());
\r
494 Shape s2 = new Shape(graph,i.next());
\r
495 if (s1.getRelatedObjects(ShapeEditorResources.g3dResource.GeometryDefinitionOf).size() == 0 &&
\r
496 s2.getRelatedObjects(ShapeEditorResources.g3dResource.GeometryDefinitionOf).size() == 0)
\r
503 public GraphRequestStatus doChanges(Graph graph) {
\r
504 List<IGraphicsNode> list = parent.getSelectionAdapter().getSelectedObjects();
\r
505 if (list.size() != 2) {
\r
506 showMessage("Link works between two objects");
\r
507 return GraphRequestStatus.transactionCancel();
\r
509 Resource r1 = list.get(0).getResource();
\r
510 Resource r2 = list.get(1).getResource();
\r
512 CSGShape shape1 = new CSGShape(graph,r1);
\r
513 CSGShape shape2 = new CSGShape(graph,r2);
\r
515 Point3d p = G3DTools.getPoint(shape2.getWorldPosition());
\r
516 AxisAngle4d aa = G3DTools.getOrientation(shape2.getWorldOrientation());
\r
517 //System.out.println(p + " " + aa);
\r
518 shape2.removeRelatedStatements(ShapeEditorResources.g3dResource.HasParent);
\r
519 r = shape2.getResource();
\r
520 //System.out.println("Link remove commit");
\r
521 //graph.commitChanges(ShapeEditorView.this);
\r
522 //shape1.getChild().add(shape2.toG3DNode()); //FIXME : stubcast
\r
523 shape1.addStatement(ShapeEditorResources.g3dResource.HasChild, shape2);
\r
524 // FIXME : this is needed
\r
525 //System.out.println("Link add commit");
\r
526 //graph.commitChanges(ShapeEditorView.this);
\r
527 //G3DTools.setTuple3(shape2.getWorldPosition(), p);
\r
528 //G3DTools.setOrientation(shape2.getWorldOrientation(), aa);
\r
529 G3DAPI.setWorldTransformation(shape2, p, aa);
\r
530 return GraphRequestStatus.transactionComplete();
\r
534 public void afterChanges(GraphRequestStatus status) {
\r
535 parent.getRenderingComposite().getDisplay().asyncExec(new Runnable(){
\r
536 public void run() {
\r
537 parent.getSelectionAdapter().updateSelection(new StructuredResourceSelection(r));
\r
542 linkAction.setText("Link");
\r
543 linkAction.setToolTipText("Link");
\r
544 linkAction.setImageDescriptor(Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/link.png"));
\r
546 unlinkAction = new WriteAction(parent,false) {
\r
549 public boolean usable(Graph graph,List<Resource> resources) {
\r
550 if (resources.size() == 1) {
\r
551 Iterator<Resource> i = resources.iterator();
\r
552 Shape s1 = new Shape(graph,i.next());
\r
553 return (s1.getRelatedObjects(ShapeEditorResources.g3dResource.GeometryDefinitionOf).size() == 0
\r
554 && s1.getParent() != null && !s1.getParent()
\r
555 .getResource().equals(CSGModellingContribution.this.parent.getModelResource()));
\r
561 public GraphRequestStatus doChanges(Graph graph) {
\r
562 List<IGraphicsNode> list = parent.getSelectionAdapter().getSelectedObjects();
\r
563 if (list.size() != 1) {
\r
564 showMessage("Unlink works with one object");
\r
565 return GraphRequestStatus.transactionCancel();
\r
567 Resource r1 = list.get(0).getResource();
\r
569 CSGShape shape1 = new CSGShape(graph,r1);
\r
570 CSGModel m = new CSGModel(graph,CSGModellingContribution.this.parent.getModelResource());
\r
571 Point3d p = G3DTools.getPoint(shape1.getWorldPosition());
\r
572 AxisAngle4d aa = G3DTools.getOrientation(shape1.getWorldOrientation());
\r
573 shape1.removeRelatedStatements(ShapeEditorResources.g3dResource.HasParent);
\r
574 //graph.commitChanges(ShapeEditorView.this);
\r
575 m.addStatement(ShapeEditorResources.g3dResource.HasChild, shape1);
\r
576 // FIXME : this is needed
\r
577 //graph.commitChanges(ShapeEditorView.this);
\r
578 //G3DTools.setTuple3(shape1.getWorldPosition(), p);
\r
579 //G3DTools.setOrientation(shape1.getWorldOrientation(), aa);
\r
580 G3DAPI.setWorldTransformation(shape1, p, aa);
\r
581 r = shape1.getResource();
\r
582 return GraphRequestStatus.transactionComplete();
\r
586 public void afterChanges(GraphRequestStatus status) {
\r
587 parent.getRenderingComposite().getDisplay().asyncExec(new Runnable(){
\r
588 public void run() {
\r
589 parent.getSelectionAdapter().updateSelection(new StructuredResourceSelection(r));
\r
594 unlinkAction.setText("Unlink");
\r
595 unlinkAction.setToolTipText("Unlink");
\r
596 unlinkAction.setImageDescriptor(Activator.imageDescriptorFromPlugin(
\r
597 Activator.PLUGIN_ID, "icons/unlink.png"));
\r
599 actions.add(unionAction);
\r
600 actions.add(intersectionAction);
\r
601 actions.add(differenceAction);
\r
602 actions.add(splitAction);
\r
603 actions.add(linkAction);
\r
604 actions.add(unlinkAction);
\r
607 boolean contains(ArrayList<Resource> parentShape2sIds, Resource id) {
\r
608 for (int i = 0; i < parentShape2sIds.size(); i++) {
\r
609 if (parentShape2sIds.get(i).equals(id))
\r
616 boolean contains(Collection<IEntity> parentShape2sIds, Resource id) {
\r
617 for (IEntity e : parentShape2sIds) {
\r
618 if (e.getResource().equals(id))
\r
625 private boolean createBooleanOp(BooleanOperation op, G3DNode parent,
\r
626 CSGShape shape1, List<IGraphicsNode> list) {
\r
628 resetShape(op.toShape()); //FIXME : stubcast
\r
629 // new boolean operation is added to the first shape's parent
\r
630 // the parent is the model
\r
632 Point3d refPos = G3DTools.getPoint(shape1.getLocalPosition());
\r
633 G3DTools.setTuple3(op.getLocalPosition(), refPos);
\r
635 G3DTools.addTuple3(shape1.getLocalPosition(), refPos);
\r
636 op.removeRelatedStatements(ShapeEditorResources.csgResource.HasMainShape);
\r
637 op.addStatement(ShapeEditorResources.csgResource.HasMainShape, shape1);
\r
639 if (!replaceShape(parent, shape1.toShape(), op.toShape())) { //FIXME : stubcast
\r
642 //model.getConsistOfSet().remove(shape1);
\r
643 for (int i = 1; i < list.size(); i++) {
\r
644 CSGShape shape2 = new CSGShape(op.getGraph(),list.get(i).getResource());
\r
645 G3DTools.addTuple3(shape2.getLocalPosition(), refPos);
\r
646 G3DNode shape2parent = shape2.getParent();
\r
647 if (shape2parent != null) {
\r
648 // we'll must link before removing or shape will be deleted
\r
649 //op.addStatement(ShapeEditorResources.csgResource.HasSecondaryShape,shape2);
\r
650 //if (!replaceShape(shape2parent, shape2, null)) {
\r
651 // op.removeStatement(ShapeEditorResources.csgResource.HasSecondaryShape, shape2);
\r
652 // // shape couldn't be removed so we'll remove the link
\r
655 if (replaceShape(shape2parent, shape2.toShape(), null)) { //FIXME : stubcast
\r
656 op.addStatement(ShapeEditorResources.csgResource.HasSecondaryShape,shape2);
\r
660 // Commit is not needed because this is final call in all transactions
\r
661 //graph.commitChanges(ShapeEditorView.this);
\r
666 * Replaces or removes parent from shape
\r
667 * @param parent parent containing shape
\r
668 * @param removed the shape
\r
669 * @param added the replacing shape or null
\r
670 * @return true if replacing or removing was successful
\r
671 * @throws TransactionException
\r
673 private boolean replaceShape(G3DNode parent, Shape removed, Shape added) {
\r
674 assert (parent != null);
\r
675 assert (removed != null);
\r
678 //CSGModel m = CSGModelFactory.create(parent.getGraph(),model);
\r
679 if (parent.getResource().equals(this.parent.getModelResource())) {
\r
680 // parent is model (rootnode).
\r
681 // shape is connected to it with "Has Child" relation
\r
682 parent.removeStatement(ShapeEditorResources.g3dResource.HasChild, removed);
\r
683 if (added != null) {
\r
684 parent.addStatement(ShapeEditorResources.g3dResource.HasChild, added);
\r
687 // the first shape's parent is boolean operation
\r
688 // so we must know if its connected as a primary shape or as a secondary shape
\r
689 if (!parent.isInstanceOf(ShapeEditorResources.csgResource.BooleanOperation)) {
\r
690 ErrorLogger.defaultLogError("Parent shape is not a boolean operation nor model ?!",null);
\r
693 BooleanOperation parentOp = new BooleanOperation(parent);
\r
694 // listing all secondary shapes in the parent boolean operation
\r
695 Collection<CSGShape> parentShape2s = parentOp.getSecondaryShape();
\r
696 ArrayList<Resource> parentShape2sIds = new ArrayList<Resource>();
\r
697 for (CSGShape shape2 : parentShape2s)
\r
698 parentShape2sIds.add(shape2.getResource());
\r
699 if (parentOp.getMainShape().getResource().equals(removed.getResource())) {
\r
700 if (added == null) {
\r
703 parent.removeStatement(ShapeEditorResources.csgResource.HasMainShape, removed);
\r
704 parent.addStatement(ShapeEditorResources.csgResource.HasMainShape, added);
\r
706 } else if (contains(parentShape2sIds, removed.getResource())) {
\r
708 parent.removeStatement(ShapeEditorResources.csgResource.HasSecondaryShape, removed);
\r
709 parent.addStatement(ShapeEditorResources.csgResource.HasSecondaryShape, added);
\r
712 ErrorLogger.defaultLogError("Parent shape is not a boolean operation nor model ?!",null);
\r
713 //coreTC.cancelTransaction();
\r
721 * Resets shape to identity rotation and zero translation
\r
725 private void resetShape(Shape shape) {
\r
726 G3DTools.resetTransformation(shape);
\r
727 Graph graph = shape.getGraph();
\r
728 if (shape.isInstanceOf(ShapeEditorResources.csgResource.Primitive)) {
\r
729 Primitive prim = new Primitive(shape);
\r
730 Collection<Property> c = prim.getSizingProperty();
\r
732 ErrorLogger.getDefault().logWarning("Shape does not contain sizing properties.", null);
\r
734 for (Property p : c) {
\r
735 if (p.isInstanceOf(graph.getBuiltins().Double)) {
\r
736 graph.setScalarDouble(p.getResource(), 1.0);
\r
737 } else if (p.isInstanceOf(graph.getBuiltins().Integer)) {
\r
738 graph.setScalarInteger(p.getResource(), 1);
\r
740 ErrorLogger.getDefault().logWarning("Cannot handle sizing property " + p.getName() , null);
\r
747 protected void showMessage(String s) {
\r
748 parent.showMessage(s);
\r
752 public Collection<ContextAction> getActions() {
\r
758 public void fillLocalToolBar(IToolBarManager manager) {
\r
763 public void fillLocalPullDown(IMenuManager manager) {
\r
768 public void dispose() {
\r
773 public void run() {
\r