@maqiuyao/dxf-viewer 0.2.14 → 0.2.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5103,6 +5103,22 @@ const Ih = new p.Vector3(0, 1, 0), Ze = class Ze {
5103
5103
  static isObjectSelectable(t) {
5104
5104
  return t.layers.isEnabled(li);
5105
5105
  }
5106
+ /**
5107
+ * 递归释放对象的 GPU 资源(geometry/material)。
5108
+ * 用于从场景移除模型后释放显存,避免切换图纸时内存泄漏。
5109
+ * 覆盖 Mesh/Line/Points 等所有携带 geometry 的可渲染对象。
5110
+ * 注意:释放后对象不可再用于渲染。
5111
+ */
5112
+ static disposeObject(t) {
5113
+ t.traverse((e) => {
5114
+ const i = e;
5115
+ if (i.geometry) {
5116
+ i.geometry.dispose(), (Array.isArray(i.material) ? i.material : [i.material]).forEach((r) => r?.dispose());
5117
+ const n = e.userData?.originalMaterial;
5118
+ n && (Array.isArray(n) ? n : [n]).forEach((r) => r?.dispose());
5119
+ }
5120
+ });
5121
+ }
5106
5122
  };
5107
5123
  /**
5108
5124
  * Outline default material
@@ -5118,7 +5134,7 @@ g(Ze, "OUTLINE_MATERIAL", new p.LineBasicMaterial({
5118
5134
  opacity: 0.3,
5119
5135
  side: p.FrontSide
5120
5136
  }));
5121
- let Q = Ze;
5137
+ let J = Ze;
5122
5138
  const Vs = new p.Matrix4(), Bh = new p.Matrix4(), us = class us {
5123
5139
  static resetMergeInfo() {
5124
5140
  this.MergeInfo.removedObjects = 0, this.MergeInfo.mergedMeshes = 0, this.MergeInfo.mergedLines = 0, this.MergeInfo.mergedPoints = 0, this.MergeInfo.mergedUnknownTypeObjects = 0, this.MergeInfo.elapsedTime = 0;
@@ -5304,7 +5320,7 @@ const Vs = new p.Matrix4(), Bh = new p.Matrix4(), us = class us {
5304
5320
  const b = r.added;
5305
5321
  return [...h, ...d, ...u].forEach((y) => {
5306
5322
  b.push(y), i && e && (e.add(y), y.updateWorldMatrix(!1, !1));
5307
- }), this.MergeInfo.removedObjects += f.length, this.MergeInfo.mergedMeshes += h.length, this.MergeInfo.mergedLines += d.length, this.MergeInfo.mergedPoints += u.length, s && e && Q.removeEmptyObjects(e), r;
5323
+ }), this.MergeInfo.removedObjects += f.length, this.MergeInfo.mergedMeshes += h.length, this.MergeInfo.mergedLines += d.length, this.MergeInfo.mergedPoints += u.length, s && e && J.removeEmptyObjects(e), r;
5308
5324
  }
5309
5325
  /**
5310
5326
  * since we'll move geometry to another merged mesh under a group, below is the structure:
@@ -5560,7 +5576,7 @@ class zt {
5560
5576
  static getVisibleObjectBoundingBox(t) {
5561
5577
  const e = new p.Box3();
5562
5578
  return t.traverse((i) => {
5563
- i.visible && (i instanceof p.Mesh || i instanceof p.Line) && Q.isObjectSelectable(i) && e.expandByObject(i);
5579
+ i.visible && (i instanceof p.Mesh || i instanceof p.Line) && J.isObjectSelectable(i) && e.expandByObject(i);
5564
5580
  }), e;
5565
5581
  }
5566
5582
  static getObjectsBoundingBox(t, e, i) {
@@ -15665,7 +15681,7 @@ class Dt {
15665
15681
  const b = r.added;
15666
15682
  return [...h, ...d, ...u].forEach((y) => {
15667
15683
  b.push(y), i && e && e.add(y);
15668
- }), this.MergeInfo.removedObjects += f.length, this.MergeInfo.mergedMeshes += h.length, this.MergeInfo.mergedLines += d.length, this.MergeInfo.mergedPoints += u.length, s && e && Q.removeEmptyObjects(e), r;
15684
+ }), this.MergeInfo.removedObjects += f.length, this.MergeInfo.mergedMeshes += h.length, this.MergeInfo.mergedLines += d.length, this.MergeInfo.mergedPoints += u.length, s && e && J.removeEmptyObjects(e), r;
15669
15685
  }
15670
15686
  /**
15671
15687
  * since we'll move geometry to another merged mesh under a group, below is the structure:
@@ -20758,7 +20774,7 @@ const Ge = new p.Vector3(1, 0, 0), Xn = new p.Vector3(0, 1, 0), bi = new p.Vecto
20758
20774
  !M && (M = [], y[E] = M), M.push(x);
20759
20775
  };
20760
20776
  m.children.forEach((x) => {
20761
- if (Q.setSharedVariablesOfObject(x), !x.userData.isLayoutLevelObject) return;
20777
+ if (J.setSharedVariablesOfObject(x), !x.userData.isLayoutLevelObject) return;
20762
20778
  const E = x.name;
20763
20779
  let M;
20764
20780
  if (E !== j.MODEL_LAYOUT_NAME) {
@@ -20766,7 +20782,7 @@ const Ge = new p.Vector3(1, 0, 0), Xn = new p.Vector3(0, 1, 0), bi = new p.Vecto
20766
20782
  !O.directAssociatedLeafObjectSet && (O.directAssociatedLeafObjectSet = /* @__PURE__ */ new Set()), M = O.directAssociatedLeafObjectSet;
20767
20783
  }
20768
20784
  x.traverse((O) => {
20769
- if (Q.setSharedVariablesOfObject(O), O.userData.isSnapGroup && (O.visible = j.SHOW_SNAP_OBJECT), O.geometry) {
20785
+ if (J.setSharedVariablesOfObject(O), O.userData.isSnapGroup && (O.visible = j.SHOW_SNAP_OBJECT), O.geometry) {
20770
20786
  const T = O.userData.layerName;
20771
20787
  this.getLayerVisible(e, T) === !1 && (O.visible = !1, O.userData.layerVisible = !1), S(O), M && M.add(O);
20772
20788
  }
@@ -20914,8 +20930,8 @@ const Ge = new p.Vector3(1, 0, 0), Xn = new p.Vector3(0, 1, 0), bi = new p.Vecto
20914
20930
  !_ && (_ = [], M[C] = _), _.push(v);
20915
20931
  };
20916
20932
  E.children.forEach((v) => {
20917
- Q.setSharedVariablesOfObject(v), v.userData.isLayoutLevelObject && v.traverse((C) => {
20918
- if (Q.setSharedVariablesOfObject(C), C.userData.isSnapGroup && (C.visible = j.SHOW_SNAP_OBJECT), C.geometry) {
20933
+ J.setSharedVariablesOfObject(v), v.userData.isLayoutLevelObject && v.traverse((C) => {
20934
+ if (J.setSharedVariablesOfObject(C), C.userData.isSnapGroup && (C.visible = j.SHOW_SNAP_OBJECT), C.geometry) {
20919
20935
  const _ = C.userData.layerName;
20920
20936
  this.getLayerVisible(f, _) === !1 && (C.visible = !1, C.userData.layerVisible = !1), O(C);
20921
20937
  }
@@ -21819,7 +21835,7 @@ const Ge = new p.Vector3(1, 0, 0), Xn = new p.Vector3(0, 1, 0), bi = new p.Vecto
21819
21835
  else {
21820
21836
  const E = new z(H.Mesh);
21821
21837
  E.geometry = y, E.material = wi, E.layers.enableAll(), E.layers.disable(qe), a.add(E);
21822
- const M = Q.createOutline(y, pe, E.matrix), O = new z(H.LineSegment);
21838
+ const M = J.createOutline(y, pe, E.matrix), O = new z(H.LineSegment);
21823
21839
  O.geometry = M.geometry, O.material = M.material, O.matrix = M.matrix, O.layers.enableAll(), O.layers.disable(qe), a.add(O);
21824
21840
  }
21825
21841
  }
@@ -23816,7 +23832,7 @@ const Ge = new p.Vector3(1, 0, 0), Xn = new p.Vector3(0, 1, 0), bi = new p.Vecto
23816
23832
  }
23817
23833
  for (let k = 0; k < A.length; k++) {
23818
23834
  const D = A[k];
23819
- Q.setSharedVariablesOfObject(D), E.push(D), o[T].children.push(D), v && v.add(D);
23835
+ J.setSharedVariablesOfObject(D), E.push(D), o[T].children.push(D), v && v.add(D);
23820
23836
  }
23821
23837
  h > l && u(), await s((m + ++O / M) / f * 100);
23822
23838
  }
@@ -23889,7 +23905,7 @@ const Ge = new p.Vector3(1, 0, 0), Xn = new p.Vector3(0, 1, 0), bi = new p.Vecto
23889
23905
  s(e), this.removeEmptyObjectsFromMark(i);
23890
23906
  }
23891
23907
  static removeEmptyObjectsFromMark(e) {
23892
- if (e.userData.validCount === 0 || Q.isEmptyObject(e)) return e.removeFromParent(), !0;
23908
+ if (e.userData.validCount === 0 || J.isEmptyObject(e)) return e.removeFromParent(), !0;
23893
23909
  for (let i = 0; i < e.children.length; )
23894
23910
  !this.removeEmptyObjectsFromMark(e.children[i]) && ++i;
23895
23911
  return !1;
@@ -24323,7 +24339,7 @@ class ys extends Or {
24323
24339
  }), S && S.size === 0) return;
24324
24340
  const E = s.getModelObject();
24325
24341
  let M = s.getLayoutLevelObject(e);
24326
- !M && (M = new p.Group(), Q.setSharedVariablesOfObject(M), M.name = e, E?.add(M)), M.add(w), w.applyMatrix4(r.msToPsMatrix), w.updateMatrixWorld(!0);
24342
+ !M && (M = new p.Group(), J.setSharedVariablesOfObject(M), M.name = e, E?.add(M)), M.add(w), w.applyMatrix4(r.msToPsMatrix), w.updateMatrixWorld(!0);
24327
24343
  let O = M.children.find((C) => C.name === X.SNAP_GROUP_NAME);
24328
24344
  !O && (O = new p.Group(), O.visible = X.SHOW_SNAP_OBJECT, O.name = X.SNAP_GROUP_NAME, M.add(O));
24329
24345
  const T = s.getLayoutLevelObject(X.MODEL_LAYOUT_NAME), v = T?.children.find((C) => C.name === X.SNAP_GROUP_NAME);
@@ -24332,7 +24348,7 @@ class ys extends Or {
24332
24348
  const _ = C.userData.layerName;
24333
24349
  if (this.isLayerFrozenForViewport(_, y) || (Kn.makeEmpty(), Kn.expandByObject(C), !a.intersectsBox(Kn))) return;
24334
24350
  const A = C.clone();
24335
- Q.setSharedVariablesOfObject(A);
24351
+ J.setSharedVariablesOfObject(A);
24336
24352
  let I = A.parent;
24337
24353
  const B = vi.identity();
24338
24354
  for (; I && I.userData.isLayoutLevelObject !== !0; )
@@ -24340,8 +24356,8 @@ class ys extends Or {
24340
24356
  B.premultiply(r.msToPsMatrix), A.applyMatrix4(B), O?.add(A), S?.add(A);
24341
24357
  const k = s.getObjectsByLayer(_);
24342
24358
  k && k.push(A);
24343
- }), x.length > 0 && Q.removeEmptyObjectsFromRemovingObjects(x, w), w.traverse((C) => {
24344
- Q.setSharedVariablesOfObject(C), this.addSpatialFilterSection(C, s.dxfData, !0);
24359
+ }), x.length > 0 && J.removeEmptyObjectsFromRemovingObjects(x, w), w.traverse((C) => {
24360
+ J.setSharedVariablesOfObject(C), this.addSpatialFilterSection(C, s.dxfData, !0);
24345
24361
  });
24346
24362
  }
24347
24363
  setMaterialUniforms(e, i) {
@@ -24507,12 +24523,12 @@ class Lo extends Or {
24507
24523
  */
24508
24524
  setDoubleSidedMaterials() {
24509
24525
  this.getModelObject().traverse((e) => {
24510
- e instanceof p.Mesh && Q.setDoubleSidedMaterialToObject(e);
24526
+ e instanceof p.Mesh && J.setDoubleSidedMaterialToObject(e);
24511
24527
  });
24512
24528
  }
24513
24529
  revertDoubleSidedMaterials() {
24514
24530
  this.getModelObject().traverse((e) => {
24515
- e instanceof p.Mesh && Q.revertDoubleSidedMaterialToObject(e);
24531
+ e instanceof p.Mesh && J.revertDoubleSidedMaterialToObject(e);
24516
24532
  });
24517
24533
  }
24518
24534
  getModelObject() {
@@ -24528,7 +24544,7 @@ class Lo extends Or {
24528
24544
  * Enables/disables edges.
24529
24545
  */
24530
24546
  async enableOutline(e, i) {
24531
- e && !this.edges && (this.edges = await Q.createOutlines(this.getModelObject(), void 0, i)), this.edges && this.edges.forEach((s) => s.visible = e);
24547
+ e && !this.edges && (this.edges = await J.createOutlines(this.getModelObject(), void 0, i)), this.edges && this.edges.forEach((s) => s.visible = e);
24532
24548
  }
24533
24549
  /**
24534
24550
  * Sets object's color.
@@ -26862,6 +26878,7 @@ class N1 {
26862
26878
  });
26863
26879
  }
26864
26880
  init() {
26881
+ this.destroy();
26865
26882
  const t = document.createElement("div");
26866
26883
  t.classList.add("model-layout-switch-bar");
26867
26884
  const e = document.createElement("div");
@@ -29086,7 +29103,7 @@ function Qa(c, t, e, i, s) {
29086
29103
  path: r
29087
29104
  };
29088
29105
  }
29089
- class J {
29106
+ class Q {
29090
29107
  static GetBytes(t) {
29091
29108
  switch (typeof t) {
29092
29109
  case "string": {
@@ -29111,7 +29128,7 @@ class J {
29111
29128
  if (t instanceof Array) {
29112
29129
  const e = [];
29113
29130
  for (let r = 0; r < t.length; r++)
29114
- e.push(J.GetBytes(t[r]));
29131
+ e.push(Q.GetBytes(t[r]));
29115
29132
  let i = 0;
29116
29133
  for (let r = 0; r < e.length; r++)
29117
29134
  i += e[r].length;
@@ -29341,7 +29358,7 @@ const yt = class yt {
29341
29358
  */
29342
29359
  static getCode(t) {
29343
29360
  const e = J1(t);
29344
- return e.length < 2 ? e[0] : e.length < 4 ? J.ToUInt16(e, 0) : J.ToUInt32(e, 0);
29361
+ return e.length < 2 ? e[0] : e.length < 4 ? Q.ToUInt16(e, 0) : Q.ToUInt32(e, 0);
29345
29362
  }
29346
29363
  /**
29347
29364
  * Parses code data and generates TextShape.
@@ -29386,10 +29403,10 @@ const yt = class yt {
29386
29403
  l++, d = e[l];
29387
29404
  break;
29388
29405
  case je.Bigfont:
29389
- l++, d = e[l], d == 0 && (l++, d = J.ToUInt16(new Uint8Array([e[l++], e[l++]]), 0), m.x = e[l++] * i, m.y = e[l++] * i, f = e[l] * i);
29406
+ l++, d = e[l], d == 0 && (l++, d = Q.ToUInt16(new Uint8Array([e[l++], e[l++]]), 0), m.x = e[l++] * i, m.y = e[l++] * i, f = e[l] * i);
29390
29407
  break;
29391
29408
  case je.Unifont:
29392
- l += 2, d = J.ToUInt16(new Uint8Array([e[l - 1], e[l]]), 0);
29409
+ l += 2, d = Q.ToUInt16(new Uint8Array([e[l - 1], e[l]]), 0);
29393
29410
  break;
29394
29411
  }
29395
29412
  d !== 0 && (u = t.getGraphicDataByCodeWithOffset(d, f, m), u && (n.push(...u.polylines.slice()), s = u.lastPoint.clone()));
@@ -29398,19 +29415,19 @@ const yt = class yt {
29398
29415
  case 8:
29399
29416
  {
29400
29417
  const d = new p.Vector2();
29401
- d.x = J.byteTosbyte(e[++l]), d.y = J.byteTosbyte(e[++l]), s.add(d.multiplyScalar(i)), o && r.push(s.clone());
29418
+ d.x = Q.byteTosbyte(e[++l]), d.y = Q.byteTosbyte(e[++l]), s.add(d.multiplyScalar(i)), o && r.push(s.clone());
29402
29419
  }
29403
29420
  break;
29404
29421
  case 9:
29405
29422
  for (; ; ) {
29406
29423
  const u = new p.Vector2();
29407
- if (u.x = J.byteTosbyte(e[++l]), u.y = J.byteTosbyte(e[++l]), !Number.isFinite(u.x) || !Number.isFinite(u.y) || u.x == 0 && u.y == 0) break;
29424
+ if (u.x = Q.byteTosbyte(e[++l]), u.y = Q.byteTosbyte(e[++l]), !Number.isFinite(u.x) || !Number.isFinite(u.y) || u.x == 0 && u.y == 0) break;
29408
29425
  s.add(u.multiplyScalar(i)), o && r.push(s.clone());
29409
29426
  }
29410
29427
  break;
29411
29428
  case 10:
29412
29429
  {
29413
- const d = e[++l] * i, u = J.byteTosbyte(e[++l]), f = (u & 112) >> 4;
29430
+ const d = e[++l] * i, u = Q.byteTosbyte(e[++l]), f = (u & 112) >> 4;
29414
29431
  let m = u & 7;
29415
29432
  m == 0 && (m = 8);
29416
29433
  const b = Math.PI / 4;
@@ -29429,7 +29446,7 @@ const yt = class yt {
29429
29446
  break;
29430
29447
  case 11:
29431
29448
  {
29432
- const d = e[++l], u = e[++l], f = e[++l], m = e[++l], b = (f * 255 + m) * i, y = J.byteTosbyte(e[++l]), S = (y & 112) >> 4;
29449
+ const d = e[++l], u = e[++l], f = e[++l], m = e[++l], b = (f * 255 + m) * i, y = Q.byteTosbyte(e[++l]), S = (y & 112) >> 4;
29433
29450
  let w = y & 7;
29434
29451
  w == 0 && (w = 8), u != 0 && w--;
29435
29452
  const x = Math.PI / 4;
@@ -29451,16 +29468,16 @@ const yt = class yt {
29451
29468
  case 12:
29452
29469
  {
29453
29470
  const d = new p.Vector2();
29454
- d.x = J.byteTosbyte(e[++l]) * i, d.y = J.byteTosbyte(e[++l]) * i;
29455
- let u = J.byteTosbyte(e[++l]);
29471
+ d.x = Q.byteTosbyte(e[++l]) * i, d.y = Q.byteTosbyte(e[++l]) * i;
29472
+ let u = Q.byteTosbyte(e[++l]);
29456
29473
  u < -127 && (u = -127), o && (u == 0 ? r.push(s.clone().add(d)) : r.push(yt.generateArcPoints(s, d, u / 127))), s.add(d);
29457
29474
  }
29458
29475
  break;
29459
29476
  case 13:
29460
29477
  for (; ; ) {
29461
29478
  const u = new p.Vector2();
29462
- if (u.x = J.byteTosbyte(e[++l]) * i, u.y = J.byteTosbyte(e[++l]) * i, !Number.isFinite(u.x) || !Number.isFinite(u.y) || u.x == 0 && u.y == 0) break;
29463
- let f = J.byteTosbyte(e[++l]);
29479
+ if (u.x = Q.byteTosbyte(e[++l]) * i, u.y = Q.byteTosbyte(e[++l]) * i, !Number.isFinite(u.x) || !Number.isFinite(u.y) || u.x == 0 && u.y == 0) break;
29480
+ let f = Q.byteTosbyte(e[++l]);
29464
29481
  f < -127 && (f = -127), o && (f == 0 ? r.push(s.clone().add(u)) : r.push(yt.generateArcPoints(s.clone(), u, f / 127))), s.add(u);
29465
29482
  }
29466
29483
  break;
@@ -30390,13 +30407,13 @@ const rr = {
30390
30407
  return this.getGraphicDataByCode(e, i)?.offset(s);
30391
30408
  }
30392
30409
  parseBigFont(e) {
30393
- J.ToInt16(e.readBytes(2), 0);
30394
- const i = J.ToInt16(e.readBytes(2), 0), s = J.ToInt16(e.readBytes(2), 0);
30410
+ Q.ToInt16(e.readBytes(2), 0);
30411
+ const i = Q.ToInt16(e.readBytes(2), 0), s = Q.ToInt16(e.readBytes(2), 0);
30395
30412
  for (let l = 0; l < s; l++)
30396
30413
  e.readBytes(2), e.readBytes(2);
30397
30414
  const n = [];
30398
30415
  for (let l = 0; l < i; l++) {
30399
- const h = e.readBytes(8), d = J.ToUInt16(new Uint8Array([h[1], h[0]]), 0), u = J.ToUInt16(h, 2), f = J.ToUInt32(h, 4);
30416
+ const h = e.readBytes(8), d = Q.ToUInt16(new Uint8Array([h[1], h[0]]), 0), u = Q.ToUInt16(h, 2), f = Q.ToUInt32(h, 4);
30400
30417
  d === 0 && u === 0 && f === 0 || n.push({
30401
30418
  code: d,
30402
30419
  length: u,
@@ -30412,22 +30429,22 @@ const rr = {
30412
30429
  a.length - o === 4 ? (this.baseUp = a[o++], this.baseDown = a[o++], this.orientation = a[o++]) : (this.baseUp = a[o++], o++, this.orientation = a[o++], this.baseDown = a[o++], this.isExtend = !0), this.data = r;
30413
30430
  }
30414
30431
  parseUniFont(e) {
30415
- const i = J.ToInt32(e.readBytes(4), 0), s = J.ToInt16(e.readBytes(2), 0), n = e.readBytes(s);
30432
+ const i = Q.ToInt32(e.readBytes(4), 0), s = Q.ToInt16(e.readBytes(2), 0), n = e.readBytes(s);
30416
30433
  this.info = new TextDecoder().decode(n);
30417
30434
  let r = this.info.indexOf("\0");
30418
30435
  this.baseUp = n[++r], this.baseDown = n[++r], this.orientation = n[++r], this.isUniCode = n[++r] == 0, this.isEmbedded = n[++r] == 0;
30419
30436
  const a = {};
30420
30437
  for (let o = 0; o < i - 1; o++) {
30421
- const l = J.ToUInt16(e.readBytes(2), 0), h = J.ToUInt16(e.readBytes(2), 0);
30438
+ const l = Q.ToUInt16(e.readBytes(2), 0), h = Q.ToUInt16(e.readBytes(2), 0);
30422
30439
  a[l] = e.readBytes(h);
30423
30440
  }
30424
30441
  this.data = a;
30425
30442
  }
30426
30443
  parseShapeFont(e) {
30427
30444
  e.readBytes(2), e.readBytes(2);
30428
- const i = J.ToInt16(e.readBytes(2), 0), s = [];
30445
+ const i = Q.ToInt16(e.readBytes(2), 0), s = [];
30429
30446
  for (let o = 0; o < i; o++) {
30430
- const l = J.ToUInt16(e.readBytes(2), 0), h = J.ToUInt16(e.readBytes(2), 0);
30447
+ const l = Q.ToUInt16(e.readBytes(2), 0), h = Q.ToUInt16(e.readBytes(2), 0);
30431
30448
  s.push({
30432
30449
  code: l,
30433
30450
  length: h
@@ -31166,6 +31183,10 @@ const ps = class ps {
31166
31183
  g(this, "gltfLoader");
31167
31184
  g(this, "font");
31168
31185
  g(this, "manager");
31186
+ /**
31187
+ * 最近一次进行中的 DXF 加载器,用于中断未完成的加载(快速切换图纸场景)
31188
+ */
31189
+ g(this, "currentDxfLoader");
31169
31190
  this.manager = t;
31170
31191
  }
31171
31192
  setLoadManager(t) {
@@ -31297,10 +31318,18 @@ const ps = class ps {
31297
31318
  async loadDxfData(t, e, i, s, n) {
31298
31319
  this.font || console.warn("[LoadingHelper] Should set font first!");
31299
31320
  const r = new X(this.manager, n);
31300
- return r.setFont(this.font), await this.font?.getFontFromIndexeddb(), r.manager.onLoad = () => {
31321
+ return this.currentDxfLoader = r, r.setFont(this.font), await this.font?.getFontFromIndexeddb(), r.manager.onLoad = () => {
31301
31322
  s && s();
31302
31323
  }, r.loadEx(t, e, i);
31303
31324
  }
31325
+ /**
31326
+ * 中断当前进行中的 DXF 加载(若有)。
31327
+ * 每次加载使用独立的 DxfLoader 实例,因此只影响最近一次未完成的加载,
31328
+ * 已完成的模型与后续新加载不受影响。
31329
+ */
31330
+ abortDxfLoading() {
31331
+ this.currentDxfLoader?.abort(), this.currentDxfLoader = void 0;
31332
+ }
31304
31333
  /**
31305
31334
  * Sets font.
31306
31335
  * Font is required for Viewer2d to view Chinese, etc.
@@ -32092,8 +32121,8 @@ class N0 {
32092
32121
  const i = t.slice(), s = [];
32093
32122
  for (let r = 0; r < t.length && r < this.intersectionLimit; r++) {
32094
32123
  const a = t[r].object;
32095
- if (a.updateWorldMatrix(!0, !1), a instanceof p.Mesh && !Q.hasOutline(a, !1)) {
32096
- const o = Q.createOutline(a.geometry);
32124
+ if (a.updateWorldMatrix(!0, !1), a instanceof p.Mesh && !J.hasOutline(a, !1)) {
32125
+ const o = J.createOutline(a.geometry);
32097
32126
  o.visible = !1, a.add(o), o.updateWorldMatrix(!0, !1), s.push(o);
32098
32127
  }
32099
32128
  }
@@ -32386,6 +32415,12 @@ class no {
32386
32415
  getMasterModel() {
32387
32416
  return this.viewer.loadedModels.find((t) => this.viewer.masterModelId === t.modelId);
32388
32417
  }
32418
+ /**
32419
+ * 失效已建立的文本搜索索引(模型被移除后调用),释放对旧模型对象的引用。
32420
+ */
32421
+ invalidateIndex() {
32422
+ this.index = [], this.indexedModelObject = void 0;
32423
+ }
32389
32424
  /**
32390
32425
  * 遍历主模型场景图,收集所有带 userData.textContent 的文本对象。
32391
32426
  */
@@ -32903,6 +32938,9 @@ class bu {
32903
32938
  addModel(t) {
32904
32939
  this.modelGroup.add(t.getModelObject());
32905
32940
  }
32941
+ removeModel(t) {
32942
+ this.modelGroup.remove(t.getModelObject());
32943
+ }
32906
32944
  enableShadow(t) {
32907
32945
  this.renderer.shadowMap.enabled = t, this.directionalLight.castShadow = t, t && (this.tryAdjustDirectionalLight(), this.updateDirectionalLightShadow()), this.modelGroup.traverse((e) => {
32908
32946
  e.isMesh && (e.castShadow = t, e.receiveShadow = t);
@@ -33431,10 +33469,20 @@ class Pr extends hi {
33431
33469
  };
33432
33470
  }
33433
33471
  /**
33434
- *
33472
+ * 从场景中移除指定模型(不销毁 viewer,用于切换图纸等场景),
33473
+ * 并释放模型的 GPU 资源(geometry/material)避免内存泄漏。
33474
+ * 注意:移除后该模型对象不可再用于渲染。
33475
+ * @param modelId 加载时指定的 modelId
33476
+ * @returns 被移除的模型,未找到时返回 undefined
33435
33477
  */
33478
+ removeModel(e) {
33479
+ const i = this.loadedModels.findIndex((n) => n.modelId === e);
33480
+ if (i === -1) return;
33481
+ const s = this.loadedModels.splice(i, 1)[0];
33482
+ return this.sceneManager.removeModel(s), J.disposeObject(s.getModelObject()), this.enableRender(), s;
33483
+ }
33436
33484
  destroy() {
33437
- this.requestAnimationFrameHandle && cancelAnimationFrame(this.requestAnimationFrameHandle), this.css2dRenderer.domElement.remove(), this.container.destroy(), this.sceneManager.destroy(), this.cameraManager.destroy(), this.overlayRender.destroy(), this.spinner.destroy(), this.progressBar?.destroy(), this.undoManager.destroy();
33485
+ this.requestAnimationFrameHandle && cancelAnimationFrame(this.requestAnimationFrameHandle), this.loaderHelper.abortDxfLoading(), this.css2dRenderer.domElement.remove(), this.container.destroy(), this.sceneManager.destroy(), this.cameraManager.destroy(), this.overlayRender.destroy(), this.spinner.destroy(), this.progressBar?.destroy(), this.undoManager.destroy();
33438
33486
  }
33439
33487
  /**
33440
33488
  * Installs a Plugin.
@@ -33692,6 +33740,7 @@ class $o extends Pr {
33692
33740
  * ```
33693
33741
  */
33694
33742
  async loadModel(e, i, s) {
33743
+ this.loaderHelper.abortDxfLoading();
33695
33744
  const n = this.getUniqueModelId(e.modelId || e.src);
33696
33745
  this.spinner.increaseJobCount(), this.loaderHelper.setFont(this.fontManager), this.masterModelId || (this.masterModelId = n);
33697
33746
  const r = n, a = Je.t("ProgressBar.Loading"), o = (d) => {
@@ -33739,6 +33788,16 @@ class $o extends Pr {
33739
33788
  this.spinner.decreaseJobCount();
33740
33789
  }
33741
33790
  }
33791
+ /**
33792
+ * 从场景中移除指定模型(不销毁 viewer,用于切换图纸等场景)。
33793
+ * 场景被清空后重置主模型与布局状态,保证后续加载的模型按全新流程初始化。
33794
+ * @param modelId 加载时指定的 modelId
33795
+ * @returns 被移除的模型,未找到时返回 undefined
33796
+ */
33797
+ removeModel(e) {
33798
+ const i = super.removeModel(e);
33799
+ return i && this.textSearchHelper?.invalidateIndex(), i && this.loadedModels.length === 0 && (this.masterModelId = "", this.activeLayoutName = "", this.layoutInfos = {}, this.units = void 0), i;
33800
+ }
33742
33801
  addModel(e) {
33743
33802
  super.addModel(e);
33744
33803
  const i = e.modelId;
@@ -33816,7 +33875,7 @@ class $o extends Pr {
33816
33875
  color: "#888",
33817
33876
  side: p.DoubleSide
33818
33877
  });
33819
- this.groundPlane = new p.Mesh(o, l), this.groundPlane.visible = !1, this.groundPlane.position.set(a.x, a.y, -r.z), this.groundPlane.scale.set(1e5, 1e5, 1), this.groundPlane.name = "groundPlane", this.groundPlane.renderOrder = vu, this.groundPlane.layers.enableAll(), Q.disableLayerChannels(this.groundPlane, [li]), this.scene.add(this.groundPlane);
33878
+ this.groundPlane = new p.Mesh(o, l), this.groundPlane.visible = !1, this.groundPlane.position.set(a.x, a.y, -r.z), this.groundPlane.scale.set(1e5, 1e5, 1), this.groundPlane.name = "groundPlane", this.groundPlane.renderOrder = vu, this.groundPlane.layers.enableAll(), J.disableLayerChannels(this.groundPlane, [li]), this.scene.add(this.groundPlane);
33820
33879
  }
33821
33880
  this.groundPlane.updateMatrix(), this.groundPlane.updateMatrixWorld(!0);
33822
33881
  }
@@ -33836,7 +33895,7 @@ class $o extends Pr {
33836
33895
  for (const s of this.loadedModels) {
33837
33896
  const n = s.getModelObject(), r = n.children.find((a) => a.name === this.activeLayoutName);
33838
33897
  r && r.children.find((o) => o.name === X.SNAP_GROUP_NAME)?.traverse((o) => {
33839
- if (Q.isLeafObject(o)) {
33898
+ if (J.isLeafObject(o)) {
33840
33899
  if (ar.makeEmpty(), ar.expandByObject(o), !oo.intersectsBox(ar)) return;
33841
33900
  e.push(o);
33842
33901
  }
@@ -34405,7 +34464,7 @@ class $o extends Pr {
34405
34464
  this.lastCameraZoom = a;
34406
34465
  let o = 1;
34407
34466
  e.traverse((h) => {
34408
- if (h.userData.isSnapObject || !Q.isLeafObject(h)) return;
34467
+ if (h.userData.isSnapObject || !J.isLeafObject(h)) return;
34409
34468
  const d = h.geometry;
34410
34469
  d.boundingBox || d.computeBoundingBox();
34411
34470
  const u = d.boundingBox.clone();
@@ -35933,7 +35992,7 @@ class K0 {
35933
35992
  u.geometry && u.layers.enableAll(), u instanceof p.Light && (u.visible = !1);
35934
35993
  });
35935
35994
  let o;
35936
- r.edges && (o = await Q.createOutlines(n, void 0, a));
35995
+ r.edges && (o = await J.createOutlines(n, void 0, a));
35937
35996
  let l = r.modelId || r.src;
35938
35997
  const h = (u) => !!this.loadedModels.find((f) => f.modelId === u);
35939
35998
  if (h(l)) {
@@ -36105,10 +36164,10 @@ class K0 {
36105
36164
  this.loadedModels.forEach((a) => {
36106
36165
  const o = a.object.id;
36107
36166
  if (t) {
36108
- this.savedMaterialsForOpacity && this.savedMaterialsForOpacity.length > 0 && Q.revertObjectOpacityById(n, o, this.savedMaterialsForOpacity);
36109
- const l = Q.setObjectOpacityById(n, o, e, i, s);
36167
+ this.savedMaterialsForOpacity && this.savedMaterialsForOpacity.length > 0 && J.revertObjectOpacityById(n, o, this.savedMaterialsForOpacity);
36168
+ const l = J.setObjectOpacityById(n, o, e, i, s);
36110
36169
  r.push(...l);
36111
- } else this.savedMaterialsForOpacity && Q.revertObjectOpacityById(n, o, this.savedMaterialsForOpacity);
36170
+ } else this.savedMaterialsForOpacity && J.revertObjectOpacityById(n, o, this.savedMaterialsForOpacity);
36112
36171
  }), t ? this.savedMaterialsForOpacity = r : this.savedMaterialsForOpacity = [];
36113
36172
  }
36114
36173
  hasTransparentObject() {
@@ -36240,7 +36299,7 @@ class K0 {
36240
36299
  });
36241
36300
  this.loadedModels.forEach((r) => {
36242
36301
  const a = r.object.id;
36243
- Q.applyMaterialToObjectById(this.scene, a, s, i, e);
36302
+ J.applyMaterialToObjectById(this.scene, a, s, i, e);
36244
36303
  }), this.outlineRoot.clear();
36245
36304
  let n = [];
36246
36305
  if (t.forEach((r) => {
@@ -36254,13 +36313,13 @@ class K0 {
36254
36313
  });
36255
36314
  }), n.length > 0) {
36256
36315
  n.forEach((a) => {
36257
- const o = Q.createOutline(a.geometry, this.outlineMaterial);
36316
+ const o = J.createOutline(a.geometry, this.outlineMaterial);
36258
36317
  o.applyMatrix4(a.matrixWorld), this.outlineRoot.add(o), o.updateWorldMatrix(!0, !1);
36259
36318
  });
36260
36319
  const r = n.map((a) => a.id);
36261
36320
  this.loadedModels.forEach((a) => {
36262
36321
  const o = a.object.id;
36263
- Q.revertAppliedMaterialToObjectById(this.scene, o, r);
36322
+ J.revertAppliedMaterialToObjectById(this.scene, o, r);
36264
36323
  }), this.transparentObjectIds = r, this.flyToObjects(n);
36265
36324
  } else
36266
36325
  P.warn("[Viewer] No object found with uniqueIds: " + t.map((r) => r.id));
@@ -36379,7 +36438,7 @@ export {
36379
36438
  Rt as OSnapMarkerType,
36380
36439
  cu as OSnapType,
36381
36440
  j0 as ObjectPixelSizeHelper,
36382
- Q as ObjectUtils,
36441
+ J as ObjectUtils,
36383
36442
  q0 as Offset,
36384
36443
  X1 as OrbitControls,
36385
36444
  Rn as OutlineColorRgba,
@@ -36,10 +36,20 @@ export declare class LoadingHelper {
36
36
  * It is kind of tricky to load image into scene, but we'll do this by creating a sprite.
37
37
  */
38
38
  loadImage(url: string, onProgress?: (event: ProgressEvent) => void): Promise<THREE.Object3D>;
39
+ /**
40
+ * 最近一次进行中的 DXF 加载器,用于中断未完成的加载(快速切换图纸场景)
41
+ */
42
+ private currentDxfLoader?;
39
43
  /**
40
44
  * Loads dxf data, including entities, tables(layers, blocks), etc.
41
45
  */
42
46
  loadDxfData(url: string, dxfDataId?: string, onProgress?: (event: ProgressEvent) => void, onLoad?: () => void, cfg?: DxfLoaderConfig): Promise<DxfData>;
47
+ /**
48
+ * 中断当前进行中的 DXF 加载(若有)。
49
+ * 每次加载使用独立的 DxfLoader 实例,因此只影响最近一次未完成的加载,
50
+ * 已完成的模型与后续新加载不受影响。
51
+ */
52
+ abortDxfLoading(): void;
43
53
  /**
44
54
  * Sets font.
45
55
  * Font is required for Viewer2d to view Chinese, etc.
@@ -41,6 +41,10 @@ export declare class TextSearchHelper {
41
41
  private index;
42
42
  constructor(viewer: Viewer2d);
43
43
  private getMasterModel;
44
+ /**
45
+ * 失效已建立的文本搜索索引(模型被移除后调用),释放对旧模型对象的引用。
46
+ */
47
+ invalidateIndex(): void;
44
48
  /**
45
49
  * 遍历主模型场景图,收集所有带 userData.textContent 的文本对象。
46
50
  */
@@ -17,6 +17,7 @@ export declare class SceneManager {
17
17
  private initPMREMGenerator;
18
18
  get maxFragmentUniforms(): number;
19
19
  addModel(model: Model): void;
20
+ removeModel(model: Model): void;
20
21
  enableShadow(enable: boolean): void;
21
22
  enableClipping(enable: boolean): void;
22
23
  setClipPlanes(planes: THREE.Plane[]): void;
@@ -212,4 +212,11 @@ export declare class ObjectUtils {
212
212
  static isObjectHittable(obj: THREE.Object3D): boolean;
213
213
  static isObjectSnapable(obj: THREE.Object3D): boolean;
214
214
  static isObjectSelectable(obj: THREE.Object3D): boolean;
215
+ /**
216
+ * 递归释放对象的 GPU 资源(geometry/material)。
217
+ * 用于从场景移除模型后释放显存,避免切换图纸时内存泄漏。
218
+ * 覆盖 Mesh/Line/Points 等所有携带 geometry 的可渲染对象。
219
+ * 注意:释放后对象不可再用于渲染。
220
+ */
221
+ static disposeObject(object: THREE.Object3D): void;
215
222
  }
@@ -224,8 +224,13 @@ export declare abstract class BaseViewer extends Event<ViewerEvents> {
224
224
  materials: number;
225
225
  };
226
226
  /**
227
- *
227
+ * 从场景中移除指定模型(不销毁 viewer,用于切换图纸等场景),
228
+ * 并释放模型的 GPU 资源(geometry/material)避免内存泄漏。
229
+ * 注意:移除后该模型对象不可再用于渲染。
230
+ * @param modelId 加载时指定的 modelId
231
+ * @returns 被移除的模型,未找到时返回 undefined
228
232
  */
233
+ removeModel(modelId: string): Model | undefined;
229
234
  destroy(): void;
230
235
  /**
231
236
  * Installs a Plugin.
@@ -303,6 +303,13 @@ export declare class Viewer2d extends BaseViewer {
303
303
  threejsObject: THREE.Object3D;
304
304
  llbMesh?: LlbMeshItem[];
305
305
  }) => void): Promise<void>;
306
+ /**
307
+ * 从场景中移除指定模型(不销毁 viewer,用于切换图纸等场景)。
308
+ * 场景被清空后重置主模型与布局状态,保证后续加载的模型按全新流程初始化。
309
+ * @param modelId 加载时指定的 modelId
310
+ * @returns 被移除的模型,未找到时返回 undefined
311
+ */
312
+ removeModel(modelId: string): Model2d | undefined;
306
313
  addModel(model: Model2d): void;
307
314
  private getDxfUnits;
308
315
  private handleOverlayDxf;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@maqiuyao/dxf-viewer",
3
- "version": "0.2.14",
3
+ "version": "0.2.16",
4
4
  "description": "基于 three.js 的 DXF 图纸 2D/3D 查看与对比库(Viewer2d / DxfCompareHelper)",
5
5
  "type": "module",
6
6
  "main": "./dist/dxf-viewer.cjs",