FreeCAD

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

24
#include "PreCompiled.h"
25

26
#ifndef _PreComp_
27
# include <Inventor/nodes/SoLightModel.h>
28
# include <Inventor/nodes/SoSeparator.h>
29
#endif
30

31
#include <App/Document.h>
32
#include <App/Origin.h>
33
#include "Base/Console.h"
34
#include <Base/Vector3D.h>
35

36
#include "ViewProviderOrigin.h"
37
#include "Application.h"
38
#include "Command.h"
39
#include "Document.h"
40
#include "ViewProviderLine.h"
41
#include "ViewProviderPlane.h"
42

43

44
using namespace Gui;
45

46

47
PROPERTY_SOURCE(Gui::ViewProviderOrigin, Gui::ViewProviderDocumentObject)
48

49
/**
50
 * Creates the view provider for an object group.
51
 */
52
ViewProviderOrigin::ViewProviderOrigin()
53
{
54
    ADD_PROPERTY_TYPE ( Size, (Base::Vector3d(10,10,10)), 0, App::Prop_None,
55
        QT_TRANSLATE_NOOP("App::Property", "The displayed size of the origin"));
56
    Size.setStatus(App::Property::ReadOnly, true);
57

58
    sPixmap = "Std_CoordinateSystem";
59
    Visibility.setValue(false);
60

61
    pcGroupChildren = new SoGroup();
62
    pcGroupChildren->ref();
63

64
    auto lm = new SoLightModel();
65
    lm->model = SoLightModel::BASE_COLOR;
66
    pcRoot->insertChild(lm, 0);
67
}
68

69
ViewProviderOrigin::~ViewProviderOrigin() {
70
    pcGroupChildren->unref();
71
    pcGroupChildren = nullptr;
72
}
73

74
std::vector<App::DocumentObject*> ViewProviderOrigin::claimChildren() const {
75
    return static_cast<App::Origin*>( getObject() )->OriginFeatures.getValues ();
76
}
77

78
std::vector<App::DocumentObject*> ViewProviderOrigin::claimChildren3D() const {
79
    return claimChildren ();
80
}
81

82
void ViewProviderOrigin::attach(App::DocumentObject* pcObject)
83
{
84
    Gui::ViewProviderDocumentObject::attach(pcObject);
85
    addDisplayMaskMode(pcGroupChildren, "Base");
86
}
87

88
std::vector<std::string> ViewProviderOrigin::getDisplayModes() const
89
{
90
    return { "Base" };
91
}
92

93
void ViewProviderOrigin::setDisplayMode(const char* ModeName)
94
{
95
    if (strcmp(ModeName, "Base") == 0)
96
        setDisplayMaskMode("Base");
97
    ViewProviderDocumentObject::setDisplayMode(ModeName);
98
}
99

100
void ViewProviderOrigin::setTemporaryVisibility(bool axis, bool plane) {
101
    auto origin = static_cast<App::Origin*>( getObject() );
102

103
    bool saveState = tempVisMap.empty();
104

105
    try {
106
        // Remember & Set axis visibility
107
        for(App::DocumentObject* obj : origin->axes()) {
108
            if (obj) {
109
                Gui::ViewProvider* vp = Gui::Application::Instance->getViewProvider(obj);
110
                if(vp) {
111
                    if (saveState) {
112
                        tempVisMap[vp] = vp->isVisible();
113
                    }
114
                    vp->setVisible(axis);
115
                }
116
            }
117
        }
118

119
        // Remember & Set plane visibility
120
        for(App::DocumentObject* obj : origin->planes()) {
121
            if (obj) {
122
                Gui::ViewProvider* vp = Gui::Application::Instance->getViewProvider(obj);
123
                if(vp) {
124
                    if (saveState) {
125
                        tempVisMap[vp] = vp->isVisible();
126
                    }
127
                    vp->setVisible(plane);
128
                }
129
            }
130
        }
131
    } catch (const Base::Exception &ex) {
132
        Base::Console().Error ("%s\n", ex.what() );
133
    }
134

135
    // Remember & Set self visibility
136
    tempVisMap[this] = isVisible();
137
    setVisible(true);
138

139
}
140

141
void ViewProviderOrigin::resetTemporaryVisibility() {
142
    for(std::pair<Gui::ViewProvider*, bool> pair : tempVisMap) {
143
        pair.first->setVisible(pair.second);
144
    }
145
    tempVisMap.clear ();
146
}
147

148
double ViewProviderOrigin::defaultSize()
149
{
150
    ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/View");
151
    return 0.25 * hGrp->GetFloat("NewDocumentCameraScale",100.0);
152
}
153

154
bool ViewProviderOrigin::isTemporaryVisibility() {
155
    return !tempVisMap.empty();
156
}
157

158
void ViewProviderOrigin::onChanged(const App::Property* prop) {
159
    if (prop == &Size) {
160
        try {
161
            Gui::Application *app = Gui::Application::Instance;
162
            Base::Vector3d sz = Size.getValue ();
163
            auto origin = static_cast<App::Origin*> ( getObject() );
164

165
            // Calculate axes and planes sizes
166
            double szXY = std::max ( sz.x, sz.y );
167
            double szXZ = std::max ( sz.x, sz.z );
168
            double szYZ = std::max ( sz.y, sz.z );
169

170
            double szX = std::min ( szXY, szXZ );
171
            double szY = std::min ( szXY, szYZ );
172
            double szZ = std::min ( szXZ, szYZ );
173

174
            // Find view providers
175
            Gui::ViewProviderPlane* vpPlaneXY, *vpPlaneXZ, *vpPlaneYZ;
176
            Gui::ViewProviderLine* vpLineX, *vpLineY, *vpLineZ;
177
            // Planes
178
            vpPlaneXY = static_cast<Gui::ViewProviderPlane *> ( app->getViewProvider ( origin->getXY () ) );
179
            vpPlaneXZ = static_cast<Gui::ViewProviderPlane *> ( app->getViewProvider ( origin->getXZ () ) );
180
            vpPlaneYZ = static_cast<Gui::ViewProviderPlane *> ( app->getViewProvider ( origin->getYZ () ) );
181
            // Axes
182
            vpLineX = static_cast<Gui::ViewProviderLine *> ( app->getViewProvider ( origin->getX () ) );
183
            vpLineY = static_cast<Gui::ViewProviderLine *> ( app->getViewProvider ( origin->getY () ) );
184
            vpLineZ = static_cast<Gui::ViewProviderLine *> ( app->getViewProvider ( origin->getZ () ) );
185

186
            // set their sizes
187
            if (vpPlaneXY) { vpPlaneXY->Size.setValue ( szXY ); }
188
            if (vpPlaneXZ) { vpPlaneXZ->Size.setValue ( szXZ ); }
189
            if (vpPlaneYZ) { vpPlaneYZ->Size.setValue ( szYZ ); }
190
            if (vpLineX) { vpLineX->Size.setValue ( szX * axesScaling ); }
191
            if (vpLineY) { vpLineY->Size.setValue ( szY * axesScaling ); }
192
            if (vpLineZ) { vpLineZ->Size.setValue ( szZ * axesScaling ); }
193

194
        } catch (const Base::Exception &ex) {
195
            // While restoring a document don't report errors if one of the lines or planes
196
            // cannot be found.
197
            App::Document* doc = getObject()->getDocument();
198
            if (!doc->testStatus(App::Document::Restoring))
199
                Base::Console().Error ("%s\n", ex.what() );
200
        }
201
    }
202

203
    ViewProviderDocumentObject::onChanged ( prop );
204
}
205

206
bool ViewProviderOrigin::onDelete(const std::vector<std::string> &) {
207
    auto origin = static_cast<App::Origin*>( getObject() );
208

209
    if ( !origin->getInList().empty() ) {
210
        return false;
211
    }
212

213
    auto objs = origin->OriginFeatures.getValues();
214
    origin->OriginFeatures.setValues({});
215

216
    for (auto obj: objs ) {
217
        Gui::Command::doCommand( Gui::Command::Doc, "App.getDocument(\"%s\").removeObject(\"%s\")",
218
                obj->getDocument()->getName(), obj->getNameInDocument() );
219
    }
220

221
    return true;
222
}
223

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

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

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

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