グローバル パラメータで寸法にラベルを付ける

グローバル パラメータの重要な特長として、寸法に「ラベルを付ける」機能を挙げることができます。

グローバル パラメータで寸法にラベルをつけると、この値はパラメータによって制御されるか(非レポート パラメータの場合)、またはパラメータの値を駆動します(レポート パラメータの場合)。1 つのレポート パラメータでラベルを付けられる寸法オブジェクトは最大 1 つです。つまり、1 つのパラメータを駆動できる寸法は 1 つのみになります。寸法にいくつかのセグメントがあり、その寸法が非レポート パラメータによってラベル付けされている場合、各セグメントの値はこのパラメータによって駆動されます。レポート パラメータでは、複数のセグメントを持つ寸法にラベルを付けることはできません。

寸法が他のグローバル パラメータによって既にラベル付けされている場合、その寸法に再度ラベルを付けると、パラメータのラベル付けが自動的に解除されます。

現在、ラベルを付けることができるのは Linear 寸法(長さ寸法)と Angular 寸法(角度寸法)に限られます。ただし、実際には他にも制限があります。 特定の寸法要素にラベルを付けられるかどうかを調べるには、CanLabelDimension() メソッドを使用します。パラメータの値およびパラメータの値によってラベル付けされる寸法は互いに依存関係にあるため、グローバル パラメータのデータ タイプは Length または Angle のいずれかにする必要があります。寸法の単位が表せるのは長さと角度に限られるためです。

次の例では、グローバル パラメータを作成し、それを使用して特定の寸法要素のセットにラベルを付けます。

コード領域: 寸法にラベル付け

public int DriveSelectedDimensions(Document document, string name, double value, ISet<ElementId> dimset)
{
    if (!GlobalParametersManager.AreGlobalParametersAllowed(document))
        throw new System.InvalidOperationException("Global parameters are not permitted in the given document");

    if (!GlobalParametersManager.IsUniqueName(document, name))
        throw new System.ArgumentException("Global parameter with such name already exists in the document", "name");

    if (value <= 0.0)
        throw new System.ArgumentException("Value of a global parameter that drives dimension must be a positive number", "value");

    int nLabeledDims = 0;   // number of labeled dimensions (for testing)

    // creation of any element must be in a transaction
    using (Transaction trans = new Transaction(document, "Create Global Parameter"))
    {
        trans.Start();

        // create a GP with the given name and type Length
        // Note: Length (or Angle) is required type of global parameters that are to label a dimension
        GlobalParameter newgp = GlobalParameter.Create(document, name, ParameterType.Length);
        if (newgp != null)
        {
            newgp.SetValue(new DoubleParameterValue(value));

            // use the parameter to label the given dimensions
            foreach (ElementId elemid in dimset)
            {
                // not just any dimension is allowed to be labeled
                // check first to avoid exceptions
                if (newgp.CanLabelDimension(elemid))
                {
                    newgp.LabelDimension(elemid);
                    nLabeledDims += 1;
                }
            }

            trans.Commit();
        }
    }

    // for illustration purposes only, we'll test the results of our modifications 

    // 1.) Check the new parameter can be found

    ElementId gpid = GlobalParametersManager.FindByName(document,name);
    if (gpid == ElementId.InvalidElementId)
    {
        TaskDialog.Show("Error", "Failed to find a newly created global parameter");
    }

    GlobalParameter gp = document.GetElement(gpid) as GlobalParameter;

    // 2. Check the number of labeled dimension is as expected

    ISet<ElementId> labeledSet = gp.GetLabeledDimensions();
    if (labeledSet.Count != nLabeledDims)
    {
        TaskDialog.Show("Error", "Have not found all the dimension that were labeled.");
    }

    return labeledSet.Count;
}

SetDrivingDimension() メソッドは次の 2 つの動作を組み合わせます。(1)パラメータが非レポート パラメータである場合はそのパラメータをレポート パラメータに変更する。(2)パラメータを使用して、指定された寸法にラベルを付ける。したがって、グローバル パラメータはレポート パラメータに適している必要があり、かつ 2 つ以上の寸法のラベル付けには使用しないようにする必要があります。レポート パラメータの詳細については、「レポート パラメータと非レポート パラメータ」のページを参照してください。

このパラメータがすでに他の寸法によって駆動されている場合は、目的の寸法でラベル付けを行う前に、他の寸法のラベル付けが解除されます。これは、1 つのレポート パラメータが 1 度にラベルを付けることができるのは 1 つの寸法に限られる(パラメータを駆動できるのは 1 つの寸法に限られる)からです。

次の例では、寸法の値によって駆動されるグローバル パラメータを作成します。

コード領域: 駆動寸法を割り当て

public bool AssignDrivingDimension(Document document, ElementId gpid, ElementId dimid)
{
    // we expect to find the global parameter in the document
    GlobalParameter gp = document.GetElement(gpid) as GlobalParameter;
    if (gp == null)
        return false;

    // we expect to find the given dimension in the document
    Dimension dim = document.GetElement(dimid) as Dimension;
    if (dim == null)
        return false;

    // not every global parameter can label
    // and not every dimension can be labeled
    if (!gp.CanLabelDimension(dimid))
        return false;

    // we need a transaction to modify the model
    using (Transaction trans = new Transaction(document,"Assign a driving dimension"))
    {
        trans.Start();

        // we cannot assign a driving dimension to a global
        // parameter that is already used to label other dimensions
        ISet<ElementId> dimset = gp.GetLabeledDimensions();
        foreach (ElementId elemid in dimset)
        {
            gp.UnlabelDimension(elemid);
        }
                
        // with the GP free of all previously labels (if there were any)
        gp.SetDrivingDimension(dimid);

        // we should be able to commit, but we test the result anyway
        if (trans.Commit() != TransactionStatus.Committed)
            return false;
    }

    return true;
}