Example 3 — Union: Merge Rectangle and Circle
Two overlapping shapes are merged into a single new shape using Union. Demonstrates the core pattern of all Boolean operations: select → operate → retrieve result.
Result
Code
/* PARAMS */
float left = 80f;
float top = 100f;
float size = 150f;
int rgb = System.Drawing.ColorTranslator.ToOle(
System.Drawing.Color.FromArgb(155, 89, 182));
/* END PARAMS */
// Create rectangle and circle (overlapping)
var oRect = ppptools.AddRect(left, top, size, size);
var oCircle = ppptools.AddOval(left + size * 0.6f, top, size, size);
// Union — merge both shapes into one
oRect.Select(MsoTriState.msoTrue);
oCircle.Select(MsoTriState.msoFalse);
ppptools.Union();
// Style the result shape
foreach (dynamic sh in ppptools.GetSelectedRange())
{
sh.Name = "UnionShape";
sh.Fill.ForeColor.RGB = rgb;
sh.Fill.Transparency = 0f;
sh.Line.Visible = MsoTriState.msoFalse;
}
Step-by-Step Explanation
1. Creating Two Overlapping Shapes
var oRect = ppptools.AddRect(left, top, size, size);
var oCircle = ppptools.AddOval(left + size * 0.6f, top, size, size);
The circle starts at left + size * 0.6f, i.e., 60% of the size to the right of the rectangle's left edge. This makes it overlap the rectangle by 40% — enough for a clear merge.
2. Building the Selection — the Core of All Boolean Operations
oRect.Select(MsoTriState.msoTrue); // First shape: replaces current selection
oCircle.Select(MsoTriState.msoFalse); // Second shape: adds to current selection
| Parameter | Meaning |
|---|---|
msoTrue |
Replace — clears the current selection first |
msoFalse |
Add — appends to the existing selection |
This selection method is the only reliable way in Roslyn scripts to mark multiple shapes for a Boolean operation. List<Shape> doesn't work due to the namespace conflict.
3. Executing the Boolean Operation
ppptools.Union();
ppptools.Union() corresponds to PowerPoint's "Merge Shapes → Union" command. The selected shapes are merged into one single shape. The original shapes are deleted in the process.
| ppptools Method | Result |
|---|---|
ppptools.Union() |
All selected shapes merge into one |
ppptools.Intersect() |
Only the overlapping area remains |
ppptools.Combine() |
Union, but the overlap is punched out (hole) |
ppptools.Subtract() |
Second shape is subtracted from the first |
4. Retrieving and Styling the Result
foreach (dynamic sh in ppptools.GetSelectedRange())
{
sh.Name = "UnionShape";
sh.Fill.ForeColor.RGB = rgb;
sh.Fill.Transparency = 0f;
sh.Line.Visible = MsoTriState.msoFalse;
}
After ppptools.Union(), the resulting shape is automatically selected. ppptools.GetSelectedRange() returns the ShapeRange object. Since Union always produces exactly one shape, the foreach only loops once — but it's still more robust than ppptools.GetSelected() for code that should also work with Intersect (which can produce multiple parts).
What's Special About This Example
- Selection order —
msoTruefor the first shape, thenmsoFalsefor each additional one. Wrong order leads to wrong results. - Originals are deleted — after
ppptools.Union(),oRectandoCircleno longer exist. Don't use them again! foreachinstead of[1]— with Union the result is always one shape;foreachis still more idiomatic because it also works withIntersectwhich may produce multiple parts.int rgb— stored asint(OLE integer) since it's reused in theforeachloop. More concise than callingToOle()each time.
Back: Example 2 — Circle · Next: Example 4 — Hole (Combine)