1
/****************************************************************************
2
* Copyright (c) 2019 Zheng Lei (realthunder) <realthunder.dev@gmail.com> *
4
* This file is part of the FreeCAD CAx development system. *
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. *
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. *
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 *
21
****************************************************************************/
23
#include "PreCompiled.h"
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>
39
#include "Inventor/SoAutoZoomTranslation.h"
40
#include "SoFCSelection.h"
41
#include "SoFCUnifiedSelection.h"
42
#include "AxisOrigin.h"
46
TYPESYSTEM_SOURCE(Gui::AxisOrigin,Base::BaseClass)
48
AxisOrigin::AxisOrigin() = default;
50
SoGroup *AxisOrigin::getNode() {
54
node.reset(new SoGroup);
55
auto pMat = new SoMaterial();
57
const SbVec3f verts[13] =
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
66
// indexes used to create the edges
67
const int32_t lines[21] =
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));
83
auto pCoords = new SoCoordinate3();
84
pCoords->point.setNum(3);
85
pCoords->point.setValues(0, 13, verts);
87
auto zoom = new SoAutoZoomTranslation;
88
zoom->scaleFactor = scale;
90
auto style = new SoDrawStyle();
91
style->lineWidth = lineSize;
92
style->pointSize = pointSize;
94
auto matBinding = new SoMaterialBinding;
95
matBinding->value = SoMaterialBinding::PER_FACE_INDEXED;
98
node->addChild(style);
99
node->addChild(matBinding);
100
node->addChild(pMat);
101
node->addChild(pCoords);
103
#define CREATE_AXIS(_type,_key,_count,_offset,_mat) do{\
104
const char *label=_key;\
106
auto iter = labels.find(_key);\
107
if(iter == labels.end())\
109
else if(iter->second.size())\
110
label = iter->second.c_str();\
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);\
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);
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();
143
} else if(node->isOfType(SoFCSelectionRoot::getClassTypeId()))
149
bool AxisOrigin::getDetailPath(const char *subname, SoFullPath *pPath, SoDetail *&) const {
152
if(!subname || !subname[0])
155
auto it = nodeMap.find(subname);
156
if(it == nodeMap.end())
159
pPath->append(it->second);
163
void AxisOrigin::setLineWidth(float size) {
171
void AxisOrigin::setPointSize(float size) {
172
if(pointSize!=size) {
179
void AxisOrigin::setAxisLength(float size) {
180
if(this->size!=size) {
187
void AxisOrigin::setPlane(float size, float dist) {
188
if(pSize!=size || this->dist!=dist) {
196
void AxisOrigin::setScale(float scale) {
197
if(this->scale!=scale) {
204
void AxisOrigin::setLabels(const std::map<std::string,std::string> &labels) {
205
this->labels = labels;