File size: 23,274 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 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 | // SPDX-License-Identifier: LGPL-2.0-or-later
/***************************************************************************
* Copyright (c) 2023 WandererFan <wandererfan@gmail.com> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library 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 Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
// a class to validate and correct dimension references
// the dimension reference auto correct algo:
//
// when a dimension is created, the shape of each reference is saved, so in addition
// to the dimensionedObject + subElement reference, we also keep a Part::TopoShape copy
// of the reference.
//
// when we later use that dimension, we check its references as follows:
// for each reference:
// // auto correct phase 1
// if ref.currentGeometry == ref.savedGeometry:
// // the reference points to the same geometry as before, so we
// // so we consider this to be correct. same geometry, same index case.
// continue
// else:
// // search all the source shapes for a subelement with the exact same
// // geometry. same geometry, different index case.
// newRef = searchForExactSameGeometry(ref) // geometry matcher
// if newRef:
// // substitute the reference we just found in place of the old
// // reference
// replace(ref, newRef)
// else:
// // auto correct phase 2 - to be implemented
// // we don't have any geometry that is identical to our saved geometry.
// // finding a match now becomes guess work. we have to find the most
// // similar geometry (with at least some level of same-ness) and use
// // that to rebuild our reference.
// // we do not have a good algo for searchForMostSimilarGeometry() yet.
// newRef = searchForMostSimilarGeometry(ref) // geometry guesser
// if newRef:
// replace(ref, newRef)
// else:
// //we can't fix this
#include <TopoDS_Shape.hxx>
#include <App/Document.h>
#include <Base/Console.h>
#include <Base/Tools.h>
#include <Mod/Part/App/TopoShape.h>
#include <Mod/Measure/App/ShapeFinder.h>
#include "GeometryMatcher.h"
#include "DimensionReferences.h"
#include "DimensionGeometry.h"
#include "DimensionAutoCorrect.h"
#include "DrawUtil.h"
#include "Preferences.h"
using namespace TechDraw;
using namespace Measure;
using DU = DrawUtil;
//! true if references point to valid geometry and the valid geometry matches the
//! corresponding saved geometry. this method does not correct anything, it just
//! verifies if the references point to the same geometry as when the reference
//! was created.
bool DimensionAutoCorrect::referencesHaveValidGeometry(std::vector<bool>& referenceState) const
{
// Base::Console().message("DAC::referencesHaveValidGeometry()\n");
ReferenceVector refsAll = getDimension()->getEffectiveReferences();
const std::vector<Part::TopoShape> savedGeometry = getDimension()->SavedGeometry.getValues();
if (savedGeometry.empty() || savedGeometry.size() != refsAll.size()) {
// this must be an old document without savedGeometry property. We can not
// validate the references in this case so we hope they are valid.
referenceState = std::vector<bool>(refsAll.size(), true);
return true;
}
bool result {true};
size_t iRef {0};
for (auto& entry : refsAll) {
if (entry.hasGeometry()) {
// entry points to something, is it the correct geom?
if (isMatchingGeometry(entry, savedGeometry.at(iRef))) {
referenceState.emplace_back(true);
}
else {
result = false;
referenceState.emplace_back(false);
}
}
else {
result = false;
referenceState.emplace_back(false);
}
iRef++;
}
return result;
}
//! try to correct references that point to non-existent geometry or when the saved
//! geometry does not match the current geometry the reference points to.
//! referenceState is the output of a previous use of referencesHaveValidGeometry.
bool DimensionAutoCorrect::autocorrectReferences(std::vector<bool>& referenceState,
ReferenceVector& repairedRefs) const
{
// Base::Console().message("DAC::autocorrectReferences()\n");
if (!Preferences::autoCorrectDimRefs()) {
return false;
}
bool result {true};
ReferenceVector refsAll = getDimension()->getEffectiveReferences();
const std::vector<Part::TopoShape> savedGeometry = getDimension()->SavedGeometry.getValues();
if (savedGeometry.empty() || savedGeometry.size() != refsAll.size()) {
// this must be an old document without savedGeometry property. We can not
// validate the references in this case so we have to assume the references are
// correct.
return true;
}
size_t iRef {0};
for (const auto& state : referenceState) {
if (state) {
// ref points to valid geometry that matches saved geometry
referenceState.at(iRef) = true;
repairedRefs.push_back(refsAll.at(iRef));
iRef++;
continue;
}
const Part::TopoShape& temp = savedGeometry.at(iRef);
if (temp.isNull()) {
result = false;
referenceState.at(iRef) = false;
repairedRefs.push_back(refsAll.at(iRef));
iRef++;
// we could exit here instead of checking all the refs?
continue;
}
// this ref does not point to valid geometry or
// the geometry it points to does not match the saved geometry
ReferenceEntry fixedRef = refsAll.at(iRef);
// first, look for an exact match to the saved geometry
bool success = fix1GeomExact(fixedRef, savedGeometry.at(iRef).getShape());
if (success) {
// we did find a match
referenceState.at(iRef) = true;
repairedRefs.push_back(fixedRef);
iRef++;
continue;
}
// we did not find an exact match, so check for a similar match
success = fix1GeomSimilar(fixedRef, savedGeometry.at(iRef).getShape());
if (success) {
// we did find a similar match
referenceState.at(iRef) = true;
repairedRefs.push_back(fixedRef);
iRef++;
continue;
}
// we did not find a similar match the geometry
result = false;
referenceState.at(iRef) = false;
repairedRefs.push_back(fixedRef);
iRef++;
// we could exit here
}
return result;
}
//! fix a single reference with an exact match to geomToMatch
bool DimensionAutoCorrect::fix1GeomExact(ReferenceEntry& refToFix, const TopoDS_Shape &geomToMatch) const
{
// Base::Console().message("DAC::fix1GeomExact()\n");
ReferenceEntry fixedRef = refToFix;
Part::TopoShape topoShapeToMatch(geomToMatch);
bool success {false};
if (refToFix.is3d()) {
if (!refToFix.getObject() && m_3dObjectCache.empty()) {
return false;
}
if (geomToMatch.ShapeType() == TopAbs_VERTEX) {
success = findExactVertex3d(refToFix, topoShapeToMatch);
}
else {
success = findExactEdge3d(refToFix, topoShapeToMatch);
}
}
else {
if (geomToMatch.ShapeType() == TopAbs_VERTEX) {
success = findExactVertex2d(refToFix, topoShapeToMatch);
}
else {
success = findExactEdge2d(refToFix, topoShapeToMatch);
}
}
return success;
}
//! fix a single reference with an Similar match to geomToMatch
bool DimensionAutoCorrect::fix1GeomSimilar(ReferenceEntry& refToFix, const TopoDS_Shape &geomToMatch) const
{
// Base::Console().message("DAC::fix1GeomSimilar()\n");
Part::TopoShape topoShapeToMatch(geomToMatch);
bool success {false};
if (refToFix.is3d()) {
if (!refToFix.getObject() && m_3dObjectCache.empty()) {
// can't fix this. nothing to compare.
return false;
}
if (geomToMatch.ShapeType() == TopAbs_VERTEX) {
success = findSimilarVertex3d(refToFix, topoShapeToMatch);
}
else {
success = findSimilarEdge3d(refToFix, topoShapeToMatch);
}
}
else {
if (geomToMatch.ShapeType() == TopAbs_VERTEX) {
success = findSimilarVertex2d(refToFix, topoShapeToMatch);
}
else {
success = findSimilarEdge2d(refToFix, topoShapeToMatch);
}
}
return success;
}
//! search the view for a 2d vertex that is the same as the saved reference geometry
//! and return a reference pointing to the matching vertex
bool DimensionAutoCorrect::findExactVertex2d(ReferenceEntry& refToFix,
const Part::TopoShape& refGeom) const
{
// Base::Console().message("DAC::findExactVertex2d()\n");
getMatcher()->setPointTolerance(EWTOLERANCE);
auto refObj = refToFix.getObject();
auto refDvp = dynamic_cast<TechDraw::DrawViewPart*>(refObj);
if (refDvp) {
ReferenceEntry fixedRef = searchViewForVert(refDvp, refGeom);
if (fixedRef.getObject()) {
refToFix = fixedRef;
return true;
}
}
// no match
return false;
}
//! search the view for a 2d edge that is the same as the saved reference geometry (from DVD::SavedGeometry)
//! and return a reference pointing to the matching edge.
bool DimensionAutoCorrect::findExactEdge2d(ReferenceEntry& refToFix, const Part::TopoShape& refGeom) const
{
// Base::Console().message("DAC::findExactEdge2d()\n");
auto refObj = refToFix.getObject();
auto refDvp = dynamic_cast<TechDraw::DrawViewPart*>(refObj);
if (refDvp) {
ReferenceEntry fixedRef = searchViewForExactEdge(refDvp, refGeom);
if (fixedRef.getObject()) {
refToFix = fixedRef;
return true;
}
}
// no match, return the input reference
return false;
}
//! search the model for a 3d vertex that is the same as the saved reference geometry
//! and return a reference pointing to the matching vertex
bool DimensionAutoCorrect::findExactVertex3d(ReferenceEntry& refToFix,
const Part::TopoShape& refGeom) const
{
// Base::Console().message("DAC::findExactVertex3d()\n");
getMatcher()->setPointTolerance(EWTOLERANCE);
// try the referenced object
auto refObj = refToFix.getObject();
if (refObj) {
ReferenceEntry fixedRef = searchObjForVert(refObj, refGeom);
if (fixedRef.getObject()) {
refToFix = fixedRef;
return true;
}
}
// not match in refObj (or no refObj!)
for (auto& objectName : m_3dObjectCache) {
auto object3d = getDimension()->getDocument()->getObject(objectName.c_str());
ReferenceEntry fixedRef = searchObjForVert(object3d, refGeom);
if (fixedRef.getObject()) {
refToFix = fixedRef;
return true;
}
}
return false;
}
//! search the model for a 3d edge that is the same as the saved reference geometry
//! and return a reference pointing to the matching edge.
bool DimensionAutoCorrect::findExactEdge3d(ReferenceEntry& refToFix, const Part::TopoShape& refGeom) const
{
// Base::Console().message("DAC::findExactEdge3d() - cache: %d\n", m_3dObjectCache.size());
// first, try to find a match in the referenced object
auto refObj = refToFix.getObject();
if (refObj) {
ReferenceEntry fixedRef = searchObjForEdge(refObj, refGeom);
if (fixedRef.getObject()) {
refToFix = fixedRef;
return true;
}
}
// no match in refObj, so now search the cached objects
for (auto& objectName : m_3dObjectCache) {
auto object3d = getDimension()->getDocument()->getObject(objectName.c_str());
auto shape3d = Part::Feature::getShape(object3d, Part::ShapeOption::ResolveLink | Part::ShapeOption::Transform);
auto edgesAll = getDimension()->getEdges(shape3d);
size_t iEdge {1};
for (auto& edge : edgesAll) {
if (getMatcher()->compareGeometry(edge, refGeom)) {
// found a match!
refToFix.setObjectName(objectName);
refToFix.setObject(object3d);
refToFix.setSubName(std::string("Edge") + std::to_string(iEdge));
return true;
}
iEdge++;
}
}
return false;
}
//! search the view for a vertex that is within a tolerance of the saved reference geometry
//! and return a reference pointing to the matching vertex
bool DimensionAutoCorrect::findSimilarVertex2d(ReferenceEntry& refToFix,
const Part::TopoShape& refGeom) const
{
// Base::Console().message("DAC::findSimilarVertex2d()\n");
(void)refToFix;
(void)refGeom;
// Base::Console().message("DAC::findSimilarVertex2d is not implemented yet\n");
return false;
}
//! search the view for a 2d edge that is similar to the saved reference geometry
//! and return a reference pointing to the similar edge.
bool DimensionAutoCorrect::findSimilarEdge2d(ReferenceEntry& refToFix,
const Part::TopoShape& refGeom) const
{
// Base::Console().message("DAC::findSimilarEdge2d()\n");
(void)refToFix;
(void)refGeom;
// Base::Console().message("DAC::findSimilarEdge2d is not implemented yet\n");
return false;
}
//! search the referenced 3d object and the object cache for a vertex that is within
//! a tolerance of the saved reference geometry and return a reference pointing
//! to the matching vertex
bool DimensionAutoCorrect::findSimilarVertex3d(ReferenceEntry& refToFix,
const Part::TopoShape& refGeom) const
{
// Base::Console().message("DAC::findSimilarVertex3d()\n");
(void)refToFix;
(void)refGeom;
// Base::Console().message("DAC::findSimilarVertex3d is not implemented yet\n");
return false;
}
//! search the referenced 3d object and the object cache for an edge that is
//! similar to the saved reference geometry and return a reference pointing
//! to the similar edge
bool DimensionAutoCorrect::findSimilarEdge3d(ReferenceEntry& refToFix,
const Part::TopoShape& refGeom) const
{
// Base::Console().message("DAC::findSimilarEdge3d(%s)\n", refToFix.getObjectName().c_str());
(void)refToFix;
(void)refGeom;
// Base::Console().message("DAC::findSimilarEdge3d is not implemented yet\n");
return false;
}
//! compare the geometry pointed to by a reference to the corresponding saved geometry
bool DimensionAutoCorrect::isMatchingGeometry(const ReferenceEntry& ref,
const Part::TopoShape& savedGeometry) const
{
Part::TopoShape temp;
if (ref.is3d()) {
auto shape3d = ShapeFinder::getLocatedShape(*ref.getObject(), ref.getSubName(true));
temp = Part::TopoShape(shape3d);
} else {
auto shape2d = ref.getGeometry();
temp = Part::TopoShape(shape2d);
}
if (temp.isNull()) {
// this shouldn't happen as we already know that this ref points to valid geometry
return false;
}
if (getMatcher()->compareGeometry(temp, savedGeometry)) {
// reference still points to the same geometry
return true;
}
return false;
}
//! search obj (3d object with a shape) for a match to refVertex. This is always
//! an exact match for phase 1 (GeometryMatcher), but in phase 2 (GeometryGuesser)
//! a similar match will be allowed.
ReferenceEntry DimensionAutoCorrect::searchObjForVert(App::DocumentObject* obj,
const Part::TopoShape& refVertex,
bool exact) const
{
(void)exact;
auto shape3d = ShapeFinder::getLocatedShape(*obj, "");
if (shape3d.IsNull()) {
// how to handle this?
return {};
}
auto vertsAll = getDimension()->getVertexes(shape3d);
size_t iVert {1};
for (auto& vert : vertsAll) {
bool isSame = getMatcher()->compareGeometry(refVertex, vert);
if (isSame) {
auto newSubname = std::string("Vertex") + std::to_string(iVert);
return {obj, newSubname, getDimension()->getDocument()};
}
iVert++;
}
return {};
}
//! search View (2d part display) for a match to refVertex. This can be an
//! exact or Similar match depending on the setting of exact.
ReferenceEntry DimensionAutoCorrect::searchViewForVert(DrawViewPart* obj,
const Part::TopoShape& refVertex,
bool exact) const
{
// Base::Console().message("DAC::searchViewForVert()\n");
(void)exact;
std::vector<TechDraw::VertexPtr> gVertexAll =
getDimension()->getViewPart()->getVertexGeometry();
getMatcher()->setPointTolerance(EWTOLERANCE);
int iVertex = 0;
for (auto& vert : gVertexAll) {
// vert is in display form - scaled and rotated
Part::TopoShape temp = ReferenceEntry::asCanonicalTopoShape(vert->asTopoShape(), *obj);
bool isSame = getMatcher()->compareGeometry(temp, refVertex);
if (isSame) {
auto newSubname = std::string("Vertex") + std::to_string(iVertex);
return {obj, newSubname, getDimension()->getDocument()};
}
iVertex++;
}
return {};
}
//! search View (2d part display) for an exact match to refEdge.
ReferenceEntry DimensionAutoCorrect::searchViewForExactEdge(DrawViewPart* obj,
const Part::TopoShape& refEdge) const
{
// Base::Console().message("DAC::searchViewForExactEdge()\n");
auto gEdgeAll = getDimension()->getViewPart()->getEdgeGeometry();
int iEdge {0};
for (auto& edge : gEdgeAll) {
// edge is scaled and rotated. we need it in the same scale/rotate state as
// the reference edge in order to match.
Part::TopoShape temp = ReferenceEntry::asCanonicalTopoShape(edge->asTopoShape(), *obj);
bool isSame = getMatcher()->compareGeometry(refEdge, temp);
if (isSame) {
auto newSubname = std::string("Edge") + std::to_string(iEdge);
return {obj, newSubname, getDimension()->getDocument()};
}
iEdge++;
}
return {};
}
//! search View (2d part display) for an edge that is similar to refEdge
ReferenceEntry DimensionAutoCorrect::searchViewForSimilarEdge(DrawViewPart* obj,
const Part::TopoShape& refEdge) const
{
// Base::Console().message("DAC::searchViewForSimilarEdge()\n");
(void)obj;
(void)refEdge;
Base::Console().message("DAC::searchViewForSimilarEdge is not implemented yet\n");
return {};
}
//! search model for for a 3d edge that is a match to refEdge
//! note that only the exact match is implemented in phase 1
ReferenceEntry DimensionAutoCorrect::searchObjForEdge(App::DocumentObject* obj,
const Part::TopoShape& refEdge,
bool exact) const
{
// Base::Console().message("DAC::searchObjForEdge(%s)\n", obj->Label.getValue());
(void)exact;
auto shape3d = Part::Feature::getShape(obj, Part::ShapeOption::ResolveLink | Part::ShapeOption::Transform);
if (shape3d.IsNull()) {
// how to handle this?
return {};
}
auto edgesAll = getDimension()->getEdges(shape3d);
size_t iEdge {1};
for (auto& edge : edgesAll) {
bool isSame = getMatcher()->compareGeometry(edge, refEdge);
if (isSame) {
auto newSubname = std::string("Edge") + std::to_string(iEdge);
return {obj, newSubname, getDimension()->getDocument()};
}
iEdge++;
}
return {};
}
//! rebuild 3d references from saved geometry. returns true is all references
//! have been repaired
bool DimensionAutoCorrect::fixBrokenReferences(ReferenceVector& fixedReferences) const
{
// Base::Console().message("DAC::fixBrokenReferences()\n");
bool success {true};
const std::vector<Part::TopoShape> savedGeometry = getDimension()->SavedGeometry.getValues();
int iGeom {0};
for (auto& geom : savedGeometry) {
if (fixedReferences.at(iGeom).hasGeometry()) {
iGeom++;
continue;
}
//
const TopoDS_Shape& geomShape = geom.getShape();
for (auto& objectName : m_3dObjectCache) {
auto object3d = getDimension()->getDocument()->getObject(objectName.c_str());
if (!object3d) {
// cached object has been deleted
continue;
}
// TODO: do we need to check for Similar matches here too?
ReferenceEntry newRef;
if (geomShape.ShapeType() == TopAbs_VERTEX) {
newRef = searchObjForVert(object3d, geomShape);
fixedReferences.at(iGeom) = newRef;
}
else {
newRef = searchObjForEdge(object3d, geomShape);
fixedReferences.at(iGeom) = newRef;
}
fixedReferences.at(iGeom) = newRef;
if (!newRef.getObject()) {
success = false;
}
}
}
return success;
}
GeometryMatcher* DimensionAutoCorrect::getMatcher() const
{
return getDimension()->getMatcher();
}
|