From f951e7d32b63b33d7027e95e201de344d1931c5a Mon Sep 17 00:00:00 2001 From: wmayer Date: Sun, 1 Oct 2023 21:07:49 +0200 Subject: [PATCH] Import: move OCAFBrowser to its own source files --- src/Mod/Import/Gui/AppImportGuiPy.cpp | 222 +------------------------ src/Mod/Import/Gui/CMakeLists.txt | 2 + src/Mod/Import/Gui/OCAFBrowser.cpp | 231 ++++++++++++++++++++++++++ src/Mod/Import/Gui/OCAFBrowser.h | 59 +++++++ 4 files changed, 299 insertions(+), 215 deletions(-) create mode 100644 src/Mod/Import/Gui/OCAFBrowser.cpp create mode 100644 src/Mod/Import/Gui/OCAFBrowser.h diff --git a/src/Mod/Import/Gui/AppImportGuiPy.cpp b/src/Mod/Import/Gui/AppImportGuiPy.cpp index 2da59997de2e..3043938f41cf 100644 --- a/src/Mod/Import/Gui/AppImportGuiPy.cpp +++ b/src/Mod/Import/Gui/AppImportGuiPy.cpp @@ -28,58 +28,20 @@ #include #include -#include -#include -#include -#include -#include #include -#include -#include -#include -#include -#include #if defined(__clang__) #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wextra-semi" #endif -#include #include #include #include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include #include #include -#include -#include -#include -#include #include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#if OCC_VERSION_HEX >= 0x070500 -#include -#endif + #if defined(__clang__) #pragma clang diagnostic pop #endif @@ -87,6 +49,7 @@ #include "ExportOCAFGui.h" #include "ImportOCAFGui.h" +#include "OCAFBrowser.h" #include #include @@ -117,158 +80,6 @@ FC_LOG_LEVEL_INIT("Import", true, true) namespace ImportGui { -class OCAFBrowser -{ -public: - explicit OCAFBrowser(Handle(TDocStd_Document) h) - : pDoc(h) - { - myGroupIcon = QApplication::style()->standardIcon(QStyle::SP_DirIcon); - - TDataStd::IDList(myList); - myList.Append(TDataStd_TreeNode::GetDefaultTreeID()); - myList.Append(TDataStd_Integer::GetID()); - myList.Append(TDocStd_Owner::GetID()); - myList.Append(TNaming_NamedShape::GetID()); - myList.Append(TNaming_UsedShapes::GetID()); - myList.Append(XCAFDoc_Color::GetID()); - myList.Append(XCAFDoc_ColorTool::GetID()); - myList.Append(XCAFDoc_LayerTool::GetID()); - myList.Append(XCAFDoc_ShapeTool::GetID()); - myList.Append(XCAFDoc_ShapeMapTool::GetID()); - myList.Append(XCAFDoc_Location::GetID()); - } - - void load(QTreeWidget*); - -private: - void load(const TDF_Label& label, QTreeWidgetItem* item, const QString&); - std::string toString(const TCollection_ExtendedString& extstr) const - { - char* str = new char[extstr.LengthOfCString() + 1]; - extstr.ToUTF8CString(str); - std::string text(str); - delete[] str; - return text; - } - -private: - QIcon myGroupIcon; - TDF_IDList myList; - Handle(TDocStd_Document) pDoc; -}; - -void OCAFBrowser::load(QTreeWidget* theTree) -{ - theTree->clear(); - - QTreeWidgetItem* root = new QTreeWidgetItem(); - root->setText(0, QLatin1String("0")); - root->setIcon(0, myGroupIcon); - theTree->addTopLevelItem(root); - - load(pDoc->GetData()->Root(), root, QString::fromLatin1("0")); -} - -void OCAFBrowser::load(const TDF_Label& label, QTreeWidgetItem* item, const QString& s) -{ - label.Dump(std::cout); - - Handle(TDataStd_Name) name; - if (label.FindAttribute(TDataStd_Name::GetID(), name)) { - QString text = - QString::fromLatin1("%1 %2").arg(s, QString::fromUtf8(toString(name->Get()).c_str())); - item->setText(0, text); - } - - - TDF_IDList localList; - TDF_AttributeIterator itr(label); - for (; itr.More(); itr.Next()) { - localList.Append(itr.Value()->ID()); - } - - for (TDF_ListIteratorOfIDList it(localList); it.More(); it.Next()) { - Handle(TDF_Attribute) attr; - if (label.FindAttribute(it.Value(), attr)) { - QTreeWidgetItem* child = new QTreeWidgetItem(); - item->addChild(child); - if (it.Value() == TDataStd_Name::GetID()) { - QString text; - QTextStream str(&text); - str << attr->DynamicType()->Name(); - str << " = " << toString(Handle(TDataStd_Name)::DownCast(attr)->Get()).c_str(); - child->setText(0, text); - } - else if (it.Value() == TDF_TagSource::GetID()) { - QString text; - QTextStream str(&text); - str << attr->DynamicType()->Name(); - str << " = " << Handle(TDF_TagSource)::DownCast(attr)->Get(); - child->setText(0, text); - } - else if (it.Value() == TDataStd_Integer::GetID()) { - QString text; - QTextStream str(&text); - str << attr->DynamicType()->Name(); - str << " = " << Handle(TDataStd_Integer)::DownCast(attr)->Get(); - child->setText(0, text); - } - else if (it.Value() == TNaming_NamedShape::GetID()) { - TopoDS_Shape shape = Handle(TNaming_NamedShape)::DownCast(attr)->Get(); - QString text; - QTextStream str(&text); - str << attr->DynamicType()->Name() << " = "; - if (!shape.IsNull()) { - switch (shape.ShapeType()) { - case TopAbs_COMPOUND: - str << "COMPOUND PRIMITIVE"; - break; - case TopAbs_COMPSOLID: - str << "COMPSOLID PRIMITIVE"; - break; - case TopAbs_SOLID: - str << "SOLID PRIMITIVE"; - break; - case TopAbs_SHELL: - str << "SHELL PRIMITIVE"; - break; - case TopAbs_FACE: - str << "FACE PRIMITIVE"; - break; - case TopAbs_WIRE: - str << "WIRE PRIMITIVE"; - break; - case TopAbs_EDGE: - str << "EDGE PRIMITIVE"; - break; - case TopAbs_VERTEX: - str << "VERTEX PRIMITIVE"; - break; - case TopAbs_SHAPE: - str << "SHAPE PRIMITIVE"; - break; - } - } - child->setText(0, text); - } - else { - child->setText(0, QLatin1String(attr->DynamicType()->Name())); - } - } - } - - - int i = 1; - for (TDF_ChildIterator it(label); it.More(); it.Next(), i++) { - QString text = QString::fromLatin1("%1:%2").arg(s).arg(i); - QTreeWidgetItem* child = new QTreeWidgetItem(); - child->setText(0, text); - child->setIcon(0, myGroupIcon); - item->addChild(child); - load(it.Value(), child, text); - } -} class Module: public Py::ExtensionModule { @@ -609,34 +420,15 @@ class Module: public Py::ExtensionModule Import::ReaderIges reader(file); reader.read(hDoc); } + else if (file.hasExtension({"glb", "gltf"})) { + Import::ReaderGltf reader(file); + reader.read(hDoc); + } else { throw Py::Exception(PyExc_IOError, "no supported file format"); } - static QPointer dlg = nullptr; - if (!dlg) { - dlg = new QDialog(Gui::getMainWindow()); - QTreeWidget* tree = new QTreeWidget(); - tree->setHeaderLabel(QString::fromLatin1("OCAF Browser")); - - QVBoxLayout* layout = new QVBoxLayout; - layout->addWidget(tree); - dlg->setLayout(layout); - - QDialogButtonBox* btn = new QDialogButtonBox(dlg); - btn->setStandardButtons(QDialogButtonBox::Close); - QObject::connect(btn, &QDialogButtonBox::rejected, dlg, &QDialog::reject); - QHBoxLayout* boxlayout = new QHBoxLayout; - boxlayout->addWidget(btn); - layout->addLayout(boxlayout); - } - - dlg->setWindowTitle(QString::fromUtf8(file.fileName().c_str())); - dlg->setAttribute(Qt::WA_DeleteOnClose); - dlg->show(); - - OCAFBrowser browse(hDoc); - browse.load(dlg->findChild()); + OCAFBrowser::showDialog(QString::fromStdString(file.fileName()), hDoc); hApp->Close(hDoc); } catch (Standard_Failure& e) { diff --git a/src/Mod/Import/Gui/CMakeLists.txt b/src/Mod/Import/Gui/CMakeLists.txt index b0017bf070b1..950efcf4e4cd 100644 --- a/src/Mod/Import/Gui/CMakeLists.txt +++ b/src/Mod/Import/Gui/CMakeLists.txt @@ -34,6 +34,8 @@ SET(ImportGui_SRCS ExportOCAFGui.h ImportOCAFGui.cpp ImportOCAFGui.h + OCAFBrowser.cpp + OCAFBrowser.h PreCompiled.cpp PreCompiled.h Workbench.cpp diff --git a/src/Mod/Import/Gui/OCAFBrowser.cpp b/src/Mod/Import/Gui/OCAFBrowser.cpp new file mode 100644 index 000000000000..7128292e2a24 --- /dev/null +++ b/src/Mod/Import/Gui/OCAFBrowser.cpp @@ -0,0 +1,231 @@ +// SPDX-License-Identifier: LGPL-2.1-or-later + +/*************************************************************************** + * Copyright (c) 2023 Werner Mayer * + * * + * This file is part of FreeCAD. * + * * + * FreeCAD is free software: you can redistribute it and/or modify it * + * under the terms of the GNU Lesser General Public License as * + * published by the Free Software Foundation, either version 2.1 of the * + * License, or (at your option) any later version. * + * * + * FreeCAD 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 * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with FreeCAD. If not, see * + * . * + * * + **************************************************************************/ + + +#include "PreCompiled.h" +#ifndef _PreComp_ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#endif + +#include "OCAFBrowser.h" +#include + +using namespace ImportGui; + +OCAFBrowser::OCAFBrowser(Handle(TDocStd_Document) hDoc) + : pDoc(hDoc) +{ + myGroupIcon = QApplication::style()->standardIcon(QStyle::SP_DirIcon); + + TDataStd::IDList(myList); + myList.Append(TDataStd_TreeNode::GetDefaultTreeID()); + myList.Append(TDataStd_Integer::GetID()); + myList.Append(TDocStd_Owner::GetID()); + myList.Append(TNaming_NamedShape::GetID()); + myList.Append(TNaming_UsedShapes::GetID()); + myList.Append(XCAFDoc_Color::GetID()); + myList.Append(XCAFDoc_ColorTool::GetID()); + myList.Append(XCAFDoc_LayerTool::GetID()); + myList.Append(XCAFDoc_ShapeTool::GetID()); + myList.Append(XCAFDoc_ShapeMapTool::GetID()); + myList.Append(XCAFDoc_Location::GetID()); +} + +std::string OCAFBrowser::toString(const TCollection_ExtendedString& extstr) const +{ + char* str = new char[extstr.LengthOfCString() + 1]; + extstr.ToUTF8CString(str); + std::string text(str); + delete[] str; + return text; +} + +void OCAFBrowser::load(QTreeWidget* theTree) +{ + theTree->clear(); + + QTreeWidgetItem* root = new QTreeWidgetItem(); + root->setText(0, QLatin1String("0")); + root->setIcon(0, myGroupIcon); + theTree->addTopLevelItem(root); + + load(pDoc->GetData()->Root(), root, QString::fromLatin1("0")); +} + +void OCAFBrowser::load(const TDF_Label& label, QTreeWidgetItem* item, const QString& s) +{ + label.Dump(std::cout); + + Handle(TDataStd_Name) name; + if (label.FindAttribute(TDataStd_Name::GetID(), name)) { + QString text = + QString::fromLatin1("%1 %2").arg(s, QString::fromUtf8(toString(name->Get()).c_str())); + item->setText(0, text); + } + + + TDF_IDList localList; + TDF_AttributeIterator itr(label); + for (; itr.More(); itr.Next()) { + localList.Append(itr.Value()->ID()); + } + + for (TDF_ListIteratorOfIDList it(localList); it.More(); it.Next()) { + Handle(TDF_Attribute) attr; + if (label.FindAttribute(it.Value(), attr)) { + QTreeWidgetItem* child = new QTreeWidgetItem(); + item->addChild(child); + if (it.Value() == TDataStd_Name::GetID()) { + QString text; + QTextStream str(&text); + str << attr->DynamicType()->Name(); + str << " = " << toString(Handle(TDataStd_Name)::DownCast(attr)->Get()).c_str(); + child->setText(0, text); + } + else if (it.Value() == TDF_TagSource::GetID()) { + QString text; + QTextStream str(&text); + str << attr->DynamicType()->Name(); + str << " = " << Handle(TDF_TagSource)::DownCast(attr)->Get(); + child->setText(0, text); + } + else if (it.Value() == TDataStd_Integer::GetID()) { + QString text; + QTextStream str(&text); + str << attr->DynamicType()->Name(); + str << " = " << Handle(TDataStd_Integer)::DownCast(attr)->Get(); + child->setText(0, text); + } + else if (it.Value() == TNaming_NamedShape::GetID()) { + TopoDS_Shape shape = Handle(TNaming_NamedShape)::DownCast(attr)->Get(); + QString text; + QTextStream str(&text); + str << attr->DynamicType()->Name() << " = "; + if (!shape.IsNull()) { + switch (shape.ShapeType()) { + case TopAbs_COMPOUND: + str << "COMPOUND PRIMITIVE"; + break; + case TopAbs_COMPSOLID: + str << "COMPSOLID PRIMITIVE"; + break; + case TopAbs_SOLID: + str << "SOLID PRIMITIVE"; + break; + case TopAbs_SHELL: + str << "SHELL PRIMITIVE"; + break; + case TopAbs_FACE: + str << "FACE PRIMITIVE"; + break; + case TopAbs_WIRE: + str << "WIRE PRIMITIVE"; + break; + case TopAbs_EDGE: + str << "EDGE PRIMITIVE"; + break; + case TopAbs_VERTEX: + str << "VERTEX PRIMITIVE"; + break; + case TopAbs_SHAPE: + str << "SHAPE PRIMITIVE"; + break; + } + } + child->setText(0, text); + } + else { + child->setText(0, QLatin1String(attr->DynamicType()->Name())); + } + } + } + + + int i = 1; + for (TDF_ChildIterator it(label); it.More(); it.Next(), i++) { + QString text = QString::fromLatin1("%1:%2").arg(s).arg(i); + QTreeWidgetItem* child = new QTreeWidgetItem(); + child->setText(0, text); + child->setIcon(0, myGroupIcon); + item->addChild(child); + load(it.Value(), child, text); + } +} + +void OCAFBrowser::showDialog(const QString& title, Handle(TDocStd_Document) hDoc) +{ + static QPointer dlg = nullptr; + if (!dlg) { + dlg = new QDialog(Gui::getMainWindow()); + QTreeWidget* tree = new QTreeWidget(); + tree->setHeaderLabel(QString::fromLatin1("OCAF Browser")); + + QVBoxLayout* layout = new QVBoxLayout; + layout->addWidget(tree); + dlg->setLayout(layout); + + QDialogButtonBox* btn = new QDialogButtonBox(dlg); + btn->setStandardButtons(QDialogButtonBox::Close); + QObject::connect(btn, &QDialogButtonBox::rejected, dlg, &QDialog::reject); + QHBoxLayout* boxlayout = new QHBoxLayout; + boxlayout->addWidget(btn); + layout->addLayout(boxlayout); + } + + dlg->setWindowTitle(title); + dlg->setAttribute(Qt::WA_DeleteOnClose); + dlg->show(); + + OCAFBrowser browse(hDoc); + browse.load(dlg->findChild()); +} diff --git a/src/Mod/Import/Gui/OCAFBrowser.h b/src/Mod/Import/Gui/OCAFBrowser.h new file mode 100644 index 000000000000..c5594257b151 --- /dev/null +++ b/src/Mod/Import/Gui/OCAFBrowser.h @@ -0,0 +1,59 @@ +// SPDX-License-Identifier: LGPL-2.1-or-later + +/*************************************************************************** + * Copyright (c) 2023 Werner Mayer * + * * + * This file is part of FreeCAD. * + * * + * FreeCAD is free software: you can redistribute it and/or modify it * + * under the terms of the GNU Lesser General Public License as * + * published by the Free Software Foundation, either version 2.1 of the * + * License, or (at your option) any later version. * + * * + * FreeCAD 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 * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with FreeCAD. If not, see * + * . * + * * + **************************************************************************/ + +#ifndef IMPORT_OCAFBROWSER_H +#define IMPORT_OCAFBROWSER_H + +#include +#include +#include +#include +#include + +class QString; +class QTreeWidget; +class QTreeWidgetItem; + +namespace ImportGui +{ +class OCAFBrowser +{ +public: + explicit OCAFBrowser(Handle(TDocStd_Document) hDoc); + void load(QTreeWidget*); + + static void showDialog(const QString& title, Handle(TDocStd_Document) hDoc); + +private: + void load(const TDF_Label& label, QTreeWidgetItem* item, const QString&); + std::string toString(const TCollection_ExtendedString& extstr) const; + +private: + QIcon myGroupIcon; + TDF_IDList myList; + Handle(TDocStd_Document) pDoc; +}; + +} // namespace ImportGui + +#endif // IMPORT_OCAFBROWSER_H