|
| 1 | +import initOpenCascade, { OpenCascadeInstance } from "bitbybit-occt/bitbybit-dev-occt/bitbybit-dev-occt"; |
| 2 | +import * as Inputs from "bitbybit-occt/lib/api/inputs"; |
| 3 | +import { CacheHelper } from "./cache-helper"; |
| 4 | +import { initializationComplete, onMessageInput } from "./occ-worker"; |
| 5 | + |
| 6 | +describe('OCCT wire unit tests', () => { |
| 7 | + let occt: OpenCascadeInstance; |
| 8 | + |
| 9 | + let cacheHelper: CacheHelper; |
| 10 | + beforeAll(async () => { |
| 11 | + occt = await initOpenCascade(); |
| 12 | + cacheHelper = initializationComplete(occt, true); |
| 13 | + }); |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + cacheHelper.cleanAllCache(); |
| 17 | + }); |
| 18 | + |
| 19 | + it('should', () => { |
| 20 | + expect(true).toBe(true); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should create a wire', async () => { |
| 24 | + const cdto = new Inputs.OCCT.CircleDto(1, [0, 0, 0], [0, 1, 0]); |
| 25 | + const wire = await callAction<Inputs.OCCT.CircleDto>('shapes.wire.createCircleWire', cdto); |
| 26 | + expect(wire.hash).toEqual(1646405596); |
| 27 | + expect(wire.type).toEqual('occ-shape'); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should get length of a wire', async () => { |
| 31 | + const cdto = new Inputs.OCCT.CircleDto(1, [0, 0, 0], [0, 1, 0]); |
| 32 | + const wire = await callAction<Inputs.OCCT.CircleDto>('shapes.wire.createCircleWire', cdto); |
| 33 | + expect(wire.hash).toEqual(1646405596); |
| 34 | + expect(wire.type).toEqual('occ-shape'); |
| 35 | + const length = await callAction<Inputs.OCCT.ShapeDto<Inputs.OCCT.TopoDSWirePointer>>('shapes.wire.getWireLength', { shape: wire }); |
| 36 | + expect(length).toBe(6.283185307179586); |
| 37 | + }); |
| 38 | + |
| 39 | + |
| 40 | + it('should create advanced loft through straight sections', async () => { |
| 41 | + const loft = await createLoft(callAction); |
| 42 | + expect(loft.hash).toBe(716684607); |
| 43 | + expect(loft.type).toBe('occ-shape'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should loft advanced and use cache', async () => { |
| 47 | + const loft1 = await createLoft(callAction); |
| 48 | + const loft2 = await createLoft(callAction); |
| 49 | + |
| 50 | + expect(loft1.hash).toBe(loft2.hash); |
| 51 | + expect(loft2.type).toBe('occ-shape'); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should subdivide wire into points', async () => { |
| 55 | + const cdto1 = new Inputs.OCCT.CircleDto(1, [0, 0, 0], [0, 1, 0]); |
| 56 | + const wire1 = await callAction<Inputs.OCCT.CircleDto>('shapes.wire.createCircleWire', cdto1); |
| 57 | + const divDto = new Inputs.OCCT.DivideDto(wire1, 10); |
| 58 | + const points = await callAction<Inputs.OCCT.DivideDto<Inputs.OCCT.TopoDSWirePointer>>('shapes.wire.divideWireByParamsToPoints', divDto); |
| 59 | + expect(points.length).toBe(11); |
| 60 | + expect(points).toEqual([ |
| 61 | + [0, 0, 1], |
| 62 | + [0.5877852522924731, 0, 0.8090169943749475], |
| 63 | + [0.9510565162951535, 0, 0.30901699437494745], |
| 64 | + [0.9510565162951536, 0, -0.30901699437494734], |
| 65 | + [0.5877852522924732, 0, -0.8090169943749473], |
| 66 | + [1.2246467991473532e-16, 0, -1], |
| 67 | + [-0.587785252292473, 0, -0.8090169943749475], |
| 68 | + [-0.9510565162951535, 0, -0.30901699437494756], |
| 69 | + [-0.9510565162951536, 0, 0.30901699437494723], |
| 70 | + [-0.5877852522924734, 0, 0.8090169943749473], |
| 71 | + [-2.4492935982947064e-16, 0, 1] |
| 72 | + ]); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should subdivide wire into points', async () => { |
| 76 | + const cdto1 = new Inputs.OCCT.CircleDto(1, [0, 0, 0], [0, 1, 0]); |
| 77 | + const wire1 = await callAction<Inputs.OCCT.CircleDto>('shapes.wire.createCircleWire', cdto1); |
| 78 | + const divDto = new Inputs.OCCT.DivideDto(wire1, 10); |
| 79 | + await callAction<Inputs.OCCT.DivideDto<Inputs.OCCT.TopoDSWirePointer>>('shapes.wire.divideWireByParamsToPoints', divDto); |
| 80 | + const points2 = await callAction<Inputs.OCCT.DivideDto<Inputs.OCCT.TopoDSWirePointer>>('shapes.wire.divideWireByParamsToPoints', divDto); |
| 81 | + expect(points2.length).toBe(11); |
| 82 | + expect(points2).toEqual([ |
| 83 | + [0, 0, 1], |
| 84 | + [0.5877852522924731, 0, 0.8090169943749475], |
| 85 | + [0.9510565162951535, 0, 0.30901699437494745], |
| 86 | + [0.9510565162951536, 0, -0.30901699437494734], |
| 87 | + [0.5877852522924732, 0, -0.8090169943749473], |
| 88 | + [1.2246467991473532e-16, 0, -1], |
| 89 | + [-0.587785252292473, 0, -0.8090169943749475], |
| 90 | + [-0.9510565162951535, 0, -0.30901699437494756], |
| 91 | + [-0.9510565162951536, 0, 0.30901699437494723], |
| 92 | + [-0.5877852522924734, 0, 0.8090169943749473], |
| 93 | + [-2.4492935982947064e-16, 0, 1] |
| 94 | + ]); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should get lengths of edges', async () => { |
| 98 | + const boxDto = new Inputs.OCCT.BoxDto(1, 1, 1); |
| 99 | + const box = await callAction<Inputs.OCCT.BoxDto>('shapes.solid.createBox', boxDto); |
| 100 | + expect(box.hash).toBe(-470453211); |
| 101 | + expect(box.type).toBe('occ-shape'); |
| 102 | + const edges = await callAction<Inputs.OCCT.ShapeDto<Inputs.OCCT.TopoDSShapePointer>>('shapes.edge.getEdges', { shape: box }); |
| 103 | + const lengths = await callAction<Inputs.OCCT.ShapesDto<Inputs.OCCT.TopoDSShapePointer>>('shapes.edge.getEdgesLengths', { shapes: edges }); |
| 104 | + expect(lengths.length).toBe(12); |
| 105 | + expect(lengths).toEqual([ |
| 106 | + 1, 1, 1, 1, 1, |
| 107 | + 1, 1, 1, 1, 1, |
| 108 | + 1, 1 |
| 109 | + ]); |
| 110 | + const lengths2 = await callAction<Inputs.OCCT.ShapesDto<Inputs.OCCT.TopoDSShapePointer>>('shapes.edge.getEdgesLengths', { shapes: edges }); |
| 111 | + expect(lengths2.length).toBe(12); |
| 112 | + expect(lengths2).toEqual([ |
| 113 | + 1, 1, 1, 1, 1, |
| 114 | + 1, 1, 1, 1, 1, |
| 115 | + 1, 1 |
| 116 | + ]); |
| 117 | + }); |
| 118 | + |
| 119 | + it('should get edges of a box and subdivide them into points', async () => { |
| 120 | + const boxDto = new Inputs.OCCT.BoxDto(1, 1, 1); |
| 121 | + const box = await callAction<Inputs.OCCT.BoxDto>('shapes.solid.createBox', boxDto); |
| 122 | + expect(box.hash).toBe(-470453211); |
| 123 | + expect(box.type).toBe('occ-shape'); |
| 124 | + const edges = await callAction<Inputs.OCCT.ShapeDto<Inputs.OCCT.TopoDSShapePointer>>('shapes.edge.getEdges', { shape: box }); |
| 125 | + expect(edges.length).toBe(12); |
| 126 | + const expectedEdgesResultBeforeCache = [ |
| 127 | + { hash: 1474984972, type: 'occ-shape' }, |
| 128 | + { hash: 1474985003, type: 'occ-shape' }, |
| 129 | + { hash: 1474985034, type: 'occ-shape' }, |
| 130 | + { hash: 1474985065, type: 'occ-shape' }, |
| 131 | + { hash: 1474985096, type: 'occ-shape' }, |
| 132 | + { hash: 1474985127, type: 'occ-shape' }, |
| 133 | + { hash: 1474985158, type: 'occ-shape' }, |
| 134 | + { hash: 1474985189, type: 'occ-shape' }, |
| 135 | + { hash: 1474985220, type: 'occ-shape' }, |
| 136 | + { hash: 1474985251, type: 'occ-shape' }, |
| 137 | + { hash: -1520107425, type: 'occ-shape' }, |
| 138 | + { hash: -1520107394, type: 'occ-shape' } |
| 139 | + ]; |
| 140 | + expect(edges).toEqual(expectedEdgesResultBeforeCache); |
| 141 | + const edgesAfterCache = await callAction<Inputs.OCCT.ShapeDto<Inputs.OCCT.TopoDSShapePointer>>('shapes.edge.getEdges', { shape: box }); |
| 142 | + expect(edgesAfterCache).toEqual(expectedEdgesResultBeforeCache); |
| 143 | + |
| 144 | + const points = await Promise.all( |
| 145 | + edgesAfterCache.map(edge => { |
| 146 | + const divDto = new Inputs.OCCT.DivideDto(edge, 4); |
| 147 | + return callAction<Inputs.OCCT.DivideDto<Inputs.OCCT.TopoDSWirePointer>>('shapes.edge.divideEdgeByParamsToPoints', divDto); |
| 148 | + }) |
| 149 | + ); |
| 150 | + expect(points).toEqual([ |
| 151 | + [ |
| 152 | + [-0.5, -0.5, -0.5], |
| 153 | + [-0.5, -0.5, -0.25], |
| 154 | + [-0.5, -0.5, 0], |
| 155 | + [-0.5, -0.5, 0.25], |
| 156 | + [-0.5, -0.5, 0.5] |
| 157 | + ], |
| 158 | + [ |
| 159 | + [-0.5, -0.5, 0.5], |
| 160 | + [-0.5, -0.25, 0.5], |
| 161 | + [-0.5, 0, 0.5], |
| 162 | + [-0.5, 0.25, 0.5], |
| 163 | + [-0.5, 0.5, 0.5] |
| 164 | + ], |
| 165 | + [ |
| 166 | + [-0.5, 0.5, 0.5], |
| 167 | + [-0.5, 0.5, 0.25], |
| 168 | + [-0.5, 0.5, 0], |
| 169 | + [-0.5, 0.5, -0.25], |
| 170 | + [-0.5, 0.5, -0.5] |
| 171 | + ], |
| 172 | + [ |
| 173 | + [-0.5, 0.5, -0.5], |
| 174 | + [-0.5, 0.25, -0.5], |
| 175 | + [-0.5, 0, -0.5], |
| 176 | + [-0.5, -0.25, -0.5], |
| 177 | + [-0.5, -0.5, -0.5] |
| 178 | + ], |
| 179 | + [ |
| 180 | + [0.5, -0.5, 0.5], |
| 181 | + [0.5, -0.5, 0.25], |
| 182 | + [0.5, -0.5, 0], |
| 183 | + [0.5, -0.5, -0.25], |
| 184 | + [0.5, -0.5, -0.5] |
| 185 | + ], |
| 186 | + [ |
| 187 | + [0.5, 0.5, 0.5], |
| 188 | + [0.5, 0.25, 0.5], |
| 189 | + [0.5, 0, 0.5], |
| 190 | + [0.5, -0.25, 0.5], |
| 191 | + [0.5, -0.5, 0.5] |
| 192 | + ], |
| 193 | + [ |
| 194 | + [0.5, 0.5, -0.5], |
| 195 | + [0.5, 0.5, -0.25], |
| 196 | + [0.5, 0.5, 0], |
| 197 | + [0.5, 0.5, 0.25], |
| 198 | + [0.5, 0.5, 0.5] |
| 199 | + ], |
| 200 | + [ |
| 201 | + [0.5, -0.5, -0.5], |
| 202 | + [0.5, -0.25, -0.5], |
| 203 | + [0.5, 0, -0.5], |
| 204 | + [0.5, 0.25, -0.5], |
| 205 | + [0.5, 0.5, -0.5] |
| 206 | + ], |
| 207 | + [ |
| 208 | + [-0.5, -0.5, -0.5], |
| 209 | + [-0.25, -0.5, -0.5], |
| 210 | + [0, -0.5, -0.5], |
| 211 | + [0.25, -0.5, -0.5], |
| 212 | + [0.5, -0.5, -0.5] |
| 213 | + ], |
| 214 | + [ |
| 215 | + [0.5, -0.5, 0.5], |
| 216 | + [0.25, -0.5, 0.5], |
| 217 | + [0, -0.5, 0.5], |
| 218 | + [-0.25, -0.5, 0.5], |
| 219 | + [-0.5, -0.5, 0.5] |
| 220 | + ], |
| 221 | + [ |
| 222 | + [0.5, 0.5, -0.5], |
| 223 | + [0.25, 0.5, -0.5], |
| 224 | + [0, 0.5, -0.5], |
| 225 | + [-0.25, 0.5, -0.5], |
| 226 | + [-0.5, 0.5, -0.5] |
| 227 | + ], |
| 228 | + [ |
| 229 | + [-0.5, 0.5, 0.5], |
| 230 | + [-0.25, 0.5, 0.5], |
| 231 | + [0, 0.5, 0.5], |
| 232 | + [0.25, 0.5, 0.5], |
| 233 | + [0.5, 0.5, 0.5] |
| 234 | + ] |
| 235 | + ]) |
| 236 | + }); |
| 237 | + |
| 238 | + const callAction: <T>(functionName: string, inputs: T) => any = (functionName, inputs) => { |
| 239 | + return new Promise((resolve, reject) => { |
| 240 | + try { |
| 241 | + onMessageInput({ |
| 242 | + action: { |
| 243 | + functionName, |
| 244 | + inputs |
| 245 | + }, |
| 246 | + uid: 'sdadwa', |
| 247 | + }, (data) => { |
| 248 | + if (data !== 'busy') { |
| 249 | + resolve(data.result) |
| 250 | + } |
| 251 | + if (data.error) { |
| 252 | + console.error('ERROR HAPPENED: ', data.error); |
| 253 | + reject(data.error) |
| 254 | + } |
| 255 | + }); |
| 256 | + } catch (err) { |
| 257 | + console.error('ERROR HAPPENED: ', err); |
| 258 | + reject(err) |
| 259 | + } |
| 260 | + }); |
| 261 | + } |
| 262 | + |
| 263 | +}); |
| 264 | + |
| 265 | +async function createLoft(callAction: <T>(functionName: string, inputs: T) => any) { |
| 266 | + const cdto1 = new Inputs.OCCT.CircleDto(1, [0, 0, 0], [0, 1, 0]); |
| 267 | + const wire1 = await callAction<Inputs.OCCT.CircleDto>('shapes.wire.createCircleWire', cdto1); |
| 268 | + |
| 269 | + const cdto2 = new Inputs.OCCT.CircleDto(2, [0, 1, 0], [0, 1, 0]); |
| 270 | + const wire2 = await callAction<Inputs.OCCT.CircleDto>('shapes.wire.createCircleWire', cdto2); |
| 271 | + |
| 272 | + const cdto3 = new Inputs.OCCT.CircleDto(1.5, [0, 2, 0], [0, 1, 0]); |
| 273 | + const wire3 = await callAction<Inputs.OCCT.CircleDto>('shapes.wire.createCircleWire', cdto3); |
| 274 | + |
| 275 | + const ldto = new Inputs.OCCT.LoftAdvancedDto([wire1, wire2, wire3]); |
| 276 | + ldto.closed = true; |
| 277 | + ldto.straight = true; |
| 278 | + const loft = await callAction<Inputs.OCCT.LoftAdvancedDto<Inputs.OCCT.TopoDSShapePointer>>('operations.loftAdvanced', ldto); |
| 279 | + return loft; |
| 280 | +} |
| 281 | + |
0 commit comments