Skip to content

Help with aligning connector endpoint onto center of subshape #1008

@Ozymansour

Description

@Ozymansour

I'm trying to add connectors between shapes, but I'm experiencing issues with their alignment and size.

I have a placeholder shape and several end shapes. Each end_shape is a shape of a country in the map below. The country shapes are part of a group shape called "Map.":
CleanShot 2024-09-22 at 17 51 34@2x

I want the connector's endpoint to be placed in the center of its country. The connector starts from the edge of the table placeholder to the center of country end_shape.

My problem is the entire connector appears too large and its endpoint does not align correctly with the country's center:
CleanShot 2024-09-22 at 17 45 20@2x
code:

def add_connector(slide, end_shape, placeholder_idx):
    placeholder = slide.placeholders[placeholder_idx]
    end_center_x = end_shape.left + (end_shape.width // 2)
    end_center_y = end_shape.top + (end_shape.height // 2)

    connector = slide.shapes.add_connector(
        MSO_CONNECTOR.ELBOW,
        placeholder.left,
        placeholder.top,
        end_center_x,
        end_center_y,
    )
    connector.begin_connect(placeholder, 3)

    line_elem = connector.line._get_or_add_ln()
    line_elem.append(
        parse_xml(
            '<a:tailEnd type="oval" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"/>'
        )
    )
    connector.line.width = Pt(0.5)
    connector.line.color.rgb = RGBColor(32, 64, 140)

    return connector


def find_country_shape(slide, country_name):
    map_shape = next(shape for shape in slide.shapes if shape.name == "Map")
    return next(
        (
            shape
            for shape in map_shape.shapes
            if shape.name.lower() == country_name.lower()
        ),
        None,
    )

Right now i have an temporary calculation solution but the endpoint is off-center and not ideal at all, especially if i were to increase the size of my table placeholder..

def add_connector(slide, end_shape, placeholder_idx):
    placeholder = slide.placeholders[placeholder_idx]
    scale_factor = 0.62
    end_center_x = int(
        placeholder.left
        + scale_factor * (end_shape.left + (end_shape.width // 2) - placeholder.left)
    )
    end_center_y = int(
        placeholder.top
        + scale_factor * (end_shape.top + (end_shape.height // 2) - placeholder.top)
    )

CleanShot 2024-09-22 at 17 50 07@2x

My problem is that in my first code i've calculated the center point of the country and set it as the endpoint of the connector, yet it displays this huge connector no where near the country.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions