FreeCAD

Форк
0
/
ViewProviderInventorObject.cpp 
166 строк · 6.9 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2007 Werner Mayer <wmayer[at]users.sourceforge.net>     *
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/SoDB.h>
27
# include <Inventor/SoInput.h>
28
# include <Inventor/nodes/SoSeparator.h>
29
# include <Inventor/nodes/SoTransform.h>
30
# include <QFile>
31
#endif
32

33
#include <App/Document.h>
34
#include <App/InventorObject.h>
35

36
#include "ViewProviderInventorObject.h"
37
#include "SoFCSelection.h"
38

39

40
using namespace Gui;
41

42
PROPERTY_SOURCE(Gui::ViewProviderInventorObject, Gui::ViewProviderDocumentObject)
43

44
ViewProviderInventorObject::ViewProviderInventorObject()
45
{
46
    pcBuffer = new SoSeparator();
47
    pcBuffer->ref();
48
    pcFile = new SoSeparator();
49
    pcFile->ref();
50
}
51

52
ViewProviderInventorObject::~ViewProviderInventorObject()
53
{
54
    pcBuffer->unref();
55
    pcFile->unref();
56
}
57

58
void ViewProviderInventorObject::attach(App::DocumentObject *pcObj)
59
{
60
    ViewProviderDocumentObject::attach(pcObj);
61
    auto pcFileBuf = new SoGroup();
62
    pcFileBuf->addChild(pcBuffer);
63
    pcFileBuf->addChild(pcFile);
64
    addDisplayMaskMode(pcFileBuf, "FileBuffer");
65
    addDisplayMaskMode(pcBuffer, "Buffer");
66
    addDisplayMaskMode(pcFile, "File");
67
}
68

69
void ViewProviderInventorObject::setDisplayMode(const char* ModeName)
70
{
71
    if (strcmp("File+Buffer",ModeName)==0)
72
        setDisplayMaskMode("FileBuffer");
73
    else if (strcmp("Buffer",ModeName)==0)
74
        setDisplayMaskMode("Buffer");
75
    else if (strcmp("File",ModeName)==0)
76
        setDisplayMaskMode("File");
77
    ViewProviderDocumentObject::setDisplayMode(ModeName);
78
}
79

80
std::vector<std::string> ViewProviderInventorObject::getDisplayModes() const
81
{
82
    std::vector<std::string> StrList;
83
    StrList.emplace_back("File+Buffer");
84
    StrList.emplace_back("Buffer");
85
    StrList.emplace_back("File");
86
    return StrList;
87
}
88

89
void ViewProviderInventorObject::updateData(const App::Property* prop)
90
{
91
    auto ivObj = static_cast<App::InventorObject*>(pcObject);
92
    if (prop == &ivObj->Buffer) {
93
        // read from buffer
94
        SoInput in;
95
        std::string buffer = ivObj->Buffer.getValue();
96
        coinRemoveAllChildren(pcBuffer);
97
        if (buffer.empty())
98
            return;
99
        in.setBuffer((void *)buffer.c_str(), buffer.size());
100
        SoSeparator * node = SoDB::readAll(&in);
101
        if (node) {
102
            const char* doc = this->pcObject->getDocument()->getName();
103
            const char* obj = this->pcObject->getNameInDocument();
104
            adjustSelectionNodes(node, doc, obj);
105
            pcBuffer->addChild(node);
106
        }
107
    }
108
    else if (prop == &ivObj->FileName) {
109
        // read also from file
110
        const char* filename = ivObj->FileName.getValue();
111
        QString fn = QString::fromUtf8(filename);
112
        QFile file(fn);
113
        SoInput in;
114
        coinRemoveAllChildren(pcFile);
115
        if (!fn.isEmpty() && file.open(QFile::ReadOnly)) {
116
            QByteArray buffer = file.readAll();
117
            in.setBuffer((void *)buffer.constData(), buffer.length());
118
            SoSeparator * node = SoDB::readAll(&in);
119
            if (node) {
120
                const char* doc = this->pcObject->getDocument()->getName();
121
                const char* obj = this->pcObject->getNameInDocument();
122
                adjustSelectionNodes(node, doc, obj);
123
                pcFile->addChild(node);
124
            }
125
        }
126
    }
127
    else if (prop->isDerivedFrom(App::PropertyPlacement::getClassTypeId()) &&
128
             strcmp(prop->getName(), "Placement") == 0) {
129
        // Note: If R is the rotation, c the rotation center and t the translation
130
        // vector then Inventor applies the following transformation: R*(x-c)+c+t
131
        // In FreeCAD a placement only has a rotation and a translation part but
132
        // no rotation center. This means that the following equation must be ful-
133
        // filled: R * (x-c) + c + t = R * x + t
134
        //    <==> R * x + t - R * c + c = R * x + t
135
        //    <==> (I-R) * c = 0 ==> c = 0
136
        // This means that the center point must be the origin!
137
        Base::Placement p = static_cast<const App::PropertyPlacement*>(prop)->getValue();
138
        auto q0 = (float)p.getRotation().getValue()[0];
139
        auto q1 = (float)p.getRotation().getValue()[1];
140
        auto q2 = (float)p.getRotation().getValue()[2];
141
        auto q3 = (float)p.getRotation().getValue()[3];
142
        auto px = (float)p.getPosition().x;
143
        auto py = (float)p.getPosition().y;
144
        auto pz = (float)p.getPosition().z;
145
        pcTransform->rotation.setValue(q0,q1,q2,q3);
146
        pcTransform->translation.setValue(px,py,pz);
147
        pcTransform->center.setValue(0.0f,0.0f,0.0f);
148
        pcTransform->scaleFactor.setValue(1.0f,1.0f,1.0f);
149
    }
150
}
151

152
void ViewProviderInventorObject::adjustSelectionNodes(SoNode* child, const char* docname,
153
                                                      const char* objname)
154
{
155
    if (child->getTypeId().isDerivedFrom(SoFCSelection::getClassTypeId())) {
156
        static_cast<SoFCSelection*>(child)->documentName = docname;
157
        static_cast<SoFCSelection*>(child)->objectName = objname;
158
    }
159
    else if (child->getTypeId().isDerivedFrom(SoGroup::getClassTypeId())) {
160
        auto group = static_cast<SoGroup*>(child);
161
        for (int i=0; i<group->getNumChildren(); i++) {
162
            SoNode* subchild = group->getChild(i);
163
            adjustSelectionNodes(subchild, docname, objname);
164
        }
165
    }
166
}
167

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

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

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

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