File size: 12,360 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// SPDX-License-Identifier: LGPL-2.1-or-later

/***************************************************************************
 *   Copyright (c) 2023 Werner Mayer <wmayer[at]users.sourceforge.net>     *
 *                                                                         *
 *   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                               *
 *   <https://www.gnu.org/licenses/>.                                      *
 *                                                                         *
 **************************************************************************/


#include <QApplication>
#include <QDialog>
#include <QDialogButtonBox>
#include <QPointer>
#include <QStyle>
#include <QTextStream>
#include <QTreeWidget>
#include <QTreeWidgetItem>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <TDataStd.hxx>
#include <TDataStd_Integer.hxx>
#include <TDataStd_Name.hxx>
#include <TDataStd_Real.hxx>
#include <TDF_AttributeIterator.hxx>
#include <TDF_ChildIterator.hxx>
#include <TDF_ChildIDIterator.hxx>
#include <TDF_IDList.hxx>
#include <TDF_Label.hxx>
#include <TDF_ListIteratorOfIDList.hxx>
#include <TDF_TagSource.hxx>
#include <TDF_Tool.hxx>
#include <TDocStd_Document.hxx>
#include <TDocStd_Owner.hxx>
#include <TNaming_UsedShapes.hxx>
#include <XCAFDoc.hxx>
#include <XCAFDoc_Color.hxx>
#include <XCAFDoc_ColorTool.hxx>
#include <XCAFDoc_LayerTool.hxx>
#include <XCAFDoc_Location.hxx>
#include <XCAFDoc_ShapeTool.hxx>
#include <XCAFDoc_ShapeMapTool.hxx>


#include "OCAFBrowser.h"
#include <Gui/MainWindow.h>

using namespace ImportGui;

OCAFBrowser::OCAFBrowser(const 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)
{
    // NOLINTBEGIN
    char* str = new char[extstr.LengthOfCString() + 1];
    extstr.ToUTF8CString(str);
    std::string text(str);
    delete[] str;
    return text;
    // NOLINTEND
}

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, QStringLiteral("0"));
}

void OCAFBrowser::load(const TDF_Label& label, QTreeWidgetItem* item, const QString& str)
{
    label.Dump(std::cout);

    Handle(TDataStd_Name) name;
    if (label.FindAttribute(TDataStd_Name::GetID(), name)) {
        QString text
            = QStringLiteral("%1 %2").arg(str, 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());
    }

    // Hint: In OCC 7.7 use XCAFDoc::AttributeInfo
    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() == TNaming_UsedShapes::GetID()) {
                QString text;
                QTextStream str(&text);
                str << attr->DynamicType()->Name();
                str << " = " << Handle(TNaming_UsedShapes)::DownCast(attr)->Map().Size();
                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() == TDataStd_Real::GetID()) {
                QString text;
                QTextStream str(&text);
                str << attr->DynamicType()->Name();
                str << " = " << Handle(TDataStd_Real)::DownCast(attr)->Get();
                child->setText(0, text);
            }
            else if (it.Value() == XCAFDoc_Color::GetID()) {
                Quantity_ColorRGBA rgba = Handle(XCAFDoc_Color)::DownCast(attr)->GetColorRGBA();
                Quantity_Color rgb = rgba.GetRGB();
                QString tuple = QStringLiteral("(%1, %2, %3, %4)")
                                    .arg(rgb.Red())
                                    .arg(rgb.Green())
                                    .arg(rgb.Blue())
                                    .arg(rgba.Alpha());
                QString text;
                QTextStream str(&text);
                str << attr->DynamicType()->Name();
                str << " = " << Quantity_Color::StringName(rgb.Name()) << " " << tuple;
                child->setText(0, text);
            }
            else if (it.Value() == XCAFDoc_ShapeMapTool::GetID()) {
                QString text;
                QTextStream str(&text);
                str << attr->DynamicType()->Name();
                str << " = " << Handle(XCAFDoc_ShapeMapTool)::DownCast(attr)->GetMap().Size();
                child->setText(0, text);
            }
            else if (it.Value() == TNaming_NamedShape::GetID()) {
                child->setText(0, toText(Handle(TNaming_NamedShape)::DownCast(attr)));
            }
            else if (!Handle(TDataStd_TreeNode)::DownCast(attr).IsNull()) {
                Handle(TDataStd_TreeNode) aTreeNode = Handle(TDataStd_TreeNode)::DownCast(attr);
                child->setText(0, toText(aTreeNode));
            }
            else {
                child->setText(0, QLatin1String(attr->DynamicType()->Name()));
            }
        }
    }


    int index = 1;
    for (TDF_ChildIterator it(label); it.More(); it.Next(), index++) {
        QString text = QStringLiteral("%1:%2").arg(str).arg(index);
        QTreeWidgetItem* child = new QTreeWidgetItem();
        child->setText(0, text);
        child->setIcon(0, myGroupIcon);
        item->addChild(child);
        load(it.Value(), child, text);
    }
}

QString OCAFBrowser::toText(const Handle(TNaming_NamedShape) & namedShape)
{
    TopoDS_Shape shape = namedShape->Get();
    QString text;
    QTextStream str(&text);
    str << namedShape->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;
        }
    }

    return text;
}

QString OCAFBrowser::toText(const Handle(TDataStd_TreeNode) & treeNode)
{
    Standard_CString type = "";
    if (treeNode->ID() == XCAFDoc::ShapeRefGUID()) {
        type = "Shape Instance Link";
    }
    else if (treeNode->ID() == XCAFDoc::ColorRefGUID(XCAFDoc_ColorGen)) {
        type = "Generic Color Link";
    }
    else if (treeNode->ID() == XCAFDoc::ColorRefGUID(XCAFDoc_ColorSurf)) {
        type = "Surface Color Link";
    }
    else if (treeNode->ID() == XCAFDoc::ColorRefGUID(XCAFDoc_ColorCurv)) {
        type = "Curve Color Link";
    }
    else if (treeNode->ID() == XCAFDoc::DimTolRefGUID()) {
        type = "DGT Link";
    }
    else if (treeNode->ID() == XCAFDoc::DatumRefGUID()) {
        type = "Datum Link";
    }
    else if (treeNode->ID() == XCAFDoc::MaterialRefGUID()) {
        type = "Material Link";
    }

    QString prefix = QString::fromLatin1(type);

    QString display;
    if (treeNode->HasFather()) {
        TDF_Label aLabel = treeNode->Father()->Label();
        TCollection_AsciiString entry;
        TDF_Tool::Entry(aLabel, entry);
        QString suffix = QString::fromLatin1(entry.ToCString());
        display = QStringLiteral("%1 ==> %2").arg(prefix, suffix);
    }
    else {
        Handle(TDataStd_TreeNode) aFirstChild = treeNode->First();
        QStringList aRefs;
        while (!aFirstChild.IsNull()) {
            TDF_Label aLabel = aFirstChild->Label();
            TCollection_AsciiString entry;
            TDF_Tool::Entry(aLabel, entry);
            QString suffix = QString::fromLatin1(entry.ToCString());
            aRefs.append(suffix);
            aFirstChild = aFirstChild->Next();
        }
        display = QStringLiteral("%1 <== %2").arg(prefix, aRefs.join(QChar::fromLatin1(',')));
    }

    QString text;
    QTextStream str(&text);
    str << treeNode->DynamicType()->Name();
    if (!display.isEmpty()) {
        str << " = " << display;
    }

    return text;
}

void OCAFBrowser::showDialog(const QString& title, const Handle(TDocStd_Document) & hDoc)
{
    // NOLINTBEGIN
    QDialog* dlg = new QDialog(Gui::getMainWindow());

    QTreeWidget* tree = new QTreeWidget();
    tree->setHeaderLabel(QStringLiteral("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<QTreeWidget*>());
    tree->expandAll();
    dlg->resize(400, 600);
    // NOLINTEND
}