Skip to content

Commit

Permalink
Merge pull request #139 from NREL/GE-125
Browse files Browse the repository at this point in the history
new import/export functions
  • Loading branch information
bgschiller authored Aug 18, 2017
2 parents 0cbc8d5 + 63f91c5 commit 2f5833c
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 97 deletions.
8 changes: 4 additions & 4 deletions src/api.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
window.api = {
config: null,
initAlreadyRun: false,
doImport: (data) => {
openFloorplan: (data) => {
try {
window.application.$store.dispatch('importModel', {
window.application.$store.dispatch('importFloorplan', {
clientWidth: document.getElementById('svg-grid').clientWidth,
clientHeight: document.getElementById('svg-grid').clientHeight,
data: JSON.parse(data),
Expand All @@ -21,7 +21,7 @@ window.api = {
}
return true;
},
doExport: () => window.application.$store.getters['exportData'],
exportFloorplan: () => window.application.$store.getters['exportData'],
setConfig: (config) => {
if (this.initAlreadyRun) {
throw new Error('The application has already been started, configuration cannot be changed.');
Expand Down Expand Up @@ -49,7 +49,7 @@ window.api = {
window.application.$store.dispatch('project/setUnits', { units: window.api.config.units });

// if the map modal has been disabled, mark the map as initialized so that time travel can be initialized
// TODO: we may want to intitialize timetravel in the importModel action instead
// TODO: we may want to intitialize timetravel in the importFloorplan action instead
window.application.$store.dispatch('project/setMapInitialized', { initialized: true });

this.initAlreadyRun = true;
Expand Down
10 changes: 5 additions & 5 deletions src/components/Modals/MapModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
<button @click="$refs.importInput.click()" id="import">Open Floorplan</button>
<button @click="mapEnabled = false; mapVisible = false; $emit('close')">New Floorplan</button>
<button @click="mapEnabled = true; tool='Map'; $emit('close')">New Floorplan w/ Map</button>
<input ref="importInput" @change="importModelAsFile" type="file"/>
<input ref="importInput" @change="importFloorplanAsFile" type="file"/>
</div>
</div>
</aside>
Expand Down Expand Up @@ -50,17 +50,17 @@ export default {
},
},
methods: {
importModelAsFile(event) {
importFloorplanAsFile(event) {
const file = event.target.files[0];
const reader = new FileReader();
reader.addEventListener('load', () => {
this.importModel(reader.result);
this.importFloorplan(reader.result);
}, false);

if (file) { reader.readAsText(file); }
},
importModel(data) {
this.$store.dispatch('importModel', {
importFloorplan(data) {
this.$store.dispatch('importFloorplan', {
clientWidth: document.getElementById('svg-grid').clientWidth,
clientHeight: document.getElementById('svg-grid').clientHeight,
data: JSON.parse(data),
Expand Down
2 changes: 1 addition & 1 deletion src/components/Toolbar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export default {
if (type === 'library') {
this.$store.dispatch('importLibrary', { data });
} else if (type === 'floorplan') {
this.$store.dispatch('importModel', {
this.$store.dispatch('importFloorplan', {
clientWidth: document.getElementById('svg-grid').clientWidth,
clientHeight: document.getElementById('svg-grid').clientHeight,
data,
Expand Down
4 changes: 2 additions & 2 deletions src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import geometry from './modules/geometry/index';
import models from './modules/models/index';

import exportData from './utilities/export';
import importModel from './utilities/importModel';
import importFloorplan from './utilities/importFloorplan';
import importLibrary from './utilities/importLibrary';
import mutations from './mutations';

Expand All @@ -26,7 +26,7 @@ const store = new Vuex.Store({
exportData,
},
actions: {
importModel,
importFloorplan,
importLibrary,
},
mutations,
Expand Down
83 changes: 83 additions & 0 deletions src/store/utilities/importFloorplan.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import idFactory from './generateId';

export default function importFloorplan(context, payload) {
// intializr a versionNumber if the app is running in embedded mode
if (window.api) { window.versionNumber = 0; }

// GEOMETRY
const geometry = payload.data.stories.map((story) => {
const faces = story.geometry.faces.map((face) => {
const edgeRefs = face.edge_ids.map((id, index) => ({
edge_id: id,
reverse: !face.edge_order[index],
}));
return {
id: face.id,
edgeRefs,
};
});

const edges = story.geometry.edges.map(e => ({
id: e.id,
v1: e.vertex_ids[0],
v2: e.vertex_ids[1],
}));
return {
id: story.geometry.id,
faces,
edges,
vertices: story.geometry.vertices,
};
});

// MODELS
const stories = payload.data.stories.map((s) => {
const story = {
geometry_id: s.geometry.id,
...s,
};
delete story.geometry;
return story;
});

function forEachNestedProp(obj, func, propName = null) {
if (_.isObject(obj)) {
Object.keys(obj).forEach(k => forEachNestedProp(obj[k], func, k));
} else if (_.isArray(obj)) {
obj.forEach(elem => forEachNestedProp(elem, func));
} else {
func(propName, obj);
}
}

// find the highest id in the imported floorplan
let largestId = 0;
forEachNestedProp(payload, (k, v) => {
if (k && k === 'id' && (+v > largestId)) {
largestId = v;
}
});

// set the idFactory to the next id
largestId += 1;
idFactory.setId(largestId);

context.commit('importState', {
project: payload.data.project,
application: context.state.application,
models: {
stories,
library: {
building_units: payload.data.building_units,
thermal_zones: payload.data.thermal_zones,
space_types: payload.data.space_types,
construction_sets: payload.data.construction_sets,
windows: payload.data.windows,
daylighting_controls: payload.data.daylighting_controls,
},
},
geometry,
});

document.getElementById('svg-grid').dispatchEvent(new Event('reloadGrid'));
}
4 changes: 2 additions & 2 deletions src/store/utilities/importLibrary.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import idFactory from './generateId'
import idFactory from './generateId';

export default function importLibrary(context, payload) {
let count = 0;
const types = Object.keys(payload.data);
types.forEach((type) => {
if (type === 'project') { return; }
if (type === 'project' || !context.state.models.library[type]) { return; }
const existingNames = context.state.models.library[type].map((o) => {
// /_\d+[\w\s]?$/
// if object name contains duplicate suffix, remove suffix
Expand Down
83 changes: 0 additions & 83 deletions src/store/utilities/importModel.js

This file was deleted.

0 comments on commit 2f5833c

Please sign in to comment.