@@ -67,6 +67,57 @@ export class OCCTIO {
6767 return result ;
6868 }
6969
70+ saveShapeStl ( inputs : Inputs . OCCT . SaveStlDto < TopoDS_Shape > ) : string {
71+ const shapeToUse = inputs . shape ;
72+
73+ // This could be made optional...
74+ // Clean cached triangulation data for the shape.
75+ // This allows to get lower res models out of higher res that was once computed and cached.
76+ this . occ . BRepTools . Clean ( shapeToUse , true ) ;
77+
78+ let adjustedShape ;
79+ if ( inputs . adjustYtoZ ) {
80+ const rotatedShape = this . och . transformsService . rotate ( { shape : inputs . shape , axis : [ 1 , 0 , 0 ] , angle : - 90 } ) ;
81+ adjustedShape = this . och . transformsService . mirrorAlongNormal (
82+ { shape : rotatedShape , origin : [ 0 , 0 , 0 ] , normal : [ 0 , 0 , 1 ] }
83+ ) ;
84+ rotatedShape . delete ( ) ;
85+ }
86+ const fileName = "x" ;
87+ const writer = new this . occ . StlAPI_Writer ( ) ;
88+ let transferShape ;
89+ if ( adjustedShape ) {
90+ transferShape = adjustedShape ;
91+ } else {
92+ transferShape = shapeToUse ;
93+ }
94+ const messageProgress = new this . occ . Message_ProgressRange_1 ( ) ;
95+ let result : string ;
96+ const incrementalMeshBuilder = new this . occ . BRepMesh_IncrementalMesh_2 ( transferShape , inputs . precision , false , 0.5 , false ) ;
97+
98+ // Write the STL File to the virtual Emscripten Filesystem Temporarily
99+ const writeResult = writer . Write ( transferShape , fileName , messageProgress ) ;
100+ if ( writeResult ) {
101+ // Read the STL File from the filesystem and clean up
102+ const stlFile = this . occ . FS . readFile ( "/" + fileName , { encoding : "utf8" } ) ;
103+ this . occ . FS . unlink ( "/" + fileName ) ;
104+ // Return the contents of the STL File
105+ result = stlFile ;
106+ } else {
107+ throw ( new Error ( "Failed when writing stl file." ) ) ;
108+ }
109+
110+ if ( adjustedShape ) {
111+ adjustedShape . delete ( ) ;
112+ }
113+ messageProgress . delete ( ) ;
114+
115+ if ( incrementalMeshBuilder ) {
116+ incrementalMeshBuilder . Delete ( ) ;
117+ }
118+
119+ return result ;
120+ }
70121
71122 /** This function parses the ASCII contents of a `.STEP` or `.IGES`
72123 * File as a Shape into the `externalShapes` dictionary.
0 commit comments