2

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';
            }
        }
Michael Todd
  • 211
  • 1
  • 3

2 Answers2

1
  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

Jiri Polcar
  • 306
  • 1
  • 4
  • Not 100% sure how to implement this. The code i want to reuse it from the "onUpdate": "". this code currently appears in several files, I would like to access this code from an external file and use it were ever I need it, very much like a FUNCTION, could you give me more of an example on how this could be done? – Michael Todd Jun 09 '21 at 11:09
  • I've managed to get this working using the getMaterialProperty but would still like to understand more of the SubComponents suggestion – Michael Todd Jun 10 '21 at 12:00
1

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});"
}
Jiri Polcar
  • 306
  • 1
  • 4