issue #1027 use unicode filepaths

This commit is contained in:
Sebastian Hoogen
2014-09-21 23:45:32 +02:00
committed by wmayer
parent 7db2cdc008
commit 01cf0f5872
28 changed files with 396 additions and 243 deletions

View File

@@ -143,13 +143,15 @@ extern const char* BRepBuilderAPI_FaceErrorText(BRepBuilderAPI_FaceError fe);
/* module functions */
static PyObject * open(PyObject *self, PyObject *args)
{
const char* Name;
if (!PyArg_ParseTuple(args, "s",&Name))
return NULL;
char* Name;
if (!PyArg_ParseTuple(args, "et","utf-8",&Name))
return NULL;
std::string EncodedName = std::string(Name);
PyMem_Free(Name);
PY_TRY {
//Base::Console().Log("Open in Part with %s",Name);
Base::FileInfo file(Name);
Base::FileInfo file(EncodedName.c_str());
// extract ending
if (file.extension() == "")
@@ -159,7 +161,7 @@ static PyObject * open(PyObject *self, PyObject *args)
// create new document and add Import feature
App::Document *pcDoc = App::GetApplication().newDocument("Unnamed");
#if 1
ImportStepParts(pcDoc,Name);
ImportStepParts(pcDoc,EncodedName.c_str());
#else
Part::ImportStep *pcFeature = (Part::ImportStep *)pcDoc->addObject("Part::ImportStep",file.fileNamePure().c_str());
pcFeature->FileName.setValue(Name);
@@ -169,14 +171,14 @@ static PyObject * open(PyObject *self, PyObject *args)
#if 1
else if (file.hasExtension("igs") || file.hasExtension("iges")) {
App::Document *pcDoc = App::GetApplication().newDocument("Unnamed");
ImportIgesParts(pcDoc,Name);
ImportIgesParts(pcDoc,EncodedName.c_str());
pcDoc->recompute();
}
#endif
else {
try {
TopoShape shape;
shape.read(Name);
shape.read(EncodedName.c_str());
// create new document set loaded shape
App::Document *pcDoc = App::GetApplication().newDocument(file.fileNamePure().c_str());
@@ -197,14 +199,16 @@ static PyObject * open(PyObject *self, PyObject *args)
/* module functions */
static PyObject * insert(PyObject *self, PyObject *args)
{
const char* Name;
char* Name;
const char* DocName;
if (!PyArg_ParseTuple(args, "ss",&Name,&DocName))
return NULL;
if (!PyArg_ParseTuple(args, "ets","utf-8",&Name,&DocName))
return NULL;
std::string EncodedName = std::string(Name);
PyMem_Free(Name);
PY_TRY {
//Base::Console().Log("Insert in Part with %s",Name);
Base::FileInfo file(Name);
Base::FileInfo file(EncodedName.c_str());
// extract ending
if (file.extension() == "")
@@ -216,7 +220,7 @@ static PyObject * insert(PyObject *self, PyObject *args)
if (file.hasExtension("stp") || file.hasExtension("step")) {
#if 1
ImportStepParts(pcDoc,Name);
ImportStepParts(pcDoc,EncodedName.c_str());
#else
// add Import feature
Part::ImportStep *pcFeature = (Part::ImportStep *)pcDoc->addObject("Part::ImportStep",file.fileNamePure().c_str());
@@ -226,14 +230,14 @@ static PyObject * insert(PyObject *self, PyObject *args)
}
#if 1
else if (file.hasExtension("igs") || file.hasExtension("iges")) {
ImportIgesParts(pcDoc,Name);
ImportIgesParts(pcDoc,EncodedName.c_str());
pcDoc->recompute();
}
#endif
else {
try {
TopoShape shape;
shape.read(Name);
shape.read(EncodedName.c_str());
Part::Feature *object = static_cast<Part::Feature *>(pcDoc->addObject
("Part::Feature",file.fileNamePure().c_str()));
@@ -253,9 +257,11 @@ static PyObject * insert(PyObject *self, PyObject *args)
static PyObject * exporter(PyObject *self, PyObject *args)
{
PyObject* object;
const char* filename;
if (!PyArg_ParseTuple(args, "Os",&object,&filename))
char* Name;
if (!PyArg_ParseTuple(args, "Oet",&object,"utf-8",&Name))
return NULL;
std::string EncodedName = std::string(Name);
PyMem_Free(Name);
BRep_Builder builder;
TopoDS_Compound comp;
@@ -280,7 +286,7 @@ static PyObject * exporter(PyObject *self, PyObject *args)
}
TopoShape shape(comp);
shape.write(filename);
shape.write(EncodedName.c_str());
} PY_CATCH_OCC;
@@ -290,12 +296,14 @@ static PyObject * exporter(PyObject *self, PyObject *args)
/* module functions */
static PyObject * read(PyObject *self, PyObject *args)
{
const char* Name;
if (!PyArg_ParseTuple(args, "s",&Name))
return NULL;
char* Name;
if (!PyArg_ParseTuple(args, "et","utf-8",&Name))
return NULL;
std::string EncodedName = std::string(Name);
PyMem_Free(Name);
PY_TRY {
TopoShape* shape = new TopoShape();
shape->read(Name);
shape->read(EncodedName.c_str());
return new TopoShapePy(shape);
} PY_CATCH_OCC;
}

View File

@@ -244,6 +244,7 @@ SET(Part_SRCS
modelRefine.cpp
modelRefine.h
Tools.h
encodeFilename.h
OCCError.h
FT2FC.cpp
FT2FC.h

View File

@@ -76,6 +76,7 @@
#include "ImportStep.h"
#include "PartFeature.h"
#include "ProgressIndicator.h"
#include "encodeFilename.h"
using namespace Part;
@@ -95,8 +96,11 @@ int Part::ImportStepParts(App::Document *pcDoc, const char* Name)
str << "File '" << Name << "' does not exist!";
throw Base::Exception(str.str().c_str());
}
std::string encodednamestr = encodeFilename(std::string(Name));
const char * encodedname = encodednamestr.c_str();
if (aReader.ReadFile((Standard_CString)Name) != IFSelect_RetDone) {
if (aReader.ReadFile((Standard_CString)encodedname) !=
IFSelect_RetDone) {
throw Base::Exception("Cannot open STEP file");
}

View File

@@ -166,6 +166,7 @@
#include "ProgressIndicator.h"
#include "modelRefine.h"
#include "Tools.h"
#include "encodeFilename.h"
using namespace Part;
@@ -570,8 +571,7 @@ void TopoShape::importIges(const char *FileName)
IGESControl_Controller::Init();
Interface_Static::SetIVal("read.surfacecurve.mode",3);
IGESControl_Reader aReader;
QString fn = QString::fromUtf8(FileName);
if (aReader.ReadFile((const char*)fn.toLocal8Bit()) != IFSelect_RetDone)
if (aReader.ReadFile(encodeFilename(FileName).c_str()) != IFSelect_RetDone)
throw Base::Exception("Error in reading IGES");
Handle_Message_ProgressIndicator pi = new ProgressIndicator(100);
@@ -596,8 +596,7 @@ void TopoShape::importStep(const char *FileName)
{
try {
STEPControl_Reader aReader;
QString fn = QString::fromUtf8(FileName);
if (aReader.ReadFile((const char*)fn.toLocal8Bit()) != IFSelect_RetDone)
if (aReader.ReadFile(encodeFilename(FileName).c_str()) != IFSelect_RetDone)
throw Base::Exception("Error in reading STEP");
Handle_Message_ProgressIndicator pi = new ProgressIndicator(100);
@@ -627,8 +626,7 @@ void TopoShape::importBrep(const char *FileName)
Handle_Message_ProgressIndicator pi = new ProgressIndicator(100);
pi->NewScope(100, "Reading BREP file...");
pi->Show();
QString fn = QString::fromUtf8(FileName);
BRepTools::Read(aShape,(const char*)fn.toLocal8Bit(),aBuilder,pi);
BRepTools::Read(aShape,encodeFilename(FileName).c_str(),aBuilder,pi);
pi->EndScope();
#else
BRepTools::Read(aShape,(const Standard_CString)FileName,aBuilder);
@@ -699,8 +697,7 @@ void TopoShape::exportIges(const char *filename) const
IGESControl_Writer aWriter;
aWriter.AddShape(this->_Shape);
aWriter.ComputeModel();
QString fn = QString::fromUtf8(filename);
if (aWriter.Write((const char*)fn.toLocal8Bit()) != IFSelect_RetDone)
if (aWriter.Write(encodeFilename(filename).c_str()) != IFSelect_RetDone)
throw Base::Exception("Writing of IGES failed");
}
catch (Standard_Failure) {
@@ -724,14 +721,13 @@ void TopoShape::exportStep(const char *filename) const
throw Base::Exception("Error in transferring STEP");
APIHeaderSection_MakeHeader makeHeader(aWriter.Model());
makeHeader.SetName(new TCollection_HAsciiString((const Standard_CString)filename));
makeHeader.SetName(new TCollection_HAsciiString((const Standard_CString)(encodeFilename(filename).c_str())));
makeHeader.SetAuthorValue (1, new TCollection_HAsciiString("FreeCAD"));
makeHeader.SetOrganizationValue (1, new TCollection_HAsciiString("FreeCAD"));
makeHeader.SetOriginatingSystem(new TCollection_HAsciiString("FreeCAD"));
makeHeader.SetDescriptionValue(1, new TCollection_HAsciiString("FreeCAD Model"));
QString fn = QString::fromUtf8(filename);
if (aWriter.Write((const char*)fn.toLocal8Bit()) != IFSelect_RetDone)
if (aWriter.Write(encodeFilename(filename).c_str()) != IFSelect_RetDone)
throw Base::Exception("Writing of STEP failed");
pi->EndScope();
}
@@ -743,8 +739,7 @@ void TopoShape::exportStep(const char *filename) const
void TopoShape::exportBrep(const char *filename) const
{
QString fn = QString::fromUtf8(filename);
if (!BRepTools::Write(this->_Shape,(const char*)fn.toLocal8Bit()))
if (!BRepTools::Write(this->_Shape,encodeFilename(filename).c_str()))
throw Base::Exception("Writing of BREP failed");
}
@@ -765,8 +760,7 @@ void TopoShape::exportStl(const char *filename, double deflection) const
writer.RelativeMode() = false;
writer.SetDeflection(deflection);
}
QString fn = QString::fromUtf8(filename);
writer.Write(this->_Shape,(const Standard_CString)fn.toLocal8Bit());
writer.Write(this->_Shape,encodeFilename(filename).c_str());
}
void TopoShape::exportFaceSet(double dev, double ca, std::ostream& str) const

View File

@@ -260,11 +260,13 @@ PyObject* TopoShapePy::removeShape(PyObject *args)
PyObject* TopoShapePy::read(PyObject *args)
{
char* filename;
if (!PyArg_ParseTuple(args, "s", &filename))
char* Name;
if (!PyArg_ParseTuple(args, "et","utf-8",&Name))
return NULL;
std::string EncodedName = std::string(Name);
PyMem_Free(Name);
getTopoShapePtr()->read(filename);
getTopoShapePtr()->read(EncodedName.c_str());
Py_Return;
}
@@ -292,13 +294,15 @@ PyObject* TopoShapePy::writeInventor(PyObject * args)
PyObject* TopoShapePy::exportIges(PyObject *args)
{
char* filename;
if (!PyArg_ParseTuple(args, "s", &filename))
char* Name;
if (!PyArg_ParseTuple(args, "et","utf-8",&Name))
return NULL;
std::string EncodedName = std::string(Name);
PyMem_Free(Name);
try {
// write iges file
getTopoShapePtr()->exportIges(filename);
getTopoShapePtr()->exportIges(EncodedName.c_str());
}
catch (const Base::Exception& e) {
PyErr_SetString(PartExceptionOCCError,e.what());
@@ -310,13 +314,15 @@ PyObject* TopoShapePy::exportIges(PyObject *args)
PyObject* TopoShapePy::exportStep(PyObject *args)
{
char* filename;
if (!PyArg_ParseTuple(args, "s", &filename))
char* Name;
if (!PyArg_ParseTuple(args, "et","utf-8",&Name))
return NULL;
std::string EncodedName = std::string(Name);
PyMem_Free(Name);
try {
// write step file
getTopoShapePtr()->exportStep(filename);
getTopoShapePtr()->exportStep(EncodedName.c_str());
}
catch (const Base::Exception& e) {
PyErr_SetString(PartExceptionOCCError,e.what());
@@ -328,13 +334,15 @@ PyObject* TopoShapePy::exportStep(PyObject *args)
PyObject* TopoShapePy::exportBrep(PyObject *args)
{
char* filename;
if (!PyArg_ParseTuple(args, "s", &filename))
char* Name;
if (!PyArg_ParseTuple(args, "et","utf-8",&Name))
return NULL;
std::string EncodedName = std::string(Name);
PyMem_Free(Name);
try {
// write brep file
getTopoShapePtr()->exportBrep(filename);
getTopoShapePtr()->exportBrep(EncodedName.c_str());
}
catch (const Base::Exception& e) {
PyErr_SetString(PartExceptionOCCError,e.what());
@@ -449,14 +457,16 @@ PyObject* TopoShapePy::importBrepFromString(PyObject *args)
PyObject* TopoShapePy::exportStl(PyObject *args)
{
char* filename;
double deflection = 0;
if (!PyArg_ParseTuple(args, "s|d", &filename, &deflection))
char* Name;
if (!PyArg_ParseTuple(args, "et|d","utf-8",&Name,&deflection))
return NULL;
std::string EncodedName = std::string(Name);
PyMem_Free(Name);
try {
// write stl file
getTopoShapePtr()->exportStl(filename, deflection);
getTopoShapePtr()->exportStl(EncodedName.c_str(), deflection);
}
catch (const Base::Exception& e) {
PyErr_SetString(PartExceptionOCCError,e.what());

View File

@@ -0,0 +1,48 @@
/***************************************************************************
* Copyright (c) 2014 Sebastian Hoogen <github[at]sebastianhoogen.de> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#ifndef PART_ENCODEFILENAME_H
#define PART_ENCODEFILENAME_H
#if (OCC_VERSION_HEX < 0x060800 && defined(_WIN32))
#include <QString>
#endif
namespace Part
{
inline std::string encodeFilename(std::string fn)
{
#if (OCC_VERSION_HEX < 0x060800 && defined(_WIN32))
// Workaround to support latin1 characters in path until OCCT supports
// conversion from UTF8 to wchar_t on windows
// http://tracker.dev.opencascade.org/view.php?id=22484
QByteArray str8bit = QString::fromUtf8(fn.c_str()).toLocal8Bit();
return std::string(str8bit.constData());
#else
return fn;
#endif
}
} //namespace Part
#endif // PART_ENCODEFILENAME_H