Skip to content

Commit 0871778

Browse files
committed
Preface components inside mechanism
1 parent d165af6 commit 0871778

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

src/blocks/mrc_call_python_function.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,34 @@ export function getInstanceComponentBlocks(
11421142
return contents;
11431143
}
11441144

1145+
export function getInstanceMechanismComponentBlocks(
1146+
component: storageModuleContent.Component, mechanismInRobot: storageModuleContent.MechanismInRobot): toolboxItems.ContentsType[] {
1147+
const contents: toolboxItems.ContentsType[] = [];
1148+
1149+
const classData = getClassData(component.className);
1150+
if (!classData) {
1151+
throw new Error('Could not find classData for ' + component.className);
1152+
}
1153+
const functions = classData.instanceMethods;
1154+
1155+
const componentClassData = getClassData('component.Component');
1156+
if (!componentClassData) {
1157+
throw new Error('Could not find classData for component.Component');
1158+
}
1159+
const componentFunctions = componentClassData.instanceMethods;
1160+
1161+
for (const functionData of functions) {
1162+
// Skip the functions that are also defined in componentFunctions.
1163+
if (findSuperFunctionData(functionData, componentFunctions)) {
1164+
continue;
1165+
}
1166+
const block = createInstanceMechanismComponentBlock(component, functionData, mechanismInRobot);
1167+
contents.push(block);
1168+
}
1169+
1170+
return contents;
1171+
}
1172+
11451173
function createInstanceComponentBlock(
11461174
component: storageModuleContent.Component, functionData: FunctionData): toolboxItems.Block {
11471175
const extraState: CallPythonFunctionExtraState = {
@@ -1166,6 +1194,32 @@ function createInstanceComponentBlock(
11661194
return createBlock(extraState, fields, inputs);
11671195
}
11681196

1197+
function createInstanceMechanismComponentBlock(
1198+
component: storageModuleContent.Component,
1199+
functionData: FunctionData,
1200+
mechanismInRobot: storageModuleContent.MechanismInRobot): toolboxItems.Block {
1201+
const extraState: CallPythonFunctionExtraState = {
1202+
functionKind: FunctionKind.INSTANCE_COMPONENT,
1203+
returnType: functionData.returnType,
1204+
args: [],
1205+
tooltip: functionData.tooltip,
1206+
importModule: '',
1207+
componentClassName: component.className,
1208+
componentName: mechanismInRobot.name + '.' + component.name, // Prefix with mechanism name
1209+
componentId: component.componentId,
1210+
};
1211+
const fields: {[key: string]: any} = {};
1212+
fields[FIELD_COMPONENT_NAME] = mechanismInRobot.name + '.' + component.name; // Prefix with mechanism name
1213+
fields[FIELD_FUNCTION_NAME] = functionData.functionName;
1214+
const inputs: {[key: string]: any} = {};
1215+
// For INSTANCE_COMPONENT functions, the 0 argument is 'self', but
1216+
// self is represented by the FIELD_COMPONENT_NAME field.
1217+
// We don't include the arg for the self argument because we don't need a socket for it.
1218+
const argsWithoutSelf = functionData.args.slice(1);
1219+
processArgs(argsWithoutSelf, extraState, inputs);
1220+
return createBlock(extraState, fields, inputs);
1221+
}
1222+
11691223
export function addInstanceRobotBlocks(
11701224
methods: storageModuleContent.Method[],
11711225
contents: toolboxItems.ContentsType[]) {

src/toolbox/hardware_category.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { createMechanismBlock } from '../blocks/mrc_mechanism';
2727
import { getAllPossibleComponents } from '../blocks/mrc_component';
2828
import {
2929
getInstanceComponentBlocks,
30+
getInstanceMechanismComponentBlocks,
3031
addInstanceRobotBlocks,
3132
addInstanceMechanismBlocks } from '../blocks/mrc_call_python_function';
3233
import { Editor } from '../editor/editor';
@@ -117,7 +118,7 @@ function getRobotMechanismsCategory(editor: Editor): toolboxItems.Category {
117118
componentBlocks.push({
118119
kind: 'category',
119120
name: component.name,
120-
contents: getInstanceComponentBlocks(component),
121+
contents: getInstanceMechanismComponentBlocks(component, mechanismInRobot),
121122
});
122123
});
123124
mechanismCategories.push({

0 commit comments

Comments
 (0)