| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2015 Alexander Golubev (Fat-Zer) <[email protected]> * |
| 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 |
|
| 24 |
#include <App/Document.h> |
| 25 |
#include <Base/Console.h> |
| 26 |
#include <Base/Exception.h> |
| 27 |
#include <Base/Tools.h> |
| 28 |
|
| 29 |
#include "OriginGroupExtension.h" |
| 30 |
#include "GeoFeature.h" |
| 31 |
#include "Origin.h" |
| 32 |
|
| 33 |
|
| 34 |
FC_LOG_LEVEL_INIT("App", true, true) |
| 35 |
|
| 36 |
using namespace App; |
| 37 |
|
| 38 |
EXTENSION_PROPERTY_SOURCE(App::OriginGroupExtension, App::GeoFeatureGroupExtension) |
| 39 |
|
| 40 |
OriginGroupExtension::OriginGroupExtension() |
| 41 |
{ |
| 42 |
|
| 43 |
initExtensionType(OriginGroupExtension::getExtensionClassTypeId()); |
| 44 |
|
| 45 |
EXTENSION_ADD_PROPERTY_TYPE(Origin, |
| 46 |
(nullptr), |
| 47 |
0, |
| 48 |
App::Prop_Hidden, |
| 49 |
"Origin linked to the group"); |
| 50 |
Origin.setScope(LinkScope::Child); |
| 51 |
} |
| 52 |
|
| 53 |
OriginGroupExtension::~OriginGroupExtension() = default; |
| 54 |
|
| 55 |
App::Origin* OriginGroupExtension::getOrigin() const |
| 56 |
{ |
| 57 |
App::DocumentObject* originObj = Origin.getValue(); |
| 58 |
|
| 59 |
if (!originObj) { |
| 60 |
std::stringstream err; |
| 61 |
err << "Can't find Origin for \"" << getExtendedObject()->getFullName() << "\""; |
| 62 |
throw Base::RuntimeError(err.str().c_str()); |
| 63 |
} |
| 64 |
else if (!originObj->isDerivedFrom<App::Origin>()) { |
| 65 |
std::stringstream err; |
| 66 |
err << "Bad object \"" << originObj->getFullName() << "\"(" |
| 67 |
<< originObj->getTypeId().getName() << ") linked to the Origin of \"" |
| 68 |
<< getExtendedObject()->getFullName() << "\""; |
| 69 |
throw Base::RuntimeError(err.str().c_str()); |
| 70 |
} |
| 71 |
else { |
| 72 |
return static_cast<App::Origin*>(originObj); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
bool OriginGroupExtension::extensionGetSubObject(DocumentObject*& ret, |
| 77 |
const char* subname, |
| 78 |
PyObject** pyObj, |
| 79 |
Base::Matrix4D* mat, |
| 80 |
bool transform, |
| 81 |
int depth) const |
| 82 |
{ |
| 83 |
App::DocumentObject* originObj = Origin.getValue(); |
| 84 |
const char* dot; |
| 85 |
if (originObj && originObj->isAttachedToDocument() && subname && (dot = strchr(subname, '.'))) { |
| 86 |
bool found; |
| 87 |
if (subname[0] == '$') { |
| 88 |
found = std::string(subname + 1, dot) == originObj->Label.getValue(); |
| 89 |
} |
| 90 |
else { |
| 91 |
found = std::string(subname, dot) == originObj->getNameInDocument(); |
| 92 |
} |
| 93 |
if (found) { |
| 94 |
if (mat && transform) { |
| 95 |
*mat *= const_cast<OriginGroupExtension*>(this)->placement().getValue().toMatrix(); |
| 96 |
} |
| 97 |
ret = originObj->getSubObject(dot + 1, pyObj, mat, true, depth + 1); |
| 98 |
return true; |
| 99 |
} |
| 100 |
} |
| 101 |
return GeoFeatureGroupExtension::extensionGetSubObject(ret, |
| 102 |
subname, |
| 103 |
pyObj, |
| 104 |
mat, |
| 105 |
transform, |
| 106 |
depth); |
| 107 |
} |
| 108 |
|
| 109 |
App::DocumentObject* OriginGroupExtension::getGroupOfObject(const DocumentObject* obj) |
| 110 |
{ |
| 111 |
|
| 112 |
if (!obj) { |
| 113 |
return nullptr; |
| 114 |
} |
| 115 |
|
| 116 |
bool isOriginFeature = obj->isDerivedFrom<App::DatumElement>(); |
| 117 |
|
| 118 |
auto list = obj->getInList(); |
| 119 |
for (auto o : list) { |
| 120 |
if (o->hasExtension(App::OriginGroupExtension::getExtensionClassTypeId())) { |
| 121 |
return o; |
| 122 |
} |
| 123 |
else if (isOriginFeature |
| 124 |
&& o->isDerivedFrom<App::LocalCoordinateSystem>()) { |
| 125 |
auto result = getGroupOfObject(o); |
| 126 |
if (result) { |
| 127 |
return result; |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
return nullptr; |
| 133 |
} |
| 134 |
|
| 135 |
short OriginGroupExtension::extensionMustExecute() |
| 136 |
{ |
| 137 |
if (Origin.isTouched()) { |
| 138 |
return 1; |
| 139 |
} |
| 140 |
else { |
| 141 |
return GeoFeatureGroupExtension::extensionMustExecute(); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
App::DocumentObjectExecReturn* OriginGroupExtension::extensionExecute() |
| 146 |
{ |
| 147 |
try { // try to find all base axis and planes in the origin |
| 148 |
getOrigin(); |
| 149 |
} |
| 150 |
catch (const Base::Exception& ex) { |
| 151 |
// getExtendedObject()->setError (); |
| 152 |
return new App::DocumentObjectExecReturn(ex.what()); |
| 153 |
} |
| 154 |
|
| 155 |
return GeoFeatureGroupExtension::extensionExecute(); |
| 156 |
} |
| 157 |
|
| 158 |
App::DocumentObject* OriginGroupExtension::getLocalizedOrigin(App::Document* doc) |
| 159 |
{ |
| 160 |
auto* originObject = doc->addObject<App::Origin>("Origin"); |
| 161 |
QByteArray byteArray = tr("Origin").toUtf8(); |
| 162 |
originObject->Label.setValue(byteArray.constData()); |
| 163 |
return originObject; |
| 164 |
} |
| 165 |
|
| 166 |
void OriginGroupExtension::onExtendedSetupObject() |
| 167 |
{ |
| 168 |
App::Document* doc = getExtendedObject()->getDocument(); |
| 169 |
|
| 170 |
App::DocumentObject* originObj = getLocalizedOrigin(doc); |
| 171 |
|
| 172 |
assert(originObj && originObj->isDerivedFrom<App::Origin>()); |
| 173 |
Origin.setValue(originObj); |
| 174 |
|
| 175 |
GeoFeatureGroupExtension::onExtendedSetupObject(); |
| 176 |
} |
| 177 |
|
| 178 |
void OriginGroupExtension::onExtendedUnsetupObject() |
| 179 |
{ |
| 180 |
App::DocumentObject* origin = Origin.getValue(); |
| 181 |
if (origin && !origin->isRemoving()) { |
| 182 |
origin->getDocument()->removeObject(origin->getNameInDocument()); |
| 183 |
} |
| 184 |
|
| 185 |
GeoFeatureGroupExtension::onExtendedUnsetupObject(); |
| 186 |
} |
| 187 |
|
| 188 |
void OriginGroupExtension::extensionOnChanged(const Property* p) |
| 189 |
{ |
| 190 |
if (p == &Origin) { |
| 191 |
App::DocumentObject* owner = getExtendedObject(); |
| 192 |
App::DocumentObject* origin = Origin.getValue(); |
| 193 |
// Document::Importing indicates the object is being imported (i.e. |
| 194 |
// copied). So check the Origin ownership here to prevent copy without |
| 195 |
// dependency |
| 196 |
if (origin && owner && owner->getDocument() |
| 197 |
&& owner->getDocument()->testStatus(Document::Importing)) { |
| 198 |
for (auto o : origin->getInList()) { |
| 199 |
if (o != owner |
| 200 |
&& o->hasExtension(App::OriginGroupExtension::getExtensionClassTypeId())) { |
| 201 |
App::Document* document = owner->getDocument(); |
| 202 |
// Temporarily reset 'Restoring' status to allow document to auto label new |
| 203 |
// objects |
| 204 |
Base::ObjectStatusLocker<Document::Status, Document> guard(Document::Restoring, |
| 205 |
document, |
| 206 |
false); |
| 207 |
Origin.setValue(getLocalizedOrigin(document)); |
| 208 |
FC_WARN("Reset origin in " << owner->getFullName()); |
| 209 |
return; |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
GeoFeatureGroupExtension::extensionOnChanged(p); |
| 215 |
} |
| 216 |
|
| 217 |
void OriginGroupExtension::relinkToOrigin(App::DocumentObject* obj) |
| 218 |
{ |
| 219 |
// we get all links and replace the origin objects if needed (subnames need not to change, they |
| 220 |
// would stay the same) |
| 221 |
std::vector<App::DocumentObject*> result; |
| 222 |
std::vector<App::Property*> list; |
| 223 |
obj->getPropertyList(list); |
| 224 |
auto isOriginFeature = [](App::DocumentObject* obj) -> bool { |
| 225 |
// Check if the object is a DatumElement |
| 226 |
if (auto* datumElement = dynamic_cast<App::DatumElement*>(obj)) { |
| 227 |
// Check if the DatumElement is an origin |
| 228 |
return datumElement->isOriginFeature(); |
| 229 |
} |
| 230 |
return false; |
| 231 |
}; |
| 232 |
|
| 233 |
for (App::Property* prop : list) { |
| 234 |
if (prop->isDerivedFrom<App::PropertyLink>()) { |
| 235 |
|
| 236 |
auto p = static_cast<App::PropertyLink*>(prop); |
| 237 |
if (!p->getValue() || !isOriginFeature(p->getValue())) { |
| 238 |
continue; |
| 239 |
} |
| 240 |
|
| 241 |
p->setValue(getOrigin()->getDatumElement( |
| 242 |
static_cast<DatumElement*>(p->getValue())->Role.getValue())); |
| 243 |
} |
| 244 |
else if (prop->isDerivedFrom<App::PropertyLinkList>()) { |
| 245 |
auto p = static_cast<App::PropertyLinkList*>(prop); |
| 246 |
auto vec = p->getValues(); |
| 247 |
std::vector<App::DocumentObject*> result; |
| 248 |
bool changed = false; |
| 249 |
for (App::DocumentObject* o : vec) { |
| 250 |
if (!isOriginFeature(o)) { |
| 251 |
result.push_back(o); |
| 252 |
} |
| 253 |
else { |
| 254 |
result.push_back(getOrigin()->getDatumElement( |
| 255 |
static_cast<DatumElement*>(o)->Role.getValue())); |
| 256 |
changed = true; |
| 257 |
} |
| 258 |
} |
| 259 |
if (changed) { |
| 260 |
static_cast<App::PropertyLinkList*>(prop)->setValues(result); |
| 261 |
} |
| 262 |
} |
| 263 |
else if (prop->isDerivedFrom<App::PropertyLinkSub>()) { |
| 264 |
auto p = static_cast<App::PropertyLinkSub*>(prop); |
| 265 |
if (!p->getValue() || !isOriginFeature(p->getValue())) { |
| 266 |
continue; |
| 267 |
} |
| 268 |
|
| 269 |
std::vector<std::string> subValues = p->getSubValues(); |
| 270 |
p->setValue(getOrigin()->getDatumElement( |
| 271 |
static_cast<DatumElement*>(p->getValue())->Role.getValue()), |
| 272 |
subValues); |
| 273 |
} |
| 274 |
else if (prop->isDerivedFrom<App::PropertyLinkSubList>()) { |
| 275 |
auto p = static_cast<App::PropertyLinkSubList*>(prop); |
| 276 |
auto vec = p->getSubListValues(); |
| 277 |
bool changed = false; |
| 278 |
for (auto& v : vec) { |
| 279 |
if (isOriginFeature(v.first)) { |
| 280 |
v.first = getOrigin()->getDatumElement( |
| 281 |
static_cast<DatumElement*>(v.first)->Role.getValue()); |
| 282 |
changed = true; |
| 283 |
} |
| 284 |
} |
| 285 |
if (changed) { |
| 286 |
p->setSubListValues(vec); |
| 287 |
} |
| 288 |
} |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
std::vector<DocumentObject*> OriginGroupExtension::addObjects(std::vector<DocumentObject*> objs) |
| 293 |
{ |
| 294 |
|
| 295 |
for (auto obj : objs) { |
| 296 |
relinkToOrigin(obj); |
| 297 |
} |
| 298 |
|
| 299 |
return App::GeoFeatureGroupExtension::addObjects(objs); |
| 300 |
} |
| 301 |
|
| 302 |
bool OriginGroupExtension::hasObject(const DocumentObject* obj, bool recursive) const |
| 303 |
{ |
| 304 |
|
| 305 |
if (Origin.getValue() && (obj == getOrigin() || getOrigin()->hasObject(obj))) { |
| 306 |
return true; |
| 307 |
} |
| 308 |
|
| 309 |
return App::GroupExtension::hasObject(obj, recursive); |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
// Python feature --------------------------------------------------------- |
| 314 |
|
| 315 |
namespace App |
| 316 |
{ |
| 317 |
EXTENSION_PROPERTY_SOURCE_TEMPLATE(App::OriginGroupExtensionPython, App::OriginGroupExtension) |
| 318 |
|
| 319 |
// explicit template instantiation |
| 320 |
template class AppExport ExtensionPythonT<GroupExtensionPythonT<OriginGroupExtension>>; |
| 321 |
} // namespace App |