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