Home /Modules/autocad/SLCadEntity.sleela

SLCadEntity.sleela

lib/autocad/SLCadEntity.sleela · 230 line(s) · sleela

<< lib/autocad/SLCadEntity.sleela
   SLeeLa Standard Library Definition.
   Definition: Defines SLCadEntity -- a single, individually addressable 2D
     drawing entity in the editable AutoCAD model. Where SLCadModel accumulates
     geometry append-only into a DXF string (good for one-shot rendering), an
     SLCadEntity is a mutable record the AutoCAD Creator(TM) can select, move,
     re-layer, re-label, and delete after it has been created. Each entity has a
     stable id, a kind (line / text / dim), a layer, two points (p1,p2 -- the
     second is unused for a point-anchored TEXT), a text value (for TEXT and the
     measured label of a DIM) and a text height, plus an "alive" flag so a
     delete is a soft tombstone that keeps ids stable. This is the atom the
     editable SLCadDocument stores and the SLCadCanvas paints.
   
   Family: autocad. SLeeLa owns the geometry and every edit to it; a front end
   only issues create/alter/move/delete intents. >>
#sleela 2.5
material;
import math;
class SLCadEntity {
  < ---- Descriptive metadata (version 1.11 nested-friendly accessors) ---- >
  << Eleven catalogued attributes with getter/setter pairs, so a GUI, loader,
     or catalog can read and (where appropriate) override this record.
     Values are faithful to the subject's real origin where publicly known. >>
  String metaVersion = "1.0.0";
  int metaId = 11039;
  String metaAuthor = "Kiro Agent";
  String metaDate = "2026-10-16";
  String metaCopyright = "SLeeLa project";
  String metaTrademark = "none";
  String metaCorporation = "none";
  < metaOrigin: National | <nation> | International | Galactic >
  String metaOrigin = "International";
  String metaBirthDate = "2026-10-13";
  < metaScienceRating: maturity/robustness on a 1..10 scale >
  int metaScienceRating = 9;
  < metaNationalSchedule: a real, citable instrument or "none" >
  String metaNationalSchedule = "none";

  String getMetaVersion() { return metaVersion; }
  void setMetaVersion(String v) { metaVersion = v; }
  int getMetaId() { return metaId; }
  void setMetaId(int v) { metaId = v; }
  String getAuthor() { return metaAuthor; }
  void setAuthor(String v) { metaAuthor = v; }
  String getDate() { return metaDate; }
  void setDate(String v) { metaDate = v; }
  String getCopyright() { return metaCopyright; }
  void setCopyright(String v) { metaCopyright = v; }
  String getTrademark() { return metaTrademark; }
  void setTrademark(String v) { metaTrademark = v; }
  String getCorporation() { return metaCorporation; }
  void setCorporation(String v) { metaCorporation = v; }
  String getOrigin() { return metaOrigin; }
  void setOrigin(String v) { metaOrigin = v; }
  String getBirthDate() { return metaBirthDate; }
  void setBirthDate(String v) { metaBirthDate = v; }
  int getScienceRating() { return metaScienceRating; }
  void setScienceRating(int v) { metaScienceRating = v; }
  String getNationalSchedule() { return metaNationalSchedule; }
  void setNationalSchedule(String v) { metaNationalSchedule = v; }
  < Entity-kind ordinals. A front end and the renderer agree on these. >
  < a straight segment (walls/dims geometry) >
  static final int KIND_LINE = 0;
  < a point-anchored text label (notes/title) >
  static final int KIND_TEXT = 1;
  < a linear dimension: segment + measured label >
  static final int KIND_DIM  = 2;

  < stable identity, assigned by the document >
  int id;
  < KIND_LINE / KIND_TEXT / KIND_DIM >
  int kind;
  < "walls" / "dims" / "notes" >
  String layer;
  < first point (anchor for TEXT) >
  double x1; double y1;
  < second point (ignored for TEXT) >
  double x2; double y2;
  < text payload (TEXT content; "" for a bare LINE) >
  String value;
  < text height (for TEXT / DIM label) >
  double height;
  < 1 = present, 0 = tombstoned (deleted but id reserved) >
  int alive;

  < ---- construction ------------------------------------------------------- >

  < Configure as a straight segment on `lyr` from (ax,ay) to (bx,by). >
  void asLine(int entityId, String lyr, double ax, double ay, double bx, double by) {
    id = entityId; kind = KIND_LINE; layer = lyr;
    x1 = ax; y1 = ay; x2 = bx; y2 = by;
    value = ""; height = 0.0; alive = 1;
  }

  < Configure as a text label anchored at (ax,ay) with content `text`. >
  void asText(int entityId, String lyr, double ax, double ay, String text, double h) {
    id = entityId; kind = KIND_TEXT; layer = lyr;
    x1 = ax; y1 = ay; x2 = ax; y2 = ay;
    value = text; height = h; alive = 1;
  }

  < Configure as a linear dimension between (ax,ay) and (bx,by); the measured >
  < label is derived on demand (see measured()). >
  void asDim(int entityId, String lyr, double ax, double ay, double bx, double by, double h) {
    id = entityId; kind = KIND_DIM; layer = lyr;
    x1 = ax; y1 = ay; x2 = bx; y2 = by;
    value = ""; height = h; alive = 1;
  }

  < ---- queries ------------------------------------------------------------ >

  int getId() { return id; }
  int getKind() { return kind; }
  String getLayer() { return layer; }
  boolean isLine() { return kind == KIND_LINE; }
  boolean isText() { return kind == KIND_TEXT; }
  boolean isDim()  { return kind == KIND_DIM; }
  boolean isAlive() { return alive == 1; }

  double getX1() { return x1; }
  double getY1() { return y1; }
  double getX2() { return x2; }
  double getY2() { return y2; }
  String getValue() { return value; }
  double getHeight() { return height; }

  < The measured length of a dimension (and the natural label for a DIM). >
  double length() { return math.hypot(x2 - x1, y2 - y1); }

  < The label text for this entity: its value, or the measured length for a >
  < DIM with no explicit override. >
  String label() {
    < NB: in the SLeeLa VM a String compares `== null` as true regardless of >
    < content, so emptiness is tested with `== ""` (which also covers unset). >
    if (kind == KIND_DIM && value == "") { return num(length()); }
    return value;
  }

  < The midpoint, where a DIM measurement/label sits. >
  double midX() { return (x1 + x2) / 2.0; }
  double midY() { return (y1 + y2) / 2.0; }

  < ---- inspection --------------------------------------------------------- >

  < The entity kind as a word, for reports and the GUI readout. >
  String kindName() {
    if (kind == KIND_TEXT) { return "text"; }
    if (kind == KIND_DIM)  { return "dim"; }
    return "line";
  }

  < A one-line, human-readable summary of this entity's current state, e.g. >
  < "#7 line walls (0,0)->(6000,0)"            (a wall) >
  < "#9 text notes (3000,2000) \"Lobby\""      (a label) >
  < "#4 dim dims (0,-600)->(10000,-600) =10000" (a dimension + measure) >
  < A tombstoned entity is flagged so an audit sees deletions too. >
  String describe() {
    String s = "#" + id + " " + kindName() + " " + layer;
    if (kind == KIND_TEXT) {
      s = s + " (" + num(x1) + "," + num(y1) + ") \"" + value + "\"";
    } else {
      s = s + " (" + num(x1) + "," + num(y1) + ")->(" + num(x2) + "," + num(y2) + ")";
      if (kind == KIND_DIM) { s = s + " =" + num(length()); }
    }
    if (alive == 0) { s = s + " [deleted]"; }
    return s;
  }

  < ---- alteration (the editable half) ------------------------------------- >

  < Move the whole entity by (dx,dy). >
  void translate(double dx, double dy) {
    x1 = x1 + dx; y1 = y1 + dy;
    x2 = x2 + dx; y2 = y2 + dy;
  }

  < Set the first point (and, for TEXT, keep the second coincident). >
  void setP1(double ax, double ay) {
    x1 = ax; y1 = ay;
    if (kind == KIND_TEXT) { x2 = ax; y2 = ay; }
  }
  < Set the second point (ignored for TEXT, which is point-anchored). >
  void setP2(double bx, double by) {
    if (kind == KIND_TEXT) { return; }
    x2 = bx; y2 = by;
  }
  < Re-point both ends at once. >
  void setEndpoints(double ax, double ay, double bx, double by) {
    setP1(ax, ay); setP2(bx, by);
  }

  void setLayer(String lyr) { layer = lyr; }
  void setValue(String text) { value = text; }
  void setHeight(double h) { height = h; }

  < Soft-delete: keep the id reserved so other entities keep their identity. >
  void kill() { alive = 0; }
  void revive() { alive = 1; }

  < ---- DXF ---------------------------------------------------------------- >

  < Emit just this entity's DXF group-code/value records (empty when dead). >
  String toDxf() {
    if (alive == 0) { return ""; }
    if (kind == KIND_TEXT) { return textRecord(layer, x1, y1, height, label()); }
    if (kind == KIND_DIM) {
      String s = lineRecord(layer, x1, y1, x2, y2);
      s = s + textRecord(layer, midX(), midY(), height, label());
      return s;
    }
    return lineRecord(layer, x1, y1, x2, y2);
  }

  String lineRecord(String lyr, double ax, double ay, double bx, double by) {
    String s = "0\nLINE\n8\n" + lyr + "\n";
    s = s + "10\n" + num(ax) + "\n20\n" + num(ay) + "\n30\n0.0\n";
    s = s + "11\n" + num(bx) + "\n21\n" + num(by) + "\n31\n0.0\n";
    return s;
  }

  String textRecord(String lyr, double ax, double ay, double h, String text) {
    String s = "0\nTEXT\n8\n" + lyr + "\n";
    s = s + "10\n" + num(ax) + "\n20\n" + num(ay) + "\n30\n0.0\n";
    s = s + "40\n" + num(h) + "\n1\n" + text + "\n";
    return s;
  }

  String num(double v) { return "" + v; }
}