File size: 40,004 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 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 | /***************************************************************************
* Copyright (c) 2019 WandererFan <wandererfan@gmail.com> *
* Copyright (c) 2022 Benjamin Bræstrup Sayoc <benj5378@outlook.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 *
* *
***************************************************************************/
#include <BRepBuilderAPI_MakeEdge.hxx>
#include <BRepBndLib.hxx>
#include <Bnd_Box.hxx>
#include <TopoDS_Shape.hxx>
#include <BRepTools.hxx>
#include <Base/Console.h>
#include <Base/Converter.h>
#include <Base/Tools.h>
#include "CenterLine.h"
#include "DrawUtil.h"
#include "DrawViewPart.h"
#include "Geometry.h"
#include "CenterLinePy.h"
#include "ShapeUtils.h"
using namespace TechDraw;
using DU = DrawUtil;
TYPESYSTEM_SOURCE(TechDraw::CenterLine, Base::Persistence)
CenterLine::CenterLine()
{
m_start = Base::Vector3d(0.0, 0.0, 0.0);
m_end = Base::Vector3d(0.0, 0.0, 0.0);
m_mode = Mode::VERTICAL;
m_hShift = 0.0;
m_vShift = 0.0;
m_rotate = 0.0;
m_extendBy = 0.0;
m_type = Type::FACE;
m_flip2Line = false;
m_geometry = std::make_shared<TechDraw::BaseGeom> ();
initialize();
}
CenterLine::CenterLine(const TechDraw::CenterLine* cl)
{
m_start = cl->m_start;
m_end = cl->m_end;
m_mode = cl->m_mode;
m_hShift = cl->m_hShift;
m_vShift = cl->m_vShift;
m_rotate = cl->m_rotate;
m_extendBy = cl->m_extendBy;
m_type = cl->m_type;
m_flip2Line = cl->m_flip2Line;
m_geometry = cl->m_geometry; //new BaseGeom(cl->m_geometry);??
initialize();
}
CenterLine::CenterLine(const TechDraw::BaseGeomPtr& bg,
const Mode m,
const double h,
const double v,
const double r,
const double x)
{
m_start = bg->getStartPoint();
m_end = bg->getEndPoint();
m_mode = m;
m_hShift = h;
m_vShift = v;
m_rotate = r;
m_extendBy = x;
m_type = Type::FACE;
m_flip2Line = false;
m_geometry = bg;
initialize();
}
CenterLine::CenterLine(const Base::Vector3d& pt1,
const Base::Vector3d& pt2,
const Mode m,
const double h,
const double v,
const double r,
const double x)
{
m_start = pt1;
m_end = pt2;
m_mode = m;
m_hShift = h;
m_vShift = v;
m_rotate = r;
m_extendBy = x;
m_type = Type::FACE;
m_flip2Line = false;
m_geometry = BaseGeomPtrFromVectors(pt1, pt2);
initialize();
}
CenterLine::~CenterLine()
{
}
void CenterLine::initialize()
{
m_geometry->setClassOfEdge(EdgeClass::HARD);
m_geometry->setHlrVisible( true);
m_geometry->setCosmetic(true);
m_geometry->source(SourceType::CENTERLINE);
m_geometry->setCosmeticTag(getTagAsString());
}
TechDraw::BaseGeomPtr CenterLine::BaseGeomPtrFromVectors(Base::Vector3d pt1, Base::Vector3d pt2)
{
// Base::Console().message("CE::CE(p1, p2)\n");
Base::Vector3d p1 = DrawUtil::invertY(pt1);
Base::Vector3d p2 = DrawUtil::invertY(pt2);
gp_Pnt gp1(p1.x, p1.y, p1.z);
gp_Pnt gp2(p2.x, p2.y, p2.z);
TopoDS_Edge e = BRepBuilderAPI_MakeEdge(gp1, gp2);
TechDraw::BaseGeomPtr bg = TechDraw::BaseGeom::baseFactory(e);
return bg;
}
CenterLine* CenterLine::CenterLineBuilder(const DrawViewPart* partFeat,
const std::vector<std::string>& subNames,
const Mode mode,
const bool flip)
{
// Base::Console().message("CL::CLBuilder()\n - subNames: %d\n", subNames.size());
std::pair<Base::Vector3d, Base::Vector3d> ends;
std::vector<std::string> faces;
std::vector<std::string> edges;
std::vector<std::string> verts;
std::string geomType = TechDraw::DrawUtil::getGeomTypeFromName(subNames.front());
Type type = Type::FACE;
if (geomType == "Face") {
type = Type::FACE;
ends = TechDraw::CenterLine::calcEndPoints(partFeat,
subNames,
mode,
0.0,
0.0, 0.0, 0.0);
faces = subNames;
} else if (geomType == "Edge") {
type = Type::EDGE;
ends = TechDraw::CenterLine::calcEndPoints2Lines(partFeat,
subNames,
mode,
0.0,
0.0, 0.0, 0.0, flip);
edges = subNames;
} else if (geomType == "Vertex") {
type = Type::VERTEX;
ends = TechDraw::CenterLine::calcEndPoints2Points(partFeat,
subNames,
mode,
0.0,
0.0, 0.0, 0.0, flip);
verts = subNames;
}
if ((ends.first).IsEqual(ends.second, Precision::Confusion())) {
Base::Console().warning("CenterLineBuilder - endpoints are equal: %s\n",
DrawUtil::formatVector(ends.first).c_str());
Base::Console().warning("CenterLineBuilder - check V/H/A and/or Flip parameters\n");
return nullptr;
}
auto * cl = new TechDraw::CenterLine(ends.first, ends.second);
cl->m_type = type;
cl->m_mode = mode;
cl->m_faces = faces;
cl->m_edges = edges;
cl->m_verts = verts;
cl->m_flip2Line = flip;
return cl;
}
TechDraw::BaseGeomPtr CenterLine::scaledGeometry(const TechDraw::DrawViewPart* partFeat)
{
// Base::Console().message("CL::scaledGeometry() - m_type: %d\n", m_type);
double scale = partFeat->getScale();
double viewAngleDeg = partFeat->Rotation.getValue();
std::pair<Base::Vector3d, Base::Vector3d> ends;
try {
if (m_faces.empty() &&
m_edges.empty() &&
m_verts.empty() ) {
// Base::Console().message("CL::scaledGeometry - no geometry to scale!\n");
//CenterLine was created by points without a geometry reference,
ends = calcEndPointsNoRef(m_start, m_end, scale, m_extendBy,
m_hShift, m_vShift, m_rotate, viewAngleDeg);
} else if (m_type == Type::FACE) {
ends = calcEndPoints(partFeat,
m_faces,
m_mode, m_extendBy,
m_hShift, m_vShift, m_rotate);
} else if (m_type == Type::EDGE) {
ends = calcEndPoints2Lines(partFeat,
m_edges,
m_mode,
m_extendBy,
m_hShift, m_vShift, m_rotate, m_flip2Line);
} else if (m_type == Type::VERTEX) {
ends = calcEndPoints2Points(partFeat,
m_verts,
m_mode,
m_extendBy,
m_hShift, m_vShift, m_rotate, m_flip2Line);
}
}
catch (...) {
Base::Console().error("CL::scaledGeometry - failed to calculate endpoints!\n");
return nullptr;
}
Base::Vector3d p1 = ends.first;
Base::Vector3d p2 = ends.second;
if (p1.IsEqual(p2, 0.00001)) {
Base::Console().warning("Centerline endpoints are equal. Could not draw.\n");
//what to do here? //return current geom?
return m_geometry;
}
gp_Pnt gp1(p1.x, p1.y, p1.z);
gp_Pnt gp2(p2.x, p2.y, p2.z);
TopoDS_Edge e = BRepBuilderAPI_MakeEdge(gp1, gp2);
TopoDS_Shape s = ShapeUtils::scaleShape(e, scale);
TopoDS_Edge newEdge = TopoDS::Edge(s);
TechDraw::BaseGeomPtr newGeom = TechDraw::BaseGeom::baseFactory(newEdge);
newGeom->setClassOfEdge(EdgeClass::HARD);
newGeom->setHlrVisible( true);
newGeom->setCosmetic(true);
newGeom->source(SourceType::CENTERLINE);
newGeom->setCosmeticTag(getTagAsString());
return newGeom;
}
TechDraw::BaseGeomPtr CenterLine::scaledAndRotatedGeometry(TechDraw::DrawViewPart* partFeat)
{
// Base::Console().message("CL::scaledGeometry() - m_type: %d\n", m_type);
double scale = partFeat->getScale();
double viewAngleDeg = partFeat->Rotation.getValue();
std::pair<Base::Vector3d, Base::Vector3d> ends;
try {
if (m_faces.empty() &&
m_edges.empty() &&
m_verts.empty() ) {
// Base::Console().message("CL::scaledGeometry - no geometry to scale!\n");
//CenterLine was created by points without a geometry reference,
ends = calcEndPointsNoRef(m_start, m_end, scale, m_extendBy,
m_hShift, m_vShift, m_rotate, viewAngleDeg);
} else if (m_type == Type::FACE) {
ends = calcEndPoints(partFeat,
m_faces,
m_mode, m_extendBy,
m_hShift, m_vShift, m_rotate);
} else if (m_type == Type::EDGE) {
ends = calcEndPoints2Lines(partFeat,
m_edges,
m_mode,
m_extendBy,
m_hShift, m_vShift, m_rotate, m_flip2Line);
} else if (m_type == Type::VERTEX) {
ends = calcEndPoints2Points(partFeat,
m_verts,
m_mode,
m_extendBy,
m_hShift, m_vShift, m_rotate, m_flip2Line);
}
}
catch (...) {
Base::Console().error("CL::scaledGeometry - failed to calculate endpoints!\n");
return nullptr;
}
// inversion here breaks face cl.
Base::Vector3d p1 = ends.first;
Base::Vector3d p2 = ends.second;
if (p1.IsEqual(p2, 0.00001)) {
Base::Console().warning("Centerline endpoints are equal. Could not draw.\n");
//what to do here? //return current geom?
return m_geometry;
}
TopoDS_Edge newEdge;
if (getType() == Type::FACE ) {
gp_Pnt gp1(Base::convertTo<gp_Pnt>(p1));
gp_Pnt gp2(Base::convertTo<gp_Pnt>(p2));
TopoDS_Edge e = BRepBuilderAPI_MakeEdge(gp1, gp2);
// Mirror shape in Y and scale
TopoDS_Shape s = ShapeUtils::mirrorShape(e, gp_Pnt(0.0, 0.0, 0.0), scale);
// rotate using OXYZ as the coordinate system
s = ShapeUtils::rotateShape(s, gp_Ax2(), - partFeat->Rotation.getValue());
newEdge = TopoDS::Edge(s);
} else if (getType() == Type::EDGE ||
getType() == Type::VERTEX) {
gp_Pnt gp1(Base::convertTo<gp_Pnt>(DU::invertY(p1 * scale)));
gp_Pnt gp2(Base::convertTo<gp_Pnt>(DU::invertY(p2 * scale)));
newEdge = BRepBuilderAPI_MakeEdge(gp1, gp2);
}
TechDraw::BaseGeomPtr newGeom = TechDraw::BaseGeom::baseFactory(newEdge);
if (!newGeom) {
throw Base::RuntimeError("Failed to create center line");
}
newGeom->setClassOfEdge(EdgeClass::HARD);
newGeom->setHlrVisible( true);
newGeom->setCosmetic(true);
newGeom->source(SourceType::CENTERLINE);
newGeom->setCosmeticTag(getTagAsString());
return newGeom;
}
std::string CenterLine::toString() const
{
std::stringstream ss;
ss << m_start.x << ", " <<
m_start.y << ", " <<
m_start.z << ", " <<
m_end.x << ", " <<
m_end.y << ", " <<
m_end.z << ", " <<
m_mode << ", " <<
m_type << ", " <<
m_hShift << ", " <<
m_vShift << ", " <<
m_rotate << ", " <<
m_flip2Line << ", " <<
m_extendBy << ", " <<
m_faces.size();
if (!m_faces.empty()) {
for (auto& f: m_faces) {
if (!f.empty()) {
ss << ", " << f ;
}
}
}
std::string clCSV = ss.str();
std::string fmtCSV = m_format.toString();
return clCSV + ", $$$, " + fmtCSV;
}
void CenterLine::dump(const char* title)
{
Base::Console().message("CL::dump - %s \n", title);
Base::Console().message("CL::dump - %s \n", toString().c_str());
}
//! rotate a notional 2d vector from p1 to p2 around its midpoint by angleDeg
std::pair<Base::Vector3d, Base::Vector3d> CenterLine::rotatePointsAroundMid(const Base::Vector3d& p1,
const Base::Vector3d& p2,
const Base::Vector3d& mid,
const double angleDeg)
{
std::pair<Base::Vector3d, Base::Vector3d> result;
double angleRad = Base::toRadians(angleDeg);
result.first.x = ((p1.x - mid.x) * cos(angleRad)) - ((p1.y - mid.y) * sin(angleRad)) + mid.x;
result.first.y = ((p1.x - mid.x) * sin(angleRad)) + ((p1.y - mid.y) * cos(angleRad)) + mid.y;
result.first.z = 0.0;
result.second.x = ((p2.x - mid.x) * cos(angleRad)) - ((p2.y - mid.y) * sin(angleRad)) + mid.x;
result.second.y = ((p2.x - mid.x) * sin(angleRad)) + ((p2.y - mid.y) * cos(angleRad)) + mid.y;
result.second.z = 0.0;
return result;
}
//end points for centerline with no geometry reference
std::pair<Base::Vector3d, Base::Vector3d> CenterLine::calcEndPointsNoRef(const Base::Vector3d& start,
const Base::Vector3d& end,
const double scale,
const double ext,
const double hShift,
const double vShift,
const double rotate, const double viewAngleDeg)
{
// Base::Console().message("CL::calcEndPointsNoRef()\n");
Base::Vector3d p1 = start;
Base::Vector3d p2 = end;
Base::Vector3d mid = (p1 + p2) / 2.0;
//extend
Base::Vector3d clDir = p2 - p1;
clDir.Normalize();
p1 = p1 - (clDir * ext);
p2 = p2 + (clDir * ext);
//rotate
if (!DrawUtil::fpCompare(rotate, 0.0)) {
//rotate p1, p2 about mid point
std::tie(p1, p2) = rotatePointsAroundMid(p1, p2, mid, rotate);
}
//shift
if (!DrawUtil::fpCompare(hShift, 0.0)) {
double hss = hShift * scale;
p1.x = p1.x + hss;
p2.x = p2.x + hss;
}
if (!DrawUtil::fpCompare(vShift, 0.0)) {
double vss = vShift * scale;
p1.y = p1.y + vss;
p2.y = p2.y + vss;
}
// rotate the endpoints so that when the View's Rotation is applied, the
// centerline is aligned correctly
std::pair<Base::Vector3d, Base::Vector3d> result;
result.first = p1 / scale;
result.second = p2 / scale;
Base::Vector3d midpoint = (result.first + result.second) / 2.0;
result = rotatePointsAroundMid(result.first, result.second, midpoint, viewAngleDeg * -1.0);
return result;
}
//end points for face centerline
std::pair<Base::Vector3d, Base::Vector3d> CenterLine::calcEndPoints(const DrawViewPart* partFeat,
const std::vector<std::string>& faceNames,
const CenterLine::Mode mode,
const double ext,
const double hShift,
const double vShift,
const double rotate)
{
// Base::Console().message("CL::calcEndPoints()\n");
if (faceNames.empty()) {
Base::Console().warning("CL::calcEndPoints - no faces!\n");
return std::pair<Base::Vector3d, Base::Vector3d>();
}
Bnd_Box faceBox;
faceBox.SetGap(0.0);
double scale = partFeat->getScale();
std::vector<TopoDS_Edge> faceEdgesAll;
for (auto& fn: faceNames) {
if (TechDraw::DrawUtil::getGeomTypeFromName(fn) != "Face") {
continue;
}
int idx = TechDraw::DrawUtil::getIndexFromName(fn);
std::vector<TechDraw::BaseGeomPtr> faceGeoms =
partFeat->getFaceEdgesByIndex(idx);
if (!faceGeoms.empty()) {
for (auto& fe: faceGeoms) {
if (!fe->getCosmetic()) {
faceEdgesAll.push_back(fe->getOCCEdge());
}
}
}
}
TopoDS_Shape faceEdgeCompound = DU::vectorToCompound(faceEdgesAll);
if (partFeat->Rotation.getValue() != 0.0) {
// unrotate the input shape to align with the cardinal axes so we can use bbox to
// get size measurements
faceEdgeCompound = ShapeUtils::rotateShape(faceEdgeCompound,
partFeat->getProjectionCS(),
partFeat->Rotation.getValue() * -1.0);
}
// get the center of the unrotated, scaled face
Base::Vector3d faceCenter = ShapeUtils::findCentroidVec(faceEdgeCompound, partFeat->getProjectionCS());
// we need to move the edges to the origin here to get the right limits from the bounding box
faceEdgeCompound = ShapeUtils::moveShape(faceEdgeCompound, faceCenter * -1.0);
// get the bounding box of the centered and unrotated face
BRepBndLib::AddOptimal(faceEdgeCompound, faceBox);
if (faceBox.IsVoid()) {
Base::Console().error("CL::calcEndPoints - faceBox is void!\n");
throw Base::IndexError("CenterLine wrong number of faces.");
}
double Xmin, Ymin, Zmin, Xmax, Ymax, Zmax;
faceBox.Get(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
double Xspan = fabs(Xmax - Xmin);
Xspan = (Xspan / 2.0);
double Xmid = 0.0;
Xmax = Xmid + Xspan;
Xmin = Xmid - Xspan;
double Yspan = fabs(Ymax - Ymin);
Yspan = (Yspan / 2.0);
double Ymid = 0.0;
Ymax = Ymid + Yspan;
Ymin = Ymid - Yspan;
Base::Vector3d p1, p2;
if (mode == Mode::VERTICAL) { //vertical
p1 = Base::Vector3d(Xmid, Ymax, 0.0);
p2 = Base::Vector3d(Xmid, Ymin, 0.0);
} else if (mode == Mode::HORIZONTAL) { //horizontal
p1 = Base::Vector3d(Xmin, Ymid, 0.0);
p2 = Base::Vector3d(Xmax, Ymid, 0.0);
} else { //vert == Mode::ALIGNED //aligned, but aligned doesn't make sense for face(s) bbox
Base::Console().message("CL::calcEndPoints - aligned is not applicable to Face center lines\n");
p1 = Base::Vector3d(Xmid, Ymax, 0.0);
p2 = Base::Vector3d(Xmid, Ymin, 0.0);
}
// now move the extents back to the face center. this should give us the scaled,
// unrotated ends of the cl (but in 3d coordinates, which will be handled at the time
// the cl is added to the view)
p1 += faceCenter;
p2 += faceCenter;
Base::Vector3d mid = (p1 + p2) / 2.0;
//extend
Base::Vector3d clDir = p2 - p1;
clDir.Normalize();
p1 = p1 - (clDir * ext);
p2 = p2 + (clDir * ext);
//rotate
if (!DrawUtil::fpCompare(rotate, 0.0)) {
//rotate p1, p2 about mid point
std::tie(p1, p2) = rotatePointsAroundMid(p1, p2, mid, rotate);
}
//shift
if (!DrawUtil::fpCompare(hShift, 0.0)) {
double hss = hShift * scale;
p1.x = p1.x + hss;
p2.x = p2.x + hss;
}
if (!DrawUtil::fpCompare(vShift, 0.0)) {
double vss = vShift * scale;
p1.y = p1.y + vss;
p2.y = p2.y + vss;
}
std::pair<Base::Vector3d, Base::Vector3d> result;
result.first = p1 / scale;
result.second = p2 / scale;
return result;
}
std::pair<Base::Vector3d, Base::Vector3d> CenterLine::calcEndPoints2Lines(const DrawViewPart* partFeat,
const std::vector<std::string>& edgeNames,
const Mode mode,
const double ext,
const double hShift,
const double vShift,
const double rotate,
const bool flip)
{
Q_UNUSED(flip)
// Base::Console().message("CL::calc2Lines() - mode: %d flip: %d edgeNames: %d\n", mode, flip, edgeNames.size());
std::pair<Base::Vector3d, Base::Vector3d> result;
if (edgeNames.empty()) {
Base::Console().warning("CL::calcEndPoints2Lines - no edges!\n");
return result;
}
double scale = partFeat->getScale();
std::vector<TechDraw::BaseGeomPtr> edges;
for (auto& en: edgeNames) {
if (TechDraw::DrawUtil::getGeomTypeFromName(en) != "Edge") {
continue;
}
int idx = TechDraw::DrawUtil::getIndexFromName(en);
TechDraw::BaseGeomPtr bg = partFeat->getGeomByIndex(idx);
if (bg) {
edges.push_back(bg);
} else {
Base::Console().message("CL::calcEndPoints2Lines - no geom for index: %d\n", idx);
}
}
if (edges.size() != 2) {
Base::Console().message("CL::calcEndPoints2Lines - wrong number of edges: %d!\n", edges.size());
throw Base::IndexError("CenterLine wrong number of edges.");
}
// these points are centered, rotated, scaled and inverted.
// invert the points so the math works correctly
Base::Vector3d l1p1 = DU::invertY(edges.front()->getStartPoint());
Base::Vector3d l1p2 = DU::invertY(edges.front()->getEndPoint());
Base::Vector3d l2p1 = DU::invertY(edges.back()->getStartPoint());
Base::Vector3d l2p2 = DU::invertY(edges.back()->getEndPoint());
// The centerline is drawn using the midpoints of the two lines that connect l1p1-l2p1 and l1p2-l2p2.
// However, we don't know which point should be l1p1 to get a geometrically correct result, see
// https://wiki.freecad.org/File:TD-CenterLineFlip.png for an illustration of the problem.
// Thus we test this by a circulation test, see this post for a brief explanation:
// https://forum.freecad.org/viewtopic.php?p=505733#p505615
if (DrawUtil::circulation(l1p1, l1p2, l2p1) != DrawUtil::circulation(l1p2, l2p2, l2p1)) {
Base::Vector3d temp; // reverse line 1
temp = l1p1;
l1p1 = l1p2;
l1p2 = temp;
}
Base::Vector3d p1 = (l1p1 + l2p1) / 2.0;
Base::Vector3d p2 = (l1p2 + l2p2) / 2.0;
Base::Vector3d mid = (p1 + p2) / 2.0;
// if the proposed end points prevent the creation of a vertical or horizontal centerline, we need
// to prevent the "orientation" code below from creating identical endpoints. This would create a
// zero length edge and cause problems later.
bool inhibitVertical = false;
bool inhibitHorizontal = false;
if (DU::fpCompare(p1.y, p2.y, EWTOLERANCE)) {
// proposed end points are aligned vertically, so we can't draw a vertical line to connect them
inhibitVertical = true;
}
if (DU::fpCompare(p1.x, p2.x, EWTOLERANCE)) {
// proposed end points are aligned horizontally, so we can't draw a horizontal line to connect them
inhibitHorizontal = true;
}
//orientation
if (partFeat->Rotation.getValue() == 0.0) {
// if the view is rotated, then horizontal and vertical lose their meaning
if (mode == CenterLine::Mode::VERTICAL && !inhibitVertical) {
p1.x = mid.x;
p2.x = mid.x;
} else if (mode == CenterLine::Mode::HORIZONTAL && !inhibitHorizontal) {
p1.y = mid.y;
p2.y = mid.y;
} else if (mode == CenterLine::Mode::ALIGNED) {
// no op
}
}
//extend
Base::Vector3d clDir = p2 - p1;
clDir.Normalize();
p1 = p1 - (clDir * ext);
p2 = p2 + (clDir * ext);
//rotate
if (!DrawUtil::fpCompare(rotate, 0.0)) {
//rotate p1, p2 about mid
std::tie(p1, p2) = rotatePointsAroundMid(p1, p2, mid, rotate);
}
//shift
if (!DrawUtil::fpCompare(hShift, 0.0)) {
double hss = hShift * scale;
p1.x = p1.x + hss;
p2.x = p2.x + hss;
}
if (!DrawUtil::fpCompare(vShift, 0.0)) {
double vss = vShift * scale;
p1.y = p1.y + vss;
p2.y = p2.y + vss;
}
// the cl will be scaled when drawn, so unscale now.
result.first = p1 / scale;
result.second = p2 / scale;
return result;
}
std::pair<Base::Vector3d, Base::Vector3d> CenterLine::calcEndPoints2Points(const DrawViewPart* partFeat,
const std::vector<std::string>& vertNames,
const Mode mode,
const double ext,
const double hShift,
const double vShift,
const double rotate,
const bool flip)
{
// Base::Console().message("CL::calc2Points() - mode: %d\n", mode);
if (vertNames.empty()) {
Base::Console().warning("CL::calcEndPoints2Points - no points!\n");
return std::pair<Base::Vector3d, Base::Vector3d>();
}
double scale = partFeat->getScale();
std::vector<TechDraw::VertexPtr> points;
for (auto& vn: vertNames) {
if (TechDraw::DrawUtil::getGeomTypeFromName(vn) != "Vertex") {
continue;
}
int idx = TechDraw::DrawUtil::getIndexFromName(vn);
TechDraw::VertexPtr v = partFeat->getProjVertexByIndex(idx);
if (v) {
points.push_back(v);
}
}
if (points.size() != 2) {
throw Base::IndexError("CenterLine wrong number of points.");
}
Base::Vector3d v1 = DU::invertY(points.front()->point());
Base::Vector3d v2 = DU::invertY(points.back()->point());
Base::Vector3d mid = (v1 + v2) / 2.0;
Base::Vector3d dir = v2 - v1;
// if the proposed end points prevent the creation of a vertical or horizontal centerline, we need
// to prevent the "orientation" code below from creating identical endpoints. This would create a
// zero length edge and cause problems later.
bool inhibitVertical = false;
bool inhibitHorizontal = false;
if (DU::fpCompare(v1.y, v2.y, EWTOLERANCE)) {
// proposed end points are aligned horizontally, can not draw horizontal CL
inhibitHorizontal = true;
}
if (DU::fpCompare(v1.x, v2.x, EWTOLERANCE)) {
// proposed end points are aligned vertically, can not draw vertical CL
inhibitVertical = true;
}
//orientation
if (partFeat->Rotation.getValue() == 0.0) {
// if the view is rotated, then horizontal and vertical lose their meaning
if (mode == Mode::VERTICAL && !inhibitVertical) {
//Vertical
v1.x = mid.x;
v2.x = mid.x;
} else if (mode == Mode::HORIZONTAL && !inhibitHorizontal) {
//Horizontal
v1.y = mid.y;
v2.y = mid.y;
} else if (mode == Mode::ALIGNED) { //Aligned
// no op
}
}
double length = dir.Length();
dir.Normalize();
Base::Vector3d clDir(dir.y, -dir.x, dir.z);
Base::Vector3d p1 = mid + clDir * (length / 2.0);
Base::Vector3d p2 = mid - clDir * (length / 2.0);
if (flip) { //is flip relevant to 2 point???
Base::Vector3d temp = p1;
p1 = p2;
p2 = temp;
}
//extend
p1 = p1 + (clDir * ext);
p2 = p2 - (clDir * ext);
//rotate
if (!DrawUtil::fpCompare(rotate, 0.0)) {
//rotate p1, p2 about mid
std::tie(p1, p2) = rotatePointsAroundMid(p1, p2, mid, rotate);
}
//shift
if (!DrawUtil::fpCompare(hShift, 0.0)) {
double hss = hShift * scale;
p1.x = p1.x + hss;
p2.x = p2.x + hss;
}
if (!DrawUtil::fpCompare(vShift, 0.0)) {
double vss = vShift * scale;
p1.y = p1.y + vss;
p2.y = p2.y + vss;
}
std::pair<Base::Vector3d, Base::Vector3d> result;
result.first = p1 / scale;
result.second = p2 / scale;
return result;
}
// Persistence implementers
unsigned int CenterLine::getMemSize () const
{
return 1;
}
void CenterLine::Save(Base::Writer &writer) const
{
writer.Stream() << writer.ind() << "<Start "
<< "X=\"" << m_start.x <<
"\" Y=\"" << m_start.y <<
"\" Z=\"" << m_start.z <<
"\"/>" << std::endl;
writer.Stream() << writer.ind() << "<End "
<< "X=\"" << m_end.x <<
"\" Y=\"" << m_end.y <<
"\" Z=\"" << m_end.z <<
"\"/>" << std::endl;
writer.Stream() << writer.ind() << "<Mode value=\"" << m_mode <<"\"/>" << std::endl;
writer.Stream() << writer.ind() << "<HShift value=\"" << m_hShift <<"\"/>" << std::endl;
writer.Stream() << writer.ind() << "<VShift value=\"" << m_vShift <<"\"/>" << std::endl;
writer.Stream() << writer.ind() << "<Rotate value=\"" << m_rotate <<"\"/>" << std::endl;
writer.Stream() << writer.ind() << "<Extend value=\"" << m_extendBy <<"\"/>" << std::endl;
writer.Stream() << writer.ind() << "<Type value=\"" << m_type <<"\"/>" << std::endl;
writer.Stream() << writer.ind() << "<Flip value=\"" << m_flip2Line <<"\"/>" << std::endl;
writer.Stream()
<< writer.ind()
<< "<Faces "
<< "FaceCount=\"" << m_faces.size() <<
"\">" << std::endl;
writer.incInd();
for (auto& f: m_faces) {
writer.Stream()
<< writer.ind()
<< "<Face value=\"" << f <<"\"/>" << std::endl;
}
writer.decInd();
writer.Stream() << writer.ind() << "</Faces>" << std::endl;
writer.Stream()
<< writer.ind()
<< "<Edges "
<< "EdgeCount=\"" << m_edges.size() <<
"\">" << std::endl;
writer.incInd();
for (auto& e: m_edges) {
writer.Stream()
<< writer.ind()
<< "<Edge value=\"" << e <<"\"/>" << std::endl;
}
writer.decInd();
writer.Stream() << writer.ind() << "</Edges>" << std::endl;
writer.Stream()
<< writer.ind()
<< "<CLPoints "
<< "CLPointCount=\"" << m_verts.size() <<
"\">" << std::endl;
writer.incInd();
for (auto& p: m_verts) {
writer.Stream()
<< writer.ind()
<< "<CLPoint value=\"" << p <<"\"/>" << std::endl;
}
writer.decInd();
writer.Stream() << writer.ind() << "</CLPoints>" << std::endl ;
// style is deprecated in favour of line number, but we still save and restore it
// to avoid problems with old documents.
writer.Stream() << writer.ind() << "<Style value=\"" << m_format.getStyle() << "\"/>" << std::endl;
writer.Stream() << writer.ind() << "<Weight value=\"" << m_format.getWidth() << "\"/>" << std::endl;
writer.Stream() << writer.ind() << "<Color value=\"" << m_format.getColor().asHexString() << "\"/>" << std::endl;
const char v = m_format.getVisible() ? '1' : '0';
writer.Stream() << writer.ind() << "<Visible value=\"" << v << "\"/>" << std::endl;
//stored geometry
if (!m_geometry) {
return Base::Console().error("CL::Save - m_geometry is null\n");
}
writer.Stream() << writer.ind() << "<GeometryType value=\"" << m_geometry->getGeomType() <<"\"/>" << std::endl;
if (m_geometry->getGeomType() == GeomType::GENERIC) {
GenericPtr gen = std::static_pointer_cast<Generic>(m_geometry);
gen->Save(writer);
} else if (m_geometry->getGeomType() == GeomType::CIRCLE) {
TechDraw::CirclePtr circ = std::static_pointer_cast<TechDraw::Circle>(m_geometry);
circ->Save(writer);
} else if (m_geometry->getGeomType() == GeomType::ARCOFCIRCLE) {
TechDraw::AOCPtr aoc = std::static_pointer_cast<TechDraw::AOC>(m_geometry);
aoc->Save(writer);
} else {
Base::Console().message("CL::Save - unimplemented geomType: %d\n", static_cast<int>(m_geometry->getGeomType()));
}
writer.Stream() << writer.ind() << "<LineNumber value=\"" << m_format.getLineNumber() << "\"/>" << std::endl;
}
void CenterLine::Restore(Base::XMLReader &reader)
{
if (!CosmeticVertex::restoreCosmetic()) {
return;
}
// Base::Console().message("CL::Restore - reading elements\n");
// read my Element
reader.readElement("Start");
// get the value of my Attribute
m_start.x = reader.getAttribute<double>("X");
m_start.y = reader.getAttribute<double>("Y");
m_start.z = reader.getAttribute<double>("Z");
reader.readElement("End");
m_end.x = reader.getAttribute<double>("X");
m_end.y = reader.getAttribute<double>("Y");
m_end.z = reader.getAttribute<double>("Z");
reader.readElement("Mode");
m_mode = reader.getAttribute<Mode>("value");
reader.readElement("HShift");
m_hShift = reader.getAttribute<double>("value");
reader.readElement("VShift");
m_vShift = reader.getAttribute<double>("value");
reader.readElement("Rotate");
m_rotate = reader.getAttribute<double>("value");
reader.readElement("Extend");
m_extendBy = reader.getAttribute<double>("value");
reader.readElement("Type");
m_type = reader.getAttribute<Type>("value");
reader.readElement("Flip");
m_flip2Line = reader.getAttribute<bool>("value") == 0;
reader.readElement("Faces");
int count = reader.getAttribute<long>("FaceCount");
int i = 0;
for ( ; i < count; i++) {
reader.readElement("Face");
std::string f = reader.getAttribute<const char*>("value");
m_faces.push_back(f);
}
reader.readEndElement("Faces");
reader.readElement("Edges");
count = reader.getAttribute<long>("EdgeCount");
i = 0;
for ( ; i < count; i++) {
reader.readElement("Edge");
std::string e = reader.getAttribute<const char*>("value");
m_edges.push_back(e);
}
reader.readEndElement("Edges");
reader.readElement("CLPoints");
count = reader.getAttribute<long>("CLPointCount");
i = 0;
for ( ; i < count; i++) {
reader.readElement("CLPoint");
std::string p = reader.getAttribute<const char*>("value");
m_verts.push_back(p);
}
reader.readEndElement("CLPoints");
// style is deprecated in favour of line number, but we still save and restore it
// to avoid problems with old documents.
reader.readElement("Style");
m_format.setStyle(reader.getAttribute<long>("value"));
reader.readElement("Weight");
m_format.setWidth(reader.getAttribute<double>("value"));
reader.readElement("Color");
std::string tempHex = reader.getAttribute<const char*>("value");
Base::Color tempColor;
tempColor.fromHexString(tempHex);
m_format.setColor(tempColor);
reader.readElement("Visible");
m_format.setVisible(reader.getAttribute<bool>("value"));
//stored geometry
reader.readElement("GeometryType");
GeomType gType = reader.getAttribute<GeomType>("value");
if (gType == GeomType::GENERIC) {
TechDraw::GenericPtr gen = std::make_shared<TechDraw::Generic> ();
gen->Restore(reader);
gen->setOCCEdge(GeometryUtils::edgeFromGeneric(gen));
m_geometry = gen;
} else if (gType == GeomType::CIRCLE) {
TechDraw::CirclePtr circ = std::make_shared<TechDraw::Circle> ();
circ->Restore(reader);
circ->setOCCEdge(GeometryUtils::edgeFromCircle(circ));
m_geometry = circ;
} else if (gType == GeomType::ARCOFCIRCLE) {
TechDraw::AOCPtr aoc = std::make_shared<TechDraw::AOC> ();
aoc->Restore(reader);
aoc->setOCCEdge(GeometryUtils::edgeFromCircleArc(aoc));
m_geometry = aoc;
} else {
Base::Console().warning("CL::Restore - unimplemented geomType: %d\n", static_cast<int>(gType));
}
// older documents may not have the LineNumber element, so we need to check the
// next entry. if it is a start element, then we check if it is a start element
// for LineNumber.
// test for ISOLineNumber can be removed after testing. It is a left over for the earlier
// ISO only line handling.
if (reader.readNextElement()) {
if(strcmp(reader.localName(),"LineNumber") == 0 ||
strcmp(reader.localName(),"ISOLineNumber") == 0 ) {
// this centerline has an LineNumber attribute
m_format.setLineNumber(reader.getAttribute<long>("value"));
} else {
// LineNumber not found.
m_format.setLineNumber(LineFormat::InvalidLine);
}
}
}
CenterLine* CenterLine::copy() const
{
CenterLine* newCL = new CenterLine();
newCL->m_start = m_start;
newCL->m_end = m_end;
newCL->m_mode = m_mode;
newCL->m_hShift = m_hShift;
newCL->m_vShift = m_vShift;
newCL->m_rotate = m_rotate;
newCL->m_extendBy = m_extendBy;
newCL->m_type = m_type;
newCL->m_flip2Line = m_flip2Line;
newCL->m_faces = m_faces;
newCL->m_edges = m_edges;
newCL->m_verts = m_verts;
TechDraw::BaseGeomPtr newGeom = m_geometry->copy();
newCL->m_geometry = newGeom;
newCL->m_format = m_format;
return newCL;
}
CenterLine *CenterLine::clone() const
{
CenterLine* cpy = this->copy();
cpy->setTag(this->getTag());
return cpy;
}
// To do: make const
PyObject* CenterLine::getPyObject()
{
if (PythonObject.is(Py::_None())) {
// ref counter is set to 1
PythonObject = Py::Object(new CenterLinePy(this), true);
}
return Py::new_reference_to(PythonObject);
}
void CenterLine::setShifts(const double h, const double v)
{
m_hShift = h;
m_vShift = v;
}
double CenterLine::getHShift() const
{
return m_hShift;
}
double CenterLine::getVShift() const
{
return m_vShift;
}
void CenterLine::setRotate(const double r)
{
m_rotate = r;
}
double CenterLine::getRotate() const
{
return m_rotate;
}
void CenterLine::setExtend(const double e)
{
m_extendBy = e;
}
double CenterLine::getExtend() const
{
return m_extendBy;
}
void CenterLine::setFlip(const bool f)
{
m_flip2Line = f;
}
bool CenterLine::getFlip() const
{
return m_flip2Line;
}
|