Debian Codegraph @

src/Gui/Selection/SelectionObject.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /***************************************************************************
3 * Copyright (c) 2009 Jürgen Riegel <[email protected]> *
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
25 #include <sstream>
26
27 #include <App/Application.h>
28 #include <App/Document.h>
29 #include <App/DocumentObject.h>
30 #include <Gui/Selection/SelectionObjectPy.h>
31
32 #include "SelectionObject.h"
33 #include "Selection.h"
34 #include "Application.h"
35 #include "Command.h"
36
37
38 using namespace Gui;
39
40
41 TYPESYSTEM_SOURCE_ABSTRACT(Gui::SelectionObject, Base::BaseClass)
42
43 SelectionObject::SelectionObject() = default;
44
45 SelectionObject::SelectionObject(const Gui::SelectionChanges& msg)
46 {
47 FeatName = msg.pObjectName ? msg.pObjectName : "";
48 DocName = msg.pDocName ? msg.pDocName : "";
49 TypeName = msg.pTypeName ? msg.pTypeName : "";
50 if (msg.pSubName) {
51 SubNames.emplace_back(msg.pSubName);
52 SelPoses.emplace_back(msg.x, msg.y, msg.z);
53 }
54 }
55
56 SelectionObject::SelectionObject(const App::DocumentObject* obj)
57 {
58 FeatName = obj->getNameInDocument();
59 DocName = obj->getDocument()->getName();
60 TypeName = obj->getTypeId().getName();
61 }
62
63 SelectionObject::~SelectionObject() = default;
64
65 const App::DocumentObject* SelectionObject::getObject() const
66 {
67 if (!DocName.empty()) {
68 App::Document* doc = App::GetApplication().getDocument(DocName.c_str());
69 if (doc && !FeatName.empty()) {
70 return doc->getObject(FeatName.c_str());
71 }
72 }
73 return nullptr;
74 }
75
76 App::DocumentObject* SelectionObject::getObject()
77 {
78 if (!DocName.empty()) {
79 App::Document* doc = App::GetApplication().getDocument(DocName.c_str());
80 if (doc && !FeatName.empty()) {
81 return doc->getObject(FeatName.c_str());
82 }
83 }
84 return nullptr;
85 }
86
87 bool SelectionObject::isObjectTypeOf(const Base::Type& typeId) const
88 {
89 const App::DocumentObject* obj = getObject();
90 return (obj && obj->isDerivedFrom(typeId));
91 }
92
93 std::string SelectionObject::getAsPropertyLinkSubString() const
94 {
95 std::ostringstream str;
96 str << "(" << Gui::Command::getObjectCmd(getObject()) << ",[";
97 for (const auto& it : SubNames) {
98 str << "'" << it << "',";
99 }
100 str << "])";
101 return str.str();
102 }
103
104 PyObject* SelectionObject::getPyObject()
105 {
106 return new SelectionObjectPy(new SelectionObject(*this));
107 }