Skip to content

Commit 4b77ff0

Browse files
author
North101
committed
Add replicad-script to add types sepcific to replicad .js/.ts files
1 parent 24b0570 commit 4b77ff0

File tree

7 files changed

+256
-0
lines changed

7 files changed

+256
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
yarn-error.log
3+
dist

packages/replicad-script/LICENSE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2023 QuaroTech Sàrl
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

packages/replicad-script/README.md

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
# `replicad`
2+
3+
The library to build browser based 3D models with code.
4+
5+
## How to play with it
6+
7+
I am still working on making an accessible toolchain for replicad, so for now
8+
you can play with my [online
9+
"visualiser"](https://studio.replicad.xyz/visualiser).
10+
11+
Write in a local file, select it in the tool and it should build your model.
12+
There is still a lot of work to make this intuitive, but you can play with the
13+
examples below. Note that if you use Chrome the model is recomputed as you
14+
change your local file.
15+
16+
You need to write a "main" function that receives the complete `replicad`
17+
library.
18+
19+
## Examples
20+
21+
Here are some basic examples of things you can do with replicad.
22+
23+
#### A simple example
24+
25+
```js
26+
const main = ({ Sketcher }) => {
27+
const base = new Sketcher("XZ")
28+
.hLine(-25)
29+
.halfEllipse(0, 40, 5, true)
30+
.hLine(25)
31+
.close()
32+
.revolve([0, 0, 1]);
33+
34+
const hole = new Sketcher()
35+
.quadraticBezierCurveTo([0, 20], [20, 30])
36+
.closeWithMirror()
37+
.extrude(40)
38+
.translateY(-12);
39+
40+
return base.cut(hole);
41+
};
42+
```
43+
44+
#### The open cascade bottle
45+
46+
```js
47+
const defaultParams = {
48+
width: 50,
49+
height: 70,
50+
thickness: 30,
51+
};
52+
53+
const main = (
54+
{ Sketcher, FaceSketcher, localGC, makeCylinder, makeOffset, FaceFinder },
55+
{ width: myWidth, height: myHeight, thickness: myThickness }
56+
) => {
57+
let shape = new Sketcher()
58+
.movePointerTo([-myWidth / 2, 0])
59+
.vLine(-myThickness / 4)
60+
.threePointsArc(myWidth, 0, myWidth / 2, -myThickness / 4)
61+
.vLine(myThickness / 4)
62+
.closeWithMirror()
63+
.extrude(myHeight)
64+
.fillet(myThickness / 12);
65+
66+
const myNeckRadius = myThickness / 4;
67+
const myNeckHeight = myHeight / 10;
68+
const neck = makeCylinder(
69+
myNeckRadius,
70+
myNeckHeight,
71+
[0, 0, myHeight],
72+
[0, 0, 1]
73+
);
74+
75+
shape = shape.fuse(neck);
76+
77+
shape = shape.shell({
78+
filter: new FaceFinder().inPlane("XY", [0, 0, myHeight + myNeckHeight]),
79+
thickness: myThickness / 50,
80+
});
81+
82+
const [r, gc] = localGC();
83+
84+
const neckFace = r(
85+
new FaceFinder()
86+
.containsPoint([0, myNeckRadius, myHeight])
87+
.ofSurfaceType("CYLINDRE")
88+
.find(shape.clone(), { unique: true })
89+
);
90+
91+
const bottomThreadFace = r(
92+
r(makeOffset(neckFace, -0.01 * myNeckRadius)).faces[0]
93+
);
94+
const baseThreadSketch = new FaceSketcher(bottomThreadFace)
95+
.movePointerTo([0, 0.25])
96+
.halfEllipse(2, 0.5, 0.1)
97+
.close();
98+
99+
const topThreadFace = r(
100+
r(makeOffset(neckFace, 0.05 * myNeckRadius)).faces[0]
101+
);
102+
const topThreadSketch = new FaceSketcher(topThreadFace)
103+
.movePointerTo([0, 0.25])
104+
.halfEllipse(2, 0.5, 0.05)
105+
.close();
106+
107+
const thread = baseThreadSketch.loftWith(topThreadSketch);
108+
gc();
109+
110+
return shape.fuse(thread);
111+
};
112+
```
113+
114+
## Making in work in an application
115+
116+
You will need to load opencascadejs separately (you can use
117+
`replicad-opencascadejs` to only have the method necessary for replicad). You
118+
then need to inject it in `replicad` with the function:
119+
120+
`setOC(oc)`
121+
122+
Once this is done you can use `replicad` to draw your shapes.
123+
124+
To export your shape, you can either use the `STL` or `STEP` save functions, or
125+
get the mesh and use it with three (or another 3d viewer library).
126+
127+
Ideally the computation should be done in a worker.
128+
129+
This all needs a bunch of helper, this should come soon(-ish)
130+
131+
## License
132+
133+
Copyright (C) 2021 QuaroTech Sàrl
134+
135+
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
136+
137+
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
138+
139+
You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
"name": "replicad-script",
3+
"version": "0.17.4",
4+
"description": "The library to build browser based 3D models with code",
5+
"keywords": [
6+
"cad",
7+
"opencascadejs",
8+
"brep",
9+
"STEP",
10+
"STL"
11+
],
12+
"author": "Steve Genoud <steve@sgenoud.com>",
13+
"homepage": "https://replicad.xyz",
14+
"license": "MIT",
15+
"type": "module",
16+
"main": "./dist/replicad-script.cjs",
17+
"module": "./dist/replicad-script.js",
18+
"types": "./dist/replicad-script.d.ts",
19+
"exports": {
20+
".": {
21+
"import": "./dist/replicad-script.js",
22+
"require": "./dist/replicad-script.umd.cjs"
23+
}
24+
},
25+
"directories": {
26+
"src": "src",
27+
"test": "__tests__"
28+
},
29+
"files": [
30+
"dist"
31+
],
32+
"scripts": {
33+
"build": "vite build",
34+
"start": "NO_TYPES=true vite build --watch",
35+
"build-doc": "typedoc --exclude \"../replicad-opencascadejs/*\" --out docs --theme default src",
36+
"build-docss": "typedoc --help",
37+
"test": "vitest"
38+
},
39+
"devDependencies": {
40+
"@typescript-eslint/eslint-plugin": "^5.2.0",
41+
"@typescript-eslint/parser": "^5.4.0",
42+
"eslint": "^8.1.0",
43+
"replicad": "^0.17.4",
44+
"rollup": "3.28.0",
45+
"rollup-plugin-ts": "3.4.4",
46+
"typedoc": "^0.24.8",
47+
"typescript": "^5.1.6",
48+
"vite": "^4.0.4",
49+
"vite-plugin-dts": "^3.5.2",
50+
"vitest": "^0.28.3"
51+
},
52+
"dependencies": {}
53+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import * as _replicad from "replicad";
2+
3+
export type ShapeConfig = {
4+
shape: _replicad.AnyShape;
5+
name?: string;
6+
color?: string;
7+
opacity?: number;
8+
};
9+
10+
export type ShapesConfig =
11+
| _replicad.AnyShape
12+
| ShapeConfig
13+
| (_replicad.AnyShape | ShapeConfig)[];
14+
15+
declare global {
16+
export const replicad: typeof _replicad;
17+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"compilerOptions": {
3+
"moduleResolution": "node",
4+
"target": "es6",
5+
"module": "es2015",
6+
"strict": true,
7+
"lib": ["es2022", "dom"],
8+
"preserveSymlinks": true,
9+
"sourceMap": true,
10+
"skipLibCheck": true,
11+
"declaration": true,
12+
"allowSyntheticDefaultImports": true
13+
},
14+
"include": ["./src"]
15+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { resolve } from "path";
2+
import { defineConfig } from "vite";
3+
import dts from "vite-plugin-dts";
4+
5+
export default defineConfig({
6+
build: {
7+
lib: {
8+
entry: resolve(__dirname, "src/replicad-script.ts"),
9+
name: "replicad-script",
10+
fileName: "replicad-script",
11+
formats: ["es", "umd", "cjs"],
12+
},
13+
sourcemap: true,
14+
minify: false,
15+
},
16+
plugins: [
17+
dts(),
18+
],
19+
test: {
20+
setupFiles: ["./__tests__/setup.ts"],
21+
},
22+
});

0 commit comments

Comments
 (0)