Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.

First time visiting? Here are some places to start:
  1. Looking for a certain topic? Check out the categories filter or use Search (upper right).
  2. Need support? Ask a question to our Community Support category.
  3. Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
  4. Be respectful, on topic and if you see a problem, Flag it.

If you would like to contact our Community Manager personally, feel free to send a private message or an email.

How to compute a part property

Mattéo_Le_GallMattéo_Le_Gall Member Posts: 2

Hello, I would like to calculate the price of a part based on its mass and its price/kg. I created a property (type List) to select the price/kg of the supplier. The display is the supplier name and the value is its price/kg.

I want to code a feature that get this value, multiply it by the mass of the piece and return the price of the piece to visualise it in my BOM.

Here is the script I get from an LLM but when I create my property and select this feature as the property function source, the cases of the column of the property in the BOM returns "no value", even if the supplier and the mass are known.

FeatureScript 2615;
import(path : "onshape/std/common.fs", version : "2615.0");

annotation { "Property Function Name" : "getCoutMatiereBrute" }
export const getCoutMatiereBrute = defineComputedPartProperty(function(context is Context, part is Query, definition is map) returns number
{
// 1. Lire le prix au kg depuis la propriété "Prix au kg fournisseur brut (€/kg)"
const prixStr = getProperty(context, {
entity : part,
propertyType : PropertyType.PART,
propertyName : "Prix au kg fournisseur brut (€/kg)"
});

// 2. Convertir string → nombre
var prixKg = 0.0;
if (isStringLiteral(prixStr))
{
    prixKg = stringToNumber(prixStr);
}

// 3. Lire le matériau de la pièce
const material = getProperty(context, {
    entity : part,
    propertyType : PropertyType.PART,
    propertyName : "Material"
});

// 4. Obtenir la densité (kg/m³)
const densite = lookupTableDensity(material);

// 5. Calculer le volume (m³)
const volume = evVolume(context, { entities : part });

// 6. Calculer la masse (kg)
const masse = volume * densite;

// 7. Retourner le coût brut (nombre sans unité)
return prixKg * masse;

});

Thanks for your help

Comments

Sign In or Register to comment.