其他 Windchill 功能 > 製造流程管理 > 自訂製程計劃總管 > 建立自訂方程式
  
建立自訂方程式
方程式集可用來使一組屬性與要用來計算其值的對應方程式相關聯。構成方程式集的元件如下所示:
FormulaSet 用來識別集合
FormulaMetaData 定義了 Java 方程式的名稱與描述
FormulaValuatedLink 可將屬性名稱關聯至方程式定義
下圖是需要建立來新增自訂方程式的資料範例:
「方程式集」標籤是用來選取可用於計算與作業相關聯的時間與成本的預先定義方程式集。例如,標準時間與成本方程式集,以及定期時間與成本方程式集。這些方程式集會考量在作業中產生的零件數量,並使用為工作中心定義的規格 (例如設定時間、佇列時間)。當已選取「計算時間與成本」動作時,以及將製程計劃與作業載入「製造甘特總管」時,會使用方程式與方程式集。
雖然多個方程式集可以與一個工作中心產生關聯,但只能指派一個方程式集作為該工作中心的預設方程式集。當將工作中心分配到作業時,會將這個方程式集指派給該作業。
* 
必須將方程式指派給方程式集。如果您沒有現有的方程式集,必須先定義一個方程式集,才能定義自訂方程式。PSE 可以自訂,讓您能夠使用 PSE 將其他值新增至您的自訂方程式集。如需詳細資訊,請參閱 PSE 文件集。
欲建立自訂方程式,請遵循下列步驟:
1. 使用 LoadFromFile 公用程式,載入 XML 型載入檔案,其中 <csvname> 對系統而言具唯一性。此載入檔案必須包含下列所有元素:
csvFormulaSet - 這會建立 FormulaSet 物件。
csvFormulaMetaData - 這會建立 FormulaMetaData 物件。
csvAddFormulaMetaData - 這會將 FormulaMetaData 連結至 FormulaSet。
<csvFormulaSet handler="com.ptc.windchill.mpml.formula.LoadFormula.
createFormulaSet">
<csvuser></csvuser>
<csvname>Standard Time and Cost</csvname>
<csvdescription>This formula Set is used to calculate the time and
cost of an Operation</csvdescription>
<csvfolder>/Default/Design</csvfolder>
<csvparentcontextPath></csvparentcontextPath>
<csvorganizationName></csvorganizationName>
</csvFormulaSet>
2. 連結至實行方程式介面並傳回已計算值的 java 類別名稱,來建立方程式中繼資料。例如,FloatingPointWithUnits。
使用 LoadFromFile 公用程式,載入 XML 型載入檔案,其中 <csvname> 對系統而言具唯一性,且 <csvformulaClassName> 是實行方程式介面的 Java 類別。
<csvFormulaMetaData handler="com.ptc.windchill.mpml.formula.
LoadFormula.createFormulaMetaData">
<csvuser></csvuser>
<csvname>StandardAttributeValueFormulaTime</csvname>
<csvdescription>Standard Attribute Value Formula</csvdescription>
<csvtype>time</csvtype>
<csvformulaClassName>com.ptc.windchill.mpml.formula.Standard AttributeValueFormula</csvformulaClassName>
<csvfolder>/Default/Design</csvfolder>
<csvparentcontextPath></csvparentcontextPath>
<csvorganizationName></csvorganizationName>
<csvorganizationID></csvorganizationID>
</csvFormulaMetaData>
3. 使用屬性名稱將方程式中繼資料連結至方程式集。
例如,當呼叫計算方程式的服務時,將會執行與方程式集相關聯的所有方程式中繼資料,並且會使用屬性名稱作為索引鍵來傳回所有已計算值的對應。
使用 LoadFromFile 公用程式,載入 XML 型載入檔案,其中 <csvattributeName> 是 MPMOperation 物件中的時間與成本可重新使用屬性
<csvAddFormulaMetaData handler="com.ptc.windchill.mpml.formula.
LoadFormula.addFormulaMetaDataToFormulaSet" >
<csvformulaSetName>Standard Time and Cost</csvformulaSetName>
<csvformulaMetaDataName>StandardAttributeValueFormula Time</csvformulaMetaDataName>
<csvattributeName>IBA|MPM_Attr_MPMOperationSetupTime </csvattributeName>
</csvAddFormulaMetaData>
當建立實行方程式介面的 java 類別時,這些輸入預設可在輸入對應中存取。
FormulaHelper.FORMULAVALUATED:對其連結方程式集且轉換為 ForumlaValuated 的物件。
FormulatHelper.FORMULAVALUATED_TI:對其連結方程式集且轉換為 TypeInstance 的物件類型實例。它可以是空值。
FormulaHelper.FORMULAVALUATED_ATTRIBUTE_NAME:用來將方程式中繼資料連結至方程式集且轉換為「字串」的屬性名稱。這將會在結果對應中作為索引鍵使用。
FormulaHelper.LOT:由使用者在「計算時間與成本」動作的前後關聯中指定的批號值。它會轉換為雙精度浮點數。
以下是方程式 java 類別的範例:
import com.ptc.core.meta.common.AttributeIdentifier;
import com.ptc.core.meta.common.AttributeTypeIdentifier;
import com.ptc.core.meta.common.TypeinstanceIdentifier;
import com.ptc.core.meta.context.common.AttributecontextSpec
import com.ptc.core.meta.server.TypeIdentifierUtility;
import com.ptc.core.meta.type.common.TypeInstance;
import com.ptc.core.meta.type.common.TypeInstanceFactory;
import com.ptc.windchill.mpml.MPMLinkHelper;
import java.text.NumberFormat;
import java.util.HashMap;
import wt.units.FloatingPointWithUnits;
import wt.util.WTContext;
import util.WTException;
public class StandardAttributeValueFormula implements Formula{
private static final String RESOURCE = "com.ptc.windchill.mpml.formula.formulaResource";
private static NumberFormat numberFormat =
NumberFormat.getInstance(WTContext.getContext().getLocale());
/** Creates a new instance of StandardAttributeValueFormula */
* This Formula simply return the value of the attribute.
public StandardAttributeValueFormula() {
}
public FloatingPointWithUnits calculate(HashMap inputs)throws
InvalidFormulaInputException, WTException{
//Get the object on which the formula is call
Object object = inputs.get(FormulaHelper.FORMULAVALUATED);
//Get the TI on which the formula is call
TypeInstance ti =(TypeInstance)inputs.get(FormulaHelper.FORMULAVALUATED_TI);
//Get the attribute that is calculated by the formula
String attribute_name = (String)inputs.get(FormulaHelper.FORMULAVALUATED_ATTRIBUTE_NAME);
if(object == null)
throw new InvalidFormulaInputException( RESOURCE, formulaResource.REQUIRED_FORMULA_INPUT_MISSING, new Object[]{FormulaHelper.FORMULAVALUATED} );;
TypeInstanceIdentifier tii = null;

//Get the TI of the object if null
if(ti==null){
tii = TypeIdentifierUtility.getTypeInstanceIdentifier(object);
ti = TypeInstanceFactory.newTypeInstance(tii);
}else
tii = (TypeInstanceIdentifier)ti.getIdentifier();
//Get ATI for the attribute to calculate
AttributeTypeIdentifier ati = (AttributeTypeIdentifier)MPMLinkHelper.getIdentifierFactory().get(attribute_name,tii.getDefinitionIdentifier());
AttributeIdentifier[] ais = ti.getAttributeIdentifiers(ati);
//If the attribute value is not in the TI, update it to get the value
if(ais.length<1){
AttributecontextSpec fl = new AttributecontextSpec();
fl.putEntry(ati);
ti = MPMLinkHelper.updateTypeInstance(new TypeInstance[] {ti}, fl, null)[0];
ais = ti.getAttributeIdentifiers(ati);
}
//Get the attribute value
Object value = null;
if(ais.length>0)
value = ti.get(ais[0]);
//Return the value of the attribute if it’s a FloatingPointWithUnits
if(value instanceof FloatingPointWithUnits)
return (FloatingPointWithUnits)value;
// value is null
return FloatingPointWithUnits.valueOf(numberFormat.format(0));
}
}
* 
您也可以透過建立新動作,然後使用方程式服務呼叫方程式集的執行,以將更多輸入新增至方程式。如需有關建立新動作的詳細資訊,請參閱 PSE 文件集。