JavaCAD
Importer3D.java
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010, 2014, Oracle and/or its affiliates.
3  * All rights reserved. Use is subject to license terms.
4  *
5  * This file is available and licensed under the following license:
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * - Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * - Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the distribution.
16  * - Neither the name of Oracle Corporation nor the names of its
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 package eu.mihosoft.vrl.v3d.ext.openjfx.importers;
33 
34 import java.io.IOException;
35 import java.net.URL;
36 import java.net.URLClassLoader;
37 
38 import javafx.animation.Timeline;
39 import javafx.fxml.FXMLLoader;
40 import javafx.scene.Node;
41 import javafx.scene.shape.MeshView;
42 import javafx.scene.shape.TriangleMesh;
43 import javafx.util.Pair;
44 
45 import java.util.ServiceLoader;
46 
47 // TODO: Auto-generated Javadoc
51 public final class Importer3D {
52 
58  public static String[] getSupportedFormatExtensionFilters() {
59 // return new String[]{"*.ma", "*.ase", "*.obj", "*.fxml", "*.dae"};
60  return new String[]{"*.obj"};
61  }
62 
70  public static Node load(String fileUrl) throws IOException {
71  return load(fileUrl,false);
72  }
73 
82  public static Node load(String fileUrl, boolean asPolygonMesh) throws IOException {
83  return loadIncludingAnimation(fileUrl,asPolygonMesh).getKey();
84  }
85 
94  public static Pair<Node,Timeline> loadIncludingAnimation(String fileUrl, boolean asPolygonMesh) throws IOException {
95  // get extension
96  final int dot = fileUrl.lastIndexOf('.');
97  if (dot <= 0) {
98  throw new IOException("Unknown 3D file format, url missing extension [" + fileUrl + "]");
99  }
100  final String extension = fileUrl.substring(dot + 1, fileUrl.length()).toLowerCase();
101  // Reference all the importer jars
102  ImporterFinder finder = new ImporterFinder();
103  URLClassLoader classLoader = finder.addUrlToClassPath();
104 
105  ServiceLoader<Importer> servantLoader = ServiceLoader.load(Importer.class, classLoader);
106  // Check if we have an implementation for this file type
107  Importer importer = null;
108  for (Importer plugin : servantLoader) {
109  if (plugin.isSupported(extension)) {
110  importer = plugin;
111  break;
112  }
113  }
114 
115  // Check well known loaders that might not be in a jar (ie. running from an IDE)
116  if ((importer == null) && (!extension.equals("fxml"))){
117  String [] names = {
118  "com.javafx.experiments.importers.dae.DaeImporter",
119  "com.javafx.experiments.importers.max.MaxLoader",
120  "com.javafx.experiments.importers.maya.MayaImporter",
121  "com.javafx.experiments.importers.obj.ObjOrPolyObjImporter",
122  };
123  boolean fail = true;
124  for (String name : names) {
125  try {
126  Class<?> clazz = Class.forName(name);
127  Object obj = clazz.newInstance();
128  if (obj instanceof Importer) {
129  Importer plugin = (Importer) obj;
130  if (plugin.isSupported(extension)) {
131  importer = plugin;
132  fail = false;
133  break;
134  }
135  }
136  } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
137  // FAIL SILENTLY
138  }
139  }
140  if (fail) throw new IOException("Unknown 3D file format [" + extension + "]");
141  }
142 
143  if (extension.equals("fxml")) {
144  final Object fxmlRoot = FXMLLoader.load(new URL(fileUrl));
145  if (fxmlRoot instanceof Node) {
146  return new Pair<>((Node) fxmlRoot, null);
147  } else if (fxmlRoot instanceof TriangleMesh) {
148  return new Pair<>(new MeshView((TriangleMesh) fxmlRoot), null);
149  }
150  throw new IOException("Unknown object in FXML file [" + fxmlRoot.getClass().getName() + "]");
151  } else {
152  importer.load(fileUrl, asPolygonMesh);
153  return new Pair<>(importer.getRoot(), importer.getTimeline());
154  }
155  }
156 }
static Node load(String fileUrl, boolean asPolygonMesh)
Definition: Importer3D.java:82
static Pair< Node, Timeline > loadIncludingAnimation(String fileUrl, boolean asPolygonMesh)
Definition: Importer3D.java:94
abstract void load(String url, boolean asPolygonMesh)
abstract boolean isSupported(String supportType)