Skip to content

Commit

Permalink
Tidy up.
Browse files Browse the repository at this point in the history
  • Loading branch information
ianrrees authored and wwmayer committed Mar 2, 2017
1 parent 944aa01 commit 095bb48
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 33 deletions.
30 changes: 8 additions & 22 deletions src/Mod/Mesh/App/AppMeshPy.cpp
Expand Up @@ -30,7 +30,6 @@
#include <CXX/Extensions.hxx>
#include <CXX/Objects.hxx>

#include <Base/Console.h>
#include <Base/Interpreter.h>
#include <Base/FileInfo.h>
#include <Base/Tools.h>
Expand Down Expand Up @@ -302,10 +301,13 @@ class Module : public Py::ExtensionModule<Module>
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();
}
Expand All @@ -331,29 +333,13 @@ class Module : public Py::ExtensionModule<Module>
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<App::DocumentObjectPy *>(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
Expand Down
34 changes: 26 additions & 8 deletions src/Mod/Mesh/App/Exporter.cpp
@@ -1,5 +1,5 @@
/***************************************************************************
* Copyright (c) Ian Rees <ian.rees@gmail.com> *
* Copyright (c) 2017 Ian Rees <ian.rees@gmail.com> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
Expand Down Expand Up @@ -31,6 +31,7 @@

#include "Core/Iterator.h"

#include "Base/Console.h"
#include "Base/Exception.h"
#include "Base/FileInfo.h"
#include "Base/Sequencer.h"
Expand All @@ -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)
{
Expand All @@ -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<App::Part *>(obj)->getOutList()) {
Expand All @@ -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)
Expand Down Expand Up @@ -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<MeshObject> mesh(new MeshObject());

Expand Down Expand Up @@ -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<Mesh::Feature *>(obj)->Mesh.getValue() );
MeshCore::MeshKernel kernel( mesh.getKernel() );
kernel.Transform(mesh.getTransform());
Expand Down
17 changes: 14 additions & 3 deletions src/Mod/Mesh/App/Exporter.h
@@ -1,5 +1,5 @@
/***************************************************************************
* Copyright (c) Ian Rees <ian.rees@gmail.com> *
* Copyright (c) 2017 Ian Rees <ian.rees@gmail.com> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
Expand All @@ -26,7 +26,9 @@
#include <map>
#include <ostream>

#include <App/Property.h>
#include "Base/Type.h"

#include "App/Property.h"

#include "MeshFeature.h"
#include "Core/MeshIO.h"
Expand All @@ -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;

Expand All @@ -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
Expand Down

0 comments on commit 095bb48

Please sign in to comment.