use of com.ardor3d.util.resource.URLResourceSource in project energy3d by concord-consortium.
the class Foundation method importCollada.
public Node importCollada(final URL file, final Vector3 position) throws Exception {
if (importedNodes == null) {
importedNodes = new ArrayList<Node>();
}
if (importedNodeStates == null) {
importedNodeStates = new ArrayList<NodeState>();
}
if (position != null) {
// when position is null, the cursor is already set to be wait by the loading method
SceneManager.getInstance().cursorWait(true);
}
File sourceFile = new File(file.toURI());
if (!sourceFile.exists() && Scene.getURL() != null) {
sourceFile = new File(new File(Scene.getURL().toURI()).getParentFile(), Util.getFileName(file.getPath()).replaceAll("%20", " "));
}
if (sourceFile.exists()) {
final double az = Math.toRadians(getAzimuth());
final boolean zeroAz = Util.isZero(az);
// 0.633 is determined by fitting the length in Energy3D to the length in SketchUp
final double scale = Scene.getInstance().getAnnotationScale() * 0.633;
final ColladaStorage storage = new ColladaImporter().load(new URLResourceSource(sourceFile.toURI().toURL()));
final Node originalNode = storage.getScene();
originalNode.setScale(scale);
if (position != null) {
// when position is null, the node uses the position saved in the associated NodeState object
final NodeState ns = new NodeState();
ns.setSourceURL(file);
ns.setAbsolutePosition(position.clone());
importedNodeStates.add(ns);
originalNode.setTranslation(position);
final Vector3 relativePosition = position.subtract(getAbsCenter().multiplyLocal(1, 1, 0), null).addLocal(0, 0, height);
// why not -getAzimuth()?
ns.setRelativePosition(zeroAz ? relativePosition : new Matrix3().fromAngles(0, 0, az).applyPost(relativePosition, null));
}
// now construct a new node that is a parent of all planar meshes
final Node newNode = new Node(originalNode.getName());
final String nodeString = "Node #" + importedNodes.size() + ", Foundation #" + id;
final List<Mesh> meshes = new ArrayList<Mesh>();
Util.getMeshes(originalNode, meshes);
String warnInfo = null;
final int nodeIndex = importedNodes.size();
int meshIndex = 0;
for (final Mesh m : meshes) {
final ReadOnlyTransform t = m.getWorldTransform();
final MeshData md = m.getMeshData();
switch(md.getIndexMode(0)) {
case Triangles:
final List<Mesh> children = TriangleMeshLib.getPlanarMeshes(m);
if (!children.isEmpty()) {
for (final Mesh s : children) {
s.setTransform(t);
final UserData ud = new UserData(this, nodeIndex, meshIndex);
ud.setNormal((Vector3) s.getUserData());
ud.setRenderState(s.getLocalRenderState(StateType.Texture));
ud.setTextureBuffer(s.getMeshData().getTextureBuffer(0));
s.setUserData(ud);
s.setName("Mesh #" + meshIndex + ", " + nodeString);
newNode.attachChild(s);
meshIndex++;
}
}
break;
case Lines:
break;
default:
warnInfo = md.getIndexMode(0).name();
break;
}
}
if (warnInfo != null) {
JOptionPane.showMessageDialog(MainFrame.getInstance(), "Non-triangular mesh " + warnInfo + " is found.", "Warning", JOptionPane.WARNING_MESSAGE);
}
if (newNode.getNumberOfChildren() > 0) {
importedNodes.add(newNode);
newNode.setScale(scale);
newNode.updateWorldTransform(true);
root.attachChild(newNode);
createMeshThickness(newNode);
if (!zeroAz) {
setRotatedNormalsForImportedMeshes();
}
return newNode;
}
if (position != null) {
SceneManager.getInstance().cursorWait(false);
}
} else {
if (position != null) {
// get rid of the dead nodes no longer linked to files
for (final Iterator<NodeState> it = importedNodeStates.iterator(); it.hasNext(); ) {
if (file.equals(it.next().getSourceURL())) {
it.remove();
}
}
}
}
return null;
}
Aggregations