* @param file
* @return
*/
@Override
public ImportedModel importModel(ImportSettings settings) throws IOException {
ImportedModel importedModel;
URL modelURL = settings.getModelURL();
if (!modelURL.getProtocol().equalsIgnoreCase("file")) {
final String modelURLStr = modelURL.toExternalForm();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JOptionPane.showConfirmDialog(null,
"Unable to load KMZ from this url "+modelURLStr+
"\nPlease use a local kmz file.",
"Deploy Error", JOptionPane.OK_OPTION);
}
});
return null;
}
try {
File f = null;
try {
// Use the URI.getPath() to decode any escaped characters ie %20
URI uri = modelURL.toURI();
String path = uri.getPath();
f = new File(path);
} catch (URISyntaxException ex) {
Logger.getLogger(KmzLoader.class.getName()).log(Level.SEVERE, "Error processing url "+modelURL.toExternalForm(), ex);
return null;
}
if (f==null) {
logger.warning("Unable to get file for model "+modelURL.toExternalForm());
JOptionPane.showMessageDialog(null, "Unable to get file for model "+modelURL.toExternalForm(), "Error", JOptionPane.ERROR_MESSAGE);
return null;
} else if (!f.canRead()) {
logger.warning("Can not read file "+f.getAbsolutePath());
JOptionPane.showMessageDialog(null, "Unable to read file "+f.getAbsolutePath()+"\nPlease check file permissions", "Error", JOptionPane.ERROR_MESSAGE);
return null;
}
ZipFile zipFile = null;
ZipEntry docKmlEntry = null;
try {
zipFile = new ZipFile(f);
docKmlEntry = zipFile.getEntry("doc.kml");
} catch(ZipException ze) {
logger.log(Level.WARNING,"Got a ZipException trying to open file "+f.getAbsolutePath(), ze);
return null;
}
KmlParser parser = new KmlParser();
InputStream in = zipFile.getInputStream(docKmlEntry);
try {
parser.decodeKML(in);
} catch (Exception ex) {
Logger.getLogger(KmzLoader.class.getName()).log(Level.SEVERE, null, ex);
}
List<KmlParser.KmlModel> models = parser.getModels();
HashMap<URL, String> textureFilesMapping = new HashMap();
importedModel = new KmzImportedModel(modelURL, models.get(0).getHref(), textureFilesMapping);
String zipHost = WlzipManager.getWlzipManager().addZip(zipFile);
ZipResourceLocator zipResource = new ZipResourceLocator(zipHost, zipFile, textureFilesMapping);
ResourceLocatorTool.addThreadResourceLocator(
ResourceLocatorTool.TYPE_TEXTURE,
zipResource);
JmeColladaLoader.LoaderErrorListener errorListener = new JmeColladaLoader.LoaderErrorListener(importedModel, LoaderManager.getLoaderManager().getLoaderListeners());
if (models.size()==1) {
importedModel.setModelBG(load(zipFile, models.get(0), errorListener));
} else {
Node modelBG = new Node();
for(KmlParser.KmlModel model : models) {
modelBG.attachChild(load(zipFile, model, errorListener));
}
importedModel.setModelBG(modelBG);
}
ResourceLocatorTool.removeThreadResourceLocator(ResourceLocatorTool.TYPE_TEXTURE, zipResource);
WlzipManager.getWlzipManager().removeZip(zipHost, zipFile);
} catch (ZipException ex) {
logger.log(Level.SEVERE, null, ex);
throw new IOException("Zip Error");
} catch (IOException ex) {
logger.log(Level.SEVERE, null, ex);
throw ex;
}
importedModel.setModelLoader(this);
settings.setLightingEnabled(false); // No lighting is the default for SketchUp
importedModel.setImportSettings(settings);
return importedModel;
}