FreeCAD
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#endif27
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/*
45namespace Assembly
46{
47
48Base::Placement getPlacementFromProp(App::DocumentObject* obj, const char* propName)
49{
50Base::Placement plc = Base::Placement();
51auto* propPlacement = dynamic_cast<App::PropertyPlacement*>(obj->getPropertyByName(propName));
52if (propPlacement) {
53plc = propPlacement->getValue();
54}
55return plc;
56}
57
58/* // Currently unused
59Base::Placement* getTargetPlacementRelativeTo(
60App::DocumentObject* targetObj, App::DocumentObject* part, App::DocumentObject* container,
61bool inContainerBranch, bool ignorePlacement = false)
62{
63inContainerBranch = inContainerBranch || (!ignorePlacement && part == container);
64
65Base::Console().Warning("sub --------------\n");
66if (targetObj == part && inContainerBranch && !ignorePlacement) {
67Base::Console().Warning("found0\n");
68return &getPlacementFromProp(targetObj, "Placement");
69}
70
71if (auto group = dynamic_cast<App::DocumentObjectGroup*>(part)) {
72for (auto& obj : group->getOutList()) {
73auto foundPlacement = getTargetPlacementRelativeTo(
74targetObj, obj, container, inContainerBranch, ignorePlacement
75);
76if (foundPlacement != nullptr) {
77return foundPlacement;
78}
79}
80}
81else if (auto assembly = dynamic_cast<AssemblyObject*>(part)) {
82Base::Console().Warning("h3\n");
83for (auto& obj : assembly->getOutList()) {
84auto foundPlacement = getTargetPlacementRelativeTo(
85targetObj, obj, container, inContainerBranch
86);
87if (foundPlacement == nullptr) {
88continue;
89}
90
91if (!ignorePlacement) {
92*foundPlacement = getPlacementFromProp(part, "Placement") * *foundPlacement;
93}
94
95Base::Console().Warning("found\n");
96return foundPlacement;
97}
98}
99else if (auto link = dynamic_cast<App::Link*>(part)) {
100Base::Console().Warning("h4\n");
101auto linked_obj = link->getLinkedObject();
102
103if (dynamic_cast<App::Part*>(linked_obj) || dynamic_cast<AssemblyObject*>(linked_obj)) {
104for (auto& obj : linked_obj->getOutList()) {
105auto foundPlacement = getTargetPlacementRelativeTo(
106targetObj, obj, container, inContainerBranch
107);
108if (foundPlacement == nullptr) {
109continue;
110}
111
112*foundPlacement = getPlacementFromProp(link, "Placement") * *foundPlacement;
113return foundPlacement;
114}
115}
116
117auto foundPlacement = getTargetPlacementRelativeTo(
118targetObj, linked_obj, container, inContainerBranch, true
119);
120
121if (foundPlacement != nullptr && !ignorePlacement) {
122*foundPlacement = getPlacementFromProp(link, "Placement") * *foundPlacement;
123}
124
125Base::Console().Warning("found2\n");
126return foundPlacement;
127}
128
129return nullptr;
130}
131
132Base::Placement getGlobalPlacement(App::DocumentObject* targetObj, App::DocumentObject* container =
133nullptr) { bool inContainerBranch = container == nullptr; auto rootObjects =
134App::GetApplication().getActiveDocument()->getRootObjects(); for (auto& part : rootObjects) { auto
135foundPlacement = getTargetPlacementRelativeTo(targetObj, part, container, inContainerBranch); if
136(foundPlacement != nullptr) { Base::Placement plc(foundPlacement->toMatrix()); return plc;
137}
138}
139
140return Base::Placement();
141}
142*/
143/*
144double getJointDistance(App::DocumentObject* joint)
145{
146double distance = 0.0;
147
148auto* prop = dynamic_cast<App::PropertyFloat*>(joint->getPropertyByName("Distance"));
149if (prop) {
150distance = prop->getValue();
151}
152
153return distance;
154}
155
156JointType getJointType(App::DocumentObject* joint)
157{
158JointType jointType = JointType::Fixed;
159
160auto* prop = dynamic_cast<App::PropertyEnumeration*>(joint->getPropertyByName("JointType"));
161if (prop) {
162jointType = static_cast<JointType>(prop->getValue());
163}
164
165return jointType;
166}
167
168const char* getElementFromProp(App::DocumentObject* obj, const char* propName)
169{
170auto* prop = dynamic_cast<App::PropertyString*>(obj->getPropertyByName(propName));
171if (!prop) {
172return "";
173}
174
175return prop->getValue();
176}
177
178std::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'
181std::string elementType;
182for (char ch : std::string(getElementFromProp(obj, propName))) {
183if (std::isalpha(ch)) {
184elementType += ch;
185}
186}
187return elementType;
188}
189
190App::DocumentObject* getLinkObjFromProp(App::DocumentObject* joint,
191const char* propLinkName)
192{
193auto* propObj = dynamic_cast<App::PropertyLink*>(joint->getPropertyByName(propLinkName));
194if (!propObj) {
195return nullptr;
196}
197return propObj->getValue();
198}
199
200App::DocumentObject* getObjFromNameProp(App::DocumentObject* joint,
201const char* pObjName,
202const char* pPart)
203{
204auto* propObjName = dynamic_cast<App::PropertyString*>(joint->getPropertyByName(pObjName));
205if (!propObjName) {
206return nullptr;
207}
208std::string objName = std::string(propObjName->getValue());
209
210App::DocumentObject* containingPart = getLinkObjFromProp(joint, pPart);
211if (!containingPart) {
212return nullptr;
213}
214
215if (objName == containingPart->getNameInDocument()) {
216return containingPart;
217}
218
219if (containingPart->getTypeId().isDerivedFrom(App::Link::getClassTypeId())) {
220App::Link* link = dynamic_cast<App::Link*>(containingPart);
221
222containingPart = link->getLinkedObject();
223if (!containingPart) {
224return nullptr;
225}
226}
227
228for (auto obj : containingPart->getOutList()) {
229if (objName == obj->getNameInDocument()) {
230return obj;
231}
232}
233
234return nullptr;
235}
236
237App::DocumentObject* getLinkedObjFromNameProp(App::DocumentObject* joint,
238const char* pObjName,
239const char* pPart)
240{
241auto* obj = getObjFromNameProp(joint, pObjName, pPart);
242if (obj) {
243return obj->getLinkedObject(true);
244}
245return nullptr;
246}
247
248} // namespace Assembly
249*/
250