Open new track for importing Assembly structures with OCSF reader.
This commit is contained in:
parent
4ad93594a6
commit
6344c42525
|
@ -31,6 +31,7 @@
|
|||
#include <Gui/MainWindow.h>
|
||||
#include <Gui/FileDialog.h>
|
||||
#include <Gui/Selection.h>
|
||||
#include <Gui/WaitCursor.h>
|
||||
|
||||
#include <Mod/Assembly/App/ItemAssembly.h>
|
||||
|
||||
|
@ -182,31 +183,88 @@ void CmdAssemblyAddExistingComponent::activated(int iMsg)
|
|||
// asking for file name (only step at the moment)
|
||||
QStringList filter;
|
||||
filter << QString::fromAscii("STEP (*.stp *.step)");
|
||||
filter << QString::fromAscii("STEP with colors (*.stp *.step)");
|
||||
filter << QString::fromAscii("IGES (*.igs *.iges)");
|
||||
filter << QString::fromAscii("IGES with colors (*.igs *.iges)");
|
||||
filter << QString::fromAscii("BREP (*.brp *.brep)");
|
||||
filter << QString::fromAscii("Mesh (*.stl *.obj)");
|
||||
filter << QString::fromAscii("VRML (*.wrl)");
|
||||
|
||||
QString select;
|
||||
QString fn = Gui::FileDialog::getOpenFileName(Gui::getMainWindow(), QString(), QString(), filter.join(QLatin1String(";;")), &select);
|
||||
if (!fn.isEmpty()) {
|
||||
|
||||
|
||||
openCommand("Import ExtPart");
|
||||
addModule(Doc,"Part");
|
||||
addModule(Doc,"PartDesign");
|
||||
addModule(Gui,"PartDesignGui");
|
||||
addModule(Gui,"AssemblyGui");
|
||||
addModule(Gui,"AssemblyLib");
|
||||
|
||||
std::string fName( (const char*)fn.toUtf8());
|
||||
|
||||
doCommand(Gui,"AssemblyLib.importAssembly('%s',App.ActiveDocument.%s)",fName.c_str(), dest->getNameInDocument());
|
||||
|
||||
Gui::WaitCursor wc;
|
||||
App::Document* pDoc = getDocument();
|
||||
if (!pDoc) return; // no document
|
||||
openCommand("Import an Assembly");
|
||||
if (select == filter[1] ||
|
||||
select == filter[3]) {
|
||||
doCommand(Doc, "import ImportGui");
|
||||
doCommand(Doc, "ImportGui.insert(\"%s\",\"%s\")", (const char*)fn.toUtf8(), pDoc->getName());
|
||||
}
|
||||
else {
|
||||
doCommand(Doc, "import Part");
|
||||
doCommand(Doc, "Part.insert(\"%s\",\"%s\")", (const char*)fn.toUtf8(), pDoc->getName());
|
||||
}
|
||||
commitCommand();
|
||||
this->updateActive();
|
||||
}
|
||||
}
|
||||
|
||||
//===========================================================================
|
||||
|
||||
DEF_STD_CMD(CmdAssemblyImport);
|
||||
|
||||
CmdAssemblyImport::CmdAssemblyImport()
|
||||
:Command("Assembly_Import")
|
||||
{
|
||||
sAppModule = "Assembly";
|
||||
sGroup = QT_TR_NOOP("Assembly");
|
||||
sMenuText = QT_TR_NOOP("Import assembly...");
|
||||
sToolTipText = QT_TR_NOOP("Import one or more files and create a assembly structure.");
|
||||
sWhatsThis = sToolTipText;
|
||||
sStatusTip = sToolTipText;
|
||||
sPixmap = 0;
|
||||
}
|
||||
|
||||
|
||||
void CmdAssemblyImport::activated(int iMsg)
|
||||
{
|
||||
Assembly::ItemAssembly *dest = 0;
|
||||
|
||||
unsigned int n = getSelection().countObjectsOfType(Assembly::ItemAssembly::getClassTypeId());
|
||||
if (n >= 1) {
|
||||
std::vector<App::DocumentObject*> Sel = getSelection().getObjectsOfType(Assembly::ItemAssembly::getClassTypeId());
|
||||
dest = dynamic_cast<Assembly::ItemAssembly*>(Sel.front());
|
||||
}else if(ActiveAsmObject && ActiveAsmObject->getTypeId().isDerivedFrom(Assembly::ItemAssembly::getClassTypeId())) {
|
||||
dest = dynamic_cast<Assembly::ItemAssembly*>(ActiveAsmObject);
|
||||
}
|
||||
|
||||
// asking for file name (only step at the moment)
|
||||
QStringList filter;
|
||||
filter << QString::fromAscii("STEP (*.stp *.step)");
|
||||
filter << QString::fromAscii("IGES (*.igs *.iges)");
|
||||
filter << QString::fromAscii("BREP (*.brp *.brep)");
|
||||
filter << QString::fromAscii("Mesh (*.stl *.obj)");
|
||||
filter << QString::fromAscii("VRML (*.wrl)");
|
||||
|
||||
QString select;
|
||||
QString fn = Gui::FileDialog::getOpenFileName(Gui::getMainWindow(), QString(), QString(), filter.join(QLatin1String(";;")), &select);
|
||||
if (!fn.isEmpty()) {
|
||||
Gui::WaitCursor wc;
|
||||
App::Document* pDoc = getDocument();
|
||||
if (!pDoc) return; // no document
|
||||
openCommand("Import an Assembly");
|
||||
if (select == filter[0] ||
|
||||
select == filter[1]) {
|
||||
doCommand(Doc, "import Import");
|
||||
doCommand(Doc, "Import.importAssembly('%s')", (const char*)fn.toUtf8());
|
||||
}
|
||||
else {
|
||||
return;
|
||||
}
|
||||
commitCommand();
|
||||
this->updateActive();
|
||||
}
|
||||
}
|
||||
void CreateAssemblyCommands(void)
|
||||
{
|
||||
Gui::CommandManager &rcCmdMgr = Gui::Application::Instance->commandManager();
|
||||
|
@ -214,4 +272,5 @@ void CreateAssemblyCommands(void)
|
|||
rcCmdMgr.addCommand(new CmdAssemblyAddNewPart());
|
||||
rcCmdMgr.addCommand(new CmdAssemblyAddNewComponent());
|
||||
rcCmdMgr.addCommand(new CmdAssemblyAddExistingComponent());
|
||||
rcCmdMgr.addCommand(new CmdAssemblyImport());
|
||||
}
|
||||
|
|
|
@ -88,6 +88,11 @@ Gui::MenuItem* Workbench::setupMenuBar() const
|
|||
<< "Assembly_AddNewComponent"
|
||||
<< "Assembly_AddExistingComponent";
|
||||
|
||||
Gui::MenuItem* impCmd = new Gui::MenuItem();
|
||||
root->insertItem(asmCmd, impCmd);
|
||||
impCmd->setCommand("&Import");
|
||||
*impCmd << "Assembly_Import";
|
||||
|
||||
|
||||
return root;
|
||||
}
|
||||
|
|
|
@ -52,6 +52,7 @@
|
|||
#include <CXX/Objects.hxx>
|
||||
|
||||
#include "ImportOCAF.h"
|
||||
#include "ImportOCAFAssembly.h"
|
||||
#include <Base/PyObjectBase.h>
|
||||
#include <Base/Console.h>
|
||||
#include <App/Application.h>
|
||||
|
@ -63,7 +64,6 @@
|
|||
#include <Mod/Part/App/ImportStep.h>
|
||||
#include <Mod/Part/App/encodeFilename.h>
|
||||
|
||||
|
||||
namespace Import {
|
||||
class Module : public Py::ExtensionModule<Module>
|
||||
{
|
||||
|
@ -86,6 +86,7 @@ public:
|
|||
|
||||
private:
|
||||
Py::Object importer(const Py::Tuple& args)
|
||||
|
||||
{
|
||||
char* Name;
|
||||
char* DocName=0;
|
||||
|
@ -296,8 +297,109 @@ private:
|
|||
|
||||
return Py::None();
|
||||
}
|
||||
{"importAssembly" ,importAssembly ,METH_VARARGS,
|
||||
"importAssembly(FileName,Target) -- Import a Assembly file and creates a Assembly structure."},
|
||||
};
|
||||
|
||||
PyObject * importAssembly(PyObject *self, PyObject *args)
|
||||
{
|
||||
char* Name;
|
||||
PyObject* TargetObject=0;
|
||||
if (!PyArg_ParseTuple(args, "s|O",&Name,&TargetObject))
|
||||
return 0;
|
||||
|
||||
PY_TRY {
|
||||
//Base::Console().Log("Insert in Part with %s",Name);
|
||||
Base::FileInfo file(Name);
|
||||
|
||||
App::Document *pcDoc = 0;
|
||||
|
||||
pcDoc = App::GetApplication().getActiveDocument();
|
||||
|
||||
if (!pcDoc)
|
||||
pcDoc = App::GetApplication().newDocument("ImportedAssembly");
|
||||
|
||||
|
||||
Handle(XCAFApp_Application) hApp = XCAFApp_Application::GetApplication();
|
||||
Handle(TDocStd_Document) hDoc;
|
||||
hApp->NewDocument(TCollection_ExtendedString("MDTV-CAF"), hDoc);
|
||||
|
||||
if (file.hasExtension("stp") || file.hasExtension("step")) {
|
||||
try {
|
||||
STEPCAFControl_Reader aReader;
|
||||
aReader.SetColorMode(true);
|
||||
aReader.SetNameMode(true);
|
||||
aReader.SetLayerMode(true);
|
||||
if (aReader.ReadFile((Standard_CString)Name) != IFSelect_RetDone) {
|
||||
PyErr_SetString(PyExc_Exception, "cannot read STEP file");
|
||||
return 0;
|
||||
}
|
||||
|
||||
Handle_Message_ProgressIndicator pi = new Part::ProgressIndicator(100);
|
||||
aReader.Reader().WS()->MapReader()->SetProgress(pi);
|
||||
pi->NewScope(100, "Reading STEP file...");
|
||||
pi->Show();
|
||||
aReader.Transfer(hDoc);
|
||||
pi->EndScope();
|
||||
}
|
||||
catch (OSD_Exception) {
|
||||
Handle_Standard_Failure e = Standard_Failure::Caught();
|
||||
Base::Console().Error("%s\n", e->GetMessageString());
|
||||
Base::Console().Message("Try to load STEP file without colors...\n");
|
||||
|
||||
Part::ImportStepParts(pcDoc,Name);
|
||||
pcDoc->recompute();
|
||||
}
|
||||
}
|
||||
else if (file.hasExtension("igs") || file.hasExtension("iges")) {
|
||||
try {
|
||||
IGESControl_Controller::Init();
|
||||
Interface_Static::SetIVal("read.surfacecurve.mode",3);
|
||||
IGESCAFControl_Reader aReader;
|
||||
aReader.SetColorMode(true);
|
||||
aReader.SetNameMode(true);
|
||||
aReader.SetLayerMode(true);
|
||||
if (aReader.ReadFile((Standard_CString)Name) != IFSelect_RetDone) {
|
||||
PyErr_SetString(PyExc_Exception, "cannot read IGES file");
|
||||
return 0;
|
||||
}
|
||||
|
||||
Handle_Message_ProgressIndicator pi = new Part::ProgressIndicator(100);
|
||||
aReader.WS()->MapReader()->SetProgress(pi);
|
||||
pi->NewScope(100, "Reading IGES file...");
|
||||
pi->Show();
|
||||
aReader.Transfer(hDoc);
|
||||
pi->EndScope();
|
||||
}
|
||||
catch (OSD_Exception) {
|
||||
Handle_Standard_Failure e = Standard_Failure::Caught();
|
||||
Base::Console().Error("%s\n", e->GetMessageString());
|
||||
Base::Console().Message("Try to load IGES file without colors...\n");
|
||||
|
||||
Part::ImportIgesParts(pcDoc,Name);
|
||||
pcDoc->recompute();
|
||||
}
|
||||
}
|
||||
else {
|
||||
PyErr_SetString(PyExc_Exception, "no supported file format");
|
||||
return 0;
|
||||
}
|
||||
|
||||
Import::ImportOCAFAssembly ocaf(hDoc, pcDoc, file.fileNamePure());
|
||||
ocaf.loadShapes();
|
||||
pcDoc->recompute();
|
||||
|
||||
}
|
||||
catch (Standard_Failure) {
|
||||
Handle_Standard_Failure e = Standard_Failure::Caught();
|
||||
PyErr_SetString(PyExc_Exception, e->GetMessageString());
|
||||
return 0;
|
||||
}
|
||||
PY_CATCH
|
||||
|
||||
Py_Return;
|
||||
}
|
||||
|
||||
PyObject* initModule()
|
||||
{
|
||||
return (new Module)->module().ptr();
|
||||
|
|
427
src/Mod/Import/App/AppImportPy.cpp.orig
Normal file
427
src/Mod/Import/App/AppImportPy.cpp.orig
Normal file
|
@ -0,0 +1,427 @@
|
|||
/***************************************************************************
|
||||
* Copyright (c) 2013 Werner Mayer <wmayer[at]users.sourceforge.net> *
|
||||
* *
|
||||
* 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 *
|
||||
* *
|
||||
***************************************************************************/
|
||||
|
||||
|
||||
#include "PreCompiled.h"
|
||||
#if defined(__MINGW32__)
|
||||
# define WNT // avoid conflict with GUID
|
||||
#endif
|
||||
#ifndef _PreComp_
|
||||
# include <Python.h>
|
||||
# include <climits>
|
||||
# include <Standard_Version.hxx>
|
||||
# include <Handle_TDocStd_Document.hxx>
|
||||
# include <Handle_XCAFApp_Application.hxx>
|
||||
# include <TDocStd_Document.hxx>
|
||||
# include <XCAFApp_Application.hxx>
|
||||
# include <STEPCAFControl_Reader.hxx>
|
||||
# include <STEPCAFControl_Writer.hxx>
|
||||
# include <STEPControl_Writer.hxx>
|
||||
# include <IGESCAFControl_Reader.hxx>
|
||||
# include <IGESCAFControl_Writer.hxx>
|
||||
# include <IGESControl_Controller.hxx>
|
||||
# include <IGESData_GlobalSection.hxx>
|
||||
# include <IGESData_IGESModel.hxx>
|
||||
# include <Interface_Static.hxx>
|
||||
# include <Transfer_TransientProcess.hxx>
|
||||
# include <XSControl_WorkSession.hxx>
|
||||
# include <APIHeaderSection_MakeHeader.hxx>
|
||||
# include <OSD_Exception.hxx>
|
||||
#endif
|
||||
|
||||
#include <CXX/Extensions.hxx>
|
||||
#include <CXX/Objects.hxx>
|
||||
|
||||
#include "ImportOCAF.h"
|
||||
#include "ImportOCAFAssembly.h"
|
||||
#include <Base/PyObjectBase.h>
|
||||
#include <Base/Console.h>
|
||||
#include <App/Application.h>
|
||||
#include <App/Document.h>
|
||||
#include <App/DocumentObjectPy.h>
|
||||
#include <Mod/Part/App/PartFeature.h>
|
||||
#include <Mod/Part/App/ProgressIndicator.h>
|
||||
#include <Mod/Part/App/ImportIges.h>
|
||||
#include <Mod/Part/App/ImportStep.h>
|
||||
#include <Mod/Part/App/encodeFilename.h>
|
||||
|
||||
|
||||
<<<<<<< ba8252ee7a68c3a6f954372590d46fea1c4238da
|
||||
namespace Import {
|
||||
class Module : public Py::ExtensionModule<Module>
|
||||
=======
|
||||
static PyObject * importAssembly(PyObject *self, PyObject *args)
|
||||
{
|
||||
char* Name;
|
||||
PyObject* TargetObject=0;
|
||||
if (!PyArg_ParseTuple(args, "s|O",&Name,&TargetObject))
|
||||
return 0;
|
||||
|
||||
PY_TRY {
|
||||
//Base::Console().Log("Insert in Part with %s",Name);
|
||||
Base::FileInfo file(Name);
|
||||
|
||||
App::Document *pcDoc = 0;
|
||||
|
||||
pcDoc = App::GetApplication().getActiveDocument();
|
||||
|
||||
if (!pcDoc)
|
||||
pcDoc = App::GetApplication().newDocument("ImportedAssembly");
|
||||
|
||||
|
||||
Handle(XCAFApp_Application) hApp = XCAFApp_Application::GetApplication();
|
||||
Handle(TDocStd_Document) hDoc;
|
||||
hApp->NewDocument(TCollection_ExtendedString("MDTV-CAF"), hDoc);
|
||||
|
||||
if (file.hasExtension("stp") || file.hasExtension("step")) {
|
||||
try {
|
||||
STEPCAFControl_Reader aReader;
|
||||
aReader.SetColorMode(true);
|
||||
aReader.SetNameMode(true);
|
||||
aReader.SetLayerMode(true);
|
||||
if (aReader.ReadFile((Standard_CString)Name) != IFSelect_RetDone) {
|
||||
PyErr_SetString(PyExc_Exception, "cannot read STEP file");
|
||||
return 0;
|
||||
}
|
||||
|
||||
Handle_Message_ProgressIndicator pi = new Part::ProgressIndicator(100);
|
||||
aReader.Reader().WS()->MapReader()->SetProgress(pi);
|
||||
pi->NewScope(100, "Reading STEP file...");
|
||||
pi->Show();
|
||||
aReader.Transfer(hDoc);
|
||||
pi->EndScope();
|
||||
}
|
||||
catch (OSD_Exception) {
|
||||
Handle_Standard_Failure e = Standard_Failure::Caught();
|
||||
Base::Console().Error("%s\n", e->GetMessageString());
|
||||
Base::Console().Message("Try to load STEP file without colors...\n");
|
||||
|
||||
Part::ImportStepParts(pcDoc,Name);
|
||||
pcDoc->recompute();
|
||||
}
|
||||
}
|
||||
else if (file.hasExtension("igs") || file.hasExtension("iges")) {
|
||||
try {
|
||||
IGESControl_Controller::Init();
|
||||
Interface_Static::SetIVal("read.surfacecurve.mode",3);
|
||||
IGESCAFControl_Reader aReader;
|
||||
aReader.SetColorMode(true);
|
||||
aReader.SetNameMode(true);
|
||||
aReader.SetLayerMode(true);
|
||||
if (aReader.ReadFile((Standard_CString)Name) != IFSelect_RetDone) {
|
||||
PyErr_SetString(PyExc_Exception, "cannot read IGES file");
|
||||
return 0;
|
||||
}
|
||||
|
||||
Handle_Message_ProgressIndicator pi = new Part::ProgressIndicator(100);
|
||||
aReader.WS()->MapReader()->SetProgress(pi);
|
||||
pi->NewScope(100, "Reading IGES file...");
|
||||
pi->Show();
|
||||
aReader.Transfer(hDoc);
|
||||
pi->EndScope();
|
||||
}
|
||||
catch (OSD_Exception) {
|
||||
Handle_Standard_Failure e = Standard_Failure::Caught();
|
||||
Base::Console().Error("%s\n", e->GetMessageString());
|
||||
Base::Console().Message("Try to load IGES file without colors...\n");
|
||||
|
||||
Part::ImportIgesParts(pcDoc,Name);
|
||||
pcDoc->recompute();
|
||||
}
|
||||
}
|
||||
else {
|
||||
PyErr_SetString(PyExc_Exception, "no supported file format");
|
||||
return 0;
|
||||
}
|
||||
|
||||
Import::ImportOCAFAssembly ocaf(hDoc, pcDoc, file.fileNamePure());
|
||||
ocaf.loadShapes();
|
||||
pcDoc->recompute();
|
||||
|
||||
}
|
||||
catch (Standard_Failure) {
|
||||
Handle_Standard_Failure e = Standard_Failure::Caught();
|
||||
PyErr_SetString(PyExc_Exception, e->GetMessageString());
|
||||
return 0;
|
||||
}
|
||||
PY_CATCH
|
||||
|
||||
Py_Return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static PyObject * importer(PyObject *self, PyObject *args)
|
||||
>>>>>>> Open new track for importing Assembly structures with OCSF reader.
|
||||
{
|
||||
public:
|
||||
Module() : Py::ExtensionModule<Module>("Import")
|
||||
{
|
||||
add_varargs_method("open",&Module::importer,
|
||||
"open(string) -- Open the file and create a new document."
|
||||
);
|
||||
add_varargs_method("insert",&Module::importer,
|
||||
"insert(string,string) -- Insert the file into the given document."
|
||||
);
|
||||
add_varargs_method("export",&Module::exporter,
|
||||
"export(list,string) -- Export a list of objects into a single file."
|
||||
);
|
||||
initialize("This module is the Import module."); // register with Python
|
||||
}
|
||||
|
||||
virtual ~Module() {}
|
||||
|
||||
private:
|
||||
Py::Object importer(const Py::Tuple& args)
|
||||
{
|
||||
char* Name;
|
||||
char* DocName=0;
|
||||
if (!PyArg_ParseTuple(args.ptr(), "et|s","utf-8",&Name,&DocName))
|
||||
throw Py::Exception();
|
||||
|
||||
std::string Utf8Name = std::string(Name);
|
||||
PyMem_Free(Name);
|
||||
std::string name8bit = Part::encodeFilename(Utf8Name);
|
||||
|
||||
try {
|
||||
//Base::Console().Log("Insert in Part with %s",Name);
|
||||
Base::FileInfo file(Utf8Name.c_str());
|
||||
|
||||
App::Document *pcDoc = 0;
|
||||
if (DocName) {
|
||||
pcDoc = App::GetApplication().getDocument(DocName);
|
||||
}
|
||||
if (!pcDoc) {
|
||||
pcDoc = App::GetApplication().newDocument("Unnamed");
|
||||
}
|
||||
|
||||
Handle(XCAFApp_Application) hApp = XCAFApp_Application::GetApplication();
|
||||
Handle(TDocStd_Document) hDoc;
|
||||
hApp->NewDocument(TCollection_ExtendedString("MDTV-CAF"), hDoc);
|
||||
|
||||
if (file.hasExtension("stp") || file.hasExtension("step")) {
|
||||
try {
|
||||
STEPCAFControl_Reader aReader;
|
||||
aReader.SetColorMode(true);
|
||||
aReader.SetNameMode(true);
|
||||
aReader.SetLayerMode(true);
|
||||
if (aReader.ReadFile((Standard_CString)(name8bit.c_str())) != IFSelect_RetDone) {
|
||||
throw Py::Exception(PyExc_IOError, "cannot read STEP file");
|
||||
}
|
||||
|
||||
Handle_Message_ProgressIndicator pi = new Part::ProgressIndicator(100);
|
||||
aReader.Reader().WS()->MapReader()->SetProgress(pi);
|
||||
pi->NewScope(100, "Reading STEP file...");
|
||||
pi->Show();
|
||||
aReader.Transfer(hDoc);
|
||||
pi->EndScope();
|
||||
}
|
||||
catch (OSD_Exception) {
|
||||
Handle_Standard_Failure e = Standard_Failure::Caught();
|
||||
Base::Console().Error("%s\n", e->GetMessageString());
|
||||
Base::Console().Message("Try to load STEP file without colors...\n");
|
||||
|
||||
Part::ImportStepParts(pcDoc,Utf8Name.c_str());
|
||||
pcDoc->recompute();
|
||||
}
|
||||
}
|
||||
else if (file.hasExtension("igs") || file.hasExtension("iges")) {
|
||||
Base::Reference<ParameterGrp> hGrp = App::GetApplication().GetUserParameter()
|
||||
.GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("Mod/Part")->GetGroup("IGES");
|
||||
|
||||
try {
|
||||
IGESControl_Controller::Init();
|
||||
IGESCAFControl_Reader aReader;
|
||||
// http://www.opencascade.org/org/forum/thread_20603/?forum=3
|
||||
aReader.SetReadVisible(hGrp->GetBool("SkipBlankEntities", true)
|
||||
? Standard_True : Standard_False);
|
||||
aReader.SetColorMode(true);
|
||||
aReader.SetNameMode(true);
|
||||
aReader.SetLayerMode(true);
|
||||
if (aReader.ReadFile((Standard_CString)(name8bit.c_str())) != IFSelect_RetDone) {
|
||||
throw Py::Exception(PyExc_IOError, "cannot read IGES file");
|
||||
}
|
||||
|
||||
Handle_Message_ProgressIndicator pi = new Part::ProgressIndicator(100);
|
||||
aReader.WS()->MapReader()->SetProgress(pi);
|
||||
pi->NewScope(100, "Reading IGES file...");
|
||||
pi->Show();
|
||||
aReader.Transfer(hDoc);
|
||||
pi->EndScope();
|
||||
}
|
||||
catch (OSD_Exception) {
|
||||
Handle_Standard_Failure e = Standard_Failure::Caught();
|
||||
Base::Console().Error("%s\n", e->GetMessageString());
|
||||
Base::Console().Message("Try to load IGES file without colors...\n");
|
||||
|
||||
Part::ImportIgesParts(pcDoc,Utf8Name.c_str());
|
||||
pcDoc->recompute();
|
||||
}
|
||||
}
|
||||
else {
|
||||
throw Py::Exception(Base::BaseExceptionFreeCADError, "no supported file format");
|
||||
}
|
||||
|
||||
#if 1
|
||||
Import::ImportOCAF ocaf(hDoc, pcDoc, file.fileNamePure());
|
||||
ocaf.loadShapes();
|
||||
#else
|
||||
Import::ImportXCAF xcaf(hDoc, pcDoc, file.fileNamePure());
|
||||
xcaf.loadShapes();
|
||||
#endif
|
||||
pcDoc->recompute();
|
||||
}
|
||||
catch (Standard_Failure) {
|
||||
Handle_Standard_Failure e = Standard_Failure::Caught();
|
||||
throw Py::Exception(Base::BaseExceptionFreeCADError, e->GetMessageString());
|
||||
}
|
||||
catch (const Base::Exception& e) {
|
||||
throw Py::RuntimeError(e.what());
|
||||
}
|
||||
|
||||
return Py::None();
|
||||
}
|
||||
Py::Object exporter(const Py::Tuple& args)
|
||||
{
|
||||
PyObject* object;
|
||||
char* Name;
|
||||
if (!PyArg_ParseTuple(args.ptr(), "Oet",&object,"utf-8",&Name))
|
||||
throw Py::Exception();
|
||||
|
||||
std::string Utf8Name = std::string(Name);
|
||||
PyMem_Free(Name);
|
||||
std::string name8bit = Part::encodeFilename(Utf8Name);
|
||||
|
||||
try {
|
||||
Handle(XCAFApp_Application) hApp = XCAFApp_Application::GetApplication();
|
||||
Handle(TDocStd_Document) hDoc;
|
||||
hApp->NewDocument(TCollection_ExtendedString("MDTV-CAF"), hDoc);
|
||||
Import::ExportOCAF ocaf(hDoc);
|
||||
|
||||
Py::Sequence list(object);
|
||||
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
|
||||
PyObject* item = (*it).ptr();
|
||||
if (PyObject_TypeCheck(item, &(App::DocumentObjectPy::Type))) {
|
||||
App::DocumentObject* obj = static_cast<App::DocumentObjectPy*>(item)->getDocumentObjectPtr();
|
||||
if (obj->getTypeId().isDerivedFrom(Part::Feature::getClassTypeId())) {
|
||||
Part::Feature* part = static_cast<Part::Feature*>(obj);
|
||||
std::vector<App::Color> colors;
|
||||
ocaf.saveShape(part, colors);
|
||||
}
|
||||
else {
|
||||
Base::Console().Message("'%s' is not a shape, export will be ignored.\n", obj->Label.getValue());
|
||||
}
|
||||
}
|
||||
else if (PyTuple_Check(item) && PyTuple_Size(item) == 2) {
|
||||
Py::Tuple tuple(*it);
|
||||
Py::Object item0 = tuple.getItem(0);
|
||||
Py::Object item1 = tuple.getItem(1);
|
||||
if (PyObject_TypeCheck(item0.ptr(), &(App::DocumentObjectPy::Type))) {
|
||||
App::DocumentObject* obj = static_cast<App::DocumentObjectPy*>(item0.ptr())->getDocumentObjectPtr();
|
||||
if (obj->getTypeId().isDerivedFrom(Part::Feature::getClassTypeId())) {
|
||||
Part::Feature* part = static_cast<Part::Feature*>(obj);
|
||||
App::PropertyColorList colors;
|
||||
colors.setPyObject(item1.ptr());
|
||||
ocaf.saveShape(part, colors.getValues());
|
||||
}
|
||||
else {
|
||||
Base::Console().Message("'%s' is not a shape, export will be ignored.\n", obj->Label.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Base::FileInfo file(Utf8Name.c_str());
|
||||
if (file.hasExtension("stp") || file.hasExtension("step")) {
|
||||
//Interface_Static::SetCVal("write.step.schema", "AP214IS");
|
||||
STEPCAFControl_Writer writer;
|
||||
writer.Transfer(hDoc, STEPControl_AsIs);
|
||||
|
||||
// edit STEP header
|
||||
#if OCC_VERSION_HEX >= 0x060500
|
||||
APIHeaderSection_MakeHeader makeHeader(writer.ChangeWriter().Model());
|
||||
#else
|
||||
APIHeaderSection_MakeHeader makeHeader(writer.Writer().Model());
|
||||
#endif
|
||||
Base::Reference<ParameterGrp> hGrp = App::GetApplication().GetUserParameter()
|
||||
.GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("Mod/Part")->GetGroup("STEP");
|
||||
|
||||
makeHeader.SetName(new TCollection_HAsciiString((const Standard_CString)Utf8Name.c_str()));
|
||||
makeHeader.SetAuthorValue (1, new TCollection_HAsciiString(hGrp->GetASCII("Author", "Author").c_str()));
|
||||
makeHeader.SetOrganizationValue (1, new TCollection_HAsciiString(hGrp->GetASCII("Company").c_str()));
|
||||
makeHeader.SetOriginatingSystem(new TCollection_HAsciiString(App::GetApplication().getExecutableName()));
|
||||
makeHeader.SetDescriptionValue(1, new TCollection_HAsciiString("FreeCAD Model"));
|
||||
IFSelect_ReturnStatus ret = writer.Write(name8bit.c_str());
|
||||
if (ret == IFSelect_RetError || ret == IFSelect_RetFail || ret == IFSelect_RetStop) {
|
||||
PyErr_Format(PyExc_IOError, "Cannot open file '%s'", Utf8Name.c_str());
|
||||
throw Py::Exception();
|
||||
}
|
||||
}
|
||||
else if (file.hasExtension("igs") || file.hasExtension("iges")) {
|
||||
IGESControl_Controller::Init();
|
||||
IGESCAFControl_Writer writer;
|
||||
IGESData_GlobalSection header = writer.Model()->GlobalSection();
|
||||
header.SetAuthorName(new TCollection_HAsciiString(Interface_Static::CVal("write.iges.header.author")));
|
||||
header.SetCompanyName(new TCollection_HAsciiString(Interface_Static::CVal("write.iges.header.company")));
|
||||
header.SetSendName(new TCollection_HAsciiString(Interface_Static::CVal("write.iges.header.product")));
|
||||
writer.Model()->SetGlobalSection(header);
|
||||
writer.Transfer(hDoc);
|
||||
Standard_Boolean ret = writer.Write(name8bit.c_str());
|
||||
if (!ret) {
|
||||
PyErr_Format(PyExc_IOError, "Cannot open file '%s'", Utf8Name.c_str());
|
||||
throw Py::Exception();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Standard_Failure) {
|
||||
Handle_Standard_Failure e = Standard_Failure::Caught();
|
||||
throw Py::Exception(Base::BaseExceptionFreeCADError, e->GetMessageString());
|
||||
}
|
||||
catch (const Base::Exception& e) {
|
||||
throw Py::RuntimeError(e.what());
|
||||
}
|
||||
|
||||
return Py::None();
|
||||
}
|
||||
};
|
||||
|
||||
PyObject* initModule()
|
||||
{
|
||||
return (new Module)->module().ptr();
|
||||
}
|
||||
|
||||
<<<<<<< ba8252ee7a68c3a6f954372590d46fea1c4238da
|
||||
} // namespace Import
|
||||
=======
|
||||
/* registration table */
|
||||
struct PyMethodDef Import_Import_methods[] = {
|
||||
{"open" ,open ,METH_VARARGS,
|
||||
"open(string) -- Open the file and create a new document."},
|
||||
{"insert" ,importer ,METH_VARARGS,
|
||||
"insert(string,string) -- Insert the file into the given document."},
|
||||
{"export" ,exporter ,METH_VARARGS,
|
||||
"export(list,string) -- Export a list of objects into a single file."},
|
||||
{"importAssembly" ,importAssembly ,METH_VARARGS,
|
||||
"importAssembly(FileName,Target) -- Import a Assembly file and creates a Assembly structure."},
|
||||
{NULL, NULL} /* end of table marker */
|
||||
};
|
||||
>>>>>>> Open new track for importing Assembly structures with OCSF reader.
|
|
@ -20,6 +20,7 @@ link_directories(${OCC_LIBRARY_DIR})
|
|||
|
||||
set(Import_LIBS
|
||||
Part
|
||||
Assembly
|
||||
${OCC_OCAF_LIBRARIES}
|
||||
${OCC_OCAF_DEBUG_LIBRARIES}
|
||||
)
|
||||
|
@ -29,6 +30,8 @@ SET(Import_SRCS
|
|||
AppImportPy.cpp
|
||||
ImportOCAF.cpp
|
||||
ImportOCAF.h
|
||||
ImportOCAFAssembly.cpp
|
||||
ImportOCAFAssembly.h
|
||||
StepShapePy.xml
|
||||
StepShape.h
|
||||
StepShape.cpp
|
||||
|
|
550
src/Mod/Import/App/ImportOCAFAssembly.cpp
Normal file
550
src/Mod/Import/App/ImportOCAFAssembly.cpp
Normal file
|
@ -0,0 +1,550 @@
|
|||
/***************************************************************************
|
||||
* Copyright (c) 2013 Werner Mayer <wmayer[at]users.sourceforge.net> *
|
||||
* *
|
||||
* 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 *
|
||||
* *
|
||||
***************************************************************************/
|
||||
|
||||
|
||||
#include "PreCompiled.h"
|
||||
#if defined(__MINGW32__)
|
||||
# define WNT // avoid conflict with GUID
|
||||
#endif
|
||||
#ifndef _PreComp_
|
||||
# include <climits>
|
||||
# include <Standard_Version.hxx>
|
||||
# include <BRep_Builder.hxx>
|
||||
# include <Handle_TDocStd_Document.hxx>
|
||||
# include <Handle_XCAFApp_Application.hxx>
|
||||
# include <TDocStd_Document.hxx>
|
||||
# include <XCAFApp_Application.hxx>
|
||||
# include <XCAFDoc_DocumentTool.hxx>
|
||||
# include <XCAFDoc_ShapeTool.hxx>
|
||||
# include <XCAFDoc_ColorTool.hxx>
|
||||
# include <XCAFDoc_Location.hxx>
|
||||
# include <TDF_Label.hxx>
|
||||
# include <TDF_LabelSequence.hxx>
|
||||
# include <TDF_ChildIterator.hxx>
|
||||
# include <TDataStd_Name.hxx>
|
||||
# include <Quantity_Color.hxx>
|
||||
# include <STEPCAFControl_Reader.hxx>
|
||||
# include <STEPCAFControl_Writer.hxx>
|
||||
# include <STEPControl_Writer.hxx>
|
||||
# include <IGESCAFControl_Reader.hxx>
|
||||
# include <IGESCAFControl_Writer.hxx>
|
||||
# include <IGESControl_Controller.hxx>
|
||||
# include <Interface_Static.hxx>
|
||||
# include <Transfer_TransientProcess.hxx>
|
||||
# include <XSControl_WorkSession.hxx>
|
||||
# include <TopTools_IndexedMapOfShape.hxx>
|
||||
# include <TopTools_MapOfShape.hxx>
|
||||
# include <TopExp_Explorer.hxx>
|
||||
# include <TopoDS_Iterator.hxx>
|
||||
# include <APIHeaderSection_MakeHeader.hxx>
|
||||
# include <OSD_Exception.hxx>
|
||||
#if OCC_VERSION_HEX >= 0x060500
|
||||
# include <TDataXtd_Shape.hxx>
|
||||
# else
|
||||
# include <TDataStd_Shape.hxx>
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#include "ImportOCAFAssembly.h"
|
||||
#include <Base/Console.h>
|
||||
#include <App/Application.h>
|
||||
#include <App/Document.h>
|
||||
#include <App/DocumentObjectPy.h>
|
||||
#include <Mod/Part/App/PartFeature.h>
|
||||
#include <Mod/Part/App/ProgressIndicator.h>
|
||||
#include <Mod/Part/App/ImportIges.h>
|
||||
#include <Mod/Part/App/ImportStep.h>
|
||||
|
||||
|
||||
using namespace Import;
|
||||
|
||||
|
||||
ImportOCAFAssembly::ImportOCAFAssembly(Handle_TDocStd_Document h, App::Document* d, const std::string& name)
|
||||
: pDoc(h), doc(d), default_name(name)
|
||||
{
|
||||
aShapeTool = XCAFDoc_DocumentTool::ShapeTool (pDoc->Main());
|
||||
aColorTool = XCAFDoc_DocumentTool::ColorTool(pDoc->Main());
|
||||
}
|
||||
|
||||
ImportOCAFAssembly::~ImportOCAFAssembly()
|
||||
{
|
||||
}
|
||||
|
||||
void ImportOCAFAssembly::loadShapes()
|
||||
{
|
||||
myRefShapes.clear();
|
||||
loadShapes(pDoc->Main(), TopLoc_Location(), default_name, "", false);
|
||||
}
|
||||
|
||||
void ImportOCAFAssembly::loadShapes(const TDF_Label& label, const TopLoc_Location& loc, const std::string& defaultname, const std::string& assembly, bool isRef)
|
||||
{
|
||||
int hash = 0;
|
||||
TopoDS_Shape aShape;
|
||||
if (aShapeTool->GetShape(label,aShape)) {
|
||||
hash = aShape.HashCode(HashUpper);
|
||||
}
|
||||
|
||||
Handle(TDataStd_Name) name;
|
||||
std::string part_name = defaultname;
|
||||
if (label.FindAttribute(TDataStd_Name::GetID(),name)) {
|
||||
TCollection_ExtendedString extstr = name->Get();
|
||||
char* str = new char[extstr.LengthOfCString()+1];
|
||||
extstr.ToUTF8CString(str);
|
||||
part_name = str;
|
||||
delete [] str;
|
||||
if (part_name.empty()) {
|
||||
part_name = defaultname;
|
||||
}
|
||||
else {
|
||||
bool ws=true;
|
||||
for (std::string::iterator it = part_name.begin(); it != part_name.end(); ++it) {
|
||||
if (*it != ' ') {
|
||||
ws = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ws)
|
||||
part_name = defaultname;
|
||||
}
|
||||
}
|
||||
|
||||
TopLoc_Location part_loc = loc;
|
||||
Handle(XCAFDoc_Location) hLoc;
|
||||
if (label.FindAttribute(XCAFDoc_Location::GetID(), hLoc)) {
|
||||
if (isRef)
|
||||
part_loc = part_loc * hLoc->Get();
|
||||
else
|
||||
part_loc = hLoc->Get();
|
||||
}
|
||||
|
||||
#ifdef FC_DEBUG
|
||||
Base::Console().Message("H:%d, N:%s, T:%d, A:%d, S:%d, C:%d, SS:%d, F:%d, R:%d, C:%d, SS:%d\n",
|
||||
hash,
|
||||
part_name.c_str(),
|
||||
aShapeTool->IsTopLevel(label),
|
||||
aShapeTool->IsAssembly(label),
|
||||
aShapeTool->IsShape(label),
|
||||
aShapeTool->IsCompound(label),
|
||||
aShapeTool->IsSimpleShape(label),
|
||||
aShapeTool->IsFree(label),
|
||||
aShapeTool->IsReference(label),
|
||||
aShapeTool->IsComponent(label),
|
||||
aShapeTool->IsSubShape(label)
|
||||
);
|
||||
#endif
|
||||
|
||||
std::string asm_name = assembly;
|
||||
if (aShapeTool->IsAssembly(label)) {
|
||||
asm_name = part_name;
|
||||
}
|
||||
|
||||
TDF_Label ref;
|
||||
if (aShapeTool->IsReference(label) && aShapeTool->GetReferredShape(label, ref)) {
|
||||
loadShapes(ref, part_loc, part_name, asm_name, true);
|
||||
}
|
||||
|
||||
if (isRef || myRefShapes.find(hash) == myRefShapes.end()) {
|
||||
TopoDS_Shape aShape;
|
||||
if (isRef && aShapeTool->GetShape(label, aShape))
|
||||
myRefShapes.insert(aShape.HashCode(HashUpper));
|
||||
|
||||
if (aShapeTool->IsSimpleShape(label) && (isRef || aShapeTool->IsFree(label))) {
|
||||
if (!asm_name.empty())
|
||||
part_name = asm_name;
|
||||
if (isRef)
|
||||
createShape(label, loc, part_name);
|
||||
else
|
||||
createShape(label, part_loc, part_name);
|
||||
}
|
||||
else {
|
||||
for (TDF_ChildIterator it(label); it.More(); it.Next()) {
|
||||
loadShapes(it.Value(), part_loc, part_name, asm_name, isRef);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ImportOCAFAssembly::createShape(const TDF_Label& label, const TopLoc_Location& loc, const std::string& name)
|
||||
{
|
||||
const TopoDS_Shape& aShape = aShapeTool->GetShape(label);
|
||||
if (!aShape.IsNull() && aShape.ShapeType() == TopAbs_COMPOUND) {
|
||||
TopExp_Explorer xp;
|
||||
int ctSolids = 0, ctShells = 0;
|
||||
for (xp.Init(aShape, TopAbs_SOLID); xp.More(); xp.Next(), ctSolids++)
|
||||
createShape(xp.Current(), loc, name);
|
||||
for (xp.Init(aShape, TopAbs_SHELL, TopAbs_SOLID); xp.More(); xp.Next(), ctShells++)
|
||||
createShape(xp.Current(), loc, name);
|
||||
if (ctSolids > 0 || ctShells > 0)
|
||||
return;
|
||||
}
|
||||
|
||||
createShape(aShape, loc, name);
|
||||
}
|
||||
|
||||
void ImportOCAFAssembly::createShape(const TopoDS_Shape& aShape, const TopLoc_Location& loc, const std::string& name)
|
||||
{
|
||||
Part::Feature* part = static_cast<Part::Feature*>(doc->addObject("Part::Feature"));
|
||||
if (!loc.IsIdentity())
|
||||
part->Shape.setValue(aShape.Moved(loc));
|
||||
else
|
||||
part->Shape.setValue(aShape);
|
||||
part->Label.setValue(name);
|
||||
|
||||
Quantity_Color aColor;
|
||||
App::Color color(0.8f,0.8f,0.8f);
|
||||
if (aColorTool->GetColor(aShape, XCAFDoc_ColorGen, aColor) ||
|
||||
aColorTool->GetColor(aShape, XCAFDoc_ColorSurf, aColor) ||
|
||||
aColorTool->GetColor(aShape, XCAFDoc_ColorCurv, aColor)) {
|
||||
color.r = (float)aColor.Red();
|
||||
color.g = (float)aColor.Green();
|
||||
color.b = (float)aColor.Blue();
|
||||
std::vector<App::Color> colors;
|
||||
colors.push_back(color);
|
||||
applyColors(part, colors);
|
||||
#if 0//TODO
|
||||
Gui::ViewProvider* vp = Gui::Application::Instance->getViewProvider(part);
|
||||
if (vp && vp->isDerivedFrom(PartGui::ViewProviderPart::getClassTypeId())) {
|
||||
color.r = aColor.Red();
|
||||
color.g = aColor.Green();
|
||||
color.b = aColor.Blue();
|
||||
static_cast<PartGui::ViewProviderPart*>(vp)->ShapeColor.setValue(color);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
TopTools_IndexedMapOfShape faces;
|
||||
TopExp_Explorer xp(aShape,TopAbs_FACE);
|
||||
while (xp.More()) {
|
||||
faces.Add(xp.Current());
|
||||
xp.Next();
|
||||
}
|
||||
bool found_face_color = false;
|
||||
std::vector<App::Color> faceColors;
|
||||
faceColors.resize(faces.Extent(), color);
|
||||
xp.Init(aShape,TopAbs_FACE);
|
||||
while (xp.More()) {
|
||||
if (aColorTool->GetColor(xp.Current(), XCAFDoc_ColorGen, aColor) ||
|
||||
aColorTool->GetColor(xp.Current(), XCAFDoc_ColorSurf, aColor) ||
|
||||
aColorTool->GetColor(xp.Current(), XCAFDoc_ColorCurv, aColor)) {
|
||||
int index = faces.FindIndex(xp.Current());
|
||||
color.r = (float)aColor.Red();
|
||||
color.g = (float)aColor.Green();
|
||||
color.b = (float)aColor.Blue();
|
||||
faceColors[index-1] = color;
|
||||
found_face_color = true;
|
||||
}
|
||||
xp.Next();
|
||||
}
|
||||
|
||||
if (found_face_color) {
|
||||
applyColors(part, faceColors);
|
||||
#if 0//TODO
|
||||
Gui::ViewProvider* vp = Gui::Application::Instance->getViewProvider(part);
|
||||
if (vp && vp->isDerivedFrom(PartGui::ViewProviderPartExt::getClassTypeId())) {
|
||||
static_cast<PartGui::ViewProviderPartExt*>(vp)->DiffuseColor.setValues(faceColors);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
ExportOCAFAssembly::ExportOCAFAssembly(Handle_TDocStd_Document h)
|
||||
: pDoc(h)
|
||||
{
|
||||
aShapeTool = XCAFDoc_DocumentTool::ShapeTool(pDoc->Main());
|
||||
aColorTool = XCAFDoc_DocumentTool::ColorTool(pDoc->Main());
|
||||
rootLabel = TDF_TagSource::NewChild(pDoc->Main());
|
||||
}
|
||||
|
||||
void ExportOCAFAssembly::saveShape(Part::Feature* part, const std::vector<App::Color>& colors)
|
||||
{
|
||||
const TopoDS_Shape& shape = part->Shape.getValue();
|
||||
if (shape.IsNull())
|
||||
return;
|
||||
|
||||
// Add shape and name
|
||||
//TDF_Label shapeLabel = hShapeTool->AddShape(shape, Standard_False);
|
||||
TDF_Label shapeLabel= TDF_TagSource::NewChild(rootLabel);
|
||||
#if OCC_VERSION_HEX >= 0x060500
|
||||
TDataXtd_Shape::Set(shapeLabel, shape);
|
||||
#else
|
||||
TDataStd_Shape::Set(shapeLabel, shape);
|
||||
#endif
|
||||
TDataStd_Name::Set(shapeLabel, TCollection_ExtendedString(part->Label.getValue(), 1));
|
||||
|
||||
// Add color information
|
||||
Quantity_Color col;
|
||||
|
||||
std::set<int> face_index;
|
||||
TopTools_IndexedMapOfShape faces;
|
||||
TopExp_Explorer xp(shape,TopAbs_FACE);
|
||||
while (xp.More()) {
|
||||
face_index.insert(faces.Add(xp.Current()));
|
||||
xp.Next();
|
||||
}
|
||||
|
||||
// define color per face?
|
||||
if (colors.size() == face_index.size()) {
|
||||
xp.Init(shape,TopAbs_FACE);
|
||||
while (xp.More()) {
|
||||
int index = faces.FindIndex(xp.Current());
|
||||
if (face_index.find(index) != face_index.end()) {
|
||||
face_index.erase(index);
|
||||
TDF_Label faceLabel= TDF_TagSource::NewChild(shapeLabel);
|
||||
#if OCC_VERSION_HEX >= 0x060500
|
||||
TDataXtd_Shape::Set(faceLabel, xp.Current());
|
||||
#else
|
||||
TDataStd_Shape::Set(faceLabel, xp.Current());
|
||||
#endif
|
||||
const App::Color& color = colors[index-1];
|
||||
Quantity_Parameter mat[3];
|
||||
mat[0] = color.r;
|
||||
mat[1] = color.g;
|
||||
mat[2] = color.b;
|
||||
col.SetValues(mat[0],mat[1],mat[2],Quantity_TOC_RGB);
|
||||
aColorTool->SetColor(faceLabel, col, XCAFDoc_ColorSurf);
|
||||
}
|
||||
xp.Next();
|
||||
}
|
||||
}
|
||||
else if (!colors.empty()) {
|
||||
App::Color color = colors.front();
|
||||
Quantity_Parameter mat[3];
|
||||
mat[0] = color.r;
|
||||
mat[1] = color.g;
|
||||
mat[2] = color.b;
|
||||
col.SetValues(mat[0],mat[1],mat[2],Quantity_TOC_RGB);
|
||||
aColorTool->SetColor(shapeLabel, col, XCAFDoc_ColorGen);
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
ImportXCAFAssembly::ImportXCAFAssembly(Handle_TDocStd_Document h, App::Document* d, const std::string& name)
|
||||
: hdoc(h), doc(d), default_name(name)
|
||||
{
|
||||
aShapeTool = XCAFDoc_DocumentTool::ShapeTool (hdoc->Main());
|
||||
hColors = XCAFDoc_DocumentTool::ColorTool(hdoc->Main());
|
||||
}
|
||||
|
||||
ImportXCAFAssembly::~ImportXCAFAssembly()
|
||||
{
|
||||
}
|
||||
|
||||
void ImportXCAFAssembly::loadShapes()
|
||||
{
|
||||
// collect sequence of labels to display
|
||||
TDF_LabelSequence shapeLabels, colorLabels;
|
||||
aShapeTool->GetFreeShapes (shapeLabels);
|
||||
hColors->GetColors(colorLabels);
|
||||
|
||||
// set presentations and show
|
||||
for (Standard_Integer i=1; i <= shapeLabels.Length(); i++ ) {
|
||||
// get the shapes and attributes
|
||||
const TDF_Label& label = shapeLabels.Value(i);
|
||||
loadShapes(label);
|
||||
}
|
||||
std::map<Standard_Integer, TopoDS_Shape>::iterator it;
|
||||
// go through solids
|
||||
for (it = mySolids.begin(); it != mySolids.end(); ++it) {
|
||||
createShape(it->second, true, true);
|
||||
}
|
||||
// go through shells
|
||||
for (it = myShells.begin(); it != myShells.end(); ++it) {
|
||||
createShape(it->second, true, true);
|
||||
}
|
||||
// go through compounds
|
||||
for (it = myCompds.begin(); it != myCompds.end(); ++it) {
|
||||
createShape(it->second, true, true);
|
||||
}
|
||||
// do the rest
|
||||
if (!myShapes.empty()) {
|
||||
BRep_Builder builder;
|
||||
TopoDS_Compound comp;
|
||||
builder.MakeCompound(comp);
|
||||
for (it = myShapes.begin(); it != myShapes.end(); ++it) {
|
||||
builder.Add(comp, it->second);
|
||||
}
|
||||
createShape(comp, true, false);
|
||||
}
|
||||
}
|
||||
|
||||
void ImportXCAFAssembly::createShape(const TopoDS_Shape& shape, bool perface, bool setname) const
|
||||
{
|
||||
Part::Feature* part;
|
||||
part = static_cast<Part::Feature*>(doc->addObject("Part::Feature", default_name.c_str()));
|
||||
part->Shape.setValue(shape);
|
||||
std::map<Standard_Integer, Quantity_Color>::const_iterator jt;
|
||||
jt = myColorMap.find(shape.HashCode(INT_MAX));
|
||||
|
||||
App::Color partColor(0.8f,0.8f,0.8f);
|
||||
#if 0//TODO
|
||||
Gui::ViewProvider* vp = Gui::Application::Instance->getViewProvider(part);
|
||||
if (vp && vp->isDerivedFrom(PartGui::ViewProviderPart::getClassTypeId())) {
|
||||
if (jt != myColorMap.end()) {
|
||||
App::Color color;
|
||||
color.r = jt->second.Red();
|
||||
color.g = jt->second.Green();
|
||||
color.b = jt->second.Blue();
|
||||
static_cast<PartGui::ViewProviderPart*>(vp)->ShapeColor.setValue(color);
|
||||
}
|
||||
|
||||
partColor = static_cast<PartGui::ViewProviderPart*>(vp)->ShapeColor.getValue();
|
||||
}
|
||||
#endif
|
||||
|
||||
// set label name if defined
|
||||
if (setname && !myNameMap.empty()) {
|
||||
std::map<Standard_Integer, std::string>::const_iterator jt;
|
||||
jt = myNameMap.find(shape.HashCode(INT_MAX));
|
||||
if (jt != myNameMap.end()) {
|
||||
part->Label.setValue(jt->second);
|
||||
}
|
||||
}
|
||||
|
||||
// check for colors per face
|
||||
if (perface && !myColorMap.empty()) {
|
||||
TopTools_IndexedMapOfShape faces;
|
||||
TopExp_Explorer xp(shape,TopAbs_FACE);
|
||||
while (xp.More()) {
|
||||
faces.Add(xp.Current());
|
||||
xp.Next();
|
||||
}
|
||||
|
||||
bool found_face_color = false;
|
||||
std::vector<App::Color> faceColors;
|
||||
faceColors.resize(faces.Extent(), partColor);
|
||||
xp.Init(shape,TopAbs_FACE);
|
||||
while (xp.More()) {
|
||||
jt = myColorMap.find(xp.Current().HashCode(INT_MAX));
|
||||
if (jt != myColorMap.end()) {
|
||||
int index = faces.FindIndex(xp.Current());
|
||||
App::Color color;
|
||||
color.r = (float)jt->second.Red();
|
||||
color.g = (float)jt->second.Green();
|
||||
color.b = (float)jt->second.Blue();
|
||||
faceColors[index-1] = color;
|
||||
found_face_color = true;
|
||||
}
|
||||
xp.Next();
|
||||
}
|
||||
|
||||
if (found_face_color) {
|
||||
#if 0//TODO
|
||||
Gui::ViewProvider* vp = Gui::Application::Instance->getViewProvider(part);
|
||||
if (vp && vp->isDerivedFrom(PartGui::ViewProviderPartExt::getClassTypeId())) {
|
||||
static_cast<PartGui::ViewProviderPartExt*>(vp)->DiffuseColor.setValues(faceColors);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ImportXCAFAssembly::loadShapes(const TDF_Label& label)
|
||||
{
|
||||
TopoDS_Shape aShape;
|
||||
if (aShapeTool->GetShape(label,aShape)) {
|
||||
//if (aShapeTool->IsReference(label)) {
|
||||
// TDF_Label reflabel;
|
||||
// if (aShapeTool->GetReferredShape(label, reflabel)) {
|
||||
// loadShapes(reflabel);
|
||||
// }
|
||||
//}
|
||||
if (aShapeTool->IsTopLevel(label)) {
|
||||
int ctSolids = 0, ctShells = 0, ctComps = 0;
|
||||
// add the shapes
|
||||
TopExp_Explorer xp;
|
||||
for (xp.Init(aShape, TopAbs_SOLID); xp.More(); xp.Next(), ctSolids++)
|
||||
this->mySolids[xp.Current().HashCode(INT_MAX)] = (xp.Current());
|
||||
for (xp.Init(aShape, TopAbs_SHELL, TopAbs_SOLID); xp.More(); xp.Next(), ctShells++)
|
||||
this->myShells[xp.Current().HashCode(INT_MAX)] = (xp.Current());
|
||||
// if no solids and no shells were found then go for compounds
|
||||
if (ctSolids == 0 && ctShells == 0) {
|
||||
for (xp.Init(aShape, TopAbs_COMPOUND); xp.More(); xp.Next(), ctComps++)
|
||||
this->myCompds[xp.Current().HashCode(INT_MAX)] = (xp.Current());
|
||||
}
|
||||
if (ctComps == 0) {
|
||||
for (xp.Init(aShape, TopAbs_FACE, TopAbs_SHELL); xp.More(); xp.Next())
|
||||
this->myShapes[xp.Current().HashCode(INT_MAX)] = (xp.Current());
|
||||
for (xp.Init(aShape, TopAbs_WIRE, TopAbs_FACE); xp.More(); xp.Next())
|
||||
this->myShapes[xp.Current().HashCode(INT_MAX)] = (xp.Current());
|
||||
for (xp.Init(aShape, TopAbs_EDGE, TopAbs_WIRE); xp.More(); xp.Next())
|
||||
this->myShapes[xp.Current().HashCode(INT_MAX)] = (xp.Current());
|
||||
for (xp.Init(aShape, TopAbs_VERTEX, TopAbs_EDGE); xp.More(); xp.Next())
|
||||
this->myShapes[xp.Current().HashCode(INT_MAX)] = (xp.Current());
|
||||
}
|
||||
}
|
||||
|
||||
// getting color
|
||||
Quantity_Color col;
|
||||
if (hColors->GetColor(label, XCAFDoc_ColorGen, col) ||
|
||||
hColors->GetColor(label, XCAFDoc_ColorSurf, col) ||
|
||||
hColors->GetColor(label, XCAFDoc_ColorCurv, col)) {
|
||||
// add defined color
|
||||
myColorMap[aShape.HashCode(INT_MAX)] = col;
|
||||
}
|
||||
else {
|
||||
// http://www.opencascade.org/org/forum/thread_17107/
|
||||
TopoDS_Iterator it;
|
||||
for (it.Initialize(aShape);it.More(); it.Next()) {
|
||||
if (hColors->GetColor(it.Value(), XCAFDoc_ColorGen, col) ||
|
||||
hColors->GetColor(it.Value(), XCAFDoc_ColorSurf, col) ||
|
||||
hColors->GetColor(it.Value(), XCAFDoc_ColorCurv, col)) {
|
||||
// add defined color
|
||||
myColorMap[it.Value().HashCode(INT_MAX)] = col;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// getting names
|
||||
Handle(TDataStd_Name) name;
|
||||
if (label.FindAttribute(TDataStd_Name::GetID(),name)) {
|
||||
TCollection_ExtendedString extstr = name->Get();
|
||||
char* str = new char[extstr.LengthOfCString()+1];
|
||||
extstr.ToUTF8CString(str);
|
||||
std::string label(str);
|
||||
if (!label.empty())
|
||||
myNameMap[aShape.HashCode(INT_MAX)] = label;
|
||||
delete [] str;
|
||||
}
|
||||
|
||||
#if 0
|
||||
// http://www.opencascade.org/org/forum/thread_15174/
|
||||
if (aShapeTool->IsAssembly(label)) {
|
||||
TDF_LabelSequence shapeLabels;
|
||||
aShapeTool->GetComponents(label, shapeLabels);
|
||||
Standard_Integer nbShapes = shapeLabels.Length();
|
||||
for (Standard_Integer i = 1; i <= nbShapes; i++) {
|
||||
loadShapes(shapeLabels.Value(i));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if (label.HasChild()) {
|
||||
TDF_ChildIterator it;
|
||||
for (it.Initialize(label); it.More(); it.Next()) {
|
||||
loadShapes(it.Value());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
117
src/Mod/Import/App/ImportOCAFAssembly.h
Normal file
117
src/Mod/Import/App/ImportOCAFAssembly.h
Normal file
|
@ -0,0 +1,117 @@
|
|||
/***************************************************************************
|
||||
* Copyright (c) 2013 Werner Mayer <wmayer[at]users.sourceforge.net> *
|
||||
* *
|
||||
* 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 IMPORT_ImportOCAFAssembly_H
|
||||
#define IMPORT_ImportOCAFAssembly_H
|
||||
|
||||
#include <Handle_TDocStd_Document.hxx>
|
||||
#include <Handle_XCAFDoc_ColorTool.hxx>
|
||||
#include <Handle_XCAFDoc_ShapeTool.hxx>
|
||||
#include <Quantity_Color.hxx>
|
||||
#include <TopoDS_Shape.hxx>
|
||||
#include <climits>
|
||||
#include <string>
|
||||
#include <set>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include <App/Material.h>
|
||||
|
||||
class TDF_Label;
|
||||
class TopLoc_Location;
|
||||
|
||||
namespace App {
|
||||
class Document;
|
||||
class DocumentObject;
|
||||
}
|
||||
namespace Part {
|
||||
class Feature;
|
||||
}
|
||||
|
||||
namespace Import {
|
||||
|
||||
class ImportExport ImportOCAFAssembly
|
||||
{
|
||||
public:
|
||||
ImportOCAFAssembly(Handle_TDocStd_Document h, App::Document* d, const std::string& name);
|
||||
virtual ~ImportOCAFAssembly();
|
||||
void loadShapes();
|
||||
|
||||
private:
|
||||
void loadShapes(const TDF_Label& label, const TopLoc_Location&, const std::string& partname, const std::string& assembly, bool isRef);
|
||||
void createShape(const TDF_Label& label, const TopLoc_Location&, const std::string&);
|
||||
void createShape(const TopoDS_Shape& label, const TopLoc_Location&, const std::string&);
|
||||
virtual void applyColors(Part::Feature*, const std::vector<App::Color>&){}
|
||||
|
||||
private:
|
||||
Handle_TDocStd_Document pDoc;
|
||||
App::Document* doc;
|
||||
Handle_XCAFDoc_ShapeTool aShapeTool;
|
||||
Handle_XCAFDoc_ColorTool aColorTool;
|
||||
std::string default_name;
|
||||
std::set<int> myRefShapes;
|
||||
static const int HashUpper = INT_MAX;
|
||||
};
|
||||
|
||||
class ImportExport ExportOCAFAssembly
|
||||
{
|
||||
public:
|
||||
ExportOCAFAssembly(Handle_TDocStd_Document h);
|
||||
void saveShape(Part::Feature* part, const std::vector<App::Color>&);
|
||||
|
||||
private:
|
||||
Handle_TDocStd_Document pDoc;
|
||||
Handle_XCAFDoc_ShapeTool aShapeTool;
|
||||
Handle_XCAFDoc_ColorTool aColorTool;
|
||||
TDF_Label rootLabel;
|
||||
};
|
||||
|
||||
|
||||
class ImportXCAFAssembly
|
||||
{
|
||||
public:
|
||||
ImportXCAFAssembly(Handle_TDocStd_Document h, App::Document* d, const std::string& name);
|
||||
virtual ~ImportXCAFAssembly();
|
||||
void loadShapes();
|
||||
|
||||
private:
|
||||
void createShape(const TopoDS_Shape& shape, bool perface=false, bool setname=false) const;
|
||||
void loadShapes(const TDF_Label& label);
|
||||
virtual void applyColors(Part::Feature*, const std::vector<App::Color>&){}
|
||||
|
||||
private:
|
||||
Handle_TDocStd_Document hdoc;
|
||||
App::Document* doc;
|
||||
Handle_XCAFDoc_ShapeTool aShapeTool;
|
||||
Handle_XCAFDoc_ColorTool hColors;
|
||||
std::string default_name;
|
||||
std::map<Standard_Integer, TopoDS_Shape> mySolids;
|
||||
std::map<Standard_Integer, TopoDS_Shape> myShells;
|
||||
std::map<Standard_Integer, TopoDS_Shape> myCompds;
|
||||
std::map<Standard_Integer, TopoDS_Shape> myShapes;
|
||||
std::map<Standard_Integer, Quantity_Color> myColorMap;
|
||||
std::map<Standard_Integer, std::string> myNameMap;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //IMPORT_ImportOCAFAssembly_H
|
Loading…
Reference in New Issue
Block a user