Reusing code across multiple JSON files, using subcomponents and superseding

Viewed 62

I have some code that works out what a Colour Code suffix should be depending on the Material that is selected. I want to reuse this code across several files (parts) I have read that this might be possible using subcomponents and superseding but not sure how to implement this as I want to return a value depending on the input, for example this is the code I want to reuse...

        if(supplyMFC=='Yes'){
            if(mfcColour=='racksystems_test:graphite'){
                pc='-GR';
            }
            if(mfcColour=='racksystems_test:oak'){
                pc='-OA';
            }
            if(mfcColour=='racksystems_test:white'){
                pc='-WH';
            }
            if(mfcColour=='racksystems_test:greyoak'){
                pc='-GO';
            }
            if(mfcColour=='racksystems_test:walnut'){
                pc='-WN';
            }
            if(mfcColour=='racksystems_test:beech'){
                pc='-BE';
            }
            if(mfcColour=='racksystems_test:cherry'){
                pc='-CH';
            }
            if(mfcColour=='racksystems_test:orange'){
                pc='-XX';
            }
        }
2 Answers
  1. Prepare a component, that will be used as a subComponent. Prepare parameters that will be used as input (supplyMfc, mfcColour) and for output (pc).
  2. Do the computation in this component.
  3. Use it like this:
"subComponents": [
    {
        "internalId": "MfcToPc",
        "componentId": "your reasonable componentId",
        "numberInPartList": 0,
        "active": true,
        "assignments": {
            "supplyMfc": "<expression>",
            "mfcColour": "<expression>"
        },
        "supersedings": {
            {
                "type": "parameter",
                "key": "pc"
            }
        }
    }
]

Note: you can either supersede or assign mfcColour, based on your wish.

I can see that you need to get some data that are dependend on the material value selected. For this, I recommend to consider this approach https://docs.roomle.com/scripting/resources/200_140_getmaterialproperty.html

To further give an example of https://docs.roomle.com/scripting/resources/200_70_subcomponents.html#_3-external-computation

Let's say we have a function we want to reuse across multiple components. We make and extra component for it, in means of a workaround for a missing function definition feature:

{
    "id": "foos:add",
    "parameters": [
        {
            "key": "argument1",
            "type": "Decimal"
        },
        {
            "key": "argument2",
            "type": "Decimal"
        },
        {
            "key": "result",
            "type": "Decimal"
        }
    ],
    "onUpdate": "
        result = argument1 + argument2;
    "
}

And we can further use it like following:

{
    "id": "your_catalogue:your_component",
    "subComponents": [
        {
            "internalId": "ADD",
            "componentId": "foos:add",
            "numberInPartList": "0",
            "active": "true",
            "assignments": {
                "argument1": "200",
                "argument2": "1000"
            },
            "supersedings": [
                {
                    "type": "parameter",
                    "key": "result"
                }
            ]
        }
    ],
    "onUpdate": "
        if (ifnull(inited, false) == false) {
            inited = true;
            result = 1000; /* good practice to pre-init supersedings */
        }
    ",
    "geometry": "AddCube(Vector3f{1000, 1000, result});"
}
Related