FreeCAD

Форк
0
/
AssemblyUtils.cpp 
249 строк · 8.3 Кб
1
// SPDX-License-Identifier: LGPL-2.1-or-later
2
/****************************************************************************
3
 *                                                                          *
4
 *   Copyright (c) 2023 Ondsel <development@ondsel.com>                     *
5
 *                                                                          *
6
 *   This file is part of FreeCAD.                                          *
7
 *                                                                          *
8
 *   FreeCAD is free software: you can redistribute it and/or modify it     *
9
 *   under the terms of the GNU Lesser General Public License as            *
10
 *   published by the Free Software Foundation, either version 2.1 of the   *
11
 *   License, or (at your option) any later version.                        *
12
 *                                                                          *
13
 *   FreeCAD is distributed in the hope that it will be useful, but         *
14
 *   WITHOUT ANY WARRANTY; without even the implied warranty of             *
15
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU       *
16
 *   Lesser General Public License for more details.                        *
17
 *                                                                          *
18
 *   You should have received a copy of the GNU Lesser General Public       *
19
 *   License along with FreeCAD. If not, see                                *
20
 *   <https://www.gnu.org/licenses/>.                                       *
21
 *                                                                          *
22
 ***************************************************************************/
23

24
#include "PreCompiled.h"
25
#ifndef _PreComp_
26
#endif
27

28
#include <App/Application.h>
29
#include <App/Document.h>
30
#include <App/DocumentObject.h>
31
#include <App/PropertyStandard.h>
32
// #include <App/DocumentObjectGroup.h>
33
#include <App/Link.h>
34
// #include <Base/Console.h>
35
#include <Base/Placement.h>
36
#include <Base/Tools.h>
37
#include <Base/Interpreter.h>
38

39
#include <Mod/Part/App/PartFeature.h>
40

41
#include "AssemblyUtils.h"
42

43
// ======================================= Utils ======================================
44
/*
45
namespace Assembly
46
{
47

48
Base::Placement getPlacementFromProp(App::DocumentObject* obj, const char* propName)
49
{
50
    Base::Placement plc = Base::Placement();
51
    auto* propPlacement = dynamic_cast<App::PropertyPlacement*>(obj->getPropertyByName(propName));
52
    if (propPlacement) {
53
        plc = propPlacement->getValue();
54
    }
55
    return plc;
56
}
57

58
/* // Currently unused
59
Base::Placement* getTargetPlacementRelativeTo(
60
    App::DocumentObject* targetObj, App::DocumentObject* part, App::DocumentObject* container,
61
    bool inContainerBranch, bool ignorePlacement = false)
62
{
63
    inContainerBranch = inContainerBranch || (!ignorePlacement && part == container);
64

65
    Base::Console().Warning("sub --------------\n");
66
    if (targetObj == part && inContainerBranch && !ignorePlacement) {
67
        Base::Console().Warning("found0\n");
68
        return &getPlacementFromProp(targetObj, "Placement");
69
    }
70

71
    if (auto group = dynamic_cast<App::DocumentObjectGroup*>(part)) {
72
        for (auto& obj : group->getOutList()) {
73
            auto foundPlacement = getTargetPlacementRelativeTo(
74
                targetObj, obj, container, inContainerBranch, ignorePlacement
75
            );
76
            if (foundPlacement != nullptr) {
77
                return foundPlacement;
78
            }
79
        }
80
    }
81
    else if (auto assembly = dynamic_cast<AssemblyObject*>(part)) {
82
        Base::Console().Warning("h3\n");
83
        for (auto& obj : assembly->getOutList()) {
84
            auto foundPlacement = getTargetPlacementRelativeTo(
85
                targetObj, obj, container, inContainerBranch
86
            );
87
            if (foundPlacement == nullptr) {
88
                continue;
89
            }
90

91
            if (!ignorePlacement) {
92
                *foundPlacement = getPlacementFromProp(part, "Placement") * *foundPlacement;
93
            }
94

95
            Base::Console().Warning("found\n");
96
            return foundPlacement;
97
        }
98
    }
99
    else if (auto link = dynamic_cast<App::Link*>(part)) {
100
        Base::Console().Warning("h4\n");
101
        auto linked_obj = link->getLinkedObject();
102

103
        if (dynamic_cast<App::Part*>(linked_obj) || dynamic_cast<AssemblyObject*>(linked_obj)) {
104
            for (auto& obj : linked_obj->getOutList()) {
105
                auto foundPlacement = getTargetPlacementRelativeTo(
106
                    targetObj, obj, container, inContainerBranch
107
                );
108
                if (foundPlacement == nullptr) {
109
                    continue;
110
                }
111

112
                *foundPlacement = getPlacementFromProp(link, "Placement") * *foundPlacement;
113
                return foundPlacement;
114
            }
115
        }
116

117
        auto foundPlacement = getTargetPlacementRelativeTo(
118
            targetObj, linked_obj, container, inContainerBranch, true
119
        );
120

121
        if (foundPlacement != nullptr && !ignorePlacement) {
122
            *foundPlacement = getPlacementFromProp(link, "Placement") * *foundPlacement;
123
        }
124

125
        Base::Console().Warning("found2\n");
126
        return foundPlacement;
127
    }
128

129
    return nullptr;
130
}
131

132
Base::Placement getGlobalPlacement(App::DocumentObject* targetObj, App::DocumentObject* container =
133
nullptr) { bool inContainerBranch = container == nullptr; auto rootObjects =
134
App::GetApplication().getActiveDocument()->getRootObjects(); for (auto& part : rootObjects) { auto
135
foundPlacement = getTargetPlacementRelativeTo(targetObj, part, container, inContainerBranch); if
136
(foundPlacement != nullptr) { Base::Placement plc(foundPlacement->toMatrix()); return plc;
137
        }
138
    }
139

140
    return Base::Placement();
141
}
142
*/
143
/*
144
double getJointDistance(App::DocumentObject* joint)
145
{
146
    double distance = 0.0;
147

148
    auto* prop = dynamic_cast<App::PropertyFloat*>(joint->getPropertyByName("Distance"));
149
    if (prop) {
150
        distance = prop->getValue();
151
    }
152

153
    return distance;
154
}
155

156
JointType getJointType(App::DocumentObject* joint)
157
{
158
    JointType jointType = JointType::Fixed;
159

160
    auto* prop = dynamic_cast<App::PropertyEnumeration*>(joint->getPropertyByName("JointType"));
161
    if (prop) {
162
        jointType = static_cast<JointType>(prop->getValue());
163
    }
164

165
    return jointType;
166
}
167

168
const char* getElementFromProp(App::DocumentObject* obj, const char* propName)
169
{
170
    auto* prop = dynamic_cast<App::PropertyString*>(obj->getPropertyByName(propName));
171
    if (!prop) {
172
        return "";
173
    }
174

175
    return prop->getValue();
176
}
177

178
std::string getElementTypeFromProp(App::DocumentObject* obj, const char* propName)
179
{
180
    // The prop is going to be something like 'Edge14' or 'Face7'. We need 'Edge' or 'Face'
181
    std::string elementType;
182
    for (char ch : std::string(getElementFromProp(obj, propName))) {
183
        if (std::isalpha(ch)) {
184
            elementType += ch;
185
        }
186
    }
187
    return elementType;
188
}
189

190
App::DocumentObject* getLinkObjFromProp(App::DocumentObject* joint,
191
    const char* propLinkName)
192
{
193
    auto* propObj = dynamic_cast<App::PropertyLink*>(joint->getPropertyByName(propLinkName));
194
    if (!propObj) {
195
        return nullptr;
196
    }
197
    return propObj->getValue();
198
}
199

200
App::DocumentObject* getObjFromNameProp(App::DocumentObject* joint,
201
    const char* pObjName,
202
    const char* pPart)
203
{
204
    auto* propObjName = dynamic_cast<App::PropertyString*>(joint->getPropertyByName(pObjName));
205
    if (!propObjName) {
206
        return nullptr;
207
    }
208
    std::string objName = std::string(propObjName->getValue());
209

210
    App::DocumentObject* containingPart = getLinkObjFromProp(joint, pPart);
211
    if (!containingPart) {
212
        return nullptr;
213
    }
214

215
    if (objName == containingPart->getNameInDocument()) {
216
        return containingPart;
217
    }
218

219
    if (containingPart->getTypeId().isDerivedFrom(App::Link::getClassTypeId())) {
220
        App::Link* link = dynamic_cast<App::Link*>(containingPart);
221

222
        containingPart = link->getLinkedObject();
223
        if (!containingPart) {
224
            return nullptr;
225
        }
226
    }
227

228
    for (auto obj : containingPart->getOutList()) {
229
        if (objName == obj->getNameInDocument()) {
230
            return obj;
231
        }
232
    }
233

234
    return nullptr;
235
}
236

237
App::DocumentObject* getLinkedObjFromNameProp(App::DocumentObject* joint,
238
    const char* pObjName,
239
    const char* pPart)
240
    {
241
        auto* obj = getObjFromNameProp(joint, pObjName, pPart);
242
        if (obj) {
243
            return obj->getLinkedObject(true);
244
        }
245
        return nullptr;
246
    }
247

248
} // namespace Assembly
249
*/
250

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

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

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

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