JFLib

diagram_core/src/shapeops.rs#

Source: lib/diagram_core/src/shapeops.rs

shapeops -- (JD21) Visio's shape operations (SP-08: Vereinigen, Kombinieren, Fragmentieren,
Schneiden, Subtrahieren), a frame that follows its geometry (SP-05) and Ausrichtungsfeld
aktualisieren (SP-16)

  Operation::{Union, Combine, Fragment, Intersect, Subtract}
  available(doc, shapes, op) -> bool       whether the operation can run on the selection
  operate(tx, eng, shapes, op) -> Vec<ShapeRef>   the operation; shapes[0] is the primary
                                           shape (the first one selected): the result(s), empty
                                           when nothing is left (the inputs are gone then too)
  fit_frame(tx, eng, shape) -> bool        Width, Height, Pin and the rows to the geometry's box,
                                           the shape stays where it is on the page (frame.rs)
  update_alignment_box(tx, eng, shape) -> bool    (SP-16) a group's box around its members, a
                                           shape's around its geometry (frame.rs)
  geometry_bounds(doc, shape) -> Option<[f64; 4]>  the box of a shape's geometry in its own
                                           coordinates: arcs by their extremes (frame.rs)

How an operation works:
* Each input shape becomes an operand in page coordinates: its Geometry sections (hidden ones,
  NoShow, and InfiniteLine rows left out) through the shape's transform (turns, flips, groups
  around it).  Lines stay exact; arcs (ArcTo, EllipticalArcTo, RelEllipticalArcTo, Ellipse) are
  flattened into short chords that remember their arc (geometry::Conic); other curves (NURBS,
  splines, Bézier rows) into chords that remember their row.  Closed sub-paths are the area,
  filled even-odd as Visio fills a shape's sections (overlapping sections make holes); open
  sub-paths are lines.  A group is the union of its members (and of its own geometry).
* The Boolean operations run in geom::region (exact decisions on a grid of 2^30 steps across
  the inputs; jflib-geom, docs/GEOM.md "Ebene Regionen"); every edge of the result names the
  input edge it lies on, so the result is rebuilt row by row: a run of edges on one arc is
  EllipticalArcTo again (pieces of at most half a turn, the ends projected onto the arc), a
  whole ellipse an Ellipse row, a run on one line one LineTo, other curves LineTo polylines.
  Kombinieren needs no Boolean operation: the sections are taken over as they are, each one a
  section of the result.
* The result is a new shape without master: Angle 0, its box around the geometry (arcs by their
  extremes), PinX/PinY in its middle, LocPin "Width*0.5"/"Height*0.5", every geometry cell as
  "Width*k" / "Height*k" (the control points of arcs too, and D of an upright arc as
  "Width/Height*k") so that the result stretches with its box; one Geometry section per contour
  (Visio fills them even-odd: holes stay holes).  It goes into the primary shape's parent on top
  of its siblings.  The inputs are deleted (glue to them let go, container memberships
  forgotten); with an empty result nothing new comes.

Visio's rules (the sources: Visio 5 User Guide ch. 24 pp. 210-215, the VBA reference of
Selection.Union / Combine / Fragment / Intersect / Subtract / UpdateAlignmentBox, "Merged
Shapes" and "Alignment Boxes" of the Visio 2002 developer reference, Visio 2003 Inside Out ch. 22;
details in the JD21 notes):
* belegt: format (line, fill) and text come from the primary shape (the first one selected);
  Fragmentieren gives every piece the primary's format and no text; the result is the topmost
  shape of its parent; the inputs are deleted; the result has a ShapeSheet of its own (formulas
  of the inputs are gone); Vereinigen leaves open paths out; Schneiden keeps what all inputs
  cover, of disjoint shapes nothing (and the inputs are gone); Subtrahieren takes the area of
  the others away from the primary shape, the others are deleted even where they do not
  overlap; Kombinieren keeps each input's sections, overlaps become holes; Fragmentieren makes a
  shape of every region, and lines (1-D shapes, open paths) cut regions.
* angenommen: the result is a plain shape without master (the primary's format is copied with
  its inherited cells, styles stay referenced); shape data, user cells, hyperlinks and the
  protection cells of the inputs are not taken over; LayerMember and the text block cells are;
  a closed path of a NoFill section is area too; a shape without area does not take part in
  Vereinigen, Schneiden and Subtrahieren and is left alone; a group takes part as the union of
  its members; after an operation the result(s) are selected (the app does that); Vereinigen and
  Fragmentieren also run on one shape (its own sections merged or cut), Kombinieren, Schneiden
  and Subtrahieren need two; locks (LockDelete ...) do not protect the inputs; the shapes are
  given in selection order, the primary first, and Subtrahieren takes the others away together.
  • enum Operation the shape operations (Entwicklertools › Shape-Design › Vorgänge)

    • Union -- Vereinigen (Union): the outline of all of them
    • Combine -- Kombinieren (Combine): all sections in one shape, overlaps become holes
    • Fragment -- Fragmentieren (Fragment): a shape for every region the outlines and lines make
    • Intersect -- Schneiden (Intersect): what all of them cover
    • Subtract -- Subtrahieren (Subtract): the primary shape without the others
  • fn available(doc: &Document, shapes: &[ShapeRef], op: Operation) -> bool whether op can run on these shapes (shapes[0] the primary one): Vereinigen and Fragmentieren need one shape with geometry (Vereinigen: with an area), Kombinieren two with geometry, Schneiden two with an area, Subtrahieren a primary shape with an area and another one with an area

  • fn operate(tx: &mut Transaction, eng: &mut Engine, shapes: &[ShapeRef], op: Operation) -> Result<Vec<ShapeRef>, EditError> Run a shape operation on shapes of one page or master, shapes[0] the primary shape (the first one selected): its format and text go to the result (Fragmentieren: the format only), the result goes into its parent on top. The shapes that take part are deleted (Vereinigen, Schneiden, Subtrahieren: those with an area; Kombinieren, Fragmentieren: all). -> the new shapes: one, a piece each for Fragmentieren, none when nothing is left. Invalid when the operation cannot run on these shapes (available)

Source: docs/API.md in the JFLib repository.