three-cad-viewer 3.2.2 → 3.3.0

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.
@@ -57173,6 +57173,7 @@ class Panel {
57173
57173
  constructor(display) {
57174
57174
  this.display = display;
57175
57175
  this.html = this._getHtml();
57176
+ this.callbacks = [];
57176
57177
  this.html.addEventListener("contextmenu", (ev) => {
57177
57178
  ev.preventDefault();
57178
57179
  });
@@ -57204,6 +57205,13 @@ class Panel {
57204
57205
  this.html.style.display = flag ? "inline-block" : "none";
57205
57206
  };
57206
57207
 
57208
+ /**
57209
+ * Get status of the panel
57210
+ */
57211
+ isVisible = () => {
57212
+ return this.html.style.display == "inline-block";
57213
+ };
57214
+
57207
57215
  /**
57208
57216
  * Sets the position of the panel (with the top left corner at the specified coordinates)
57209
57217
  * @param {number} x
@@ -57220,8 +57228,15 @@ class Panel {
57220
57228
  * @param {CallableFunction} callback - The callback function to register
57221
57229
  */
57222
57230
  registerCallback(eventType, callback) {
57231
+ this.callbacks.push({ callback: callback, type: eventType });
57223
57232
  this.html.addEventListener(eventType, callback);
57224
57233
  }
57234
+
57235
+ dispose() {
57236
+ for (var callback of this.callbacks) {
57237
+ this.html.removeEventListener(callback.type, callback.callback);
57238
+ }
57239
+ }
57225
57240
  }
57226
57241
 
57227
57242
  class DistancePanel extends Panel {
@@ -57561,6 +57576,12 @@ class DistanceLineArrow extends Group$1 {
57561
57576
  this.add(line);
57562
57577
  }
57563
57578
 
57579
+ dispose() {
57580
+ this.children.forEach((child) => {
57581
+ if (child.geometry) child.geometry.dispose();
57582
+ if (child.material) child.material.dispose();
57583
+ });
57584
+ }
57564
57585
  /**
57565
57586
  * Update the arrow so it keeps the same size on the screen.
57566
57587
  * @param {number} scaleFactor
@@ -57581,7 +57602,10 @@ class DistanceLineArrow extends Group$1 {
57581
57602
  .multiplyScalar((-scaleFactor * this.coneLength) / 2),
57582
57603
  );
57583
57604
  const line = this.children.find((child) => child.type == "LineSegments2");
57584
- line.geometry.setPositions([...newStart.toArray(), ...newEnd.toArray()]);
57605
+ line.geometry.setPositions([
57606
+ ...(this.arrowStart ? newStart.toArray() : this.point1),
57607
+ ...(this.arrowEnd ? newEnd.toArray() : this.point2),
57608
+ ]);
57585
57609
 
57586
57610
  if (this.arrowStart) {
57587
57611
  const startCone = this.children.find(
@@ -57615,6 +57639,9 @@ class Measurement {
57615
57639
  this.scene = new Scene();
57616
57640
  this.panel = panel;
57617
57641
  this.panelCenter = null;
57642
+ this.panelX = null;
57643
+ this.panelY = null;
57644
+ this.panelShown = false;
57618
57645
  this.responseData = null;
57619
57646
  this.measurementLineColor = 0x000000;
57620
57647
  this.connectingLineColor = 0x800080;
@@ -57627,10 +57654,7 @@ class Measurement {
57627
57654
  this.panelDragData.y = e.clientY;
57628
57655
  e.stopPropagation();
57629
57656
  });
57630
- document.addEventListener("mouseup", (e) => {
57631
- this.panelDragData.clicked = false;
57632
- e.stopPropagation();
57633
- });
57657
+ document.addEventListener("mouseup", this._mouseup);
57634
57658
  document.addEventListener("mousemove", this._dragPanel);
57635
57659
  }
57636
57660
 
@@ -57730,28 +57754,6 @@ class Measurement {
57730
57754
  }
57731
57755
  }
57732
57756
 
57733
- _computePanelCenter() {
57734
- const camera = this.viewer.camera.getCamera();
57735
- const zCam = new Vector3();
57736
- const xCam = new Vector3();
57737
- const yCam = new Vector3();
57738
-
57739
- camera.getWorldDirection(zCam);
57740
- zCam.multiplyScalar(-1);
57741
- // Check if zCam is parallel to camera.up
57742
- if (Math.abs(zCam.dot(camera.up)) >= 0.99) {
57743
- // Choose a different vector to cross with zCam
57744
- xCam.crossVectors(new Vector3(1, 0, 0), zCam).normalize();
57745
- } else {
57746
- xCam.crossVectors(camera.up, zCam).normalize();
57747
- }
57748
- yCam.crossVectors(zCam, xCam).normalize();
57749
- const offsetDistance = this.viewer.bbox.boundingSphere().radius;
57750
- this.panelCenter = this.viewer.bbox
57751
- .boundingSphere()
57752
- .center.add(xCam.multiplyScalar(offsetDistance));
57753
- }
57754
-
57755
57757
  /**
57756
57758
  * React to each new selected element in the viewer.
57757
57759
  * obj: ObjectGroup
@@ -57770,21 +57772,37 @@ class Measurement {
57770
57772
  this._updateMeasurement();
57771
57773
  };
57772
57774
 
57775
+ _mouseup = (e) => {
57776
+ this.panelDragData.clicked = false;
57777
+ e.stopPropagation();
57778
+ };
57779
+
57773
57780
  _movePanel = () => {
57774
- var worldCoord = this.panelCenter;
57775
- var screenCoord = worldCoord
57776
- .clone()
57777
- .project(this.viewer.camera.getCamera());
57778
- screenCoord.x = Math.round(
57779
- ((1 + screenCoord.x) * this.viewer.renderer.domElement.offsetWidth) / 2,
57780
- );
57781
- screenCoord.y = Math.round(
57782
- ((1 - screenCoord.y) * this.viewer.renderer.domElement.offsetHeight) / 2,
57783
- );
57784
- const panelStyle = window.getComputedStyle(this.panel.html);
57785
- const x = screenCoord.x - parseFloat(panelStyle.width) / 2;
57786
- const y = screenCoord.y - parseFloat(panelStyle.height) / 2;
57787
- this.panel.relocate(x, y);
57781
+ if (!this.panel.isVisible()) return;
57782
+
57783
+ const canvasRect = this.viewer.renderer.domElement.getBoundingClientRect();
57784
+ const panelRect = this.panel.html.getBoundingClientRect();
57785
+ if (this.panelX == null) {
57786
+ this.panelX = canvasRect.width - panelRect.width - 2;
57787
+ this.panelY = canvasRect.height - panelRect.height - 2;
57788
+ }
57789
+
57790
+ this.panel.relocate(this.panelX, this.panelY);
57791
+
57792
+ const panelCenterX = this.panelX + panelRect.width / 2;
57793
+ const panelCenterY = this.panelY + panelRect.height / 2;
57794
+ const ndcX = panelCenterX / (canvasRect.width / 2) - 1;
57795
+ const ndcY = 1 - panelCenterY / (canvasRect.height / 2);
57796
+ const ndcZ = this.viewer.ortho ? -0.9 : 1; // seems like a nice default ...
57797
+ var panelCenter = new Vector3(ndcX, ndcY, ndcZ);
57798
+
57799
+ const camera = this.viewer.camera.getCamera();
57800
+ camera.updateProjectionMatrix();
57801
+ camera.updateMatrixWorld();
57802
+ this.panelCenter = panelCenter.unproject(camera);
57803
+
57804
+ this.scene.clear();
57805
+ this._makeLines();
57788
57806
  };
57789
57807
 
57790
57808
  /**
@@ -57794,33 +57812,31 @@ class Measurement {
57794
57812
  */
57795
57813
  _dragPanel = (e) => {
57796
57814
  if (!this.panelDragData.clicked) return;
57815
+ const panelRect = this.panel.html.getBoundingClientRect();
57816
+ const canvasRect = this.viewer.renderer.domElement.getBoundingClientRect();
57817
+ let dx = e.clientX - this.panelDragData.x;
57818
+ let dy = e.clientY - this.panelDragData.y;
57819
+
57820
+ if (
57821
+ !(
57822
+ (panelRect.x + dx < canvasRect.x && e.movementX <= 0) ||
57823
+ (panelRect.x + dx > canvasRect.x + canvasRect.width - panelRect.width &&
57824
+ e.movementX >= 0)
57825
+ )
57826
+ ) {
57827
+ this.panelX += dx;
57828
+ }
57829
+ if (
57830
+ !(
57831
+ (panelRect.y + dy < canvasRect.y && e.movementY <= 0) ||
57832
+ (panelRect.y + dy >
57833
+ canvasRect.y + canvasRect.height - panelRect.height &&
57834
+ e.movementY >= 0)
57835
+ )
57836
+ ) {
57837
+ this.panelY += dy;
57838
+ }
57797
57839
 
57798
- const viewer = this.viewer;
57799
- const camera = viewer.camera.getCamera();
57800
-
57801
- let x = e.clientX - this.panelDragData.x;
57802
- let y = e.clientY - this.panelDragData.y;
57803
- const viewerWidth = this.viewer.renderer.domElement.offsetWidth;
57804
- const viewerHeight = this.viewer.renderer.domElement.offsetHeight;
57805
- const viewerToClientWidthRatio =
57806
- (0.5 * viewerWidth) / document.documentElement.clientWidth; // I dont get why we need to use half of the viewer width
57807
- const viewerToClientHeightRatio =
57808
- (0.5 * viewerHeight) / document.documentElement.clientHeight;
57809
-
57810
- x /= document.documentElement.clientWidth; // x becomes a percentage of the client width
57811
- y /= document.documentElement.clientHeight;
57812
- x /= viewerToClientWidthRatio; // rescale the x value so it represent a percentage of the viewer width
57813
- y /= viewerToClientHeightRatio;
57814
-
57815
- // First transform world vec in screen vec
57816
- // Then add the offset vec and then retransform back to world vec
57817
- const panelCenter = this.panelCenter.clone().project(camera);
57818
- const offsetVec = new Vector3(x, -y, 0);
57819
- panelCenter.add(offsetVec);
57820
- panelCenter.unproject(camera);
57821
- this.panelCenter = panelCenter;
57822
-
57823
- // Clear and update the scene
57824
57840
  this.scene.clear();
57825
57841
  this._updateMeasurement();
57826
57842
 
@@ -57856,17 +57872,24 @@ class Measurement {
57856
57872
  update() {
57857
57873
  const camera = this.viewer.camera.getCamera();
57858
57874
  const zoom = this.viewer.camera.getZoom();
57859
- this.coneLength = this.viewer.bb_radius / 15;
57875
+ this.coneLength =
57876
+ this.viewer.bb_radius /
57877
+ (Math.max(this.viewer.cadWidth, this.viewer.height) / 60);
57860
57878
  this._adjustArrowsScaleFactor(zoom);
57861
57879
  this.viewer.renderer.clearDepth();
57862
57880
  this.viewer.renderer.render(this.scene, camera);
57863
57881
  this._movePanel();
57864
57882
  }
57883
+
57865
57884
  dispose() {
57885
+ document.removeEventListener("mouseup", this._mouseup);
57886
+ document.removeEventListener("mousemove", this._dragPanel);
57887
+
57866
57888
  for (var i in this.scene.children) {
57867
57889
  this.scene.children[i].dispose();
57868
57890
  this.scene.children[i] = null;
57869
57891
  }
57892
+ this.panel.dispose();
57870
57893
  this.panel = null;
57871
57894
  this.viewer = null;
57872
57895
  this.scene = null;
@@ -57887,10 +57910,10 @@ class DistanceMeasurement extends Measurement {
57887
57910
  const xdist = Math.abs(distVec.x);
57888
57911
  const ydist = Math.abs(distVec.y);
57889
57912
  const zdist = Math.abs(distVec.z);
57890
- this.panel.total = total.toFixed(2);
57891
- this.panel.x_distance = xdist.toFixed(2);
57892
- this.panel.y_distance = ydist.toFixed(2);
57893
- this.panel.z_distance = zdist.toFixed(2);
57913
+ this.panel.total = total.toFixed(3);
57914
+ this.panel.x_distance = xdist.toFixed(3);
57915
+ this.panel.y_distance = ydist.toFixed(3);
57916
+ this.panel.z_distance = zdist.toFixed(3);
57894
57917
  }
57895
57918
 
57896
57919
  _getMaxObjSelected() {
@@ -57925,6 +57948,7 @@ class DistanceMeasurement extends Measurement {
57925
57948
  lineWidth,
57926
57949
  this.connectingLineColor,
57927
57950
  false,
57951
+ false,
57928
57952
  );
57929
57953
  this.scene.add(connectingLine);
57930
57954
  }
@@ -57984,6 +58008,7 @@ class PropertiesMeasurement extends Measurement {
57984
58008
  lineWidth,
57985
58009
  this.connectingLineColor,
57986
58010
  false,
58011
+ false,
57987
58012
  );
57988
58013
  this.scene.add(connectingLine);
57989
58014
  }
@@ -58048,7 +58073,7 @@ class AngleMeasurement extends Measurement {
58048
58073
  this.panelCenter,
58049
58074
  lineWidth,
58050
58075
  this.connectingLineColor,
58051
- true,
58076
+ false,
58052
58077
  false,
58053
58078
  );
58054
58079
  const item2Line = new DistanceLineArrow(
@@ -58057,7 +58082,7 @@ class AngleMeasurement extends Measurement {
58057
58082
  this.panelCenter,
58058
58083
  lineWidth,
58059
58084
  this.connectingLineColor,
58060
- true,
58085
+ false,
58061
58086
  false,
58062
58087
  );
58063
58088
  this.scene.add(item1Line);
@@ -58108,10 +58133,7 @@ class Tools {
58108
58133
  */
58109
58134
  enable(toolType) {
58110
58135
  // Disable the currently enabled tool (if any)
58111
- if (this.enabledTool) {
58112
- this.viewer.display.shapeFilterDropDownMenu.reset();
58113
- this._disable();
58114
- }
58136
+ this.disable();
58115
58137
 
58116
58138
  switch (toolType) {
58117
58139
  case ToolTypes.DISTANCE:
@@ -58130,6 +58152,13 @@ class Tools {
58130
58152
  this.enabledTool = toolType;
58131
58153
  }
58132
58154
 
58155
+ disable() {
58156
+ if (this.enabledTool) {
58157
+ this.viewer.display.shapeFilterDropDownMenu.reset();
58158
+ this._disable();
58159
+ }
58160
+ }
58161
+
58133
58162
  /**
58134
58163
  * Disables the currently enabled tool.
58135
58164
  */
@@ -58955,6 +58984,7 @@ class Display {
58955
58984
  !["distance", "properties", "angle"].includes(this.currentButton)
58956
58985
  ) {
58957
58986
  this.viewer.toggleGroup(true);
58987
+ this.viewer.toggleTab(true);
58958
58988
  }
58959
58989
  this.viewer.setRaycastMode(flag);
58960
58990
  this.shapeFilterDropDownMenu.setRaycaster(this.viewer.raycaster);
@@ -58973,6 +59003,7 @@ class Display {
58973
59003
  } else {
58974
59004
  if (this.currentButton == name || name == "explode") {
58975
59005
  this.viewer.toggleGroup(false);
59006
+ this.viewer.toggleTab(false);
58976
59007
  this.currentButton = null;
58977
59008
  }
58978
59009
  this.viewer.checkChanges({ activeTool: ToolTypes.NONE });
@@ -64630,33 +64661,33 @@ class Controls {
64630
64661
  const defaultDirections = {
64631
64662
  y_up: {
64632
64663
  // compatible to fusion 360
64633
- iso: { pos: new Vector3(1, 1, 1), z_rot: 0 },
64634
- front: { pos: new Vector3(0, 0, 1), z_rot: 0 },
64635
- rear: { pos: new Vector3(0, 0, -1), z_rot: 0 },
64636
- left: { pos: new Vector3(-1, 0, 0), z_rot: 0 },
64637
- right: { pos: new Vector3(1, 0, 0), z_rot: 0 },
64638
- top: { pos: new Vector3(0, 1, 0), z_rot: 0 },
64639
- bottom: { pos: new Vector3(0, -1, 0), z_rot: 0 },
64664
+ iso: { pos: new Vector3(1, 1, 1), quat: null },
64665
+ front: { pos: new Vector3(0, 0, 1), quat: null },
64666
+ rear: { pos: new Vector3(0, 0, -1), quat: null },
64667
+ left: { pos: new Vector3(-1, 0, 0), quat: null },
64668
+ right: { pos: new Vector3(1, 0, 0), quat: null },
64669
+ top: { pos: new Vector3(0, 1, 0), quat: null },
64670
+ bottom: { pos: new Vector3(0, -1, 0), quat: null },
64640
64671
  },
64641
64672
  z_up: {
64642
64673
  // compatible to FreeCAD, OnShape
64643
- iso: { pos: new Vector3(1, -1, 1), z_rot: 0 },
64644
- front: { pos: new Vector3(0, -1, 0), z_rot: 0 },
64645
- rear: { pos: new Vector3(0, 1, 0), z_rot: 0 },
64646
- left: { pos: new Vector3(-1, 0, 0), z_rot: 0 },
64647
- right: { pos: new Vector3(1, 0, 0), z_rot: 0 },
64648
- top: { pos: new Vector3(0, 0, 1), z_rot: -Math.PI / 2 },
64649
- bottom: { pos: new Vector3(0, 0, -1), z_rot: -Math.PI / 2 },
64674
+ iso: { pos: new Vector3(1, -1, 1), quat: null },
64675
+ front: { pos: new Vector3(0, -1, 0), quat: null },
64676
+ rear: { pos: new Vector3(0, 1, 0), quat: null },
64677
+ left: { pos: new Vector3(-1, 0, 0), quat: null },
64678
+ right: { pos: new Vector3(1, 0, 0), quat: null },
64679
+ top: { pos: new Vector3(0, 0, 1), quat: [0, 0, 0, 1] },
64680
+ bottom: { pos: new Vector3(0, 0, -1), quat: [1, 0, 0, 0] },
64650
64681
  },
64651
64682
  legacy: {
64652
64683
  // legacy Z up
64653
- iso: { pos: new Vector3(1, 1, 1), z_rot: 0 },
64654
- front: { pos: new Vector3(1, 0, 0), z_rot: 0 },
64655
- rear: { pos: new Vector3(-1, 0, 0), z_rot: 0 },
64656
- left: { pos: new Vector3(0, 1, 0), z_rot: 0 },
64657
- right: { pos: new Vector3(0, -1, 0), z_rot: 0 },
64658
- top: { pos: new Vector3(0, 0, 1), z_rot: 0 },
64659
- bottom: { pos: new Vector3(0, 0, -1), z_rot: 0 },
64684
+ iso: { pos: new Vector3(1, 1, 1), quat: null },
64685
+ front: { pos: new Vector3(1, 0, 0), quat: null },
64686
+ rear: { pos: new Vector3(-1, 0, 0), quat: null },
64687
+ left: { pos: new Vector3(0, 1, 0), quat: null },
64688
+ right: { pos: new Vector3(0, -1, 0), quat: null },
64689
+ top: { pos: new Vector3(0, 0, 1), quat: null },
64690
+ bottom: { pos: new Vector3(0, 0, -1), quat: null },
64660
64691
  },
64661
64692
  };
64662
64693
 
@@ -64837,13 +64868,9 @@ class Camera {
64837
64868
  // For the default directions quaternion can be ignored, it will be reset automatically
64838
64869
  this.setupCamera(true, defaultDirections[this.up][dir].pos, null, zoom);
64839
64870
  this.lookAtTarget();
64840
- if (defaultDirections[this.up][dir].z_rot != 0) {
64841
- var quaternion = new Quaternion();
64842
- quaternion.setFromAxisAngle(
64843
- new Vector3(0, 0, 1),
64844
- defaultDirections[this.up][dir].z_rot,
64845
- );
64846
- quaternion.multiply(this.getQuaternion());
64871
+
64872
+ if (defaultDirections[this.up][dir].quat != null) {
64873
+ var quaternion = defaultDirections[this.up][dir].quat;
64847
64874
  this.setQuaternion(quaternion);
64848
64875
  }
64849
64876
  }
@@ -64958,7 +64985,7 @@ class Camera {
64958
64985
  }
64959
64986
  }
64960
64987
 
64961
- const version = "3.2.2";
64988
+ const version = "3.3.0";
64962
64989
 
64963
64990
  Mesh.prototype.dispose = function () {
64964
64991
  if (this.geometry) {
@@ -65872,10 +65899,20 @@ class Viewer {
65872
65899
  }
65873
65900
 
65874
65901
  this.display.setExplodeCheck(false);
65902
+ this.display.setExplode("", false);
65875
65903
 
65876
65904
  // clear render canvas
65877
65905
  this.renderer.clear();
65878
65906
 
65907
+ // deselect measurement tools
65908
+ if (this.cadTools) {
65909
+ this.cadTools.disable();
65910
+ if (this.display.currentButton != null) {
65911
+ this.display.toolbarButtons[this.display.currentButton].set(false);
65912
+ this.display.setTool(this.display.currentButton, false);
65913
+ }
65914
+ }
65915
+
65879
65916
  // dispose scene
65880
65917
 
65881
65918
  for (var i in this.scene.children) {
@@ -66037,7 +66074,16 @@ class Viewer {
66037
66074
  this.display.addCadTree(t);
66038
66075
  this.treeview.render();
66039
66076
  timer.split("rendered tree");
66077
+ timer.stop();
66078
+ }
66040
66079
 
66080
+ /**
66081
+ * Toggle tab and ensure collaps is treated correctly
66082
+ * Needs to be called in sync with toggleGroup!
66083
+ * @param boolean disable - true to disable clipping tab
66084
+ */
66085
+ toggleTab(disable) {
66086
+ var timer = new Timer("toggleTab", this.timeit);
66041
66087
  this.display.selectTabByName("tree");
66042
66088
  timer.split("collapse tree");
66043
66089
  switch (this.collapse) {
@@ -66058,7 +66104,7 @@ class Viewer {
66058
66104
  this.checkChanges({ states: this.getStates() }, true);
66059
66105
  timer.split("notify state changes");
66060
66106
  timer.stop();
66061
- this.display.toggleClippingTab(!expanded);
66107
+ this.display.toggleClippingTab(!disable);
66062
66108
  }
66063
66109
 
66064
66110
  /**
@@ -66298,6 +66344,8 @@ class Viewer {
66298
66344
 
66299
66345
  this.clipping.setVisible(false);
66300
66346
 
66347
+ this.toggleTab(false);
66348
+
66301
66349
  this.display.metalnessSlider.setValue(this.metalness * 100);
66302
66350
  this.display.roughnessSlider.setValue(this.roughness * 100);
66303
66351
  this.display.ambientlightSlider.setValue(this.ambientIntensity * 100);
@@ -66806,7 +66854,9 @@ class Viewer {
66806
66854
  );
66807
66855
  this.raycaster.init();
66808
66856
  } else {
66809
- this.raycaster.dispose();
66857
+ if (this.raycaster) {
66858
+ this.raycaster.dispose();
66859
+ }
66810
66860
  this.raycaster = null;
66811
66861
  }
66812
66862
  }