/******************************************************************************* * Copyright (c) 2012, 2013 Association for Decentralized Information Management in * Industry THTH ry. * 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 org.simantics.g3d.vtk.handlers; import org.eclipse.core.commands.AbstractHandler; import org.eclipse.core.commands.Command; import org.eclipse.core.commands.ExecutionEvent; import org.eclipse.core.commands.ExecutionException; import org.eclipse.ui.IWorkbenchPart; import org.eclipse.ui.handlers.HandlerUtil; import org.simantics.g3d.vtk.common.VtkView; import org.simantics.utils.threads.AWTThread; import org.simantics.utils.threads.ThreadUtils; import vtk.vtkCamera; import vtk.vtkRenderer; public class ParallelPerspectiveHandler extends AbstractHandler { @Override public Object execute(ExecutionEvent event) throws ExecutionException { Command command = event.getCommand(); boolean oldValue = HandlerUtil.toggleCommandState(command); final boolean activate = !oldValue; final IWorkbenchPart ap = HandlerUtil.getActiveEditor(event); final VtkView panel = (VtkView)ap.getAdapter(VtkView.class); ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() { @Override public void run() { vtkRenderer ren = panel.getRenderer(); vtkCamera cam = ren.GetActiveCamera(); if (activate){ double distance = cam.GetDistance(); double angle = cam.GetViewAngle(); double scale = Math.tan(Math.toRadians(angle / 2)) * distance; cam.SetParallelScale(scale); cam.SetParallelProjection(1); ren.ResetCameraClippingRange(); } else { cam.SetParallelProjection(0); ren.ResetCameraClippingRange(); } panel.refresh(); } }); return null; } }