def create_composition(composition_db: sm.Composition, catalog: cat_.Catalog) -> cat_.Item:
    item = cat_.Item(
        composition_db.id,
        composition_db.name,
        composition_db.code,
        composition_db.group_id,
        description=composition_db.name)
    primary_quantity = item.get_variable('PrimaryQuantity')
    primary_quantity.formula = '=' + units_convert[composition_db.units]['formula']
    primary_quantity.units = units_convert[composition_db.units]['units']

    catalog.add_item(item)

    for attribution in composition_db.grouped_attributions:
        create_attribution(attribution.Attribution, item, catalog, attribution.sum_coefficient)

    return item
Beispiel #2
0
def create_full_composition(composition_db: sm.Composition,
                            catalog: cat_.Catalog) -> cat_.Item:
    item = cat_.Item(composition_db.id,
                     composition_db.name,
                     composition_db.code,
                     composition_db.group_id,
                     description=composition_db.name)
    primary_quantity = item.get_variable('PrimaryQuantity')
    primary_quantity.formula = '=' + units_convert[
        composition_db.units]['formula']
    primary_quantity.units = units_convert[composition_db.units]['units']

    catalog.add_item(item)

    for step in composition_db.steps:
        step: sm.Step = step

        if len(step.attributions):
            s = cat_.Step(step.id, step.name, step.composition_id)
            c = s.get_variable('Coefficient')
            c.formula = f"={step.coefficient}"
            item.append(s)

            for attribution in step.attributions:
                attribution: sm.Attribution = attribution

                step_resource = cat_.StepResource(attribution.id,
                                                  attribution.step_id,
                                                  attribution.resource_id)
                s.append(step_resource)

                coef = cat_.Variable('Coefficient', formula='=0')
                step_resource.add_variable(coef)

                primary_quantity = cat_.Variable('PrimaryQuantity')
                primary_quantity.units = units_convert[
                    attribution.resource.units]['units']
                primary_quantity.formula = f'{item.get_variable("PrimaryQuantity").formula}*{coef.name}'
                step_resource.add_variable(primary_quantity)

                coef = step_resource.get_variable('Coefficient')
                coef.formula = f"={step.coefficient * attribution.coefficient * units_convert[attribution.resource.units]['Coefficient']}"

    return item