From 095bb48c22c0a712b247d943512b70a9d67ad3ed Mon Sep 17 00:00:00 2001 From: Ian Rees Date: Sun, 19 Feb 2017 12:52:00 +1300 Subject: [PATCH] Tidy up. --- src/Mod/Mesh/App/AppMeshPy.cpp | 30 ++++++++---------------------- src/Mod/Mesh/App/Exporter.cpp | 34 ++++++++++++++++++++++++++-------- src/Mod/Mesh/App/Exporter.h | 17 ++++++++++++++--- 3 files changed, 48 insertions(+), 33 deletions(-) diff --git a/src/Mod/Mesh/App/AppMeshPy.cpp b/src/Mod/Mesh/App/AppMeshPy.cpp index 0b25c12d3..badc123ea 100644 --- a/src/Mod/Mesh/App/AppMeshPy.cpp +++ b/src/Mod/Mesh/App/AppMeshPy.cpp @@ -30,7 +30,6 @@ #include #include -#include #include #include #include @@ -302,10 +301,13 @@ private: static char *kwList[] = {"objectList", "filename", "tolerance", "exportAmfCompressed", NULL}; - // NOTE FOR PYTHON 3: Should switch exportAmfCompressed from using integer 'i' to bool 'p' - // TODO Deal with above nicely, and double check that the docstring is correct with regards to tol's default - if (!PyArg_ParseTupleAndKeywords( args.ptr(), keywds.ptr(), "Oet|fi", kwList, &objects, - "utf-8", &fileNamePy, + if (!PyArg_ParseTupleAndKeywords( args.ptr(), keywds.ptr(), +#if PY_MAJOR_VERSION >= 3 + "Oet|fp", +#else + "Oet|fi", +#endif // Python version switch + kwList, &objects, "utf-8", &fileNamePy, &fTolerance, &exportAmfCompressed )) { throw Py::Exception(); } @@ -331,29 +333,13 @@ private: exporter.reset( new MergeExporter(outputFileName, exportFormat) ); } - const auto meshFeatId( Base::Type::fromName("Mesh::Feature") ); - const auto partFeatId( Base::Type::fromName("Part::Feature") ); - const auto appPartId( Base::Type::fromName("App::Part") ); - const auto appDOGId( Base::Type::fromName("App::DocumentObjectGroup") ); - Py::Sequence list(objects); for (auto it : list) { PyObject *item = it.ptr(); if (PyObject_TypeCheck(item, &(App::DocumentObjectPy::Type))) { auto obj( static_cast(item)->getDocumentObjectPtr() ); - if (obj->getTypeId().isDerivedFrom(meshFeatId)) { - exporter->addMeshFeat( obj ); - } else if (obj->getTypeId().isDerivedFrom(partFeatId)) { - exporter->addPartFeat( obj, fTolerance ); - } else if ( obj->getTypeId().isDerivedFrom(appPartId) || - obj->getTypeId().isDerivedFrom(appDOGId) ) { - exporter->addAppGroup( obj, fTolerance ); - } else { - Base::Console().Message( - "'%s' is of type %s, and can not be exported as a mesh.\n", - obj->Label.getValue(), obj->getTypeId().getName() ); - } + exporter->addObject(obj, fTolerance); } } exporter.reset(); // deletes Exporter, mesh file is written by destructor diff --git a/src/Mod/Mesh/App/Exporter.cpp b/src/Mod/Mesh/App/Exporter.cpp index 4fbab116b..532aa5b49 100644 --- a/src/Mod/Mesh/App/Exporter.cpp +++ b/src/Mod/Mesh/App/Exporter.cpp @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (c) Ian Rees * + * Copyright (c) 2017 Ian Rees * * * * This file is part of the FreeCAD CAx development system. * * * @@ -31,6 +31,7 @@ #include "Core/Iterator.h" +#include "Base/Console.h" #include "Base/Exception.h" #include "Base/FileInfo.h" #include "Base/Sequencer.h" @@ -44,6 +45,13 @@ using namespace Mesh; using namespace MeshCore; +Exporter::Exporter() : + meshFeatId( Base::Type::fromName("Mesh::Feature") ), + partFeatId( Base::Type::fromName("Part::Feature") ), + appPartId( Base::Type::fromName("App::Part") ), + appDOGId( Base::Type::fromName("App::DocumentObjectGroup") ) +{ } + //static std::string Exporter::xmlEscape(const std::string &input) { @@ -58,11 +66,6 @@ std::string Exporter::xmlEscape(const std::string &input) bool Exporter::addAppGroup(App::DocumentObject *obj, float tol) { - const auto meshFeatId( Base::Type::fromName("Mesh::Feature") ); - const auto partFeatId( Base::Type::fromName("Part::Feature") ); - const auto appPartId( Base::Type::fromName("App::Part") ); - const auto appDOGId( Base::Type::fromName("App::DocumentObjectGroup") ); - auto ret(true); for (auto it : static_cast(obj)->getOutList()) { @@ -80,6 +83,22 @@ bool Exporter::addAppGroup(App::DocumentObject *obj, float tol) return ret; } +bool Exporter::addObject(App::DocumentObject *obj, float tol) +{ + if (obj->getTypeId().isDerivedFrom(meshFeatId)) { + return addMeshFeat( obj ); + } else if (obj->getTypeId().isDerivedFrom(partFeatId)) { + return addPartFeat( obj, tol ); + } else if ( obj->getTypeId().isDerivedFrom(appPartId) || + obj->getTypeId().isDerivedFrom(appDOGId) ) { + return addAppGroup( obj, tol ); + } else { + Base::Console().Message( + "'%s' is of type %s, and can not be exported as a mesh.\n", + obj->Label.getValue(), obj->getTypeId().getName() ); + return false; + } +} MergeExporter::MergeExporter(std::string fileName, MeshIO::Format) :fName(fileName) @@ -241,7 +260,7 @@ AmfExporter::~AmfExporter() bool AmfExporter::addPartFeat(App::DocumentObject *obj, float tol) { auto *shape(obj->getPropertyByName("Shape")); - // TODO: Look into a different way to extract mesh with vertex normals + if (shape && shape->getTypeId().isDerivedFrom(App::PropertyComplexGeoData::getClassTypeId())) { Base::Reference mesh(new MeshObject()); @@ -269,7 +288,6 @@ bool AmfExporter::addPartFeat(App::DocumentObject *obj, float tol) bool AmfExporter::addMeshFeat(App::DocumentObject *obj) { - // TODO: Add name, colour, etc. from the mesh feature const MeshObject &mesh( static_cast(obj)->Mesh.getValue() ); MeshCore::MeshKernel kernel( mesh.getKernel() ); kernel.Transform(mesh.getTransform()); diff --git a/src/Mod/Mesh/App/Exporter.h b/src/Mod/Mesh/App/Exporter.h index cbb9db1d1..d2f195bde 100644 --- a/src/Mod/Mesh/App/Exporter.h +++ b/src/Mod/Mesh/App/Exporter.h @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (c) Ian Rees * + * Copyright (c) 2017 Ian Rees * * * * This file is part of the FreeCAD CAx development system. * * * @@ -26,7 +26,9 @@ #include #include -#include +#include "Base/Type.h" + +#include "App/Property.h" #include "MeshFeature.h" #include "Core/MeshIO.h" @@ -48,6 +50,8 @@ namespace Mesh class Exporter { public: + Exporter(); + virtual bool addMeshFeat(App::DocumentObject *obj) = 0; virtual bool addPartFeat(App::DocumentObject *obj, float tol) = 0; @@ -57,12 +61,19 @@ class Exporter * added successfully. */ bool addAppGroup(App::DocumentObject *obj, float tol); + + bool addObject(App::DocumentObject *obj, float tol); + virtual ~Exporter() = default; protected: /// Does some simple escaping of characters for XML-type exports - //TODO: Use xerces or something instead? static std::string xmlEscape(const std::string &input); + + const Base::Type meshFeatId; + const Base::Type partFeatId; + const Base::Type appPartId; + const Base::Type appDOGId; }; /// Creates a single mesh, in a file, from one or more objects