Skip to content

Commit

Permalink
✨ 图片自动排列:支持根据位置排列 #15
Browse files Browse the repository at this point in the history
  • Loading branch information
Achuan-2 committed Jan 20, 2025
1 parent fcf2591 commit abf947e
Showing 1 changed file with 100 additions and 23 deletions.
123 changes: 100 additions & 23 deletions Achuan的PPT插件/Ribbon1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
using System.IO;
using System.Collections.Generic; // Add this line



namespace Achuan的PPT插件
{

public partial class Ribbon1
{
PowerPoint.Application app;
Expand Down Expand Up @@ -267,41 +270,74 @@ private void imgAutoAlign_Click(object sender, RibbonControlEventArgs e)

if (!float.TryParse(imgAutoAlign_rowSpace.Text, out rowSpace) || rowSpace < 0)
{
rowSpace = colSpace; // Use column spacing if row spacing is not provided
rowSpace = colSpace;
}

bool useCustomWidth = float.TryParse(imgWidthEditBpx.Text, out imgWidth) && imgWidth > 0;
bool useCustomHeight = float.TryParse(imgHeightEditBox.Text, out imgHeight) && imgHeight > 0;

PowerPoint.Shape firstShape = sel.ShapeRange[1];

// Create a list of shapes to sort
// Create groups based on vertical position
var groups = new List<ImageGroup>();
var shapes = new List<PowerPoint.Shape>();
foreach (PowerPoint.Shape shape in sel.ShapeRange)
{
shapes.Add(shape);
}

// Sort shapes by position if checkbox is checked
if (positionSortCheckBox.Checked)
// Sort shapes by top position first
shapes = shapes.OrderBy(s => s.Top).ToList();

// Group shapes based on vertical overlap
foreach (var shape in shapes)
{
bool addedToExistingGroup = false;
foreach (var group in groups)
{
if (group.OverlapsWith(shape))
{
group.AddShape(shape);
addedToExistingGroup = true;
break;
}
}

if (!addedToExistingGroup)
{
var newGroup = new ImageGroup();
newGroup.AddShape(shape);
groups.Add(newGroup);
}
}

// Sort shapes within each group by x position
foreach (var group in groups)
{
shapes = shapes.OrderBy(s => s.Top) // First sort by Y position (Top)
.ThenBy(s => s.Left) // Then sort by X position (Left)
.ToList();
group.Shapes.Sort((a, b) => a.Left.CompareTo(b.Left));
}

float startX = firstShape.Left;
float startY = firstShape.Top;
// Sort groups by MinTop
groups.Sort((a, b) => a.MinTop.CompareTo(b.MinTop));

// Get the starting position from the first shape of the first group
float startX = groups[0].Shapes[0].Left;
float currentY = groups[0].Shapes[0].Top;
float currentX = startX;
float currentY = startY;
int currentCol = 0;
float rowMaxHeight = 0;
int colCount = 0;

// Arrange shapes in order
foreach (PowerPoint.Shape shape in shapes)
// Flatten all shapes from all groups into a single list for arrangement
var allShapes = new List<PowerPoint.Shape>();
foreach (var group in groups)
{
allShapes.AddRange(group.Shapes);
}

foreach (var shape in allShapes)
{
// Apply size settings if specified
if (!useCustomHeight && !useCustomWidth)
{
shape.Height = firstShape.Height;
shape.Height = allShapes[0].Height;
}
else
{
Expand All @@ -315,18 +351,24 @@ private void imgAutoAlign_Click(object sender, RibbonControlEventArgs e)
}
}

// Position the shape
shape.Left = currentX;
shape.Top = currentY;

currentCol++;
if (currentCol >= colNum)
// Track maximum height in current row
rowMaxHeight = Math.Max(rowMaxHeight, shape.Height);

colCount++;
if (colCount >= colNum)
{
currentCol = 0;
currentX = startX;
currentY += shape.Height + rowSpace;
// Move to next row
colCount = 0;
currentX = startX; // Reset X position to startX
currentY += rowMaxHeight + rowSpace;
rowMaxHeight = 0;
}
else
{
else{

currentX += shape.Width + colSpace;
}
}
Expand Down Expand Up @@ -816,6 +858,41 @@ private PowerPoint.Shape InsertCodeBlock(string code, string language, float lef

return textBox;
}
public class ImageGroup
{
public List<PowerPoint.Shape> Shapes { get; set; } = new List<PowerPoint.Shape>();
public float MinTop { get; set; }
public float MaxBottom { get; set; }

public bool OverlapsWith(PowerPoint.Shape shape)
{
float shapeHeight = shape.Height;
float threshold = shapeHeight * 0.6f; // 80% of shape height
float shapeBottom = shape.Top + shapeHeight;

// Calculate overlap height
float overlapStart = Math.Max(MinTop, shape.Top);
float overlapEnd = Math.Min(MaxBottom, shapeBottom);
float overlapHeight = overlapEnd - overlapStart;

return overlapHeight >= threshold;
}

public void AddShape(PowerPoint.Shape shape)
{
if (Shapes.Count == 0)
{
MinTop = shape.Top;
MaxBottom = shape.Top + shape.Height;
}
else
{
MinTop = Math.Min(MinTop, shape.Top);
MaxBottom = Math.Max(MaxBottom, shape.Top + shape.Height);
}
Shapes.Add(shape);
}
}
private class MarkdownSegment
{
public string Content { get; set; }
Expand Down

0 comments on commit abf947e

Please sign in to comment.