其他 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 文档。