]> gerrit.simantics Code Review - simantics/3d.git/blob
2b7d49ec83953614d0532f5a9910fba2dd54a09f
[simantics/3d.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.\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
7  *\r
8  * Contributors:\r
9  *     VTT Technical Research Centre of Finland - initial API and implementation\r
10  *******************************************************************************/\r
11 package org.simantics.proconf.g3d.shapeeditor.actions;\r
12 \r
13 import java.io.File;\r
14 import java.io.FilenameFilter;\r
15 import java.util.Stack;\r
16 \r
17 import org.eclipse.jface.action.Action;\r
18 import org.eclipse.swt.SWT;\r
19 import org.eclipse.swt.widgets.DirectoryDialog;\r
20 import org.simantics.proconf.g3d.base.ThreeDimensionalEditorBase;\r
21 import org.simantics.proconf.g3d.occ.geometry.OccTriangulator;\r
22 \r
23 import com.jme.bounding.BoundingBox;\r
24 import com.jme.renderer.ColorRGBA;\r
25 import com.jme.scene.Geometry;\r
26 import com.jme.scene.state.MaterialState;\r
27 \r
28 \r
29 public class LoadFolderAction extends Action {\r
30         private ThreeDimensionalEditorBase editor;\r
31         \r
32         public LoadFolderAction(ThreeDimensionalEditorBase editor) {\r
33                 super("Load folder");\r
34                 this.editor = editor;\r
35                 \r
36         }\r
37         \r
38         public void run() {\r
39         \r
40       DirectoryDialog loadDialog = new DirectoryDialog(editor.getRenderingComposite().getShell(), SWT.OPEN);\r
41       loadDialog.setText("Select directory");\r
42 \r
43       String directory = loadDialog.open();\r
44       if (directory != null) {\r
45           File file = new File(directory);\r
46           if (!file.isDirectory())\r
47               return;\r
48           Stack<File> directories = new Stack<File>();\r
49           directories.push(file);\r
50           boolean recursive = true;\r
51           while (!directories.empty()) {\r
52               File dir = directories.pop();\r
53 \r
54               String[] files = dir.list(new PLYFilter());\r
55               for (String filename : files) {\r
56                   Geometry g = OccTriangulator.getGeometryFromFile(dir.getAbsolutePath() + "/" + filename)[0];\r
57                 MaterialState ms = editor.getRenderingComponent().getDisplaySystem().getRenderer().createMaterialState();\r
58                 ms.setAmbient(new ColorRGBA(0.f,0.f,0.f,0.f));\r
59                 ms.setEmissive(new ColorRGBA(0.f,0.f,0.f,0.f));\r
60                 ms.setShininess(128.f);\r
61                 ms.setDiffuse(new ColorRGBA(0.8f,0.8f,0.8f,0.f));\r
62                 ms.setSpecular(new ColorRGBA(1.f,1.f,1.f,0.f));\r
63                 ms.setMaterialFace(MaterialState.MF_FRONT_AND_BACK);\r
64                 if (g.getColorBuffer(0) != null) {\r
65                         ms.setColorMaterial(MaterialState.CM_DIFFUSE);\r
66                 }\r
67                 g.setModelBound(new BoundingBox());\r
68                 g.updateModelBound();\r
69                 g.setRenderState(ms);\r
70                 editor.getRenderingComponent().getShadowRoot().attachChild(g);\r
71                 g.updateWorldBound();\r
72                 g.lock();\r
73                \r
74               }\r
75               if (recursive) {\r
76                   File[] newDirs = dir.listFiles(new DirectoryFilter());\r
77                   for (File d : newDirs)\r
78                       directories.push(d);\r
79               }\r
80           }\r
81           \r
82       }\r
83 \r
84 \r
85     }\r
86         \r
87          protected class DirectoryFilter implements FilenameFilter {\r
88                 public boolean accept(File dir, String name) {\r
89                     File file = new File(dir.getAbsolutePath() + "/" + name);\r
90                     return file.isDirectory();\r
91                 }\r
92             }\r
93 \r
94             protected class PLYFilter implements FilenameFilter {\r
95                 public boolean accept(File dir, String name) {\r
96                     return (name.endsWith("ply"));\r
97                 }\r
98             }\r
99 \r
100 }\r