-
Notifications
You must be signed in to change notification settings - Fork 1
/
Shell.ts
435 lines (360 loc) · 15.4 KB
/
Shell.ts
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/**
*
* Face.ts - A collection of Wires that form the (inner and outher) enclosure of a surface: together form a shell
*
*/
import { Vector, Point, Shape, Vertex, Edge, Wire, Face, Solid, ShapeCollection } from './internal'
import { PointLike, isCoordArray, isMakeShellInput, MakeShellInput, AnyShape, ThickenDirection, isThickenDirection, AnyShapeSequence, isAnyShapeSequence } from './internal' // types
import { checkInput, cacheOperation, addResultShapesToScene, protectOC } from './decorators'; // Direct import to avoid error in ts-node/jest
import { flattenEntities } from './internal' // utils
// this can disable TS errors when subclasses are not initialized yet
type ISolid = Solid
export class Shell extends Shape
{
/*
Inherited from Shape:
+ _ocShape
+ _ocId
Inheritec from Obj
+ _position:Vector
+ _oc
*/
constructor(entities?:any, ...args:Array<any>)
{
super();
if(entities == null)
{
//console.warn(`Shell::constructor: Empty Shell created!`)
}
else if(ShapeCollection.isShapeCollection(entities))
{
this.fromAll(entities);
}
else
{
let selectedEntities = (Array.isArray(entities) && !isCoordArray(entities)) ? (entities as Array<any>).concat(args) as Array<any>: [entities, ...args];
selectedEntities = flattenEntities(selectedEntities); // NOTE: we could also use flattenEntitiesToArray
if(selectedEntities.length == 0)
{
console.warn(`Shell::constructor: Empty Shell created!`)
}
else {
this.fromAll(selectedEntities);
}
}
}
@checkInput('MakeShellInput', 'auto')
fromAll(entities:MakeShellInput): Shell
{
let shapes = entities as ShapeCollection; // auto converted
if (shapes.getShapesByType('Face').length >= 1)
{
return this.fromFaces(shapes);
}
else if (shapes.getShapesByType('Edge').length >= 2)
{
return this.fromEdges(shapes);
}
}
@checkInput('Face', 'auto')
fromFace(face:Face):Shell
{
// BRep_Builder: https://dev.opencascade.org/doc/refman/html/class_b_rep___builder.html
let ocBRepBuilder = new this._oc.BRep_Builder();
let newOcShell = new this._oc.TopoDS_Shell();
ocBRepBuilder.MakeShell(newOcShell);
ocBRepBuilder.Add(newOcShell, face._ocShape);
return this._fromOcShell(newOcShell);
}
@checkInput('MakeShellInput', 'ShapeCollection')
fromFaces(faces:MakeShellInput):Shell
{
let shapes = faces as ShapeCollection; // auto converted
let facesCollection = shapes.getShapesByType('Face');
if (facesCollection.length == 0)
{
console.error('Shell::fromAll: Could not create Shell, no Faces given. Empty Shell created!');
return null; // NOTE: this will create an empty Shell
}
if (facesCollection.length == 1)
{
return this.fromFace(facesCollection.first() as Face);
}
let ocShellBuilder = new this._oc.BRepBuilderAPI_Sewing(1e-6, true, true, true, false);
facesCollection.forEach( curFace => ocShellBuilder.Add(curFace._ocShape));
ocShellBuilder.Perform( new this._oc.Message_ProgressRange_1());
let ocShell = ocShellBuilder.SewedShape();
if (ocShell.IsNull())
{
console.error(`Shell::fromFaces: Could not combine ${faces.length} Faces into Shell!`);
return null;
}
else {
// successfull sew
return this._fromOcShell(ocShell).checkAndFix() as Shell; // !!!! check and fix needed? !!!!
}
}
/** Creates an Shell by interpolating between two (=Loft) or more Edges
* Generally used in the context of filling but might be handy as seperate function */
@checkInput('AnyShapeSequence', 'ShapeCollection')
fromEdges(edges:AnyShapeSequence):Shell
{
/* OC Docs:
- Filling: https://dev.opencascade.org/doc/occt-7.4.0/refman/html/class_b_rep_fill___filling.html#details
- ContrainedFilling: https://dev.opencascade.org/doc/refman/html/class_geom_fill___constrained_filling.html
- SimpleBound: https://dev.opencascade.org/doc/refman/html/class_geom_fill___simple_bound.html
- see example in docs: https://dev.opencascade.org/doc/overview/html/occt_user_guides__modeling_algos.html
*/
let uncheckedEdges = edges as ShapeCollection; // auto converted
let checkedEdges = uncheckedEdges.getShapesByType('Edge');
if(checkedEdges.length == 0)
{
throw new Error(`Face::fromEdges: Please supply an array of Edges`);
}
else if (checkedEdges.length < 2)
{
throw new Error(`Face::fromEdges: Please supply an array of at least 2 Edges`);
}
else if (checkedEdges.length == 2)
{
// a lofted Face Surface
let firstEdge = edges[0];
let otherEdge = edges[1];
let shellLoft = firstEdge._toWire().lofted(otherEdge, false); // Shell only ( can have only 1 Face)
this._fromOcShell(shellLoft._ocShape);
return this;
}
else if(checkedEdges.length > 4)
{
throw new Error(`Face::fromBoundaryEdges: Please supply an array of maximum 4 Edges`);
}
else {
// create a ContrainedFilling out of 3 or 4 Edges
let ocConstrainedFilling = new this._oc.GeomFill_ConstrainedFilling(180,100); // maxDeg, maxSeg
let ocBoundaries = checkedEdges.toArray().map ( e => {
let geomAdaptorCurve = (e as Edge)._toOcCurve().Curve();
let handleAdaptor3dHCurve = geomAdaptorCurve.Trim(geomAdaptorCurve.FirstParameter(), geomAdaptorCurve.LastParameter(), 1e-3);
let geomFillSimpleBound = new this._oc.GeomFill_SimpleBound( handleAdaptor3dHCurve , 1e-3, 1e-3); // Tol3d TolAng
let handleGeomBound = new this._oc.Handle_GeomFill_Boundary_2(geomFillSimpleBound);
return handleGeomBound
});
if(checkedEdges.length == 3 )
{
ocConstrainedFilling.Init_1(ocBoundaries[0], ocBoundaries[1], ocBoundaries[2], false); // noCheck
}
else {
ocConstrainedFilling.Init_2(ocBoundaries[0], ocBoundaries[1], ocBoundaries[2], ocBoundaries[3], false); // noCheck
}
let ocSurfaceBspline = ocConstrainedFilling.Surface(); // Handle_Geom_BSplineSurface: https://dev.opencascade.org/doc/refman/html/class_geom___b_spline_surface.html
// make Face/Shell from Surface
let ocSurface = new this._oc.Handle_Geom_Surface_2(ocSurfaceBspline.get()); // NOTE: We can easily 'transform' inheritance chains with the Handle logic: To Handle_Geom_Surface_2
let ocShell = new this._oc.BRepBuilderAPI_MakeShell_2(ocSurface, false).Shell(); // see: https://dev.opencascade.org/doc/refman/html/class_b_rep_builder_a_p_i___make_shell.html
if(!ocShell)
{
console.error(`Shell::fromEdges: Could not make Shell`);
return null;
}
let newShell = new Shape()._fromOcShape(ocShell).specific() as Shell;
this._fromOcShell(newShell._ocShape);
return this;
}
}
_fromOcShell(ocShell:any):Shell // TODO: OC typing
{
if (ocShell && (ocShell instanceof this._oc.TopoDS_Shell || ocShell instanceof this._oc.TopoDS_Shape) && !ocShell.IsNull())
{
// For easy debug, always make sure the wrapped OC Shape is TopoDS_Shell
ocShell = this._makeSpecificOcShape(ocShell, 'Shell');
this._ocShape = ocShell;
this._ocId = this._hashcode();
this.round(); // round to tolerance
return this;
}
else {
throw new Error(`Shell::_fromOcShell: Incoming ocShape is not a TopoDS_Shell! Check not null, OC Shape type and if empty`);
}
}
//// TRANSFORMATIONS ////
/** Get the outer Wire of Shell (private) */
_toWire():Wire
{
return this.outerWire();
}
/** Get the outer Wire of Shell */
@addResultShapesToScene
toWire():Wire
{
return this._toWire();
}
/** Returns the first Face. Handy for one Face Shells (private) */
_toFace():Face
{
return this.faces()[0];
}
@addResultShapesToScene
toFace():Face
{
return this._toFace();
}
_toSolid():ISolid
{
return new Solid().fromShell(this);
}
@addResultShapesToScene
toSolid():ISolid
{
return new Solid().fromShell(this);
}
_setToOc()
{
// we only direct OC creation methods
}
ocGeom():any
{
/** This returns the most specified subclass of Shape */
let s = this._makeSpecificOcShape(this._ocShape, 'Shell');
return s;
}
_ocGeom():any
{
this._ocShape;
}
//// COMPUTED PROPERTIES ////
/** Get outer Wire of this Shell */
outerWire():Wire
{
// Shell is only one Face, then the ShapeAnalysis tool does not work. Revert back to the one on Face
if(this.faces().length == 1)
{
return this.faces()[0].outerWire();
}
else {
let ocAnalysor = new this._oc.ShapeAnalysis_FreeBounds_2(this._ocShape, 1e-3, false, true);
let ocOuterWire = ocAnalysor.GetClosedWires(); // gives back a Compound of outerwires, but _fromOcShape will make this into one Wire
return new Shape()._fromOcShape(ocOuterWire) as Wire;
}
}
/** Calculated center of Surface (not middle!) */
center():Point
{
/** OC docs:
* - GProps_GProps: https://dev.opencascade.org/doc/occt-7.4.0/refman/html/class_g_prop___g_props.html
* - BRepGProp: https://dev.opencascade.org/doc/occt-7.4.0/refman/html/class_b_rep_g_prop.html#abd91b892df8d0f6b8571deed5562ca1f
* */
let ocProps = new this._oc.GProp_GProps_1();
let BRepGProp = this._oc.BRepGProp.prototype.constructor;
BRepGProp.SurfaceProperties_1(this._ocShape, ocProps, false, false);
return new Point()._fromOcPoint(ocProps.CentreOfMass());
}
is2DXY(): boolean
{
return this.vertices().every(v => (v as Vertex).z <= this._oc.SHAPE_TOLERANCE)
}
// !!!! TODO !!!!
normal():Vector
{
console.warn('Shell::normal: Not implemented yet! Using method on first Face!');
return (this.faces().first() as Face).normal(); // NOTE: Don't use center because it might not be on the Shell
}
/** Get normal at a given Point on the surface */
// !!!! TODO !!!!
normalAt(point:Point):Vector
{
console.warn('Shell::normalAt: Not implemented yet! Use method on first Face!');
return (this.faces().first() as Face).normalAt(point);
}
//// OPERATIONS ON SHELL ////
/*
Shape._offsetted
Shape.offset
Shape.offsetted
*/
/** Thicken the shell to create a Solid (private: without adding to Scene) */
@checkInput([Number,'ThickenDirection'],['auto','auto'])
_thickened(amount:number, direction:ThickenDirection):ISolid
{
/*
* NOTE: OC changing: we might need to update here. Looks like this will be direct functions without constructor
* OC docs:
* - https://dev.opencascade.org/doc/occt-7.5.0/refman/html/class_b_rep_offset_a_p_i___make_offset_shape.html
* - https://dev.opencascade.org/doc/occt-7.5.0/refman/html/_b_rep_offset___mode_8hxx.html#a660437f6c00ce59d43e4a930fba2f84c
*/
// NOTE: same API as thickened everywhere else!
// NOTE: amount - or + does not really matter
// special default case: centered thickening
if(direction == 'center' || direction == null)
{
let oneSideShell = this._offsetted(amount/2);
let otherSideShell = this._offsetted(-amount/2);
let newSolid = (oneSideShell as Shell)._bridge(otherSideShell as Shell);
return newSolid;
}
else {
// we got a direction indication: either a string or real Point
let directionPosition:Point;
if (typeof direction == 'string')
{
// direction indications using the bbox indication
let percXYZ = this._alignStringToAlignPerc(direction);
let bbox = this.bbox().enlarged(10); // make a bigger bbox just to avoid zero dimensions for horizontal/vertical lines
directionPosition = bbox.getPositionAtPerc(percXYZ[0], percXYZ[1], percXYZ[2]);
}
else {
directionPosition = new Vector().fromAll(direction);
}
// we got a direction to thicken to
let oneSideSolid = this._bridgeThickened(amount);
let otherSideSolid = this._bridgeThickened(-amount);
let d1 = directionPosition._toVertex().distance(oneSideSolid.center()._toVertex());
let d2 = directionPosition._toVertex().distance(otherSideSolid.center()._toVertex());
if(d1 <= d2)
{
return oneSideSolid;
}
else {
return otherSideSolid;
}
}
}
/** Thicken the shell to create a Solid (private: without adding to Scene) */
@checkInput([Number,'ThickenDirection'],['auto','auto'])
thickened(amount:number, direction:ThickenDirection):ISolid
{
return this._thickened(amount,direction);
}
/** Private method that is used by thickened */
@checkInput(Number, 'auto')
_bridgeThickened(amount:number):ISolid
{
let result = this._offsetted(amount);
// !!!! OC does not produce Solids with this algorithm. CadQuery uses BRepOffset_MakeOffset() which is not available in opencascade.js
// This is a workaround by finding two outerwires for the original shell and offsetted shell, then create extra Shell for sides by lofting the two outer wires
if (result.type() == 'Shell')
{
// We get an offset Shell in the result
let newSolid = this._bridge(result as Shell);
return newSolid;
}
else {
console.error(`Shell:thickened: Offsetted failed in thicken operation. Check validity of given Shell!`);
return null;
}
}
/** Stitch two Shells together to create a Solid (EXPERIMENTAL) */
@protectOC([`This is an experimental method. Might not work!`])
@checkInput(Shell, 'auto')
_bridge(other:Shell):ISolid
{
let offsetShellWire = (other as Shell).outerWire();
let originaShellWire = this.outerWire();
let sideShell = originaShellWire._lofted(offsetShellWire, false) as Shell;
let newSolid = new Solid().fromShells(new ShapeCollection(this, other as Shell,sideShell));
return newSolid;
}
//// OUTPUT ////
/** Export entity and minimal data as string (used for outputting on console and hashing ) */
toString():string
{
return `<Shell numFaces="${this.faces().length}">`;
}
}