/******************************************************************************* * Copyright (c) 2007 VTT Technical Research Centre of Finland and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ package fi.vtt.simantics.processeditor.actions; import java.util.List; import javax.vecmath.Point3d; import javax.vecmath.Vector3d; import org.eclipse.jface.action.Action; import org.eclipse.jface.action.IToolBarManager; import org.simantics.db.Graph; import org.simantics.db.GraphRequestAdapter; import org.simantics.db.GraphRequestStatus; import org.simantics.db.Resource; import org.simantics.layer0.utils.EntityFactory; import org.simantics.layer0.utils.IEntity; import org.simantics.proconf.g3d.actions.ConstrainedTransformAction; import org.simantics.proconf.g3d.base.G3DAPI; import org.simantics.proconf.g3d.base.G3DTools; import org.simantics.proconf.g3d.base.MathTools; import org.simantics.proconf.g3d.base.ThreeDimensionalEditorBase; import org.simantics.proconf.g3d.gizmo.TransformInlineGizmo; import org.simantics.proconf.g3d.scenegraph.IGraphicsNode; import org.simantics.proconf.g3d.stubs.G3DNode; import fi.vtt.simantics.processeditor.Activator; import fi.vtt.simantics.processeditor.ProcessResource; import fi.vtt.simantics.processeditor.common.PipingTools2; import fi.vtt.simantics.processeditor.stubs.InlineComponent; import fi.vtt.simantics.processeditor.stubs.PipeControlPoint; public class TranslateInlineComponentAction extends ConstrainedTransformAction { TransformInlineGizmo gizmo; List mos; Point3d start; Point3d end; Vector3d dir; double istep = 10.0; int decimals = 2; public TranslateInlineComponentAction(ThreeDimensionalEditorBase parent) { super(parent); gizmo = new TransformInlineGizmo(component.getDisplaySystem().getRenderer()); } public void init() { this.setText("Translate"); this.setToolTipText("Translate the object"); this.setImageDescriptor(Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/translate_d.png")); } @Override public boolean usable(Graph graph, List resources) { // TODO : it should be possible to move multiple components on the same straight if (resources.size() != 1) return false; IEntity r = EntityFactory.create(graph,resources.get(0)); PipeControlPoint pcp = null; if (r.isInstanceOf(ProcessResource.plant3Dresource.FixedLengthInlineComponent)) { InlineComponent component = new InlineComponent(r); pcp = component.getControlPoint(); } else { return false; } if (pcp.getNext() == null || pcp.getPrevious() == null) return false; return true; } @Override public void deactivate() { parent.setGizmo(null); super.deactivate(); } @Override public void activate() { parent.setGizmo(gizmo); String text = ""; mos = parent.getSelectionAdapter().getSelectedObjects(); // for (IGraphicsNode mo : mos) { // text += GraphicsNodeTools.getWorldTranslation(mo.getGraphicsNode());//mo.getWorldPosition() + " "; // } mos.iterator().next().getGroup().attachChild(gizmo.getNode()); setInfoText(text); start = new Point3d(); end = new Point3d(); dir = new Vector3d(); parent.getSession().syncRead(new GraphRequestAdapter() { @Override public GraphRequestStatus perform(Graph g) throws Exception { InlineComponent ic = new InlineComponent(mos.iterator().next().getG3DNode(g)); PipingTools2.getInlineMovement(ic, start, end); //PipingTools2.getInlineComponentEnds(ic, start, end); dir.set(end); dir.sub(start); return GraphRequestStatus.transactionComplete(); } }); updateGizmo(); parent.setViewChanged(true); } Vector3d getTranslate() { Vector3d translate = new Vector3d(); Vector3d o = new Vector3d(); Vector3d d = new Vector3d(); parent.createPickRay(o, d); //Vector3d p = gizmo.getPosition(); if (gizmo.isSelected()) { double s[] = new double[1]; Vector3d i1 = new Vector3d(); Vector3d i2 = new Vector3d(); s = new double[2]; MathTools.intersectStraightStraight( start, dir,o, d, i2, i1,s); translate.set(dir); if (s[0] < 0.0) s[0] = 0.0; else if (s[0] > 1.0) s[0] = 1.0; translate.scale(s[0]); translate.add(start); if (useConstraints) { Vector3d t = new Vector3d(translate); // FIXME : snapped point may be outside of proper range Point3d snap = detector.getPointSnap2(t, dir); if (snap != null) { translate = new Vector3d(snap); } } return translate; } return null; } Vector3d prevTranslate = new Vector3d(); @Override public void doChanges(Graph graph) throws Exception { if (input.mousePressed()) { //prevTranslate = getTranslate(); } if (input.mouseClicked()) { end(); return; } if (!input.mouseDragged()) { parent.getDefaultAction().update(); return; } parent.setViewChanged(true); List mos = parent.getSelectionAdapter().getSelectedObjects(); Vector3d translate = getTranslate(); if (translate == null) { //cameraRotateAction.update(); parent.getDefaultAction().update(); updateGizmo(); return; } //translate.sub(prevTranslate); // if ((input.dragModifiers() & MouseEvent.CTRL_MASK) > 0) { // String text = ""; // for (IGraphicsNode mo : mos) { // // Point3d p = mo.getWorldPosition(); // p.add(translate); // p.x = Math.round(istep * p.x) / istep; // BigDecimal bx = new BigDecimal(p.x); // bx.setScale(decimals, BigDecimal.ROUND_HALF_UP); // p.x = bx.doubleValue(); // // // text += p + " "; // mo.setWorldTranslation(p); // // } // this.parent.setInfoText(text); // // } else { String text = ""; for (IGraphicsNode mo : mos) { G3DNode node = mo.getG3DNode(graph); G3DAPI.setWorldPosition(node, translate); //G3DTools.setLocalTranslation(node, translate); //mo.setLocalTranslation(translate); text += G3DTools.getVector(node.getWorldPosition()) +" " + translate;//mo.getWorldPosition() + " " + translate; } setInfoText(text); // } updateGizmo(); } protected void updateGizmo() { gizmo.update(camera.getCameraPos(),component); } public void setInfoText(String text) { } }