File size: 6,248 Bytes
ec3a66b
76a620b
 
 
 
 
 
 
ec3a66b
 
 
 
 
76a620b
 
 
 
ec3a66b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76a620b
 
 
 
 
 
 
 
ec3a66b
 
76a620b
 
 
 
 
ec3a66b
 
 
 
 
 
 
 
76a620b
 
 
 
 
ec3a66b
76a620b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec3a66b
 
76a620b
 
 
 
 
 
 
 
 
ec3a66b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206

let scene, camera, renderer, objects = [];
let activeTool = null;
let sketchPlane = null;
let sketchPoints = [];
let history = [];
let historyIndex = -1;
let gridHelper;
function init() {
    // Scene setup
    scene = new THREE.Scene();
    scene.background = new THREE.Color(0xecf0f1);
    
    // Add grid helper
    gridHelper = new THREE.GridHelper(20, 20, 0x888888, 0xcccccc);
    scene.add(gridHelper);
// Camera setup
    camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
    camera.position.z = 5;
    
    // Renderer setup
    renderer = new THREE.WebGLRenderer({ antialias: true });
    renderer.setSize(window.innerWidth, window.innerHeight);
    document.getElementById('canvas-container').appendChild(renderer.domElement);
    
    // Lights
    const ambientLight = new THREE.AmbientLight(0x404040);
    scene.add(ambientLight);
    
    const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
    directionalLight.position.set(1, 1, 1);
    scene.add(directionalLight);
    
    // Controls
    const controls = new THREE.OrbitControls(camera, renderer.domElement);
    // Event listeners
    document.getElementById('sketch-mode').addEventListener('click', () => setActiveTool('sketch'));
    document.getElementById('extrude-mode').addEventListener('click', () => setActiveTool('extrude'));
    document.getElementById('revolve-mode').addEventListener('click', () => setActiveTool('revolve'));
    document.getElementById('dimension-tool').addEventListener('click', () => setActiveTool('dimension'));
    document.getElementById('fillet-tool').addEventListener('click', () => setActiveTool('fillet'));
    document.getElementById('chamfer-tool').addEventListener('click', () => setActiveTool('chamfer'));
    document.getElementById('undo').addEventListener('click', undo);
    document.getElementById('redo').addEventListener('click', redo);
    document.getElementById('clear-all').addEventListener('click', clearScene);
    
    // Add mouse event listeners
    renderer.domElement.addEventListener('mousedown', onMouseDown);
    renderer.domElement.addEventListener('mousemove', onMouseMove);
    renderer.domElement.addEventListener('mouseup', onMouseUp);
// Window resize handler
    window.addEventListener('resize', () => {
        camera.aspect = window.innerWidth / window.innerHeight;
        camera.updateProjectionMatrix();
        renderer.setSize(window.innerWidth, window.innerHeight);
    });
    
    animate();
}
function setActiveTool(tool) {
    activeTool = tool;
    updateStatus(`Tool: ${tool.charAt(0).toUpperCase() + tool.slice(1)}`);
    updatePropertiesPanel();
}

function onMouseDown(event) {
    if (!activeTool) return;
    
    const mouse = getNormalizedMousePosition(event);
    const raycaster = new THREE.Raycaster();
    raycaster.setFromCamera(mouse, camera);
    
    switch(activeTool) {
        case 'sketch':
            // Create sketch plane logic
            break;
        case 'extrude':
            // Extrude selected sketch logic
            break;
        case 'dimension':
            // Add dimension logic
            break;
    }
}

function onMouseMove(event) {
    // Handle mouse move for tools
}

function onMouseUp(event) {
    // Handle mouse up for tools
}

function getNormalizedMousePosition(event) {
    return {
        x: (event.clientX / window.innerWidth) * 2 - 1,
        y: -(event.clientY / window.innerHeight) * 2 + 1
    };
}

function updateStatus(message) {
    document.getElementById('status-message').textContent = message;
}

function updatePropertiesPanel() {
    const panel = document.getElementById('properties-content');
    panel.innerHTML = '';
    
    if (!activeTool) return;
    
    const div = document.createElement('div');
    div.innerHTML = `
        <h4>${activeTool.charAt(0).toUpperCase() + activeTool.slice(1)} Settings</h4>
        ${getToolPropertiesHTML(activeTool)}
    `;
    panel.appendChild(div);
}

function getToolPropertiesHTML(tool) {
    switch(tool) {
        case 'sketch':
            return `
                <div class="property-group">
                    <label>Plane</label>
                    <select>
                        <option>XY</option>
                        <option>XZ</option>
                        <option>YZ</option>
                    </select>
                </div>
            `;
        case 'extrude':
            return `
                <div class="property-group">
                    <label>Distance (mm)</label>
                    <input type="number" value="10" min="1">
                </div>
            `;
        // Add other tools properties
        default:
            return '';
    }
}

function saveState() {
    history = history.slice(0, historyIndex + 1);
    history.push(JSON.stringify(scene.toJSON()));
    historyIndex++;
}

function undo() {
    if (historyIndex <= 0) return;
    historyIndex--;
    loadState();
}

function redo() {
    if (historyIndex >= history.length - 1) return;
    historyIndex++;
    loadState();
}

function loadState() {
    const state = history[historyIndex];
    scene = new THREE.Scene();
    const loader = new THREE.ObjectLoader();
    loader.parse(JSON.parse(state), function(obj) {
        scene = obj;
        renderer.render(scene, camera);
    });
}
function createSphere() {
    const geometry = new THREE.SphereGeometry(0.5, 32, 32);
    const material = new THREE.MeshStandardMaterial({ 
        color: Math.random() * 0xffffff,
        metalness: 0.3,
        roughness: 0.7
    });
    const sphere = new THREE.Mesh(geometry, material);
    scene.add(sphere);
    objects.push(sphere);
}

function createCylinder() {
    const geometry = new THREE.CylinderGeometry(0.5, 0.5, 1, 32);
    const material = new THREE.MeshStandardMaterial({ 
        color: Math.random() * 0xffffff,
        metalness: 0.4,
        roughness: 0.6
    });
    const cylinder = new THREE.Mesh(geometry, material);
    scene.add(cylinder);
    objects.push(cylinder);
}

function clearScene() {
    objects.forEach(obj => scene.remove(obj));
    objects = [];
}

function animate() {
    requestAnimationFrame(animate);
    renderer.render(scene, camera);
}

window.onload = init;