Skip to content

Commit

Permalink
Merge pull request #304 from NREL/import-floorplan-units
Browse files Browse the repository at this point in the history
assume project units if none are present in floorplan import
  • Loading branch information
bgschiller authored Mar 6, 2018
2 parents aa21430 + 597c193 commit ef8c3c6
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 32 deletions.
1 change: 1 addition & 0 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ window.api = {
try {
window.application.$store.dispatch('importLibrary', { data: JSON.parse(data) });
} catch (err) {
console.error(err);
return false;
}
return true;
Expand Down
13 changes: 10 additions & 3 deletions src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,20 @@ const store = new Vuex.Store({
importFloorplan,
importLibrary,
changeUnits(context, { newUnits }) {
console.log(`moving from ${context.state.project.config.units} to ${newUnits}`);
const oldUnits = context.state.project.config.units;
console.log(`moving from ${oldUnits} to ${newUnits}`);
if (oldUnits !== 'ip' && oldUnits !== 'si') {
console.error(`Expected data.project.config.units to be "ip" or "si", received "${oldUnits}"`);
}
if (newUnits !== 'ip' && newUnits !== 'si') {
console.error(`Expected oldUnits to be "ip" or "si", received "${newUnits}"`);
}
context.commit(
'changeUnits',
convertState(
context.state,
context.state.project.config.units === 'm' ? 'si_units' : 'ip_units',
newUnits === 'm' ? 'si_units' : 'ip_units'));
oldUnits,
newUnits));
context.dispatch('project/setUnits', { units: newUnits });
},
},
Expand Down
2 changes: 1 addition & 1 deletion src/store/modules/models/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ if (ip_defaults.Project.config.units !== 'ip') {
}
const si_defaults = _.mapValues(
ip_defaults,
(value, key) => getConverter(key, 'ip_units', 'si_units')(value));
(value, key) => getConverter(key, 'ip', 'si')(value));

export const getDefaults = key =>
_.cloneDeep(_.get(window, 'application.$store.state.project.config.units', 'ip') === 'ip' ?
Expand Down
17 changes: 13 additions & 4 deletions src/store/utilities/importLibrary.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,19 @@ export default function importLibrary(context, payload) {
});
});

const
librarySystem = payload.data.project.config.units === 'ft' ? 'ip_units' : 'si_units',
projectSystem = context.state.project.config.units === 'ft' ? 'ip_units' : 'si_units',
localUnitsPayload = convertLibrary(payload.data, librarySystem, projectSystem);
const projectSystem = context.state.project.config.units;
const librarySystemRaw = _.get(payload, 'data.project.config.units');
let librarySystem = (
librarySystemRaw === 'ft' ? 'ip' :
librarySystemRaw === 'm' ? 'si' :
librarySystemRaw);

if (librarySystem !== 'ip' && librarySystem !== 'si') {
console.warn(`Expected data.project.config.units to be "ip" or "si", received "${librarySystemRaw}"`);
console.warn('unable to determine units of library -- using project units');
librarySystem = projectSystem;
}
const localUnitsPayload = convertLibrary(payload.data, librarySystem, projectSystem);

window.eventBus.$emit('success', `Imported ${count} object${count !== 1 ? 's' : ''}`);
// merge the import data with the existing library objects
Expand Down
21 changes: 12 additions & 9 deletions src/store/utilities/unitConversion.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,19 @@ export const conversionFactor = (fromUnits, toUnits) => {
return factor;
};

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

if (fromSystem === toSystem) { return _.identity; }
if (_fromSystem === _toSystem) { return _.identity; }

const fromSystem = `${_fromSystem}_units`;
const toSystem = `${_toSystem}_units`;

const pathUnits = _.get(units, path);
if (
Expand All @@ -87,16 +90,16 @@ export const getConverter = (path, fromSystem, toSystem) => {
return val => val * factor;
}
if (pathUnits.$ref) {
return getConverter(pathUnits.$ref, fromSystem, toSystem);
return getConverter(pathUnits.$ref, _fromSystem, _toSystem);
}
if (pathUnits.arrayOf) {
const converter = getConverter(pathUnits.arrayOf, fromSystem, toSystem);
const converter = getConverter(pathUnits.arrayOf, _fromSystem, _toSystem);
return arr => arr.map(converter);
}
if (_.isObject(pathUnits)) {
const converters = _.mapValues(
pathUnits,
(val, key) => getConverter(`${path}.${key}`, fromSystem, toSystem));
(val, key) => getConverter(`${path}.${key}`, _fromSystem, _toSystem));
return obj => _.mapValues(obj, (val, key) => (converters[key] || _.identity)(val));
}
throw new Error(`Path ${path} did not lead to a useful spot in 'units'`);
Expand Down
30 changes: 15 additions & 15 deletions test/unit/specs/unitConversions/unitConversions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ describe('conversionFactor', () => {
describe('getConverter', () => {
it('produces an acceptable conversion for Vertices', () => {
const
toIP = getConverter('Vertex', 'si_units', 'ip_units'),
toSI = getConverter('Vertex', 'ip_units', 'si_units'),
toIP = getConverter('Vertex', 'si', 'ip'),
toSI = getConverter('Vertex', 'ip', 'si'),
siVert = { x: 12, y: -5 },
ipVert = { x: 12 * 3.280839895, y: -5 * 3.280839895 };

Expand Down Expand Up @@ -50,7 +50,7 @@ describe('getConverter', () => {
},
show_import_export: false,
},
siProject = getConverter('Project', 'ip_units', 'si_units')(ipProject);
siProject = getConverter('Project', 'ip', 'si')(ipProject);

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

it('provides the identity func when converting unknown objects', () => {
assertEqual(
getConverter('User', 'si_units', 'ip_units'),
getConverter('User', 'si', 'ip'),
_.identity);
});

Expand Down Expand Up @@ -113,20 +113,20 @@ describe('getConverter', () => {
assert(isNearlyEqual(
_.omit(siWindow, ['name', 'id']),
_.omit(
getConverter('WindowDefinition', 'ip_units', 'si_units')(ipWindow),
getConverter('WindowDefinition', 'ip', 'si')(ipWindow),
['name', 'id'])));

assert(isNearlyEqual(
_.omit(ipWindow, ['name', 'id']),
_.omit(
getConverter('WindowDefinition', 'si_units', 'ip_units')(siWindow),
getConverter('WindowDefinition', 'si', 'ip')(siWindow),
['name', 'id'])));
});

it('applies recursive Vertex conversion to Geometry', () => {
const
geomConvert = getConverter('Geometry', 'si_units', 'ip_units'),
vertConvert = getConverter('Vertex', 'si_units', 'ip_units'),
geomConvert = getConverter('Geometry', 'si', 'ip'),
vertConvert = getConverter('Vertex', 'si', 'ip'),
ipGeometry = geomConvert(simpleGeometry);
assertEqual(
_.omit(simpleGeometry, 'vertices'),
Expand All @@ -138,20 +138,20 @@ describe('getConverter', () => {
});

describe('convertSchema', () => {
const siFloorplan = convertSchema(ipFloorplan, 'ip_units', 'si_units');
const siFloorplan = convertSchema(ipFloorplan, 'ip', 'si');
it('has the same keys as original', () => {
assertEqual(_.keys(siFloorplan), _.keys(ipFloorplan));
});

it('converted the project', () => {
const projectConvert = getConverter('Project', 'ip_units', 'si_units');
const projectConvert = getConverter('Project', 'ip', 'si');
assertEqual(
siFloorplan.project,
projectConvert(ipFloorplan.project));
});

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

assertEqual(
_.chain(siFloorplan.stories)
Expand Down Expand Up @@ -435,16 +435,16 @@ describe('convertState', () => {
},
"timetravelInitialized": true
}`),
siState = convertState(ipState, 'ip_units', 'si_units');
siState = convertState(ipState, 'ip', 'si');
it('converts nested library values', () => {
const convertWindowDefn = getConverter('WindowDefinition', 'ip_units', 'si_units');
const convertWindowDefn = getConverter('WindowDefinition', 'ip', 'si');
assert(isNearlyEqual(
siState.models.library.window_definitions[0],
convertWindowDefn(ipState.models.library.window_definitions[0])));
});

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

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

assertEqual(
siState.models.stories[0].spaces[0],
Expand Down

0 comments on commit ef8c3c6

Please sign in to comment.