Skip to content

Commit

Permalink
Fix: Fix minor bugs lead by mismatching identifiers (Snapmaker#2359)
Browse files Browse the repository at this point in the history
* Fix: Fix Snapmaker Original wrong case path
* Fix: Fix project file missing origin setting
* Fix: Fix old project file with old identifiers
  • Loading branch information
parachvte authored Sep 18, 2023
1 parent db75629 commit f05defe
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 42 deletions.
56 changes: 50 additions & 6 deletions src/app/flux/project/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import pkg from '../../../package.json';
import api from '../../api';
import {
COORDINATE_MODE_BOTTOM_CENTER,
COORDINATE_MODE_BOTTOM_LEFT,
COORDINATE_MODE_BOTTOM_RIGHT,
COORDINATE_MODE_CENTER,
COORDINATE_MODE_TOP_LEFT,
COORDINATE_MODE_TOP_RIGHT,
DISPLAYED_TYPE_MODEL,
HEAD_TYPE_ENV_NAME,
LEFT_EXTRUDER,
Expand All @@ -17,11 +21,12 @@ import {
RIGHT_EXTRUDER,
SOURCE_TYPE
} from '../../constants';
import { convertMaterialsToWorkpiece } from '../../constants/coordinate';
import { CylinderWorkpieceReference, OriginType, RectangleWorkpieceReference, convertMaterialsToWorkpiece } from '../../constants/coordinate';
import { HEAD_CNC, HEAD_LASER, HEAD_PRINTING, SINGLE_EXTRUDER_TOOLHEAD_FOR_SM2 } from '../../constants/machines';
import { checkIsGCodeFile, checkIsSnapmakerProjectFile } from '../../lib/check-name';
import { logModuleVisit } from '../../lib/gaEvent';
import i18n from '../../lib/i18n';
import log from '../../lib/log';
import { PROCESS_STAGE } from '../../lib/manager/ProgressManager';
import workerManager from '../../lib/manager/workerManager';
import { bubbleSortByAttribute } from '../../lib/numeric-utils';
Expand Down Expand Up @@ -172,7 +177,7 @@ export const actions = {
const envObj = JSON.parse(content);
if (!envObj.models.length) return;
} catch (e) {
console.info(`Unable to parse environment JSON for ${headType}`);
log.info(`Unable to parse environment JSON for ${headType}`);
}

dispatch(actions.updateState(headType, { findLastEnvironment: true, content }));
Expand All @@ -183,7 +188,7 @@ export const actions = {
try {
await api.env.removeEnv({ headType });
} catch (e) {
console.log(e);
log.error(e);
}

dispatch(actions.updateState(headType, { unSaved: false }));
Expand Down Expand Up @@ -290,12 +295,51 @@ export const actions = {
dispatch(editorActions.updateWorkpieceObject(envHeadType));
}

const isRotate = materials ? materials.isRotate : false;

// origin
if (origin) {
dispatch(editorActions.setOrigin(envHeadType, origin));
} else {
// Old project does not have origin saved, we asssume that they use "Workpiece" origin type
let reference;
if (!isRotate) {
switch (coordinateMode.value) {
case COORDINATE_MODE_CENTER.value:
reference = RectangleWorkpieceReference.Center;
break;
case COORDINATE_MODE_BOTTOM_LEFT.value:
reference = RectangleWorkpieceReference.BottomLeft;
break;
case COORDINATE_MODE_BOTTOM_RIGHT.value:
reference = RectangleWorkpieceReference.BottomRight;
break;
case COORDINATE_MODE_TOP_LEFT.value:
reference = RectangleWorkpieceReference.TopLeft;
break;
case COORDINATE_MODE_TOP_RIGHT.value:
reference = RectangleWorkpieceReference.TopLeft;
break;
default:
reference = RectangleWorkpieceReference.Center;
break;
}
} else {
switch (coordinateMode.value) {
case COORDINATE_MODE_BOTTOM_CENTER.value:
default:
reference = CylinderWorkpieceReference.FrontCenter;
break;
}
}

dispatch(editorActions.setOrigin(envHeadType, {
type: OriginType.Workpiece,
reference: reference,
referenceMetadata: {},
}));
}

const isRotate = materials ? materials.isRotate : false;
const oversize = some(keys(coordinateSize), (key) => {
return currentSize[key] < coordinateSize[key];
});
Expand Down Expand Up @@ -385,7 +429,7 @@ export const actions = {

Promise.all(promiseArray).then(() => {
dispatch(actions.afterOpened(envHeadType));
}).catch(console.error);
}).catch(log.error);
},

exportFile: (targetFile, renderGcodeFileName = null) => async (dispatch) => {
Expand Down Expand Up @@ -542,7 +586,7 @@ export const actions = {
try {
envObj = JSON.parse(content);
} catch (e) {
console.error(e);
log.error(e);
return;
}
const machineInfo = envObj.machineInfo;
Expand Down
5 changes: 0 additions & 5 deletions src/app/models/ModelGroup2D.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,11 +464,6 @@ class ModelGroup2D extends EventEmitter {
}

public getSelectedModelsForHotZoneCheck() {
// TODO: Refactor this
// if (this.series !== 'A400') {
// return [];
// }

return this.getModels<Model3D>();
}

Expand Down
22 changes: 15 additions & 7 deletions src/app/ui/pages/cnc-main/CNCMainPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ import { actions as projectActions } from '../../../flux/project';
import i18n from '../../../lib/i18n';
import modal from '../../../lib/modal';
import { getUploadModeByFilename } from '../../../lib/units';
import {
SnapmakerA150Machine,
SnapmakerA250Machine,
SnapmakerA350Machine,
SnapmakerArtisanMachine,
SnapmakerOriginalExtendedMachine,
SnapmakerOriginalMachine
} from '../../../machines';
import { machineStore } from '../../../store/local-storage';
import { Button } from '../../components/Buttons';
import Checkbox from '../../components/Checkbox';
Expand All @@ -24,8 +32,8 @@ import { logPageView, renderModal, renderPopup, useUnsavedTitle } from '../../ut
import CNCVisualizer from '../../widgets/CNCVisualizer';
import Thumbnail from '../../widgets/CncLaserShared/Thumbnail';
import useRenderMainToolBar from '../CncLaserShared/MainToolBar';
import useRenderRemoveModelsWarning from '../CncLaserShared/RemoveAllModelsWarning';
import RenderProjectRightView from '../CncLaserShared/ProjectRightView';
import useRenderRemoveModelsWarning from '../CncLaserShared/RemoveAllModelsWarning';
import HomePage from '../HomePage';
import Workspace from '../Workspace';
import {
Expand Down Expand Up @@ -244,32 +252,32 @@ const Cnc: React.FC<CNCMainPageProps> = ({ location }) => {
};
} else {
switch (series) {
case 'Original Long Z-axis':
case 'Original':
case SnapmakerOriginalMachine.identifier:
case SnapmakerOriginalExtendedMachine.identifier:
pathConfig = {
path: './UserCase/cnc/original_standard/cnc_original_standard.snapcnc',
name: 'cnc_original_standard.snapcnc'
};
break;
case 'A150':
case SnapmakerA150Machine.identifier:
pathConfig = {
path: './UserCase/cnc/a150_standard/cnc_a150_standard.snapcnc',
name: 'cnc_a150_standard.snapcnc'
};
break;
case 'A250':
case SnapmakerA250Machine.identifier:
pathConfig = {
path: './UserCase/cnc/a250_standard/cnc_a250_standard.snapcnc',
name: 'cnc_a250_standard.snapcnc'
};
break;
case 'A350':
case SnapmakerA350Machine.identifier:
pathConfig = {
path: './UserCase/cnc/a350_standard/cnc_a350_standard.snapcnc',
name: 'cnc_a350_standard.snapcnc'
};
break;
case 'A400':
case SnapmakerArtisanMachine.identifier:
pathConfig = {
path: './UserCase/cnc/Luban Lock.snapcnc',
name: 'Luban Lock.snapcnc',
Expand Down
2 changes: 1 addition & 1 deletion src/app/ui/widgets/Enclosure/Enclosure.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ const Enclosure: React.FC = () => {
/>
</div>
{/* Disable adjustment for door detection
{(isConnected && connectionType === 'wifi' && headType !== '3dp' && series !== 'A400') && (
{(isConnected && connectionType === 'wifi' && headType !== '3dp') && (
<TipTrigger
title={i18n._('key-Workspace/Enclosure-Door Detection')}
content={(
Expand Down
73 changes: 50 additions & 23 deletions src/server/services/api/api-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,15 @@ import {
findMachineByName,
getMachineToolHeadConfigPath,
isDualExtruder,
LEVEL_ONE_POWER_LASER_FOR_ORIGINAL,
LEVEL_ONE_POWER_LASER_FOR_SM2,
MACHINE_TOOL_HEADS,
SINGLE_EXTRUDER_TOOLHEAD_FOR_ORIGINAL,
SINGLE_EXTRUDER_TOOLHEAD_FOR_SM2,
STANDARD_CNC_TOOLHEAD_FOR_ORIGINAL,
STANDARD_CNC_TOOLHEAD_FOR_SM2,
} from '../../../app/constants/machines';
import { SnapmakerA150Machine, SnapmakerA250Machine, SnapmakerA350Machine, SnapmakerOriginalExtendedMachine, SnapmakerOriginalMachine } from '../../../app/machines';
import { printToolHead, standardCNCToolHead, standardLaserToolHead } from '../../../app/machines/snapmaker-2-toolheads';
import { cncToolHeadOriginal, laserToolHeadOriginal, printToolHeadOriginal } from '../../../app/machines/snapmaker-original-toolheads';
import { generateRandomPathName } from '../../../shared/lib/random-utils';
import { removeSpecialChars } from '../../../shared/lib/utils';
import DataStorage, { rmDir } from '../../DataStorage';
import { ERR_BAD_REQUEST, ERR_INTERNAL_SERVER_ERROR, HEAD_CNC, HEAD_LASER, HEAD_PRINTING } from '../../constants';
import { PROTOCOL_TEXT } from '../../controllers/constants';
import DataStorage, { rmDir } from '../../DataStorage';
import { unzipFile, zipFolder } from '../../lib/archive';
import { packFirmware } from '../../lib/firmware-build';
import logger from '../../lib/logger';
Expand Down Expand Up @@ -48,32 +44,63 @@ function traverse(models, callback) {
});
}

// Default toolHead for original
export const INITIAL_TOOL_HEAD_FOR_ORIGINAL = {
function getFormalMachineIdentifier(machineIdentifier) {
switch (machineIdentifier) {
case 'A150':
return SnapmakerA150Machine.identifier;
case 'A250':
return SnapmakerA250Machine.identifier;
case 'A350':
return SnapmakerA350Machine.identifier;
default:
return machineIdentifier;
}
}


// Default tool heads for Snapmaker original
export const INITIAL_SNAPMAKER_ORIGINAL_TOOL_HEAD_IDENTIFIER_MAP = {
printingToolhead:
MACHINE_TOOL_HEADS[SINGLE_EXTRUDER_TOOLHEAD_FOR_ORIGINAL].value,
laserToolhead: MACHINE_TOOL_HEADS[LEVEL_ONE_POWER_LASER_FOR_ORIGINAL].value,
cncToolhead: MACHINE_TOOL_HEADS[STANDARD_CNC_TOOLHEAD_FOR_ORIGINAL].value
printToolHeadOriginal.identifier,
laserToolhead: laserToolHeadOriginal.identifier,
cncToolhead: cncToolHeadOriginal.identifier,
};

export const INITIAL_TOOL_HEAD_FOR_SM2 = {
printingToolhead:
MACHINE_TOOL_HEADS[SINGLE_EXTRUDER_TOOLHEAD_FOR_SM2].value,
laserToolhead: MACHINE_TOOL_HEADS[LEVEL_ONE_POWER_LASER_FOR_SM2].value,
cncToolhead: MACHINE_TOOL_HEADS[STANDARD_CNC_TOOLHEAD_FOR_SM2].value
export const INITIAL_SNAPMAKER_2_TOOL_HEAD_IDENTIFIER_MAP = {
printingToolhead: printToolHead.identifier,
laserToolhead: standardLaserToolHead.identifier,
cncToolhead: standardCNCToolHead.identifier,
};

function getDefaultToolHeadMap(machineIdentifier) {
switch (machineIdentifier) {
case SnapmakerOriginalMachine.identifier:
case SnapmakerOriginalExtendedMachine.identifier:
return INITIAL_SNAPMAKER_ORIGINAL_TOOL_HEAD_IDENTIFIER_MAP;
case SnapmakerA150Machine.identifier:
case SnapmakerA250Machine.identifier:
case SnapmakerA350Machine.identifier:
return INITIAL_SNAPMAKER_2_TOOL_HEAD_IDENTIFIER_MAP;
default:
return INITIAL_SNAPMAKER_ORIGINAL_TOOL_HEAD_IDENTIFIER_MAP;
}
}

function getConfigDir(machineInfo) {
let configPath = '';

if (machineInfo) {
const series = machineInfo?.series;
const machineIdentifier = getFormalMachineIdentifier(series);

const headType = machineInfo?.headType;
const toolHead = machineInfo?.toolHead || (series === 'Original' ? INITIAL_TOOL_HEAD_FOR_ORIGINAL : INITIAL_TOOL_HEAD_FOR_SM2);
const toolHeadIdentifierMap = machineInfo?.toolHead || getDefaultToolHeadMap(machineIdentifier);

const machine = findMachineByName(series);
const machine = findMachineByName(machineIdentifier);
if (machine) {
configPath = getMachineToolHeadConfigPath(machine, toolHead[`${headType}Toolhead`]);
configPath = getMachineToolHeadConfigPath(machine, toolHeadIdentifierMap[`${headType}Toolhead`]);
} else {
log.warn(`Unable to find machine with identifier ${machineIdentifier}.`);
}
}

Expand Down Expand Up @@ -603,11 +630,11 @@ export const recoverProjectFile = async (req, res) => {
});
res.end();
} catch (e) {
log.error(`Failed to recover file: ${file.path}`);
log.error(`Failed to recover from project file: ${file.path}`);
log.error(e);

res.status(ERR_INTERNAL_SERVER_ERROR).send({
msg: `Failed to recover file: ${e}`
msg: `Failed to recover from project file: ${e}`,
});
}
};

0 comments on commit f05defe

Please sign in to comment.