Personnaliser les formes de présentation en C++

Modifier une forme à l’aide des points d’édition

Considérez un carré. Dans PowerPoint, en utilisant points d’édition, vous pouvez

  • déplacer le coin du carré vers l’intérieur ou l’extérieur
  • spécifier la courbure d’un coin ou d’un point
  • ajouter de nouveaux points au carré
  • manipuler les points du carré, etc.

Essentiellement, vous pouvez effectuer ces tâches sur n’importe quelle forme. Avec les points d’édition, vous pouvez modifier une forme ou créer une nouvelle forme à partir d’une forme existante.

Conseils pour l’édition de formes

overview_image

Avant de commencer à modifier les formes PowerPoint à l’aide des points d’édition, vous voudrez peut‑être prendre en compte les points suivants concernant les formes :

  • Une forme (ou son tracé) peut être fermée ou ouverte.
  • Lorsqu’une forme est fermée, elle n’a pas de point de départ ou d’arrivée. Lorsqu’une forme est ouverte, elle possède un début et une fin.
  • Toutes les formes comportent au moins 2 points d’ancrage reliés entre eux par des lignes
  • Une ligne est soit droite, soit courbe. Les points d’ancrage déterminent la nature de la ligne.
  • Les points d’ancrage existent sous forme de points d’angle, de points droits ou de points lisses :
    • Un point d’angle est un point où 2 lignes droites se rejoignent sous un angle.
    • Un point lisse est un point où 2 poignées existent sur une ligne droite et les segments de la ligne se rejoignent en une courbe fluide. Dans ce cas, toutes les poignées sont séparées du point d’ancrage par une distance égale.
    • Un point droit est un point où 2 poignées existent sur une ligne droite et les segments de cette ligne se rejoignent en une courbe fluide. Dans ce cas, les poignées n’ont pas besoin d’être séparées du point d’ancrage par une distance égale.
  • En déplaçant ou en modifiant les points d’ancrage (ce qui change l’angle des lignes), vous pouvez modifier l’apparence d’une forme.

Pour modifier les formes PowerPoint via les points d’édition, Aspose.Slides fournit la classe GeometryPath et l’interface IGeometryPath.

Opérations d’édition simples

Ce code C++ vous montre comment

Ajouter une ligne à la fin d’un chemin

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

Ajouter une ligne à une position spécifiée sur un chemin :

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

Ajouter une courbe de Bézier cubique à la fin d’un chemin :

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

Ajouter une courbe de Bézier cubique à la position spécifiée sur un chemin :

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);

Ajouter une courbe de Bézier quadratique à la fin d’un chemin :

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

Ajouter une courbe de Bézier quadratique à la position spécifiée sur un chemin :

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

Ajouter un arc donné à un chemin :

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

Fermer la figure courante d’un chemin :

void CloseFigure();

Définir la position du point suivant :

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

Supprimer le segment de chemin à un indice donné :

void RemoveAt(int32_t index);

Ajouter des points personnalisés à une forme

  1. Créez une instance de la classe GeometryShape et définissez le type ShapeType.Rectangle.
  2. Obtenez une instance de la classe GeometryPath à partir de la forme.
  3. Ajoutez un nouveau point entre les deux points supérieurs du chemin.
  4. Ajoutez un nouveau point entre les deux points inférieurs du chemin.
  5. Appliquez le chemin à la forme.

Ce code C++ vous montre comment ajouter des points personnalisés à une forme :

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

Supprimer des points d’une forme

  1. Créez une instance de la classe GeometryShape et définissez le type ShapeType.Heart.
  2. Obtenez une instance de la classe GeometryPath à partir de la forme.
  3. Supprimez le segment du chemin.
  4. Appliquez le chemin à la forme.

Ce code C++ vous montre comment supprimer des points d’une forme :

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

Créer une forme personnalisée

  1. Calculez les points de la forme.
  2. Créez une instance de la classe GeometryPath.
  3. Remplissez le chemin avec les points.
  4. Créez une instance de la classe GeometryShape.
  5. Appliquez le chemin à la forme.

Ce code C++ vous montre comment créer une forme personnalisée :

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

Créer une forme personnalisée composite

  1. Créez une instance de la classe GeometryShape.
  2. Créez une première instance de la classe GeometryPath.
  3. Créez une deuxième instance de la classe GeometryPath.
  4. Appliquez les chemins à la forme.

Ce code C++ vous montre comment créer une forme personnalisée composite :

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

Créer une forme personnalisée avec coins arrondis

Ce code C++ vous montre comment créer une forme personnalisée avec des coins arrondis (vers l’intérieur) ;

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);

Déterminer si la géométrie d’une forme est fermée

Une forme fermée est définie comme une forme dont tous les côtés sont connectés, formant une seule frontière sans lacunes. Une telle forme peut être une forme géométrique simple ou un contour personnalisé complexe. L’exemple de code suivant montre comment vérifier si la géométrie d’une forme est fermée :

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;
}

Convertir GeometryPath en GraphicsPath

  1. Créez une instance de la classe GeometryShape.
  2. Créez une instance de la classe GraphicsPath du namespace System.Drawing.Drawing2D.
  3. Convertissez l’instance GraphicsPath en instance GeometryPath en utilisant ShapeUtil.
  4. Appliquez les chemins à la forme.

Ce code C++—une implémentation des étapes ci‑dessus—dé montre le processus de conversion de GeometryPath en 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

Que se passe-t-il du remplissage et du contour après le remplacement de la géométrie ?

Le style reste associé à la forme ; seul le contour change. Le remplissage et le contour sont appliqués automatiquement à la nouvelle géométrie.

Comment faire pivoter correctement une forme personnalisée avec sa géométrie ?

Utilisez la propriété rotation de la forme ; la géométrie pivote avec la forme car elle est liée au système de coordonnées de la forme.

Puis‑je convertir une forme personnalisée en image pour « verrouiller » le résultat ?

Oui. Exportez la zone de la diapositive requise ou la forme elle‑même vers un format raster ; cela simplifie le travail ultérieur avec des géométries lourdes.