Skip to content

Commit 75ec076

Browse files
committed
feat: import SVG and STL format
1 parent 7ef7173 commit 75ec076

7 files changed

Lines changed: 702 additions & 21 deletions

File tree

CLAUDE.md

Lines changed: 100 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ CSG Builder: TypeScript-based 3D mesh creation using component architecture. Use
88

99
## Learning Resources
1010

11-
### **projects/examples/** - Tutorial Series (A-M)
11+
### **projects/examples/** - Tutorial Series (A-N)
1212

1313
Progressive examples with comprehensive inline comments:
1414

1515
**Foundational (A-G):** Primitives, operations, alignment, partials, composition, custom profiles, revolution
16-
**Advanced (H-M):** Scaling, transforms, 3D grids, patterns, optimization, production composition
16+
**Advanced (H-N):** Scaling, transforms, 3D grids, patterns, optimization, production composition, import capabilities
1717

18-
**Start with A-G for basics, then H-M for advanced patterns.**
18+
**Start with A-G for basics, then H-N for advanced patterns.**
1919

2020
### **projects/castle/** - Production Architecture
2121

@@ -159,6 +159,99 @@ Solid.revolutionSolidFromPath([straight(5), curve(2, 90), ...], { angle?, color?
159159

160160
**Profile coordinates:** X=radius from center, Y=height, rotates around Y-axis
161161

162+
## Import Capabilities
163+
164+
### STL Import
165+
166+
Load external STL files (binary or ASCII format) as Solid components:
167+
168+
```typescript
169+
// Import STL file using Vite's import syntax
170+
import stlData from './model.stl?raw'; // ASCII STL
171+
// For binary STL, use ?url and fetch as ArrayBuffer
172+
173+
// Create Solid from STL data
174+
const imported = Solid.fromSTL(stlData, { color: 'blue' });
175+
176+
// Use in boolean operations
177+
const cube = Solid.cube(20, 20, 20, { color: 'red' });
178+
const result = Solid.SUBTRACT(cube, imported);
179+
180+
// Transform imported geometry
181+
const transformed = Solid.fromSTL(stlData, { color: 'green' })
182+
.scale({ all: 0.5 })
183+
.rotate({ y: 45 })
184+
.move({ y: 10 });
185+
```
186+
187+
**Key points:**
188+
189+
- Supports both binary and ASCII STL formats
190+
- Automatically normalized for CSG operations
191+
- Works with all standard Solid methods (transforms, CSG, grids)
192+
- Uses STLLoader from Three.js (no additional dependencies)
193+
194+
### SVG Path Import
195+
196+
Import SVG path data and extrude into 3D profiles:
197+
198+
```typescript
199+
// Simple SVG rectangle path
200+
const rectPath = 'M 0 0 L 20 0 L 20 10 L 0 10 Z';
201+
const rect = Solid.profilePrismFromSVG(rectPath, 5, { color: 'blue' });
202+
203+
// Complex path with curves (Q = quadratic bezier) - creates wavy pattern
204+
const curvedPath = 'M 0 5 Q 5 0, 10 5 Q 15 10, 20 5 L 20 10 L 0 10 Z';
205+
const curved = Solid.profilePrismFromSVG(curvedPath, 8, { color: 'pink' });
206+
207+
// Star shape
208+
const starPath = 'M 10 0 L 12 8 L 20 8 L 14 13 L 16 21 L 10 16 L 4 21 L 6 13 L 0 8 L 8 8 Z';
209+
const star = Solid.profilePrismFromSVG(starPath, 3, { color: 'gold' });
210+
211+
// Use in boolean operations
212+
const plate = Solid.cube(30, 20, 5, { color: 'gray' });
213+
const result = Solid.SUBTRACT(plate, star);
214+
```
215+
216+
**SVG Path Commands Supported:**
217+
218+
- M/m - Move to
219+
- L/l - Line to
220+
- H/h - Horizontal line
221+
- V/v - Vertical line
222+
- C/c - Cubic bezier curve
223+
- Q/q - Quadratic bezier curve
224+
- A/a - Arc
225+
- Z/z - Close path
226+
227+
**Key points:**
228+
229+
- Extrudes SVG path along Y-axis
230+
- Handles SVG coordinate system (Y-down → Y-up conversion)
231+
- Works with all CSG operations
232+
- Perfect for logos, custom profiles, 2D designs
233+
- Uses SVGLoader from Three.js (no additional dependencies)
234+
235+
### Boolean Operations with Imports
236+
237+
All imported geometries work seamlessly with CSG operations:
238+
239+
```typescript
240+
// STL + Primitive
241+
const stl = Solid.fromSTL(stlData, { color: 'red' });
242+
const cube = Solid.cube(20, 20, 20, { color: 'red' });
243+
const combined = Solid.UNION(stl, cube);
244+
245+
// SVG + STL
246+
const svg = Solid.profilePrismFromSVG(path, 5, { color: 'blue' });
247+
const stl = Solid.fromSTL(data, { color: 'blue' });
248+
const result = Solid.SUBTRACT(stl, svg);
249+
250+
// Grid from imports
251+
const imported = Solid.fromSTL(data, { color: 'purple' });
252+
const grid = Solid.GRID_XY(imported, { cols: 3, rows: 3, spacing: [5, 5] });
253+
```
254+
162255
## Component Patterns
163256

164257
### Basic Component
@@ -234,6 +327,10 @@ const w3 = Wall(30); // Different params, new computation
234327

235328
`cube(w,h,d,opts)`, `cylinder(r,h,opts)`, `sphere(r,opts)`, `cone(r,h,opts)`, `prism(sides,r,h,opts)`, `trianglePrism(r,h,opts)`
236329

330+
### Import Methods
331+
332+
`fromSTL(data,opts)`, `profilePrismFromSVG(svgPathData,height,opts)`
333+
237334
### Custom Profiles
238335

239336
`profilePrism(h,builder,opts)`, `profilePrismFromPoints(h,pts,opts)`, `profilePrismFromPath(h,segs,opts)`

README.md

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ CSG Builder allows you to create 3D meshes using TypeScript code with a React-li
1616
- **Primitive Shapes** - Cubes, cylinders, spheres, cones, and polygon prisms with customizable dimensions
1717
- **Custom Profile Prisms** - Extrude 2D profiles into 3D shapes using points, paths, or Shape API
1818
- **Body of Revolution** - Create rotationally symmetric objects (chess pieces, vases, bottles) by rotating profiles
19+
- **Import Capabilities** - Import STL files and SVG paths, use them in CSG operations like any primitive
1920
- **Partial Geometries** - Create pie slices, hemispheres, and wedges with CSG-based angle cutting
2021
- **Static CSG Operations** - Immutable SUBTRACT, UNION, INTERSECT, and MERGE operations for complex geometries
2122
- **Grid Arrays** - Create 1D, 2D, and 3D arrays with static GRID_X, GRID_XY, GRID_XYZ methods
@@ -288,6 +289,55 @@ const quarterVase = Solid.revolutionSolidFromPoints(
288289
**Profile coords:** X=radius, Y=height. Rotates around Y-axis.
289290
**Uses:** Chess pieces, vases, bottles, tableware, architectural elements
290291

292+
### Example: Import Capabilities
293+
294+
Load external STL files and SVG paths to use in your designs:
295+
296+
```typescript
297+
import { Solid } from '$lib/3d/Solid';
298+
299+
// Import STL file using Vite's import syntax
300+
import stlData from './model.stl?raw'; // ASCII STL
301+
// For binary STL, use ?url and fetch as ArrayBuffer
302+
303+
// 1. STL IMPORT - Load external 3D models
304+
const imported = Solid.fromSTL(stlData, { color: 'blue' });
305+
306+
// Use imported geometry in CSG operations
307+
const cube = Solid.cube(20, 20, 20, { color: 'red' });
308+
const result = Solid.SUBTRACT(cube, imported);
309+
310+
// Transform imported models
311+
const transformed = Solid.fromSTL(stlData, { color: 'green' })
312+
.scale({ all: 0.5 })
313+
.rotate({ y: 45 })
314+
.move({ y: 10 });
315+
316+
// 2. SVG PATH IMPORT - Extrude SVG paths into 3D
317+
const starPath = 'M 10 0 L 12 8 L 20 8 L 14 13 L 16 21 L 10 16 L 4 21 L 6 13 L 0 8 L 8 8 Z';
318+
const star = Solid.profilePrismFromSVG(starPath, 3, { color: 'gold' });
319+
320+
// Curved SVG paths with quadratic bezier curves (Q)
321+
const curvedPath = 'M 0 5 Q 5 0, 10 5 Q 15 10, 20 5 L 20 10 L 0 10 Z';
322+
const curved = Solid.profilePrismFromSVG(curvedPath, 8, { color: 'pink' });
323+
324+
// Use SVG as cutter
325+
const plate = Solid.cube(30, 20, 5, { color: 'gray' });
326+
const cutout = Solid.SUBTRACT(plate, star);
327+
328+
// 3. COMBINE IMPORTS - Mix imported and primitive shapes
329+
const base = Solid.fromSTL(stlData, { color: 'brown' }).align('bottom');
330+
const decoration = Solid.profilePrismFromSVG('M 0 0 L 5 0 L 5 5 L 0 5 Z', 2, { color: 'brown' });
331+
const combined = Solid.UNION(base, decoration);
332+
```
333+
334+
**Supported formats:**
335+
336+
- **STL:** Binary and ASCII formats (via Three.js STLLoader)
337+
- **SVG:** Path data with M, L, C, Q, A commands (via Three.js SVGLoader)
338+
- Works with all CSG operations, transformations, and grids
339+
- No additional dependencies required
340+
291341
## Performance Optimization
292342

293343
Cache expensive component computations that are called repeatedly:
@@ -322,6 +372,7 @@ const w3 = Wall(30); // Different params, new computation
322372
### API Reference
323373

324374
**Primitives:** `cube(w,h,d,opts)`, `cylinder(r,h,opts)`, `sphere(r,opts)`, `cone(r,h,opts)`, `prism(sides,r,h,opts)`, `trianglePrism(r,h,opts)`
375+
**Import:** `fromSTL(data,opts)`, `profilePrismFromSVG(svgPathData,height,opts)`
325376
**Profiles:** `profilePrism(h,builder,opts)`, `profilePrismFromPoints(h,points,opts)`, `profilePrismFromPath(h,segments,opts)`
326377
**Revolution:** `revolutionSolid(builder,opts)`, `revolutionSolidFromPoints(points,opts)`, `revolutionSolidFromPath(segments,opts)`
327378
**Path Factories:** `straight(length)`, `curve(radius,angle)` - positive=right, negative=left, 0=sharp
@@ -473,16 +524,16 @@ npm run export --silent -- "Brick Wall" > wall.stl
473524

474525
## Examples & Learning Resources
475526

476-
**projects/examples/** - Progressive tutorial (A-M) with inline docs:
527+
**projects/examples/** - Progressive tutorial (A-N) with inline docs:
477528

478529
- **A-G:** Primitives, operations, alignment, partials, composition, profiles, revolution
479-
- **H-M:** Scaling, transforms, 3D grids, patterns, optimization, production composition
530+
- **H-N:** Scaling, transforms, 3D grids, patterns, optimization, production composition, import capabilities
480531

481532
**projects/castle/** - Production multi-file architecture: modular structure, caching, cross-file dependencies, advanced CSG
482533

483534
**projects/sample/** - Working examples: box, brick wall, window, shapes showcase, chess pieces
484535

485-
Start with examples A-G, then explore castle project for production patterns.
536+
Start with examples A-G, then explore H-N for advanced features including STL/SVG imports.
486537

487538
## Troubleshooting
488539

plan.MD

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,3 @@ Analysis of potential improvements and missing features for the CSG Builder proj
1515
- **CHAMFER/FILLET** - Edge rounding (critical for manufacturing)
1616
- **Circular Array** - Polar patterns (currently only XYZ grids)
1717
- **OFFSET** - Grow/shrink with clearances
18-
19-
### 3. Import Capabilities (Currently None)
20-
21-
- Import STL/OBJ as components
22-
- Import SVG paths for profile extrusion
23-
- Boolean operations with imported meshes

0 commit comments

Comments
 (0)