式とグローバル パラメータ

式は、非レポート パラメータに割り当てることができます。

ファミリ パラメータの場合と同様に、式をグローバル パラメータに割り当てるには GlobalParameter.SetFormula() メソッドを使用することができます。式を設定するグローバル パラメータは非レポート パラメータである必要があるため、式の割り当て前にあらかじめレポート パラメータを非レポート パラメータに変更しておく必要があります。

評価した式の値は、パラメータの値タイプと互換性を持っている必要があります。たとえば、Double (Number)パラメータに割り当てられた式では Integer パラメータを使用できます。その逆も可能です。 その一方で、IntegerNumber のタイプのパラメータ内の式で、Length 引数や Angle 引数を使用することはできません。

式では、すべての標準の算術演算と論理演算を使用することができます(andornot は関数として使用します)。 論理演算に対する入力は、ブール値(YesNo タイプのパラメータ)である必要があります。したがって、算術演算を適用できるのは数値に限られます。文字列(文字)引数はどの演算にも使用できませんが、If 構文の論理演算の結果として使用することはできます。 パラメータのタイプ(と単位)に応じて、異なる値タイプのパラメータを組み合わせることができます。ただし、IntegerNumber (double)のように単位のない値は、単位のない値同士でのみ組み合わせることができます。

式は非常に複雑になる場合があります。また、一部の式は特定のパラメータに割り当てることができません。IsValidFormula() メソッドを使用すると、式がグローバル パラメータに使用できるかどうかを検証できます。SetFormula() を呼び出す際にグローバル パラメータに無効な式を使用すると、例外が発生します。

GetFormula() は、現在の式を文字列の形式で返します。

次のコード サンプルでは 4 つのグローバル パラメータを作成します。次に、そのうち 1 つのグローバル パラメータが、4 番目のグローバル パラメータのブール値に応じて、残り 2 つのパラメータのどちらかの値を持つように式を設定します。

コード領域: 式を設定

public void SetCombinationParameters(Document document)
{
    GlobalParameter gpB = null;
    GlobalParameter gpT = null;
    GlobalParameter gpF = null;
    GlobalParameter gpX = null;

    int TRUE = 1;
    int FALSE = 0;

    // transaction to create global parameters and set their values
    using (Transaction trans = new Transaction(document, "Creating global parameters"))
    {
        // create 4 new global parameters

        trans.Start();

        gpB = GlobalParameter.Create(document, "GPB", ParameterType.YesNo);
        gpT = GlobalParameter.Create(document, "GPT", ParameterType.Text);
        gpF = GlobalParameter.Create(document, "GPF", ParameterType.Text);
        gpX = GlobalParameter.Create(document, "GPX", ParameterType.Text);

        // assign initial values and a formula to the global parameters

        gpB.SetValue(new IntegerParameterValue(TRUE));
        gpT.SetValue(new StringParameterValue("TypeA"));
        gpF.SetValue(new StringParameterValue("TypeB"));

        // Set the formula to GPX so that its final value is either the value of GPT (TypeA)
        // or GPF (TypeB) depending on whether the value of GPB is True or False.
        // Note: in this particular case we are certain the formula is valid, but if weren't 
        // certain, we could use a validation method as we are now going to illustrate here:
        string expression = "if(GPB,GPT,GPF)"; // XPX <== if (GPB == TRUE) then GPT else GPF
        if (gpX.IsValidFormula(expression))
        {
            gpX.SetFormula(expression);  
        }

        trans.Commit();
    }

    // we can test that the formula works
    // since the boolean value is TRUE, the value of the GPX parameter
    // should be the same as the value of the GPT parameters

    StringParameterValue sTrue = gpT.GetValue() as StringParameterValue;
    StringParameterValue sFalse = gpF.GetValue() as StringParameterValue;
    StringParameterValue sValue = gpX.GetValue() as StringParameterValue;

    if (sValue.Value != sTrue.Value)
    {
        TaskDialog.Show("Error", "Unexpected value of a global parameter");
    }

    // we can also test that evaluation of the formula is affected by changes

    using (Transaction trans = new Transaction(document, "Change value of a YesNo parameter"))
    {
        trans.Start();
        gpB.SetValue(new IntegerParameterValue(FALSE));
        trans.Commit();
    }

    sValue = gpX.GetValue() as StringParameterValue;

    if (sValue.Value != sFalse.Value)
    {
        TaskDialog.Show("Error", "Unexpected value of a global parameter");
    }

}