@@ -109,7 +109,35 @@ v8::Handle<Value> writeBREP(const v8::Arguments& args)
109
109
return scope.Close (v8::True ());
110
110
}
111
111
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);
112
137
138
+ } CATCH_AND_RETHROW (" Failed to write STL file " );
139
+ return scope.Close (v8::True ());
140
+ }
113
141
114
142
static int extractSubShape (const TopoDS_Shape& shape, std::list<Local<Object> >& shapes)
115
143
{
@@ -195,8 +223,8 @@ static Local<Array> convert(std::list<Local<Object> > & shapes) {
195
223
Local<Array> arr = Array::New ((int )shapes.size ());
196
224
int i=0 ;
197
225
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++;
200
228
}
201
229
return arr;
202
230
}
@@ -227,9 +255,20 @@ struct data_s {
227
255
Persistent<Function> progressCallback;
228
256
std::list<TopoDS_Shape > shapes;
229
257
int retValue;
258
+ double percent;
230
259
double progress;
231
260
};
232
261
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
+ }
233
272
234
273
MyProgressIndicator::MyProgressIndicator (uv_async_t & async)
235
274
:Message_ProgressIndicator(),m_async(async),m_lastValue(0 )
@@ -240,7 +279,8 @@ Standard_Boolean MyProgressIndicator::Show(const Standard_Boolean force)
240
279
data_t * data = (data_t *)this ->m_async .data ;
241
280
double value = this ->GetPosition ();
242
281
double delta = (value-this ->m_lastValue );
243
- if ( delta > 0.05 ) {
282
+ if ( delta > 0.01 ) {
283
+ data->percent = value;
244
284
data->progress = int (delta*1000 );// this->GetPosition();
245
285
this ->m_lastValue = value;
246
286
uv_async_send (&m_async);
@@ -270,8 +310,8 @@ void notify_progress(uv_async_t* handle,int status/*unused*/)
270
310
data_t * data = (data_t *)handle->data ;
271
311
if (!data->progressCallback .IsEmpty ()) {
272
312
// 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);
275
315
}
276
316
}
277
317
@@ -301,13 +341,17 @@ void _readStepAsync(uv_work_t *req) {
301
341
302
342
progress->NewScope (5 ," reading" );
303
343
if (aReader.ReadFile (filename.c_str ()) != IFSelect_RetDone) {
344
+
304
345
std::strstream str;
305
346
str << " cannot read STEP file " << filename << std::ends;
347
+ std::cerr << " cannot read " << std::endl;
306
348
data->message = str.str ();
307
349
// Local<Value> argv[] = { Local<Value>(String::New()) };
308
350
// Local<Value> res = callback->Call(global, 1, argv);
309
351
// return scope.Close(Undefined());
310
352
progress->EndScope ();
353
+ progress->SetValue (105.0 );
354
+ progress->Show ();
311
355
data->retValue = 1 ;
312
356
return ;
313
357
}
@@ -341,6 +385,7 @@ void _readStepAsync(uv_work_t *req) {
341
385
}
342
386
}
343
387
catch (...) {
388
+ std::cerr << " EXCEPTION" << std::endl;
344
389
data->message =" caught C++ exception in readStep" ;
345
390
data->retValue = 1 ;
346
391
return ;
@@ -381,22 +426,10 @@ void _readStepAsyncAfter(uv_work_t *req,int status) {
381
426
Local<Value> argv[2 ] = {Integer::New (data->retValue ), Local<Value>(String::New (data->message .c_str ())) };
382
427
Local<Value> res = data->callback ->Call (Context::GetCurrent ()->Global (), 2 , argv);
383
428
}
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);
392
430
}
393
431
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
+
400
433
void readStepAsync (const std::string& filename,v8::Local<Function> callback,v8::Local<Function> progressCallback)
401
434
{
402
435
data_t * data = new data_t ;
@@ -409,6 +442,9 @@ void readStepAsync(const std::string& filename,v8::Local<Function> callback,v8::
409
442
uv_queue_work (uv_default_loop (), &data->req , _readStepAsync, _readStepAsyncAfter);
410
443
}
411
444
445
+
446
+
447
+
412
448
v8::Handle <Value> readSTEP (const v8::Arguments& args)
413
449
{
414
450
@@ -428,12 +464,71 @@ v8::Handle<Value> readSTEP(const v8::Arguments& args)
428
464
// return ThrowException(Exception::TypeError(String::New("expecting a callback function")));
429
465
}
430
466
431
- Local<Object> global = v8::Context::GetCurrent ()->Global ();
432
467
readStepAsync (filename,callback,progressCallback);
433
-
434
468
return scope.Close (Undefined ());
435
469
}
436
470
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
+
437
532
v8::Handle <Value> readBREP (const v8::Arguments& args)
438
533
{
439
534
HandleScope scope;
@@ -445,30 +540,12 @@ v8::Handle<Value> readBREP(const v8::Arguments& args)
445
540
if (!extractCallback (args[1 ],callback)) {
446
541
return ThrowException (Exception::TypeError (String::New (" expecting a callback function" )));
447
542
}
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);
470
548
return scope.Close (Undefined ());
471
-
472
549
}
473
550
474
551
0 commit comments