C++ でプレゼンテーションの図形をカスタマイズ

編集ポイントを使用して図形を変更する

正方形を考えてみましょう。PowerPoint では、編集ポイントを使用して

  • 正方形の角を内側または外側に移動できる
  • 角やポイントの曲率を指定できる
  • 正方形に新しいポイントを追加できる
  • 正方形上のポイントを操作できる、など

基本的に、任意の図形に対して上記の操作を行うことができます。編集ポイントを使用すると、既存の図形から形状を変更したり、新しい図形を作成したりできます。

図形編集のヒント

overview_image

編集ポイントで PowerPoint の図形を編集し始める前に、図形に関して次の点を考慮してください。

  • 図形(またはそのパス)は閉じている場合と開いている場合があります。
  • 図形が閉じている場合、開始点や終了点がありません。開いている場合は、始点と終点があります。
  • すべての図形は少なくとも 2 つのアンカーポイントで構成され、線で結ばれています。
  • 線は直線または曲線のいずれかです。アンカーポイントが線の性質を決定します。
  • アンカーポイントはコーナーポイント、ストレートポイント、スムーズポイントのいずれかです。
    • コーナーポイントは、2 本の直線が角度を持って結合する点です。
    • スムーズポイントは、2 本のハンドルが直線上にあり、線分が滑らかな曲線で結合する点です。この場合、すべてのハンドルはアンカーポイントから同じ距離だけ離れています。
    • ストレートポイントは、2 本のハンドルが直線上にあり、線分が滑らかな曲線で結合する点です。この場合、ハンドルはアンカーポイントから等距離である必要はありません。
  • アンカーポイントを移動または編集すると(線の角度が変わります)、図形の外観を変更できます。

PowerPoint の図形を編集ポイントで操作するには、Aspose.SlidesGeometryPath クラスと IGeometryPath インターフェイスを提供します。

簡単な編集操作

この C++ コードは次の操作方法を示します。

パスの末尾に直線を追加する

void LineTo(PointF point);
void LineTo(float x, float y);

パスの指定位置に直線を追加する

void LineTo(PointF point, uint32_t index);
void LineTo(float x, float y, uint32_t index);

パスの末尾に 3 次ベジェ曲線を追加する

void CubicBezierTo(PointF point1, PointF point2, PointF point3);
void CubicBezierTo(float x1, float y1, float x2, float y2, float x3, float y3);

パスの指定位置に 3 次ベジェ曲線を追加する

void CubicBezierTo(PointF point1, PointF point2, PointF point3, uint32_t index);
void CubicBezierTo(float x1, float y1, float x2, float y2, float x3, float y3, uint32_t index);

パスの末尾に二次ベジェ曲線を追加する

void QuadraticBezierTo(PointF point1, PointF point2);
void QuadraticBezierTo(float x1, float y1, float x2, float y2);

パスの指定位置に二次ベジェ曲線を追加する

void QuadraticBezierTo(PointF point1, PointF point2, uint32_t index);
void QuadraticBezierTo(float x1, float y1, float x2, float y2, uint32_t index);

パスに円弧を付加する

void ArcTo(float width, float heigth, float startAngle, float sweepAngle);

パスの現在の図形を閉じる

void CloseFigure();

次のポイントの位置を設定する

void MoveTo(PointF point);
void MoveTo(float x, float y);

指定インデックスのパスセグメントを削除する

void RemoveAt(int32_t index);

図形にカスタムポイントを追加する

  1. GeometryShape クラスのインスタンスを作成し、ShapeType.Rectangle タイプを設定します。
  2. 図形から GeometryPath クラスのインスタンスを取得します。
  3. パス上の上部 2 点の間に新しいポイントを追加します。
  4. パス上の下部 2 点の間に新しいポイントを追加します。
  5. パスを図形に適用します。

この C++ コードはカスタムポイントを図形に追加する方法を示します:

SharedPtr<Presentation> pres = System::MakeObject<Presentation>();

SharedPtr<IShapeCollection> shapes = pres->get_Slides()->idx_get(0)->get_Shapes();
SharedPtr<GeometryShape> shape = System::ExplicitCast<GeometryShape>(shapes->AddAutoShape(ShapeType::Rectangle, 100.0f, 100.0f, 200.0f, 100.0f));

SharedPtr<IGeometryPath> geometryPath = shape->GetGeometryPaths()->idx_get(0);

geometryPath->LineTo(100.0f, 50.0f, 1);
geometryPath->LineTo(100.0f, 50.0f, 4);
shape->SetGeometryPath(geometryPath);

example1_image

図形からポイントを削除する

  1. GeometryShape クラスのインスタンスを作成し、ShapeType.Heart タイプを設定します。
  2. 図形から GeometryPath クラスのインスタンスを取得します。
  3. パスのセグメントを削除します。
  4. パスを図形に適用します。

この C++ コードは図形からポイントを削除する方法を示します:

SharedPtr<Presentation> pres = System::MakeObject<Presentation>();

SharedPtr<IShapeCollection> shapes = pres->get_Slides()->idx_get(0)->get_Shapes();
SharedPtr<GeometryShape> shape = System::ExplicitCast<GeometryShape>(shapes->AddAutoShape(ShapeType::Heart, 100.0f, 100.0f, 300.0f, 300.0f));

SharedPtr<IGeometryPath> path = shape->GetGeometryPaths()->idx_get(0);
path->RemoveAt(2);
shape->SetGeometryPath(path);

example2_image

カスタム形状を作成する

  1. 図形のポイントを計算します。
  2. GeometryPath クラスのインスタンスを作成します。
  3. パスにポイントを設定します。
  4. GeometryShape クラスのインスタンスを作成します。
  5. パスを図形に適用します。

この C++ コードはカスタム形状を作成する方法を示します:

SharedPtr<List<PointF>> points = System::MakeObject<List<PointF>>();

float R = 100.0f, r = 50.0f;
int32_t step = 72;

for (int32_t angle = -90; angle < 270; angle += step)
{
    double radians = angle * (Math::PI / 180.f);
    double x = outerRadius * Math::Cos(radians);
    double y = outerRadius * Math::Sin(radians);
    points->Add(PointF((float)x + outerRadius, (float)y + outerRadius));

    radians = Math::PI * (angle + step / 2) / 180.0;
    x = innerRadiusr * Math::Cos(radians);
    y = innerRadiusr * Math::Sin(radians);
    points->Add(PointF((float)x + outerRadius, (float)y + outerRadius));
}

SharedPtr<GeometryPath> starPath = System::MakeObject<GeometryPath>();
starPath->MoveTo(points->idx_get(0));

for (int32_t i = 1; i < points->get_Count(); i++)
{
    starPath->LineTo(points->idx_get(i));
}

starPath->CloseFigure();

SharedPtr<Presentation> pres = System::MakeObject<Presentation>();

SharedPtr<IShapeCollection> shapes = pres->get_Slides()->idx_get(0)->get_Shapes();
SharedPtr<GeometryShape> shape = System::ExplicitCast<GeometryShape>(shapes->AddAutoShape(ShapeType::Rectangle, 100.0f, 100.0f, R * 2, R * 2));

shape->SetGeometryPath(starPath);

example3_image

複合カスタム形状を作成する

  1. GeometryShape クラスのインスタンスを作成します。
  2. 最初の GeometryPath インスタンスを作成します。
  3. 2 番目の GeometryPath インスタンスを作成します。
  4. パスを図形に適用します。

この C++ コードは複合カスタム形状を作成する方法を示します:

SharedPtr<Presentation> pres = System::MakeObject<Presentation>();

SharedPtr<IShapeCollection> shapes = pres->get_Slides()->idx_get(0)->get_Shapes();
SharedPtr<GeometryShape> shape = System::ExplicitCast<GeometryShape>(shapes->AddAutoShape(ShapeType::Rectangle, 100.0f, 100.0f, 200.0f, 100.0f));

SharedPtr<IGeometryPath> geometryPath0 = System::MakeObject<GeometryPath>();
geometryPath0->MoveTo(0.0f, 0.0f);
geometryPath0->LineTo(shape->get_Width(), 0.0f);
geometryPath0->LineTo(shape->get_Width(), shape->get_Height() / 3);
geometryPath0->LineTo(0.0f, shape->get_Height() / 3);
geometryPath0->CloseFigure();

SharedPtr<IGeometryPath> geometryPath1 = System::MakeObject<GeometryPath>();
geometryPath1->MoveTo(0.0f, shape->get_Height() / 3 * 2);
geometryPath1->LineTo(shape->get_Width(), shape->get_Height() / 3 * 2);
geometryPath1->LineTo(shape->get_Width(), shape->get_Height());
geometryPath1->LineTo(0.0f, shape->get_Height());
geometryPath1->CloseFigure();

shape->SetGeometryPaths(System::MakeArray<SharedPtr<IGeometryPath>>({ geometryPath0, geometryPath1 }));

example4_image

角が丸いカスタム形状を作成する

この C++ コードは、内側にカーブした角を持つカスタム形状を作成する方法を示します:

float shapeX = 20.f;
float shapeY = 20.f;
float shapeWidth = 300.f;
float shapeHeight = 200.f;

float leftTopSize = 50.f;
float rightTopSize = 20.f;
float rightBottomSize = 40.f;
float leftBottomSize = 10.f;

auto presentation = System::MakeObject<Presentation>();

auto childShape = presentation->get_Slides()->idx_get(0)->get_Shapes()->AddAutoShape(ShapeType::Custom, shapeX, shapeY, shapeWidth, shapeHeight);

auto geometryPath = System::MakeObject<GeometryPath>();

PointF point1(leftTopSize, 0.0f);
PointF point2(shapeWidth - rightTopSize, 0.0f);
PointF point3(shapeWidth, shapeHeight - rightBottomSize);
PointF point4(leftBottomSize, shapeHeight);
PointF point5(0.0f, leftTopSize);

geometryPath->MoveTo(point1);
geometryPath->LineTo(point2);
geometryPath->ArcTo(rightTopSize, rightTopSize, 180.0f, -90.0f);
geometryPath->LineTo(point3);
geometryPath->ArcTo(rightBottomSize, rightBottomSize, -90.0f, -90.0f);
geometryPath->LineTo(point4);
geometryPath->ArcTo(leftBottomSize, leftBottomSize, 0.0f, -90.0f);
geometryPath->LineTo(point5);
geometryPath->ArcTo(leftTopSize, leftTopSize, 90.0f, -90.0f);

geometryPath->CloseFigure();

childShape->SetGeometryPath(geometryPath);

presentation->Save(u"output.pptx", SaveFormat::Pptx);

図形ジオメトリが閉じているか確認する方法

閉じた図形とは、すべての辺がつながり、隙間なく単一の境界を形成しているものを指します。単純な幾何形状でも、複雑なカスタム輪郭でも同様です。次のコード例は、図形ジオメトリが閉じているかどうかを確認する方法を示します:

bool IsGeometryClosed(SharedPtr<IGeometryShape> geometryShape)
{
    bool isClosed = false;

    for (auto&& geometryPath : geometryShape->GetGeometryPaths())
    {
        auto dataLength = geometryPath->get_PathData()->get_Length();
        if (dataLength == 0)
            continue;

        auto lastSegment = geometryPath->get_PathData()[dataLength - 1];
        isClosed = lastSegment->get_PathCommand() == PathCommandType::Close;

        if (!isClosed)
            return false;
    }

    return isClosed;
}

GeometryPath を GraphicsPath に変換する

  1. GeometryShape クラスのインスタンスを作成します。
  2. System.Drawing.Drawing2D 名前空間の GraphicsPath クラスのインスタンスを作成します。
  3. ShapeUtil を使用して、GraphicsPath インスタンスを GeometryPath インスタンスに変換します。
  4. パスを図形に適用します。

上記手順を実装したこの C++ コードは、GeometryPath から GraphicsPath への変換プロセスを示しています:

SharedPtr<Presentation> pres = System::MakeObject<Presentation>();

SharedPtr<IShapeCollection> shapes = pres->get_Slides()->idx_get(0)->get_Shapes();
SharedPtr<GeometryShape> shape = System::ExplicitCast<GeometryShape>(shapes->AddAutoShape(ShapeType::Rectangle, 100.0f, 100.0f, 300.0f, 100.0f));

SharedPtr<IGeometryPath> originalPath = shape->GetGeometryPaths()->idx_get(0);
originalPath->set_FillMode(PathFillModeType::None);

SharedPtr<Drawing2D::GraphicsPath> graphicsPath = System::MakeObject<Drawing2D::GraphicsPath>();
graphicsPath->AddString(u"Text in shape", System::MakeObject<FontFamily>(u"Arial"), 1, 40.0f, PointF(10.0f, 10.0f), StringFormat::get_GenericDefault());

SharedPtr<IGeometryPath> textPath = ShapeUtil::GraphicsPathToGeometryPath(graphicsPath);
textPath->set_FillMode(PathFillModeType::Normal);

shape->SetGeometryPaths(System::MakeArray<SharedPtr<IGeometryPath>>({ originalPath, textPath }));

example5_image

FAQ

ジオメトリを置き換えた後、塗りつぶしと輪郭はどうなりますか?

スタイルは図形に残り、輪郭だけが変更されます。塗りつぶしと輪郭は新しいジオメトリに自動的に適用されます。

ジオメトリとともにカスタム形状を正しく回転させるにはどうすればよいですか?

図形の rotation プロパティを使用します。ジオメトリは図形にバインドされているため、図形と一緒に回転します。

カスタム形状を画像に変換して「ロック」できますか?

はい。必要な slide 領域または shape 自体をラスタ形式でエクスポートできます。これにより、重いジオメトリの後続作業が簡素化されます。