Skip to content

Commit f8fac11

Browse files
committed
* add writeSTL
1 parent b6f0108 commit f8fac11

13 files changed

+355197
-78
lines changed

binding.gyp

+1-2
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,7 @@
7575
],
7676

7777
"libraries": [
78-
7978
'-lTKAdvTools',
80-
8179
'-lTKBO',
8280
'-lTKBool',
8381
'-lTKBRep',
@@ -101,6 +99,7 @@
10199
'-lTKSTEP',
102100
'-lTKFillet',
103101
'-lTKXSBase',
102+
'-lTKSTL',
104103
],
105104
}
106105
]

lib/mesh.js

+16-16
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,29 @@ function toArray(meshBuffer)
66
{
77
var res = [];
88
for (var i =0;i< meshBuffer.length;i++) {
9-
res.push(meshBuffer[i]);
9+
res.push(parseFloat(meshBuffer[i].toFixed(3)));
1010
}
1111
return res;
1212
}
1313
exports.init= function() {
1414

15-
var assert = require('assert');
16-
console.log('adding occ.Mesh.prototype.toJSON function');
17-
occ.Mesh.prototype.toJSON = function() {
15+
var assert = require('assert');
16+
console.log('adding occ.Mesh.prototype.toJSON function');
17+
occ.Mesh.prototype.toJSON = function() {
1818

1919
// see https://github.com/mrdoob/three.js/wiki/JSON-Model-format-3.1
20-
var json = {
21-
metadata: { "formatVersion" : 3 },
22-
materials: [ {
23-
DbgColor : 15658734, // => 0xeeeeee
24-
DbgIndex : 0,
25-
DbgName : "dummy",
26-
colorDiffuse : [ 1, 0, 0 ]
27-
} ],
28-
vertices: [],
29-
normals: [],
30-
faces: []
31-
};
20+
var json = {
21+
metadata: { "formatVersion" : 3 },
22+
materials: [ {
23+
DbgColor : 15658734, // => 0xeeeeee
24+
DbgIndex : 0,
25+
DbgName : "dummy",
26+
colorDiffuse : [ 1, 0, 0 ]
27+
} ],
28+
vertices: [],
29+
normals: [],
30+
faces: []
31+
};
3232

3333
json.vertices = toArray(this.vertices);
3434
json.normals = toArray(this.normals);

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"file-utils": "*",
1010
"temporary": "0.0.5",
1111
"gettemporaryfilepath": "0.0.1",
12-
"progress": "~0.1.0"
12+
"progress": "~0.1.0",
13+
"pace": "0.0.4"
1314
},
1415
"devDependencies": {
1516
"grunt": "~0.4.1",

sample/routes/object.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ exports.buildCSG1 = function(req,res)
8686

8787
exports.load_cadfile = function(req,res) {
8888

89+
90+
function progress(percent) {
91+
console.log(" -------------------> ", percent);
92+
}
8993
console.log(" loading ",req.body.filename);
9094

9195
occ.readSTEP(req.body.filename, function(err,solids) {
@@ -96,7 +100,7 @@ exports.load_cadfile = function(req,res) {
96100
console.log(" readStep has succeeded");
97101
res.send(buildResponse(solids,[]));
98102
}
99-
});
103+
},progress);
100104

101105

102106

src/Face.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ Handle<v8::Value> Face::_mesh(Local<String> property,const AccessorInfo &info)
164164
}
165165
Face* pThis = ObjectWrap::Unwrap<Face>(info.This());
166166
if (pThis->m_cacheMesh.IsEmpty()) {
167-
pThis->m_cacheMesh = Persistent<Object>::New(pThis->createMesh(0.01,0.5,true));
167+
pThis->m_cacheMesh = Persistent<Object>::New(pThis->createMesh(0.5,20*3.14159/180.0,true));
168168
}
169169
return scope.Close(pThis->m_cacheMesh);
170170
}

src/Solid.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ Handle<v8::Value> Solid::_mesh(Local<String> property,const AccessorInfo &info)
521521
}
522522
Solid* pThis = ObjectWrap::Unwrap<Solid>(info.This());
523523
if (pThis->m_cacheMesh.IsEmpty()) {
524-
pThis->m_cacheMesh = Persistent<Object>::New(pThis->createMesh(0.1,5*3.14159/180.0,true));
524+
pThis->m_cacheMesh = Persistent<Object>::New(pThis->createMesh(0.5,20*3.14159/180.0,true));
525525
}
526526
return scope.Close(pThis->m_cacheMesh);
527527
}

src/Tools.cc

+121-44
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,35 @@ v8::Handle<Value> writeBREP(const v8::Arguments& args)
109109
return scope.Close(v8::True());
110110
}
111111

112+
v8::Handle<Value> writeSTL(const v8::Arguments& args)
113+
{
114+
HandleScope scope;
115+
std::string filename;
116+
if (!extractFileName(args[0],filename)) {
117+
return ThrowException(Exception::TypeError(String::New("expecting a file name")));
118+
}
119+
std::list<Shape*> shapes;
120+
for (int i=1; i<args.Length(); i++) {
121+
extractShapes(args[i],shapes);
122+
}
123+
if (shapes.size()==0) {
124+
return scope.Close(Boolean::New(false));
125+
}
126+
try {
127+
BRep_Builder B;
128+
TopoDS_Compound C;
129+
B.MakeCompound(C);
130+
for (std::list<Shape*>::iterator it = shapes.begin();it != shapes.end();it++) {
131+
TopoDS_Shape shape = (*it)->shape();
132+
B.Add(C,shape);
133+
}
134+
StlAPI_Writer writer;
135+
writer.ASCIIMode() = Standard_False;
136+
writer.Write(C,filename.c_str(),Standard_True);
112137

138+
} CATCH_AND_RETHROW("Failed to write STL file ");
139+
return scope.Close(v8::True());
140+
}
113141

114142
static int extractSubShape(const TopoDS_Shape& shape, std::list<Local<Object> >& shapes)
115143
{
@@ -195,8 +223,8 @@ static Local<Array> convert(std::list<Local<Object> > & shapes) {
195223
Local<Array> arr = Array::New((int)shapes.size());
196224
int i=0;
197225
for (std::list<Local<Object> >::iterator it = shapes.begin();it != shapes.end();it++) {
198-
arr->Set(i,*it);
199-
i++;
226+
arr->Set(i,*it);
227+
i++;
200228
}
201229
return arr;
202230
}
@@ -227,9 +255,20 @@ struct data_s {
227255
Persistent<Function> progressCallback;
228256
std::list<TopoDS_Shape > shapes;
229257
int retValue;
258+
double percent;
230259
double progress;
231260
};
232261

262+
void dispose(data_s* data)
263+
{
264+
uv_close(reinterpret_cast<uv_handle_t*>(&data->async),0);
265+
data->callback.Dispose();
266+
if (!data->progressCallback.IsEmpty()) {
267+
data->progressCallback.Dispose();
268+
}
269+
delete data;
270+
271+
}
233272

234273
MyProgressIndicator::MyProgressIndicator(uv_async_t& async)
235274
:Message_ProgressIndicator(),m_async(async),m_lastValue(0)
@@ -240,7 +279,8 @@ Standard_Boolean MyProgressIndicator::Show(const Standard_Boolean force)
240279
data_t* data = (data_t *)this->m_async.data;
241280
double value = this->GetPosition();
242281
double delta = (value-this->m_lastValue);
243-
if ( delta > 0.05) {
282+
if ( delta > 0.01) {
283+
data->percent = value;
244284
data->progress = int(delta*1000);// this->GetPosition();
245285
this->m_lastValue = value;
246286
uv_async_send(&m_async);
@@ -270,8 +310,8 @@ void notify_progress(uv_async_t* handle,int status/*unused*/)
270310
data_t* data = (data_t *)handle->data;
271311
if (!data->progressCallback.IsEmpty()) {
272312
// std::cout << " progress received" << data->progress << " \n";
273-
Local<Value> argv[1] = {Integer::New(data->progress) };
274-
Local<Value> res = data->progressCallback->Call(Context::GetCurrent()->Global(), 1, argv);
313+
Local<Value> argv[2] = { Number::New(data->percent),Integer::New((int)data->progress) };
314+
Local<Value> res = data->progressCallback->Call(Context::GetCurrent()->Global(), 2, argv);
275315
}
276316
}
277317

@@ -301,13 +341,17 @@ void _readStepAsync(uv_work_t *req) {
301341

302342
progress->NewScope(5,"reading");
303343
if (aReader.ReadFile(filename.c_str()) != IFSelect_RetDone) {
344+
304345
std::strstream str;
305346
str << " cannot read STEP file " << filename << std::ends;
347+
std::cerr << "cannot read "<< std::endl;
306348
data->message = str.str();
307349
// Local<Value> argv[] = { Local<Value>(String::New()) };
308350
// Local<Value> res = callback->Call(global, 1, argv);
309351
// return scope.Close(Undefined());
310352
progress->EndScope();
353+
progress->SetValue(105.0);
354+
progress->Show();
311355
data->retValue = 1;
312356
return;
313357
}
@@ -341,6 +385,7 @@ void _readStepAsync(uv_work_t *req) {
341385
}
342386
}
343387
catch(...) {
388+
std::cerr << " EXCEPTION" << std::endl;
344389
data->message ="caught C++ exception in readStep";
345390
data->retValue = 1;
346391
return;
@@ -381,22 +426,10 @@ void _readStepAsyncAfter(uv_work_t *req,int status) {
381426
Local<Value> argv[2] = {Integer::New(data->retValue), Local<Value>(String::New(data->message.c_str())) };
382427
Local<Value> res = data->callback->Call(Context::GetCurrent()->Global(), 2, argv);
383428
}
384-
385-
uv_close(reinterpret_cast<uv_handle_t*>(&data->async),0);
386-
387-
data->callback.Dispose();
388-
if (!data->progressCallback.IsEmpty()) {
389-
data->progressCallback.Dispose();
390-
}
391-
delete data;
429+
dispose(data);
392430
}
393431

394-
int uv_queue_work__(uv_loop_t* loop, uv_work_t* req, uv_work_cb work_cb, uv_after_work_cb after_work_cb)
395-
{
396-
work_cb(req);
397-
after_work_cb(req,0);
398-
return 0;
399-
}
432+
400433
void readStepAsync(const std::string& filename,v8::Local<Function> callback,v8::Local<Function> progressCallback)
401434
{
402435
data_t* data = new data_t;
@@ -409,6 +442,9 @@ void readStepAsync(const std::string& filename,v8::Local<Function> callback,v8::
409442
uv_queue_work(uv_default_loop(), &data->req, _readStepAsync, _readStepAsyncAfter);
410443
}
411444

445+
446+
447+
412448
v8::Handle<Value> readSTEP(const v8::Arguments& args)
413449
{
414450

@@ -428,12 +464,71 @@ v8::Handle<Value> readSTEP(const v8::Arguments& args)
428464
// return ThrowException(Exception::TypeError(String::New("expecting a callback function")));
429465
}
430466

431-
Local<Object> global = v8::Context::GetCurrent()->Global();
432467
readStepAsync(filename,callback,progressCallback);
433-
434468
return scope.Close(Undefined());
435469
}
436470

471+
472+
void _readBREPAsync(uv_work_t *req)
473+
{
474+
475+
data_t* data = (data_t *) req->data;
476+
uv_async_init(req->loop,&data->async,notify_progress);
477+
data->async.data = data;
478+
479+
data->retValue = 0;
480+
std::string filename = data->filename;
481+
482+
483+
try {
484+
occHandle(Message_ProgressIndicator) progress = new MyProgressIndicator(data->async) ;
485+
progress->SetScale(1,100,1);
486+
progress->Show();
487+
488+
// read brep-file
489+
TopoDS_Shape shape;
490+
BRep_Builder aBuilder;
491+
if (!BRepTools::Read(shape, filename.c_str(), aBuilder,progress)) {
492+
std::strstream str;
493+
str << " cannot read BREP file " << filename << std::ends;
494+
std::cerr << str.str() << std::endl;
495+
data->message = str.str();
496+
data->retValue = 1;
497+
progress->SetValue(100.0);
498+
progress->Show();
499+
return;
500+
}
501+
data->shapes.push_back(shape);
502+
progress->SetValue(100.0);
503+
progress->Show();
504+
}
505+
catch(...) {
506+
data->message ="caught C++ exception in _readBREPAsync";
507+
data->retValue = -3;
508+
return;
509+
510+
}
511+
}
512+
513+
void _readBREPAsyncAfter(uv_work_t *req,int status)
514+
{
515+
_readStepAsyncAfter(req,status);
516+
}
517+
518+
519+
void readBREPAsync(const std::string& filename,v8::Local<Function> callback,v8::Local<Function> progressCallback)
520+
{
521+
data_t* data = new data_t;
522+
data->req.data = data;
523+
data->filename = filename;
524+
data->callback = Persistent<Function>::New(callback);
525+
if (!progressCallback.IsEmpty()) {
526+
data->progressCallback = Persistent<Function>::New(progressCallback);
527+
}
528+
uv_queue_work(uv_default_loop(), &data->req, _readBREPAsync, _readBREPAsyncAfter);
529+
}
530+
531+
437532
v8::Handle<Value> readBREP(const v8::Arguments& args)
438533
{
439534
HandleScope scope;
@@ -445,30 +540,12 @@ v8::Handle<Value> readBREP(const v8::Arguments& args)
445540
if (!extractCallback(args[1],callback)) {
446541
return ThrowException(Exception::TypeError(String::New("expecting a callback function")));
447542
}
448-
449-
Local<Object> global = v8::Context::GetCurrent()->Global();
450-
451-
try {
452-
// read brep-file
453-
TopoDS_Shape shape;
454-
BRep_Builder aBuilder;
455-
if (!BRepTools::Read(shape, filename.c_str(), aBuilder)) {
456-
Local<Value> argv[] = { Local<Value>(String::New(" cannot read BREP file")) };
457-
Local<Value> res = callback->Call(global, 1, argv);
458-
return scope.Close(Undefined());
459-
}
460-
std::list<Local<Object> > shapes;
461-
extractShape(shape, shapes);
462-
Local<Array> arr = convert(shapes);
463-
Local<Value> err = Integer::New(0);
464-
Local<Value> argv[2] = { err, arr };
465-
Local<Value> res = callback->Call(global, 2, argv);
466-
467-
} CATCH_AND_RETHROW("Failed to read BREP file");
468-
469-
543+
v8::Local<Function> progressCallback;
544+
if (!extractCallback(args[2],progressCallback)) {
545+
// return ThrowException(Exception::TypeError(String::New("expecting a callback function")));
546+
}
547+
readBREPAsync(filename,callback,progressCallback);
470548
return scope.Close(Undefined());
471-
472549
}
473550

474551

src/Tools.h

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ v8::Handle<Value> writeSTEP(const v8::Arguments& args);
44
v8::Handle<Value> readSTEP(const v8::Arguments& args);
55
v8::Handle<Value> writeBREP(const v8::Arguments& args);
66
v8::Handle<Value> readBREP(const v8::Arguments& args);
7+
v8::Handle<Value> writeSTL(const v8::Arguments& args);

src/V8Wrapper.cc

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ void Initialize(Handle<Object> target)
5757
target->Set(String::NewSymbol("common"), FunctionTemplate::New(ShapeFactory::common)->GetFunction());
5858
target->Set(String::NewSymbol("compound"), FunctionTemplate::New(ShapeFactory::compound)->GetFunction());
5959

60+
target->Set(String::NewSymbol("writeSTL"), FunctionTemplate::New(writeSTL)->GetFunction());
6061
target->Set(String::NewSymbol("writeSTEP"), FunctionTemplate::New(writeSTEP)->GetFunction());
6162
target->Set(String::NewSymbol("writeBREP"), FunctionTemplate::New(writeBREP)->GetFunction());
6263
target->Set(String::NewSymbol("readSTEP"), FunctionTemplate::New(readSTEP)->GetFunction());

0 commit comments

Comments
 (0)