| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2015 Stefan Tröger <[email protected]> * |
| 3 |
* Copyright (c) 2015 Alexander Golubev (Fat-Zer) <[email protected]> * |
| 4 |
* Copyright (c) 2024 Ondsel (PL Boyer) <[email protected]> * |
| 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 <string> |
| 25 |
|
| 26 |
#include <App/Document.h> |
| 27 |
#include <Base/Exception.h> |
| 28 |
#include <Base/Placement.h> |
| 29 |
#include <Base/Tools.h> |
| 30 |
|
| 31 |
#include "Datums.h" |
| 32 |
|
| 33 |
using namespace App; |
| 34 |
|
| 35 |
PROPERTY_SOURCE(App::DatumElement, App::GeoFeature) |
| 36 |
PROPERTY_SOURCE(App::Plane, App::DatumElement) |
| 37 |
PROPERTY_SOURCE(App::Line, App::DatumElement) |
| 38 |
PROPERTY_SOURCE(App::Point, App::DatumElement) |
| 39 |
PROPERTY_SOURCE_WITH_EXTENSIONS(App::LocalCoordinateSystem, App::GeoFeature) |
| 40 |
|
| 41 |
DatumElement::DatumElement(bool hideRole) |
| 42 |
: baseDir{0.0, 0.0, 1.0} |
| 43 |
{ |
| 44 |
ADD_PROPERTY_TYPE(Role, |
| 45 |
(""), |
| 46 |
0, |
| 47 |
App::Prop_ReadOnly, |
| 48 |
"Role of the datum in the local coordinate system."); |
| 49 |
|
| 50 |
// The role is hidden by default. It is visible only when the datum is in a LCS |
| 51 |
if (hideRole) { |
| 52 |
Role.setStatus(Property::Hidden, true); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
DatumElement::~DatumElement() = default; |
| 57 |
|
| 58 |
bool DatumElement::getCameraAlignmentDirection(Base::Vector3d& directionZ, Base::Vector3d& directionX, const char* subname) const |
| 59 |
{ |
| 60 |
Q_UNUSED(subname); |
| 61 |
|
| 62 |
Placement.getValue().getRotation().multVec(baseDir, directionZ); |
| 63 |
|
| 64 |
if (baseDir == Base::Vector3d::UnitZ) { |
| 65 |
Placement.getValue().getRotation().multVec(Base::Vector3d::UnitX, directionX); |
| 66 |
} |
| 67 |
|
| 68 |
return true; |
| 69 |
} |
| 70 |
|
| 71 |
App::LocalCoordinateSystem* DatumElement::getLCS() const |
| 72 |
{ |
| 73 |
auto inList = getInList(); |
| 74 |
for (auto* obj : inList) { |
| 75 |
auto* lcs = dynamic_cast<App::LocalCoordinateSystem*>(obj); |
| 76 |
if (lcs) { |
| 77 |
return lcs; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
return nullptr; |
| 82 |
} |
| 83 |
|
| 84 |
bool DatumElement::isOriginFeature() const |
| 85 |
{ |
| 86 |
const auto* lcs = getLCS(); |
| 87 |
return lcs ? lcs->isOrigin() : false; |
| 88 |
} |
| 89 |
|
| 90 |
Base::Vector3d DatumElement::getBasePoint() const |
| 91 |
{ |
| 92 |
Base::Vector3d pos = Placement.getValue().getPosition(); |
| 93 |
|
| 94 |
const auto* lcs = getLCS(); |
| 95 |
if (lcs && !lcs->isOrigin()) { |
| 96 |
pos += lcs->Placement.getValue().getPosition(); |
| 97 |
} |
| 98 |
|
| 99 |
return pos; |
| 100 |
} |
| 101 |
|
| 102 |
Base::Vector3d DatumElement::getDirection() const |
| 103 |
{ |
| 104 |
Base::Vector3d dir(baseDir); |
| 105 |
Base::Placement plc = Placement.getValue(); |
| 106 |
Base::Rotation rot = plc.getRotation(); |
| 107 |
rot.multVec(dir, dir); |
| 108 |
|
| 109 |
const auto* lcs = getLCS(); |
| 110 |
if (lcs && !lcs->isOrigin()) { |
| 111 |
Base::Rotation lcsRot = lcs->Placement.getValue().getRotation(); |
| 112 |
lcsRot.multVec(dir, dir); |
| 113 |
} |
| 114 |
|
| 115 |
return dir; |
| 116 |
} |
| 117 |
|
| 118 |
Base::Vector3d DatumElement::getBaseDirection() const |
| 119 |
{ |
| 120 |
return baseDir; |
| 121 |
} |
| 122 |
|
| 123 |
void DatumElement::setBaseDirection(const Base::Vector3d& dir) |
| 124 |
{ |
| 125 |
baseDir = dir; |
| 126 |
} |
| 127 |
|
| 128 |
// ---------------------------------------------------------------------------- |
| 129 |
|
| 130 |
Line::Line() |
| 131 |
{ |
| 132 |
setBaseDirection(Base::Vector3d(1, 0, 0)); |
| 133 |
} |
| 134 |
|
| 135 |
Point::Point() |
| 136 |
{ |
| 137 |
setBaseDirection(Base::Vector3d(0, 0, 0)); |
| 138 |
} |
| 139 |
|
| 140 |
// ---------------------------------------------------------------------------- |
| 141 |
|
| 142 |
LocalCoordinateSystem::LocalCoordinateSystem() |
| 143 |
{ |
| 144 |
ADD_PROPERTY_TYPE(OriginFeatures, |
| 145 |
(nullptr), |
| 146 |
0, |
| 147 |
App::Prop_Hidden, |
| 148 |
"Axis and baseplanes controlled by the LCS"); |
| 149 |
|
| 150 |
Group.setStatus(Property::Transient, true); |
| 151 |
|
| 152 |
setStatus(App::NoAutoExpand, true); |
| 153 |
|
| 154 |
GroupExtension::initExtension(this); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
LocalCoordinateSystem::~LocalCoordinateSystem() = default; |
| 159 |
|
| 160 |
bool LocalCoordinateSystem::getCameraAlignmentDirection(Base::Vector3d& directionZ, |
| 161 |
Base::Vector3d& directionX, |
| 162 |
const char* subname) const |
| 163 |
{ |
| 164 |
Q_UNUSED(subname); |
| 165 |
|
| 166 |
Placement.getValue().getRotation().multVec(Base::Vector3d::UnitZ, directionZ); |
| 167 |
Placement.getValue().getRotation().multVec(Base::Vector3d::UnitX, directionX); |
| 168 |
|
| 169 |
return true; |
| 170 |
} |
| 171 |
|
| 172 |
App::DatumElement* LocalCoordinateSystem::getDatumElement(const char* role) const |
| 173 |
{ |
| 174 |
const auto& features = OriginFeatures.getValues(); |
| 175 |
auto featIt = std::find_if(features.begin(), features.end(), [role](App::DocumentObject* obj) { |
| 176 |
return obj->isDerivedFrom<App::DatumElement>() |
| 177 |
&& strcmp(static_cast<App::DatumElement*>(obj)->Role.getValue(), role) == 0; |
| 178 |
}); |
| 179 |
if (featIt != features.end()) { |
| 180 |
return static_cast<App::DatumElement*>(*featIt); |
| 181 |
} |
| 182 |
// During restore, if role lookup fails (e.g. timing issues or fallback to internal name), |
| 183 |
// we suppress the error. The default getSubObject will try to resolve it by Internal Name next. |
| 184 |
if (!getDocument()->testStatus(App::Document::Restoring)) { |
| 185 |
std::stringstream err; |
| 186 |
err << "LocalCoordinateSystem \"" << getFullName() |
| 187 |
<< "\" doesn't contain feature with role \"" << role << '"'; |
| 188 |
throw Base::RuntimeError(err.str().c_str()); |
| 189 |
} |
| 190 |
return nullptr; |
| 191 |
} |
| 192 |
|
| 193 |
App::Line* LocalCoordinateSystem::getAxis(const char* role) const |
| 194 |
{ |
| 195 |
App::DatumElement* feat = getDatumElement(role); |
| 196 |
if (feat->isDerivedFrom<App::Line>()) { |
| 197 |
return static_cast<App::Line*>(feat); |
| 198 |
} |
| 199 |
std::stringstream err; |
| 200 |
err << "LocalCoordinateSystem \"" << getFullName() << "\" contains bad Axis object for role \"" |
| 201 |
<< role << '"'; |
| 202 |
throw Base::RuntimeError(err.str().c_str()); |
| 203 |
} |
| 204 |
|
| 205 |
App::Plane* LocalCoordinateSystem::getPlane(const char* role) const |
| 206 |
{ |
| 207 |
App::DatumElement* feat = getDatumElement(role); |
| 208 |
if (feat->isDerivedFrom<App::Plane>()) { |
| 209 |
return static_cast<App::Plane*>(feat); |
| 210 |
} |
| 211 |
std::stringstream err; |
| 212 |
err << "LocalCoordinateSystem \"" << getFullName() << "\" contains bad Plane object for role \"" |
| 213 |
<< role << '"'; |
| 214 |
throw Base::RuntimeError(err.str().c_str()); |
| 215 |
} |
| 216 |
|
| 217 |
App::Point* LocalCoordinateSystem::getPoint(const char* role) const |
| 218 |
{ |
| 219 |
App::DatumElement* feat = getDatumElement(role); |
| 220 |
if (feat->isDerivedFrom<App::Point>()) { |
| 221 |
return static_cast<App::Point*>(feat); |
| 222 |
} |
| 223 |
std::stringstream err; |
| 224 |
err << "LocalCoordinateSystem \"" << getFullName() << "\" contains bad Point object for role \"" |
| 225 |
<< role << '"'; |
| 226 |
throw Base::RuntimeError(err.str().c_str()); |
| 227 |
} |
| 228 |
|
| 229 |
short LocalCoordinateSystem::mustExecute() const |
| 230 |
{ |
| 231 |
if (OriginFeatures.isTouched()) { |
| 232 |
return 1; |
| 233 |
} |
| 234 |
return DocumentObject::mustExecute(); |
| 235 |
} |
| 236 |
|
| 237 |
App::DocumentObjectExecReturn* LocalCoordinateSystem::execute() |
| 238 |
{ |
| 239 |
try { // try to find all base axis and planes in the origin |
| 240 |
for (const char* role : AxisRoles) { |
| 241 |
App::Line* axis = getAxis(role); |
| 242 |
assert(axis); |
| 243 |
(void)axis; |
| 244 |
} |
| 245 |
for (const char* role : PlaneRoles) { |
| 246 |
App::Plane* plane = getPlane(role); |
| 247 |
assert(plane); |
| 248 |
(void)plane; |
| 249 |
} |
| 250 |
} |
| 251 |
catch (const Base::Exception& ex) { |
| 252 |
setError(); |
| 253 |
return new App::DocumentObjectExecReturn(ex.what()); |
| 254 |
} |
| 255 |
|
| 256 |
return DocumentObject::execute(); |
| 257 |
} |
| 258 |
|
| 259 |
const std::vector<LocalCoordinateSystem::SetupData>& LocalCoordinateSystem::getSetupData() |
| 260 |
{ |
| 261 |
using std::numbers::pi; |
| 262 |
|
| 263 |
static const std::vector<SetupData> setupData = { |
| 264 |
// clang-format off |
| 265 |
{App::Line::getClassTypeId(), AxisRoles[0], tr("X-axis"), Base::Rotation()}, |
| 266 |
{App::Line::getClassTypeId(), AxisRoles[1], tr("Y-axis"), Base::Rotation(Base::Vector3d(1, 1, 1), pi * 2 / 3)}, |
| 267 |
{App::Line::getClassTypeId(), AxisRoles[2], tr("Z-axis"), Base::Rotation(Base::Vector3d(1,-1, 1), pi * 2 / 3)}, |
| 268 |
{App::Plane::getClassTypeId(), PlaneRoles[0], tr("XY-plane"), Base::Rotation()}, |
| 269 |
{App::Plane::getClassTypeId(), PlaneRoles[1], tr("XZ-plane"), Base::Rotation(1.0, 0.0, 0.0, 1.0)}, |
| 270 |
{App::Plane::getClassTypeId(), PlaneRoles[2], tr("YZ-plane"), Base::Rotation(Base::Vector3d(1, 1, 1), pi * 2 / 3)}, |
| 271 |
{App::Point::getClassTypeId(), PointRoles[0], tr("Origin"), Base::Rotation()} |
| 272 |
// clang-format on |
| 273 |
}; |
| 274 |
return setupData; |
| 275 |
} |
| 276 |
|
| 277 |
DatumElement* LocalCoordinateSystem::createDatum(const SetupData& data) |
| 278 |
{ |
| 279 |
App::Document* doc = getDocument(); |
| 280 |
std::string objName = doc->getUniqueObjectName(data.role); |
| 281 |
App::DocumentObject* featureObj = doc->addObject(data.type.getName(), objName.c_str()); |
| 282 |
|
| 283 |
assert(featureObj && featureObj->isDerivedFrom<App::DatumElement>()); |
| 284 |
|
| 285 |
QByteArray byteArray = data.label.toUtf8(); |
| 286 |
featureObj->Label.setValue(byteArray.constData()); |
| 287 |
|
| 288 |
auto* feature = static_cast<App::DatumElement*>(featureObj); |
| 289 |
feature->Placement.setValue(Base::Placement(Base::Vector3d(), data.rot)); |
| 290 |
feature->Role.setValue(data.role); |
| 291 |
|
| 292 |
feature->Placement.setStatus(Property::Hidden, true); |
| 293 |
return feature; |
| 294 |
} |
| 295 |
|
| 296 |
LocalCoordinateSystem::SetupData LocalCoordinateSystem::getData(const char* role) |
| 297 |
{ |
| 298 |
const auto& setupData = getSetupData(); |
| 299 |
for (auto data : setupData) { |
| 300 |
if (std::strcmp(role, data.role) == 0) { |
| 301 |
return data; |
| 302 |
} |
| 303 |
} |
| 304 |
return LocalCoordinateSystem::SetupData(); |
| 305 |
} |
| 306 |
|
| 307 |
void LocalCoordinateSystem::setupObject() |
| 308 |
{ |
| 309 |
std::vector<App::DocumentObject*> links; |
| 310 |
const auto& setupData = getSetupData(); |
| 311 |
for (auto data : setupData) { |
| 312 |
links.push_back(createDatum(data)); |
| 313 |
} |
| 314 |
|
| 315 |
OriginFeatures.setValues(links); |
| 316 |
} |
| 317 |
|
| 318 |
void LocalCoordinateSystem::unsetupObject() |
| 319 |
{ |
| 320 |
const auto& objsLnk = OriginFeatures.getValues(); |
| 321 |
// Copy to set to assert we won't call method more then one time for each object |
| 322 |
const std::set<App::DocumentObject*> objs(objsLnk.begin(), objsLnk.end()); |
| 323 |
// Remove all controlled objects |
| 324 |
for (auto obj : objs) { |
| 325 |
// Check that previous deletes didn't indirectly remove one of our objects |
| 326 |
const auto& objsLnk2 = OriginFeatures.getValues(); |
| 327 |
if (std::ranges::find(objsLnk2, obj) != objsLnk2.end()) { |
| 328 |
if (!obj->isRemoving()) { |
| 329 |
obj->getDocument()->removeObject(obj->getNameInDocument()); |
| 330 |
} |
| 331 |
} |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
void LocalCoordinateSystem::onDocumentRestored() |
| 336 |
{ |
| 337 |
GeoFeature::onDocumentRestored(); |
| 338 |
|
| 339 |
// In 0.22 origins did not have point. |
| 340 |
migrateOriginPoint(); |
| 341 |
} |
| 342 |
|
| 343 |
void LocalCoordinateSystem::migrateOriginPoint() |
| 344 |
{ |
| 345 |
auto features = OriginFeatures.getValues(); |
| 346 |
|
| 347 |
auto isOrigin = [](App::DocumentObject* obj) { |
| 348 |
return obj->isDerivedFrom<App::DatumElement>() && |
| 349 |
strcmp(static_cast<App::DatumElement*>(obj)->Role.getValue(), PointRoles[0]) == 0; |
| 350 |
}; |
| 351 |
if (std::ranges::none_of(features, isOrigin)) { |
| 352 |
auto data = getData(PointRoles[0]); |
| 353 |
auto* origin = createDatum(data); |
| 354 |
origin->purgeTouched(); |
| 355 |
features.push_back(origin); |
| 356 |
OriginFeatures.setValues(features); |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
// ---------------------------------------------------------------------------- |
| 361 |
|
| 362 |
bool LocalCoordinateSystem::extensionGetSubObject(DocumentObject*& ret, |
| 363 |
const char* subname, |
| 364 |
PyObject** pyobj, |
| 365 |
Base::Matrix4D* mat, |
| 366 |
bool, |
| 367 |
int depth) const |
| 368 |
{ |
| 369 |
if (Base::Tools::isNullOrEmpty(subname)) { |
| 370 |
return false; |
| 371 |
} |
| 372 |
|
| 373 |
// mapping of object name to role name |
| 374 |
std::string name(subname); |
| 375 |
for (int i = 0; i < 3; i++) { |
| 376 |
if (name.rfind(LocalCoordinateSystem::AxisRoles[i], 0) == 0) { |
| 377 |
name = LocalCoordinateSystem::AxisRoles[i]; |
| 378 |
break; |
| 379 |
} |
| 380 |
if (name.rfind(LocalCoordinateSystem::PlaneRoles[i], 0) == 0) { |
| 381 |
name = LocalCoordinateSystem::PlaneRoles[i]; |
| 382 |
break; |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
if (name.rfind(LocalCoordinateSystem::PointRoles[0], 0) == 0) { |
| 387 |
name = LocalCoordinateSystem::PointRoles[0]; |
| 388 |
} |
| 389 |
|
| 390 |
try { |
| 391 |
ret = getDatumElement(name.c_str()); |
| 392 |
if (!ret) { |
| 393 |
return false; |
| 394 |
} |
| 395 |
const char* dot = strchr(subname, '.'); |
| 396 |
if (dot) { |
| 397 |
subname = dot + 1; |
| 398 |
} |
| 399 |
else { |
| 400 |
subname = ""; |
| 401 |
} |
| 402 |
ret = ret->getSubObject(subname, pyobj, mat, true, depth + 1); |
| 403 |
return true; |
| 404 |
} |
| 405 |
catch (const Base::Exception& e) { |
| 406 |
e.reportException(); |
| 407 |
return false; |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
bool LocalCoordinateSystem::hasObject(const DocumentObject* obj, [[maybe_unused]] bool recursive) const |
| 412 |
{ |
| 413 |
const auto& features = OriginFeatures.getValues(); |
| 414 |
return std::ranges::find(features, obj) != features.end(); |
| 415 |
} |