File size: 5,492 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
// SPDX-License-Identifier: LGPL-2.1-or-later

/****************************************************************************
 *   Copyright (c) 2018 Zheng, Lei (realthunder) <realthunder.dev@gmail.com>*
 *                                                                          *
 *   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 <TDataStd_Name.hxx>
#include <TDF_ChildIterator.hxx>
#include <TDF_Tool.hxx>


#include <boost/algorithm/string.hpp>
#include <boost/format.hpp>

#include "Tools.h"
#include <Base/Console.h>
#include <Mod/Part/App/TopoShape.h>

// See https://dev.opencascade.org/content/occt-3d-viewer-becomes-srgb-aware
#define OCC_COLOR_SPACE Quantity_TOC_sRGB

FC_LOG_LEVEL_INIT("Import", true, true)

using namespace Import;

Base::Color Tools::convertColor(const Quantity_ColorRGBA& rgba)
{
    Standard_Real red, green, blue;
    rgba.GetRGB().Values(red, green, blue, OCC_COLOR_SPACE);
    return Base::Color(
        static_cast<float>(red),
        static_cast<float>(green),
        static_cast<float>(blue),
        static_cast<float>(rgba.Alpha())
    );
}

Quantity_ColorRGBA Tools::convertColor(const Base::Color& col)
{
    return Quantity_ColorRGBA(Quantity_Color(col.r, col.g, col.b, OCC_COLOR_SPACE), col.a);
}

static inline std::ostream& operator<<(std::ostream& os, const Quantity_ColorRGBA& rgba)
{
    Base::Color color = Tools::convertColor(rgba);
    auto toHex = [](float v) {
        return boost::format("%02X") % static_cast<int>(v * 255);
    };
    return os << "#" << toHex(color.r) << toHex(color.g) << toHex(color.b) << toHex(color.a);
}

std::string Tools::labelName(TDF_Label label)
{
    std::string txt;
    Handle(TDataStd_Name) name;
    if (!label.IsNull() && label.FindAttribute(TDataStd_Name::GetID(), name)) {
        TCollection_ExtendedString extstr = name->Get();
        char* str = new char[extstr.LengthOfCString() + 1];
        extstr.ToUTF8CString(str);
        txt = str;
        delete[] str;
        boost::trim(txt);
    }
    return txt;
}

void Tools::printLabel(
    TDF_Label label,
    Handle(XCAFDoc_ShapeTool) aShapeTool,
    Handle(XCAFDoc_ColorTool) aColorTool,
    const char* msg
)
{
    if (label.IsNull() || !FC_LOG_INSTANCE.isEnabled(FC_LOGLEVEL_LOG)) {
        return;
    }
    if (!msg) {
        msg = "Label: ";
    }
    TCollection_AsciiString entry;
    TDF_Tool::Entry(label, entry);
    std::ostringstream ss;
    ss << msg << entry << ", " << labelName(label) << (aShapeTool->IsShape(label) ? ", shape" : "")
       << (aShapeTool->IsTopLevel(label) ? ", topLevel" : "")
       << (aShapeTool->IsFree(label) ? ", free" : "")
       << (aShapeTool->IsAssembly(label) ? ", assembly" : "")
       << (aShapeTool->IsSimpleShape(label) ? ", simple" : "")
       << (aShapeTool->IsCompound(label) ? ", compound" : "")
       << (aShapeTool->IsReference(label) ? ", reference" : "")
       << (aShapeTool->IsComponent(label) ? ", component" : "")
       << (aShapeTool->IsSubShape(label) ? ", subshape" : "");
    if (aShapeTool->IsSubShape(label)) {
        auto shape = aShapeTool->GetShape(label);
        if (!shape.IsNull()) {
            ss << ", " << Part::TopoShape::shapeName(shape.ShapeType(), true);
        }
    }
    if (aShapeTool->IsShape(label)) {
        Quantity_ColorRGBA c;
        if (aColorTool->GetColor(label, XCAFDoc_ColorGen, c)) {
            ss << ", gc: " << c;
        }
        if (aColorTool->GetColor(label, XCAFDoc_ColorSurf, c)) {
            ss << ", sc: " << c;
        }
        if (aColorTool->GetColor(label, XCAFDoc_ColorCurv, c)) {
            ss << ", cc: " << c;
        }
    }

    ss << std::endl;
    Base::Console().notify<Base::LogStyle::Log>("ImportOCAF2", ss.str().c_str());
}

void Tools::dumpLabels(
    TDF_Label label,
    Handle(XCAFDoc_ShapeTool) aShapeTool,
    Handle(XCAFDoc_ColorTool) aColorTool,
    int depth
)
{
    std::string indent(depth * 2, ' ');
    printLabel(label, aShapeTool, aColorTool, indent.c_str());
    TDF_ChildIterator it;
    for (it.Initialize(label); it.More(); it.Next()) {
        dumpLabels(it.Value(), aShapeTool, aColorTool, depth + 1);
    }
}