ラベル スタイルを定義する

ラベル スタイルは、ラベルのさまざまなフィーチャ(「コンポーネント」)のコレクションです。これらのコンポーネントのコレクションには、LabelStyle::GetComponents() メソッドを使用してアクセスします。このメソッドは、取得するコンポーネントの型(LabelStyleComponentType)を使用します。次のコンポーネント タイプがあります。

ラベル スタイル タイプによっては、これらすべてが有効であるとは限りません。たとえば、ティック マーク コンポーネントをポイント用のラベル スタイルに追加しても、表示上の効果はまったくありません。また、ラベル スタイルは、グラフィカル オブジェクトが現在のドキュメントの一部であるかどうかに依存します。たとえば、スタイルが現在のドキュメントの一部ではないブロックを参照している場合、指定したブロックまたはティック コンポーネントは表示されません。

ラベル スタイルにフィーチャを追加するには、LabelStyle::AddComponent() メソッドを使用して新規コンポーネントを対応するコレクションに追加します。次に、そのコンポーネントのプロパティを適切な値に設定します。その際、Visible プロパティが True に設定されていることを常に確認してください。

try
{
    // Add a line to the collection of lines in our label style
    ObjectId lineComponentId = oLabelStyle.AddComponent("New Line Component", LabelStyleComponentType.Line);
    // Get the new component:
    ObjectIdCollection lineCompCol = oLabelStyle.GetComponents(LabelStyleComponentType.Line);
    var newLineComponent = ts.GetObject(lineComponentId, OpenMode.ForWrite) as LabelStyleLineComponent;
    // Now we can modify the component
    newLineComponent.General.Visible.Value = true;
    newLineComponent.Line.Color.Value = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByAci, 40); // orange-yellow
    newLineComponent.Line.Angle.Value = 2.094; // radians, = 120 deg
    // negative lengths are allowed - they mean the line is drawn
    // in the opposite direction to the angle specified:
    newLineComponent.Line.Length.Value = -0.015;
    newLineComponent.Line.StartPointXOffset.Value = 0.005;
    newLineComponent.Line.StartPointYOffset.Value = -0.005;
}
// Thrown if component isn't valid, or name is duplicated
catch (System.ArgumentException e)
{
    Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("Error: {0}\n", e.Message);
}

最初に作成されたラベル スタイル オブジェクトは、環境設定に基づいて設定されます。このため、新しいラベル スタイル オブジェクトには既にフィーチャが含まれている場合があります。新しいラベル スタイル オブジェクトを作成する場合、こうしたフィーチャがあるかどうかをチェックする必要があります。チェックしない場合、意図しない要素が含まれてしまうことがあります。

// Check to see whether any text components already exist.  
// If not, add one.  
if (oLabelStyle.GetComponentsCount(LabelStyleComponentType.Text) == 0)
{
    // Add a text component 
    oLabelStyle.AddComponent("New Text ", LabelStyleComponentType.Text);
}
// Now modify the first one:
ObjectIdCollection textCompCol = oLabelStyle.GetComponents(LabelStyleComponentType.Text);
var newTextComponent = ts.GetObject(textCompCol[0], OpenMode.ForWrite) as LabelStyleTextComponent;

また、環境設定では使用する単位も定義されます。作成するアプリケーションで異なる図面を使用する場合、環境設定を考慮する必要があります。考慮しない場合、それぞれのドキュメントでラベルが予期しない動作を示すことがあります。