FreeCAD

Форк
0
/
AxisOrigin.cpp 
208 строк · 6.7 Кб
1
/****************************************************************************
2
 *   Copyright (c) 2019 Zheng Lei (realthunder) <realthunder.dev@gmail.com> *
3
 *                                                                          *
4
 *   This file is part of the FreeCAD CAx development system.               *
5
 *                                                                          *
6
 *   This library is free software; you can redistribute it and/or          *
7
 *   modify it under the terms of the GNU Library General Public            *
8
 *   License as published by the Free Software Foundation; either           *
9
 *   version 2 of the License, or (at your option) any later version.       *
10
 *                                                                          *
11
 *   This library  is distributed in the hope that it will be useful,       *
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of         *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
14
 *   GNU Library General Public License for more details.                   *
15
 *                                                                          *
16
 *   You should have received a copy of the GNU Library General Public      *
17
 *   License along with this library; see the file COPYING.LIB. If not,     *
18
 *   write to the Free Software Foundation, Inc., 59 Temple Place,          *
19
 *   Suite 330, Boston, MA  02111-1307, USA                                 *
20
 *                                                                          *
21
 ****************************************************************************/
22

23
#include "PreCompiled.h"
24

25
#ifndef _PreComp_
26
# include <Inventor/nodes/SoCoordinate3.h>
27
# include <Inventor/nodes/SoSeparator.h>
28
# include <Inventor/nodes/SoSwitch.h>
29
# include <Inventor/SoPickedPoint.h>
30
# include <Inventor/nodes/SoMaterial.h>
31
# include <Inventor/nodes/SoMaterialBinding.h>
32
# include <Inventor/nodes/SoDrawStyle.h>
33
# include <Inventor/nodes/SoIndexedLineSet.h>
34
# include <Inventor/nodes/SoIndexedPointSet.h>
35
# include <Inventor/nodes/SoDrawStyle.h>
36
# include <Inventor/SoFullPath.h>
37
#endif
38

39
#include "Inventor/SoAutoZoomTranslation.h"
40
#include "SoFCSelection.h"
41
#include "SoFCUnifiedSelection.h"
42
#include "AxisOrigin.h"
43

44
using namespace Gui;
45

46
TYPESYSTEM_SOURCE(Gui::AxisOrigin,Base::BaseClass)
47

48
AxisOrigin::AxisOrigin() = default;
49

50
SoGroup *AxisOrigin::getNode() {
51
    if(node)
52
        return node;
53

54
    node.reset(new SoGroup);
55
    auto pMat = new SoMaterial();
56

57
    const SbVec3f verts[13] =
58
    {
59
        SbVec3f(0,0,0), SbVec3f(size,0,0),
60
        SbVec3f(0,size,0), SbVec3f(0,0,size),
61
        SbVec3f(dist,dist,0), SbVec3f(dist,pSize,0), SbVec3f(pSize,dist,0),  // XY Plane
62
        SbVec3f(dist,0,dist), SbVec3f(dist,0,pSize), SbVec3f(pSize,0,dist),  // XY Plane
63
        SbVec3f(0,dist,dist), SbVec3f(0,pSize,dist), SbVec3f(0,dist,pSize)  // XY Plane
64
    };
65

66
    // indexes used to create the edges
67
    const int32_t lines[21] =
68
    {
69
        0,1,-1,
70
        0,2,-1,
71
        0,3,-1,
72
        5,4,6,-1,
73
        8,7,9,-1,
74
        11,10,12,-1
75
    };
76

77
    pMat->diffuseColor.setNum(3);
78
    pMat->diffuseColor.set1Value(0, SbColor(1.0f, 0.2f, 0.2f));
79
    pMat->diffuseColor.set1Value(1, SbColor(0.2f, 0.6f, 0.2f));
80
    pMat->diffuseColor.set1Value(2, SbColor(0.2f, 0.2f, 1.0f));
81
    pMat->diffuseColor.set1Value(4, SbColor(0.8f, 0.8f, 0.8f));
82

83
    auto pCoords = new SoCoordinate3();
84
    pCoords->point.setNum(3);
85
    pCoords->point.setValues(0, 13, verts);
86

87
    auto zoom = new SoAutoZoomTranslation;
88
    zoom->scaleFactor = scale;
89

90
    auto style = new SoDrawStyle();
91
    style->lineWidth = lineSize;
92
    style->pointSize = pointSize;
93

94
    auto matBinding = new SoMaterialBinding;
95
    matBinding->value = SoMaterialBinding::PER_FACE_INDEXED;
96

97
    node->addChild(zoom);
98
    node->addChild(style);
99
    node->addChild(matBinding);
100
    node->addChild(pMat);
101
    node->addChild(pCoords);
102

103
#define CREATE_AXIS(_type,_key,_count,_offset,_mat) do{\
104
        const char *label=_key;\
105
        if(labels.size()){\
106
            auto iter = labels.find(_key);\
107
            if(iter == labels.end())\
108
                break;\
109
            else if(iter->second.size())\
110
                label = iter->second.c_str();\
111
        }\
112
        auto pAxis = new SoFCSelection;\
113
        pAxis->applySettings();\
114
        pAxis->style = SoFCSelection::EMISSIVE_DIFFUSE;\
115
        pAxis->subElementName = label;\
116
        nodeMap[label].reset(pAxis);\
117
        node->addChild(pAxis);\
118
        auto _type  = new SoIndexed##_type##Set;\
119
        pAxis->addChild(_type);\
120
        _type->coordIndex.setNum(_count);\
121
        _type->coordIndex.setValues(0,_count,lines+_offset);\
122
        _type->materialIndex.setValue(_mat);\
123
    }while(0)
124

125
    CREATE_AXIS(Point,"O",1,0,4);
126
    CREATE_AXIS(Line,"X",3,0,0);
127
    CREATE_AXIS(Line,"Y",3,3,1);
128
    CREATE_AXIS(Line,"Z",3,6,2);
129
    CREATE_AXIS(Line,"XY",4,9,2);
130
    CREATE_AXIS(Line,"XZ",4,13,1);
131
    CREATE_AXIS(Line,"YZ",4,17,0);
132
    return node;
133
}
134

135
bool AxisOrigin::getElementPicked(const SoPickedPoint *pp, std::string &subname) const {
136
    SoPath *path = pp->getPath();
137
    int length = path->getLength();
138
    for(int i=0;i<length;++i) {
139
        auto node = path->getNodeFromTail(i);
140
        if(node->isOfType(SoFCSelection::getClassTypeId())) {
141
            subname = static_cast<SoFCSelection*>(node)->subElementName.getValue().getString();
142
            return true;
143
        } else if(node->isOfType(SoFCSelectionRoot::getClassTypeId()))
144
            break;
145
    }
146
    return false;
147
}
148

149
bool AxisOrigin::getDetailPath(const char *subname, SoFullPath *pPath, SoDetail *&) const {
150
    if(!node)
151
        return false;
152
    if(!subname || !subname[0])
153
        return true;
154

155
    auto it = nodeMap.find(subname);
156
    if(it == nodeMap.end())
157
        return false;
158
    pPath->append(node);
159
    pPath->append(it->second);
160
    return true;
161
}
162

163
void AxisOrigin::setLineWidth(float size) {
164
    if(size!=lineSize) {
165
        node.reset();
166
        nodeMap.clear();
167
        lineSize = size;
168
    }
169
}
170

171
void AxisOrigin::setPointSize(float size) {
172
    if(pointSize!=size) {
173
        pointSize = size;
174
        node.reset();
175
        nodeMap.clear();
176
    }
177
}
178

179
void AxisOrigin::setAxisLength(float size) {
180
    if(this->size!=size) {
181
        this->size = size;
182
        node.reset();
183
        nodeMap.clear();
184
    }
185
}
186

187
void AxisOrigin::setPlane(float size, float dist) {
188
    if(pSize!=size || this->dist!=dist) {
189
        pSize = size;
190
        this->dist = dist;
191
        node.reset();
192
        nodeMap.clear();
193
    }
194
}
195

196
void AxisOrigin::setScale(float scale) {
197
    if(this->scale!=scale) {
198
        this->scale = scale;
199
        node.reset();
200
        nodeMap.clear();
201
    }
202
}
203

204
void AxisOrigin::setLabels(const std::map<std::string,std::string> &labels) {
205
    this->labels = labels;
206
    node.reset();
207
    nodeMap.clear();
208
}
209

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.