-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlength-measurement.js
More file actions
159 lines (137 loc) · 4.09 KB
/
length-measurement.js
File metadata and controls
159 lines (137 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import * as THREE from 'three';
import * as OBCFront from '@thatopen/components-front';
export class LengthMeasurementManager {
constructor(components, world, container) {
this.components = components;
this.world = world;
this.container = container;
this.measurer = null;
this.measurerReady = false;
this.enabled = false;
}
init() {
console.log('Initializing Length Measurement...');
try {
// Get the LengthMeasurement component from the front module
const LengthMeasurement = OBCFront.LengthMeasurement;
if (!LengthMeasurement) {
console.warn('LengthMeasurement not found in OBCFront');
console.warn('Available components:', Object.keys(OBCFront).slice(0, 20));
return false;
}
// Create the measurer instance
this.measurer = new LengthMeasurement(this.components);
this.measurer.world = this.world;
this.measurer.enabled = false; // Start disabled
this.measurer.color.set(0x494cb6); // Set measurement color
// Setup double-click to create measurements
if (this.container) {
this.container.ondblclick = () => this.create();
} else {
// Fallback to canvas element
const canvas = document.querySelector('canvas');
if (canvas) {
canvas.addEventListener('dblclick', () => this.create());
}
}
// Setup keyboard shortcuts
document.addEventListener('keydown', (e) => this.handleKeyPress(e));
this.measurerReady = true;
console.log('Length Measurement initialized successfully');
return true;
} catch (error) {
console.warn('Could not initialize Length Measurement:', error.message);
this.measurerReady = false;
return false;
}
}
create() {
if (!this.measurer || !this.enabled) return;
try {
this.measurer.create();
console.log('Length measurement created');
} catch (error) {
console.error('Error creating measurement:', error);
}
}
handleKeyPress(event) {
if (!this.measurer || !this.enabled) return;
// Delete measurement on Delete or Backspace
if (event.key === 'Delete' || event.key === 'Backspace') {
try {
this.measurer.delete();
console.log('Measurement deleted');
} catch (error) {
console.warn('Could not delete measurement:', error);
}
}
// Enter to complete measurement
if (event.key === 'Enter') {
try {
this.measurer.endCreation();
console.log('Measurement completed');
} catch (error) {
console.warn('Could not complete measurement:', error);
}
}
}
// Toggle measurement tool on/off
toggle(enabled) {
if (!this.measurer) {
console.warn('Length measurement not initialized');
return false;
}
this.enabled = enabled;
this.measurer.enabled = enabled;
console.log('Length Measurement:', enabled ? 'enabled' : 'disabled');
return true;
}
// Set measurement color
setColor(color) {
if (!this.measurer) {
console.warn('Measurer not initialized');
return false;
}
try {
this.measurer.color.setStyle(color);
console.log('Measurement color changed to:', color);
return true;
} catch (error) {
console.error('Error setting measurement color:', error);
return false;
}
}
// Clear all measurements
clearAll() {
if (!this.measurer) {
console.warn('Measurer not ready');
return false;
}
try {
this.measurer.deleteAll();
console.log('All measurements cleared');
return true;
} catch (error) {
console.error('Error clearing measurements:', error);
return false;
}
}
// Get measurements list
getMeasurements() {
if (!this.measurer) return [];
try {
return Array.from(this.measurer.list.values());
} catch (error) {
console.warn('Could not get measurements:', error);
return [];
}
}
// Get measurement info
getInfo() {
return {
ready: this.measurerReady,
enabled: this.enabled,
count: this.getMeasurements().length,
};
}
}