メインコンテンツに移動

図面内のすべての図形を列挙する

図面内のすべての図形を列挙して、図形の情報を取得するサンプルコードです。

C#
// ドキュメントを取得
Document doc = ActiveDocument;
 
// 図面内の部分図数を列挙
foreach (Drawing drawing in doc.Drawings)
{
    // 部分図内の図形を列挙する
    Shape shape = null;
    while ((shape = drawing.Shapes.GetNextShape(shape)) != null)
    {
        // 図形のレイヤ情報を取得
        Layer layer = shape.Layer;
        string layerName = layer.Name;
 
        // 図形の情報を取得
        if (shape is BasicShape)
        {
            int colorNumber = ((BasicShape)shape).Color.Number;
            string lineTypeName = ((BasicShape)shape).Linetype.Name;
            double lineWidth = ((BasicShape)shape).Linewidth.Width;
        }
        else if (shape is TextShape)
        {
            int colorNumber = ((TextShape)shape).Color.Number;
            string text = ((TextShape)shape).Text;
            string fontName = ((TextShape)shape).FontName;
        }
        else if (shape is LeadShape)
        {
            int colorNumber = ((LeadShape)shape).Color.Number;
            string text = ((LeadShape)shape).Text;
        }
        else if (shape is BalloonShape)
        {
            int colorNumber = ((BalloonShape)shape).Color.Number;
            string text = ((BalloonShape)shape).Text;
            BalloonType balloonType = ((BalloonShape)shape).BalloonType;
        }
        else if (shape is DimensionShape)
        {
            int colorNumber = ((DimensionShape)shape).Color.Number;
            string text = ((DimensionShape)shape).DimensionText;
        }
 
    }