123 lines
3.6 KiB
JavaScript
Executable File
123 lines
3.6 KiB
JavaScript
Executable File
// $Id$
|
|
// $HeadURL$
|
|
// $Author$
|
|
// Created by Manfred Gronak, , 2021-05
|
|
|
|
var fs = require("fs");
|
|
var parser = require('xml2json');
|
|
|
|
module.exports = function() {
|
|
|
|
this.VwmXmlToWrl = function VwmXmlToWrl(OriginalxmlFile) {
|
|
console.log(" VwmXmlToWrl: "+ OriginalxmlFile); // Datei geschrieben
|
|
|
|
// if (OriginalxmlFile.includes("bldg_1287.flt.VWM_Building.xml")) {
|
|
// console.log(" Skip VwmXmlToWrl: "+ OriginalxmlFile); // Datei geschrieben
|
|
// } else {
|
|
// return;
|
|
// }
|
|
|
|
|
|
|
|
var coordIndex = "]\n}\ncoordIndex [ ";
|
|
var counter = 0;
|
|
|
|
var wrl_filename = OriginalxmlFile + ".wrl";
|
|
|
|
var content = fs.readFileSync(OriginalxmlFile);
|
|
var jsonFileInhalt = parser.toJson(content);
|
|
var jsonContent = JSON.parse(jsonFileInhalt);
|
|
var i;
|
|
|
|
var resultfile = "#VRML V2.0 utf8\nBackground {\nskyColor 0.2 0.3 0.1}\nTransform {\ntranslation -2.5 -2.5 -2.5\n"
|
|
resultfile = resultfile + "children [\n Shape {\nappearance Appearance {\nmaterial Material {\n";
|
|
resultfile = resultfile + "diffuseColor 1 0 1\n}\n}\ngeometry IndexedFaceSet {\nsolid FALSE\n";
|
|
resultfile = resultfile + "coord Coordinate {\npoint [\n";
|
|
|
|
var RoomArray = jsonContent.BuildingVulnerability.Rooms.Room;
|
|
if (RoomArray == null) {
|
|
console.log("Datei " + OriginalxmlFile + ": " + " WARNING, no RoomArray detected");
|
|
return;
|
|
}
|
|
|
|
if (Array.isArray(RoomArray)) {
|
|
// console.log("1Array: ");
|
|
} else {
|
|
// No Array..
|
|
// wrap obj into an array to ensure 'RoomArray.forEach(...'
|
|
var dummy = new Array;
|
|
dummy[0] = RoomArray;
|
|
RoomArray = dummy;
|
|
}
|
|
|
|
|
|
|
|
RoomArray.forEach(Room => {
|
|
|
|
var OuterWallArray = Room.RoomOuterWalls.OuterWall;
|
|
|
|
if (Array.isArray(OuterWallArray)) {
|
|
|
|
OuterWallArray.forEach(Outerwall => {
|
|
// console.log("Array: " + Outerwall.OuterWallAreaElement);
|
|
var n = 0;
|
|
var PolygonPoints = Outerwall.OuterWallAreaElement.PolygonPoint;
|
|
|
|
PolygonPoints.forEach(PolygonPoint => {
|
|
// console.log(PolygonPoint.X + " " + PolygonPoint.Y + " " + PolygonPoint.Z) ;
|
|
resultfile = resultfile + PolygonPoint.X + " " + PolygonPoint.Y + " " + PolygonPoint.Z + ",\n";
|
|
|
|
n = n + 1;
|
|
counter = counter + 1;
|
|
})
|
|
|
|
for (i = counter - n; i < counter; i++) {
|
|
coordIndex = coordIndex + " " + i + ",";
|
|
}
|
|
coordIndex = coordIndex + " -1\n";
|
|
});
|
|
|
|
} else {
|
|
// console.log("NoArray: " + OuterWallArray);
|
|
|
|
if (OuterWallArray != undefined) {
|
|
// RoomArray is object:
|
|
// that means OuterWallArray could be used as (a single) Outerwall
|
|
// console.log("OBJ" + OuterWallArray);
|
|
|
|
var n = 0;
|
|
var PolygonPoints = OuterWallArray.OuterWallAreaElement.PolygonPoint;
|
|
|
|
PolygonPoints.forEach(PolygonPoint => {
|
|
// console.log(PolygonPoint.X + " " + PolygonPoint.Y + " " + PolygonPoint.Z) ;
|
|
resultfile = resultfile + PolygonPoint.X + " " + PolygonPoint.Y + " " + PolygonPoint.Z + ",\n";
|
|
|
|
n = n + 1;
|
|
counter = counter + 1;
|
|
})
|
|
|
|
for (i = counter - n; i < counter; i++) {
|
|
coordIndex = coordIndex + " " + i + ",";
|
|
}
|
|
coordIndex = coordIndex + " -1\n";
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
resultfile = resultfile + coordIndex;
|
|
resultfile = resultfile + " ]\n}\n}\n]\n}\n";
|
|
|
|
fs.writeFileSync(wrl_filename, resultfile, function(err) {
|
|
// If an error occurred, show it
|
|
if(err)
|
|
{
|
|
console.log("Error on writing file: " + wrl_filename);
|
|
console.err(err);
|
|
}
|
|
});
|
|
|
|
} // end of function
|
|
} // export
|