How To Do


How do I calculate the area, perimeter, and holes of a closed DXF shape using MathHelper.FindClosedAreaData?


FindClosedAreaData — Documentation

FindClosedAreaData

MathHelper — DXFReaderNETWinForms

Overview

FindClosedAreaData is a static method on MathHelper that analyzes a set of DXF entities and computes geometric data for a closed area — the outer boundary plus any inner holes (internal contours).

public static bool FindClosedAreaData(
  List<EntityObject> Entities,
  out double ExternalLenght,
  out double ExternalArea,
  out double InternalLenght,
  out double InternalArea,
  out int InternalCountoursNumber)

Purpose

Given a list of drawing entities (lines, arcs, polylines, circles, ellipses, splines, etc.), the method:

  1. Finds all closed loops formed by those entities.
  2. Identifies the outer contour (external boundary).
  3. Treats the remaining loops as internal contours (holes).
  4. Returns perimeter and area for both external and internal parts.

Typical use: calculating material area, filled area, or cut length from a DXF shape with holes.

Parameters

ParameterMeaning
Entities Input list of DXF entities to analyze
ExternalLenght Perimeter of the outer boundary
ExternalArea Area enclosed by the outer boundary
InternalLenght Total perimeter of all internal contours (holes)
InternalArea Total area of all internal contours
InternalCountoursNumber Number of internal contours (holes)

Return value: true if a valid closed area was found; false otherwise.

Step-by-step logic

1. Initialization

All output values are set to 0. Tolerance (Epsilon) is set from the drawing's linear unit precision (LUprec).

2. Find closed loops

Calls FindLoops(Entities), which:

  • Ignores zero-length and disconnected entities
  • Separates already-closed entities (circles, closed polylines, etc.) from open ones
  • Connects open entities into closed loops where possible

If no loops are found, the method returns false.

3. Normalize loops

Each loop is converted into a uniform representation in myEnts:

  • Single-entity loop → clone of that entity
  • Multi-entity loop → merged into one closed LwPolyline

4. Identify the external contour

Calls ExternalContour(myEnts) to pick the outermost closed boundary (the largest enclosing contour).

Then ClosedLoop(externalContour) verifies that boundary is actually closed. If not, returns false.

5. Compute external metrics

Area and perimeter are calculated from the external contour, depending on entity type:

  • CircleCircle.Area, Circle.Lenght
  • EllipseEllipse.Area, Ellipse.Lenght
  • LwPolyline / Polyline → direct area and length
  • Spline → converted to polyline first, then measured
  • Multiple entities → combined into one LwPolyline, then measured

6. Compute internal metrics

Every entity in myEnts that is not part of the external contour is treated as an internal contour (hole). The same area/length logic is applied, and values are summed into InternalArea and InternalLenght.

7. Finalize

  • InternalCountoursNumber = loops.Count - 1 (total loops minus the one external contour)
  • Drawing precision settings are restored
  • Returns true

On any exception, it returns false.

Practical example

bool ret = MathHelper.FindClosedAreaData(
    dxfReaderNETControl1.DXF.Entities,
    out ExternalLenght,
    out ExternalArea,
    out InternalLenght,
    out InternalArea,
    out InternalCountoursNumber);

if (ret)
{
    // External perimeter and area
    // Internal (holes) perimeter and area
    // Filled area = ExternalArea - InternalArea
    // Number of holes = InternalCountoursNumber
}

Filled area (net usable area) is typically:

ExternalArea - InternalArea

Supported entity types

The method handles:

  • Circles
  • Ellipses
  • Lightweight polylines (LwPolyline)
  • Polylines
  • Splines (approximated as polylines)

Other entity types in internal contours may be converted via LwPolyline in the default branch.

Important notes

  1. Single connected shape expected — The method assumes entities form one outer area with optional holes. Multiple separate closed shapes may not behave as intended.
  2. Precision mattersLUprec controls tolerance when detecting connections and closures.
  3. Naming — Parameter names use Lenght and Countours (typos in the API).
  4. Side effects — The method temporarily changes Epsilon and LUprec, then restores them. It also sets DXF.Modified = false before returning success.


Warning!
All samples, data, places and images in this section are only for tutorial. They are fictitious and may not correspond to real cases.

Any similarities to actual persons or places is merely coincidental.