Skip to content

Commit ef8c3c6

Browse files
authored
Merge pull request #304 from NREL/import-floorplan-units
assume project units if none are present in floorplan import
2 parents aa21430 + 597c193 commit ef8c3c6

6 files changed

Lines changed: 52 additions & 32 deletions

File tree

src/api.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ window.api = {
2222
try {
2323
window.application.$store.dispatch('importLibrary', { data: JSON.parse(data) });
2424
} catch (err) {
25+
console.error(err);
2526
return false;
2627
}
2728
return true;

src/store/index.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,20 @@ const store = new Vuex.Store({
3030
importFloorplan,
3131
importLibrary,
3232
changeUnits(context, { newUnits }) {
33-
console.log(`moving from ${context.state.project.config.units} to ${newUnits}`);
33+
const oldUnits = context.state.project.config.units;
34+
console.log(`moving from ${oldUnits} to ${newUnits}`);
35+
if (oldUnits !== 'ip' && oldUnits !== 'si') {
36+
console.error(`Expected data.project.config.units to be "ip" or "si", received "${oldUnits}"`);
37+
}
38+
if (newUnits !== 'ip' && newUnits !== 'si') {
39+
console.error(`Expected oldUnits to be "ip" or "si", received "${newUnits}"`);
40+
}
3441
context.commit(
3542
'changeUnits',
3643
convertState(
3744
context.state,
38-
context.state.project.config.units === 'm' ? 'si_units' : 'ip_units',
39-
newUnits === 'm' ? 'si_units' : 'ip_units'));
45+
oldUnits,
46+
newUnits));
4047
context.dispatch('project/setUnits', { units: newUnits });
4148
},
4249
},

src/store/modules/models/factory.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ if (ip_defaults.Project.config.units !== 'ip') {
3434
}
3535
const si_defaults = _.mapValues(
3636
ip_defaults,
37-
(value, key) => getConverter(key, 'ip_units', 'si_units')(value));
37+
(value, key) => getConverter(key, 'ip', 'si')(value));
3838

3939
export const getDefaults = key =>
4040
_.cloneDeep(_.get(window, 'application.$store.state.project.config.units', 'ip') === 'ip' ?

src/store/utilities/importLibrary.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,19 @@ export default function importLibrary(context, payload) {
3030
});
3131
});
3232

33-
const
34-
librarySystem = payload.data.project.config.units === 'ft' ? 'ip_units' : 'si_units',
35-
projectSystem = context.state.project.config.units === 'ft' ? 'ip_units' : 'si_units',
36-
localUnitsPayload = convertLibrary(payload.data, librarySystem, projectSystem);
33+
const projectSystem = context.state.project.config.units;
34+
const librarySystemRaw = _.get(payload, 'data.project.config.units');
35+
let librarySystem = (
36+
librarySystemRaw === 'ft' ? 'ip' :
37+
librarySystemRaw === 'm' ? 'si' :
38+
librarySystemRaw);
39+
40+
if (librarySystem !== 'ip' && librarySystem !== 'si') {
41+
console.warn(`Expected data.project.config.units to be "ip" or "si", received "${librarySystemRaw}"`);
42+
console.warn('unable to determine units of library -- using project units');
43+
librarySystem = projectSystem;
44+
}
45+
const localUnitsPayload = convertLibrary(payload.data, librarySystem, projectSystem);
3746

3847
window.eventBus.$emit('success', `Imported ${count} object${count !== 1 ? 's' : ''}`);
3948
// merge the import data with the existing library objects

src/store/utilities/unitConversion.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,19 @@ export const conversionFactor = (fromUnits, toUnits) => {
6464
return factor;
6565
};
6666

67-
export const getConverter = (path, fromSystem, toSystem) => {
67+
export const getConverter = (path, _fromSystem, _toSystem) => {
6868
// this because we don't have a type system
69-
if (!_.includes(['si_units', 'ip_units'], fromSystem)) {
70-
throw new Error(`expected fromSystem to be 'si_units' or 'ip_units'. received ${fromSystem}`);
69+
if (!_.includes(['si', 'ip'], _fromSystem)) {
70+
throw new Error(`expected fromSystem to be 'si' or 'ip'. received ${_fromSystem}`);
7171
}
72-
if (!_.includes(['si_units', 'ip_units'], toSystem)) {
73-
throw new Error(`expected toSystem to be 'si_units' or 'ip_units'. received ${toSystem}`);
72+
if (!_.includes(['si', 'ip'], _toSystem)) {
73+
throw new Error(`expected toSystem to be 'si' or 'ip'. received ${_toSystem}`);
7474
}
7575

76-
if (fromSystem === toSystem) { return _.identity; }
76+
if (_fromSystem === _toSystem) { return _.identity; }
77+
78+
const fromSystem = `${_fromSystem}_units`;
79+
const toSystem = `${_toSystem}_units`;
7780

7881
const pathUnits = _.get(units, path);
7982
if (
@@ -87,16 +90,16 @@ export const getConverter = (path, fromSystem, toSystem) => {
8790
return val => val * factor;
8891
}
8992
if (pathUnits.$ref) {
90-
return getConverter(pathUnits.$ref, fromSystem, toSystem);
93+
return getConverter(pathUnits.$ref, _fromSystem, _toSystem);
9194
}
9295
if (pathUnits.arrayOf) {
93-
const converter = getConverter(pathUnits.arrayOf, fromSystem, toSystem);
96+
const converter = getConverter(pathUnits.arrayOf, _fromSystem, _toSystem);
9497
return arr => arr.map(converter);
9598
}
9699
if (_.isObject(pathUnits)) {
97100
const converters = _.mapValues(
98101
pathUnits,
99-
(val, key) => getConverter(`${path}.${key}`, fromSystem, toSystem));
102+
(val, key) => getConverter(`${path}.${key}`, _fromSystem, _toSystem));
100103
return obj => _.mapValues(obj, (val, key) => (converters[key] || _.identity)(val));
101104
}
102105
throw new Error(`Path ${path} did not lead to a useful spot in 'units'`);

test/unit/specs/unitConversions/unitConversions.spec.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ describe('conversionFactor', () => {
2020
describe('getConverter', () => {
2121
it('produces an acceptable conversion for Vertices', () => {
2222
const
23-
toIP = getConverter('Vertex', 'si_units', 'ip_units'),
24-
toSI = getConverter('Vertex', 'ip_units', 'si_units'),
23+
toIP = getConverter('Vertex', 'si', 'ip'),
24+
toSI = getConverter('Vertex', 'ip', 'si'),
2525
siVert = { x: 12, y: -5 },
2626
ipVert = { x: 12 * 3.280839895, y: -5 * 3.280839895 };
2727

@@ -50,7 +50,7 @@ describe('getConverter', () => {
5050
},
5151
show_import_export: false,
5252
},
53-
siProject = getConverter('Project', 'ip_units', 'si_units')(ipProject);
53+
siProject = getConverter('Project', 'ip', 'si')(ipProject);
5454

5555
it('converts nested attributes', () => {
5656
assert(isNearlyEqual(
@@ -61,7 +61,7 @@ describe('getConverter', () => {
6161

6262
it('provides the identity func when converting unknown objects', () => {
6363
assertEqual(
64-
getConverter('User', 'si_units', 'ip_units'),
64+
getConverter('User', 'si', 'ip'),
6565
_.identity);
6666
});
6767

@@ -113,20 +113,20 @@ describe('getConverter', () => {
113113
assert(isNearlyEqual(
114114
_.omit(siWindow, ['name', 'id']),
115115
_.omit(
116-
getConverter('WindowDefinition', 'ip_units', 'si_units')(ipWindow),
116+
getConverter('WindowDefinition', 'ip', 'si')(ipWindow),
117117
['name', 'id'])));
118118

119119
assert(isNearlyEqual(
120120
_.omit(ipWindow, ['name', 'id']),
121121
_.omit(
122-
getConverter('WindowDefinition', 'si_units', 'ip_units')(siWindow),
122+
getConverter('WindowDefinition', 'si', 'ip')(siWindow),
123123
['name', 'id'])));
124124
});
125125

126126
it('applies recursive Vertex conversion to Geometry', () => {
127127
const
128-
geomConvert = getConverter('Geometry', 'si_units', 'ip_units'),
129-
vertConvert = getConverter('Vertex', 'si_units', 'ip_units'),
128+
geomConvert = getConverter('Geometry', 'si', 'ip'),
129+
vertConvert = getConverter('Vertex', 'si', 'ip'),
130130
ipGeometry = geomConvert(simpleGeometry);
131131
assertEqual(
132132
_.omit(simpleGeometry, 'vertices'),
@@ -138,20 +138,20 @@ describe('getConverter', () => {
138138
});
139139

140140
describe('convertSchema', () => {
141-
const siFloorplan = convertSchema(ipFloorplan, 'ip_units', 'si_units');
141+
const siFloorplan = convertSchema(ipFloorplan, 'ip', 'si');
142142
it('has the same keys as original', () => {
143143
assertEqual(_.keys(siFloorplan), _.keys(ipFloorplan));
144144
});
145145

146146
it('converted the project', () => {
147-
const projectConvert = getConverter('Project', 'ip_units', 'si_units');
147+
const projectConvert = getConverter('Project', 'ip', 'si');
148148
assertEqual(
149149
siFloorplan.project,
150150
projectConvert(ipFloorplan.project));
151151
});
152152

153153
it('converted each vertex', () => {
154-
const vertConvert = getConverter('Vertex', 'ip_units', 'si_units');
154+
const vertConvert = getConverter('Vertex', 'ip', 'si');
155155

156156
assertEqual(
157157
_.chain(siFloorplan.stories)
@@ -435,16 +435,16 @@ describe('convertState', () => {
435435
},
436436
"timetravelInitialized": true
437437
}`),
438-
siState = convertState(ipState, 'ip_units', 'si_units');
438+
siState = convertState(ipState, 'ip', 'si');
439439
it('converts nested library values', () => {
440-
const convertWindowDefn = getConverter('WindowDefinition', 'ip_units', 'si_units');
440+
const convertWindowDefn = getConverter('WindowDefinition', 'ip', 'si');
441441
assert(isNearlyEqual(
442442
siState.models.library.window_definitions[0],
443443
convertWindowDefn(ipState.models.library.window_definitions[0])));
444444
});
445445

446446
it('converts nested geometry state', () => {
447-
const convertGeom = getConverter('Geometry', 'ip_units', 'si_units');
447+
const convertGeom = getConverter('Geometry', 'ip', 'si');
448448
assert(isNearlyEqual(
449449
siState.geometry[0],
450450
convertGeom(ipState.geometry[0])));
@@ -457,7 +457,7 @@ describe('convertState', () => {
457457
});
458458

459459
it('converts space properties', () => {
460-
const convertSpace = getConverter('Space', 'ip_units', 'si_units');
460+
const convertSpace = getConverter('Space', 'ip', 'si');
461461

462462
assertEqual(
463463
siState.models.stories[0].spaces[0],

0 commit comments

Comments
 (0)