This readDXF method is an almost exact duplicate of the one in ImportGui::Module. The only difference is the CDxfRead class derivation that is created. It would seem desirable to have most of this code in just one place, passing it e.g. a pointer to a function that does the 4 lines during the lifetime of the CDxfRead object, but right now Import::Module and ImportGui::Module cannot see each other'
| 381 | // each other's functions so this shared code would need some place to live where |
| 382 | // both places could include a declaration. |
| 383 | Py::Object readDXF(const Py::Tuple& args) |
| 384 | { |
| 385 | char* Name = nullptr; |
| 386 | const char* DocName = nullptr; |
| 387 | const char* optionSource = nullptr; |
| 388 | std::string defaultOptions = "User parameter:BaseApp/Preferences/Mod/Draft"; |
| 389 | bool IgnoreErrors = true; |
| 390 | if ( |
| 391 | !PyArg_ParseTuple(args.ptr(), "et|sbs", "utf-8", &Name, &DocName, &IgnoreErrors, &optionSource) |
| 392 | ) { |
| 393 | throw Py::Exception(); |
| 394 | } |
| 395 | |
| 396 | std::string EncodedName = std::string(Name); |
| 397 | PyMem_Free(Name); |
| 398 | |
| 399 | Base::FileInfo file(EncodedName.c_str()); |
| 400 | if (!file.exists()) { |
| 401 | throw Py::RuntimeError("File doesn't exist"); |
| 402 | } |
| 403 | |
| 404 | if (optionSource) { |
| 405 | defaultOptions = optionSource; |
| 406 | } |
| 407 | |
| 408 | App::Document* pcDoc = nullptr; |
| 409 | if (DocName) { |
| 410 | pcDoc = App::GetApplication().getDocument(DocName); |
| 411 | } |
| 412 | else { |
| 413 | pcDoc = App::GetApplication().getActiveDocument(); |
| 414 | } |
| 415 | if (!pcDoc) { |
| 416 | pcDoc = App::GetApplication().newDocument(DocName); |
| 417 | } |
| 418 | |
| 419 | try { |
| 420 | // read the DXF file |
| 421 | ImpExpDxfRead dxf_file(EncodedName, pcDoc); |
| 422 | dxf_file.setOptionSource(defaultOptions); |
| 423 | dxf_file.setOptions(); |
| 424 | |
| 425 | auto startTime = std::chrono::high_resolution_clock::now(); |
| 426 | dxf_file.DoRead(IgnoreErrors); |
| 427 | auto endTime = std::chrono::high_resolution_clock::now(); |
| 428 | std::chrono::duration<double> elapsed = endTime - startTime; |
| 429 | dxf_file.setImportTime(elapsed.count()); |
| 430 | |
| 431 | pcDoc->recompute(); |
| 432 | return dxf_file.getStatsAsPyObject(); |
| 433 | } |
| 434 | catch (const Standard_Failure& e) { |
| 435 | throw Py::RuntimeError(e.GetMessageString()); |
| 436 | } |
| 437 | catch (const Base::Exception& e) { |
| 438 | throw Py::RuntimeError(e.what()); |
| 439 | } |
| 440 | } |
no test coverage detected