How to Connect Shapes with Connectors in C++

How to Connect Shapes with Connectors in C++

Connectors in Aspose.Slides FOSS are line shapes that attach to connection sites on other shapes. When you move a connected shape, the connector endpoint moves with it. The most common connector type is BENT_CONNECTOR3, which routes around obstacles with a single elbow bend.


Prerequisites

git clone https://github.com/aspose-slides-foss/Aspose.Slides-FOSS-for-Cpp.git
cd Aspose.Slides-FOSS-for-Cpp && mkdir build && cd build
cmake .. && cmake --build .

Connection Site Indexes

Every shape has four numbered connection sites:

IndexPosition
0Top center
1Left center
2Bottom center
3Right center

Connect Two Shapes

#include <Aspose/Slides/Foss/presentation.h>

int main() {
    namespace asf = Aspose::Slides::Foss;

    asf::Presentation prs;
    auto& slide = prs.slides()[0];

    // Add two rectangles
    auto& box1 = slide.shapes().add_auto_shape(
        asf::ShapeType::RECTANGLE, 50, 200, 200, 100);
    auto& box2 = slide.shapes().add_auto_shape(
        asf::ShapeType::RECTANGLE, 450, 200, 200, 100);

    box1.add_text_frame("Start");
    box2.add_text_frame("End");

    // Add a bent connector (initial bounds are overwritten by the connection)
    auto& conn = slide.shapes().add_connector(
        asf::ShapeType::BENT_CONNECTOR3, 0, 0, 10, 10);

    // Connect right side of box1 (site 3) to left side of box2 (site 1)
    conn.set_start_shape_connected_to(box1);
    conn.set_start_shape_connection_site_index(3);
    conn.set_end_shape_connected_to(box2);
    conn.set_end_shape_connection_site_index(1);

    prs.save("connected.pptx", asf::SaveFormat::PPTX);
    return 0;
}

The placeholder bounds (0, 0, 10, 10) passed to add_connector are ignored once the connection endpoints are set; PowerPoint re-routes the connector to the attached shapes.


Connector Types

namespace asf = Aspose::Slides::Foss;

// Straight line
asf::ShapeType::STRAIGHT_CONNECTOR1;

// Single elbow (L-shape)
asf::ShapeType::BENT_CONNECTOR2;

// Double elbow (Z-shape): most common
asf::ShapeType::BENT_CONNECTOR3;

// Curved connector
asf::ShapeType::CURVED_CONNECTOR3;

Style the Connector Line

#include <Aspose/Slides/Foss/presentation.h>

int main() {
    namespace asf = Aspose::Slides::Foss;

    asf::Presentation prs;
    auto& slide = prs.slides()[0];

    auto& box1 = slide.shapes().add_auto_shape(
        asf::ShapeType::RECTANGLE, 50, 150, 180, 80);
    auto& box2 = slide.shapes().add_auto_shape(
        asf::ShapeType::RECTANGLE, 500, 300, 180, 80);

    auto& conn = slide.shapes().add_connector(
        asf::ShapeType::BENT_CONNECTOR3, 0, 0, 10, 10);
    conn.set_start_shape_connected_to(box1);
    conn.set_start_shape_connection_site_index(2);   // bottom of box1
    conn.set_end_shape_connected_to(box2);
    conn.set_end_shape_connection_site_index(0);     // top of box2

    // Style: dashed blue line, 2 pt width
    auto& lf = conn.line_format();
    lf.set_width(2.0);
    lf.fill_format().solid_fill_color().set_color(asf::Color::blue());
    lf.set_dash_style(asf::LineDashStyle::DASH);

    prs.save("styled-connector.pptx", asf::SaveFormat::PPTX);
    return 0;
}

Flowchart with Multiple Connectors

#include <Aspose/Slides/Foss/presentation.h>

int main() {
    namespace asf = Aspose::Slides::Foss;

    asf::Presentation prs;
    auto& slide = prs.slides()[0];

    // Three-step flowchart
    auto& step1 = slide.shapes().add_auto_shape(
        asf::ShapeType::RECTANGLE, 350, 50, 200, 70);
    auto& step2 = slide.shapes().add_auto_shape(
        asf::ShapeType::RECTANGLE, 350, 220, 200, 70);
    auto& step3 = slide.shapes().add_auto_shape(
        asf::ShapeType::RECTANGLE, 350, 390, 200, 70);

    step1.add_text_frame("Step 1");
    step2.add_text_frame("Step 2");
    step3.add_text_frame("Step 3");

    auto connect_vertical = [&](auto& top_shape, auto& bottom_shape) {
        auto& c = slide.shapes().add_connector(
            asf::ShapeType::BENT_CONNECTOR3, 0, 0, 10, 10);
        c.set_start_shape_connected_to(top_shape);
        c.set_start_shape_connection_site_index(2);   // bottom
        c.set_end_shape_connected_to(bottom_shape);
        c.set_end_shape_connection_site_index(0);     // top
    };

    connect_vertical(step1, step2);
    connect_vertical(step2, step3);

    prs.save("flowchart.pptx", asf::SaveFormat::PPTX);
    return 0;
}

Read Connector Properties

#include <Aspose/Slides/Foss/presentation.h>
#include <iostream>

int main() {
    namespace asf = Aspose::Slides::Foss;

    asf::Presentation prs("connected.pptx");
    for (size_t i = 0; i < prs.slides()[0].shapes().size(); ++i) {
        auto& shape = prs.slides()[0].shapes()[i];
        if (shape.is_connector()) {
            auto& connector = static_cast<asf::Connector&>(shape);
            std::cout << "Connector found\n";
        }
    }
    return 0;
}

See Also