From 97b67884647dba51fff3160f35a946fb10890102 Mon Sep 17 00:00:00 2001 From: wandererfan Date: Sat, 7 Jan 2023 07:17:15 -0500 Subject: [PATCH] [TD]fix wrong calculation of 3d two edge angle --- src/Mod/TechDraw/App/DimensionGeometry.cpp | 75 +-- src/Mod/TechDraw/App/DrawUtil.cpp | 662 ++++++++++-------- src/Mod/TechDraw/App/DrawUtil.h | 304 +++++---- src/Mod/TechDraw/App/DrawViewDimension.cpp | 743 +++++++++++---------- src/Mod/TechDraw/App/DrawViewPart.cpp | 40 +- src/Mod/TechDraw/App/DrawViewPart.h | 12 +- 6 files changed, 987 insertions(+), 849 deletions(-) diff --git a/src/Mod/TechDraw/App/DimensionGeometry.cpp b/src/Mod/TechDraw/App/DimensionGeometry.cpp index eab1c154ed..32361dd1ad 100644 --- a/src/Mod/TechDraw/App/DimensionGeometry.cpp +++ b/src/Mod/TechDraw/App/DimensionGeometry.cpp @@ -30,8 +30,8 @@ #include -#include "DrawViewPart.h" #include "DrawUtil.h" +#include "DrawViewPart.h" #include "DimensionGeometry.h" @@ -44,12 +44,14 @@ pointPair::pointPair(const pointPair& pp) second(pp.second()); } +//move the points by offset void pointPair::move(Base::Vector3d offset) { m_first = m_first - offset; m_second = m_second - offset; } +// project the points onto the dvp's paper plane. Points are still in R3 coords. void pointPair::project(DrawViewPart* dvp) { Base::Vector3d normal = DrawUtil::toVector3d(dvp->getProjectionCS().Direction()); @@ -58,11 +60,12 @@ void pointPair::project(DrawViewPart* dvp) m_second = m_second.ProjectToPlane(stdOrigin, normal) * dvp->getScale(); } +// map the points onto the dvp's XY coordinate system void pointPair::mapToPage(DrawViewPart* dvp) { gp_Trsf xOXYZ; gp_Ax3 OXYZ; - xOXYZ.SetTransformation(OXYZ, gp_Ax3(dvp->getProjectionCS())); + xOXYZ.SetTransformation(OXYZ, gp_Ax3(dvp->getRotatedCS())); gp_Vec gvFirst = DU::togp_Vec(m_first).Transformed(xOXYZ); m_first = DU::toVector3d(gvFirst); @@ -80,8 +83,7 @@ void pointPair::dump(std::string text) const { Base::Console().Message("pointPair - %s\n", text.c_str()); Base::Console().Message("pointPair - first: %s second: %s\n", - DU::formatVector(first()).c_str(), - DU::formatVector(second()).c_str()); + DU::formatVector(first()).c_str(), DU::formatVector(second()).c_str()); } anglePoints::anglePoints() @@ -91,26 +93,23 @@ anglePoints::anglePoints() m_vertex = Base::Vector3d(0.0, 0.0, 0.0); } -anglePoints::anglePoints(const anglePoints& ap) - : m_ends(ap.ends()), - m_vertex(ap.vertex()) -{ +anglePoints::anglePoints(const anglePoints& ap) : m_ends(ap.ends()), m_vertex(ap.vertex()) {} -} - -anglePoints& anglePoints::operator= (const anglePoints& ap) +anglePoints& anglePoints::operator=(const anglePoints& ap) { m_ends = ap.ends(); m_vertex = ap.vertex(); return *this; } +// move the points by offset void anglePoints::move(Base::Vector3d offset) { m_ends.move(offset); m_vertex = m_vertex - offset; } +// project the points onto the dvp's paper plane. Points are still in R3 coords. void anglePoints::project(DrawViewPart* dvp) { Base::Vector3d normal = DrawUtil::toVector3d(dvp->getProjectionCS().Direction()); @@ -119,17 +118,19 @@ void anglePoints::project(DrawViewPart* dvp) m_vertex = m_vertex.ProjectToPlane(stdOrigin, normal) * dvp->getScale(); } +// map the points onto the dvp's XY coordinate system void anglePoints::mapToPage(DrawViewPart* dvp) { m_ends.mapToPage(dvp); - + gp_Trsf xOXYZ; gp_Ax3 OXYZ; - xOXYZ.SetTransformation(OXYZ, gp_Ax3(dvp->getProjectionCS())); + xOXYZ.SetTransformation(OXYZ, gp_Ax3(dvp->getRotatedCS())); gp_Vec gvVertex = DU::togp_Vec(m_vertex).Transformed(xOXYZ); m_vertex = DU::toVector3d(gvVertex); } +// map the points onto the coordinate system used for drawing where -Y direction is "up" void anglePoints::invertY() { m_ends.invertY(); @@ -140,38 +141,29 @@ void anglePoints::dump(std::string text) const { Base::Console().Message("anglePoints - %s\n", text.c_str()); Base::Console().Message("anglePoints - ends - first: %s second: %s\n", - DU::formatVector(first()).c_str(), - DU::formatVector(second()).c_str()); - Base::Console().Message("anglePoints - vertex: %s\n", - DU::formatVector(vertex()).c_str()); + DU::formatVector(first()).c_str(), DU::formatVector(second()).c_str()); + Base::Console().Message("anglePoints - vertex: %s\n", DU::formatVector(vertex()).c_str()); } arcPoints::arcPoints() { - isArc = false; - radius = 0.0; - center = Base::Vector3d(0.0, 0.0, 0.0); - onCurve.first(Base::Vector3d(0.0, 0.0, 0.0)); - onCurve.second(Base::Vector3d(0.0, 0.0, 0.0)); - arcEnds.first(Base::Vector3d(0.0, 0.0, 0.0)); - arcEnds.second(Base::Vector3d(0.0, 0.0, 0.0)); - midArc = Base::Vector3d(0.0, 0.0, 0.0); - arcCW = false; + isArc = false; + radius = 0.0; + center = Base::Vector3d(0.0, 0.0, 0.0); + onCurve.first(Base::Vector3d(0.0, 0.0, 0.0)); + onCurve.second(Base::Vector3d(0.0, 0.0, 0.0)); + arcEnds.first(Base::Vector3d(0.0, 0.0, 0.0)); + arcEnds.second(Base::Vector3d(0.0, 0.0, 0.0)); + midArc = Base::Vector3d(0.0, 0.0, 0.0); + arcCW = false; } arcPoints::arcPoints(const arcPoints& ap) - : isArc(ap.isArc) - , radius(ap.radius) - , center(ap.center) - , onCurve(ap.onCurve) - , arcEnds(ap.arcEnds) - , midArc(ap.midArc) - , arcCW(ap.arcCW) -{ + : isArc(ap.isArc), radius(ap.radius), center(ap.center), onCurve(ap.onCurve), + arcEnds(ap.arcEnds), midArc(ap.midArc), arcCW(ap.arcCW) +{} -} - -arcPoints& arcPoints::operator= (const arcPoints& ap) +arcPoints& arcPoints::operator=(const arcPoints& ap) { isArc = ap.isArc; radius = ap.radius; @@ -210,7 +202,7 @@ void arcPoints::mapToPage(DrawViewPart* dvp) { gp_Trsf xOXYZ; gp_Ax3 OXYZ; - xOXYZ.SetTransformation(OXYZ, gp_Ax3(dvp->getProjectionCS())); + xOXYZ.SetTransformation(OXYZ, gp_Ax3(dvp->getRotatedCS())); gp_Vec gvCenter = DU::togp_Vec(center).Transformed(xOXYZ); center = DU::toVector3d(gvCenter); @@ -237,7 +229,8 @@ void arcPoints::invertY() void arcPoints::dump(std::string text) const { Base::Console().Message("arcPoints - %s\n", text.c_str()); - Base::Console().Message("arcPoints - radius: %.3f center: %s\n", radius, DrawUtil::formatVector(center).c_str()); + Base::Console().Message("arcPoints - radius: %.3f center: %s\n", radius, + DrawUtil::formatVector(center).c_str()); Base::Console().Message("arcPoints - isArc: %d arcCW: %d\n", isArc, arcCW); Base::Console().Message("arcPoints - onCurve: %s %s\n", DrawUtil::formatVector(onCurve.first()).c_str(), @@ -245,7 +238,5 @@ void arcPoints::dump(std::string text) const Base::Console().Message("arcPoints - arcEnds: %s %s\n", DrawUtil::formatVector(arcEnds.first()).c_str(), DrawUtil::formatVector(arcEnds.second()).c_str()); - Base::Console().Message("arcPoints - midArc: %s\n", - DrawUtil::formatVector(midArc).c_str()); + Base::Console().Message("arcPoints - midArc: %s\n", DrawUtil::formatVector(midArc).c_str()); } - diff --git a/src/Mod/TechDraw/App/DrawUtil.cpp b/src/Mod/TechDraw/App/DrawUtil.cpp index 9c8950cf20..0123a82691 100644 --- a/src/Mod/TechDraw/App/DrawUtil.cpp +++ b/src/Mod/TechDraw/App/DrawUtil.cpp @@ -34,28 +34,28 @@ #include #include -#include -#include -#include #include #include +#include #include #include #include #include #include #include +#include +#include #include #include +#include +#include +#include +#include #include #include #include #include #include -#include -#include -#include -#include #endif #include @@ -75,15 +75,15 @@ using namespace TechDraw; /*static*/ int DrawUtil::getIndexFromName(std::string geomName) { -// Base::Console().Message("DU::getIndexFromName(%s)\n", geomName.c_str()); - boost::regex re("\\d+$"); // one of more digits at end of string + // Base::Console().Message("DU::getIndexFromName(%s)\n", geomName.c_str()); + boost::regex re("\\d+$");// one of more digits at end of string boost::match_results what; boost::match_flag_type flags = boost::match_default; -// char* endChar; + // char* endChar; std::string::const_iterator begin = geomName.begin(); auto pos = geomName.rfind('.'); - if(pos!=std::string::npos) - begin += pos+1; + if (pos != std::string::npos) + begin += pos + 1; std::string::const_iterator end = geomName.end(); std::stringstream ErrorMsg; @@ -94,8 +94,9 @@ using namespace TechDraw; if (boost::regex_search(begin, end, what, re, flags)) { - return int (std::stoi(what.str())); - } else { + return int(std::stoi(what.str())); + } + else { ErrorMsg << "getIndexFromName: malformed geometry name - " << geomName; throw Base::ValueError(ErrorMsg.str()); } @@ -103,13 +104,13 @@ using namespace TechDraw; std::string DrawUtil::getGeomTypeFromName(std::string geomName) { - boost::regex re("^[a-zA-Z]*"); //one or more letters at start of string + boost::regex re("^[a-zA-Z]*");//one or more letters at start of string boost::match_results what; boost::match_flag_type flags = boost::match_default; std::string::const_iterator begin = geomName.begin(); auto pos = geomName.rfind('.'); - if(pos!=std::string::npos) - begin += pos+1; + if (pos != std::string::npos) + begin += pos + 1; std::string::const_iterator end = geomName.end(); std::stringstream ErrorMsg; @@ -118,8 +119,9 @@ std::string DrawUtil::getGeomTypeFromName(std::string geomName) } if (boost::regex_search(begin, end, what, re, flags)) { - return what.str(); //TODO: use std::stoi() in c++11 - } else { + return what.str();//TODO: use std::stoi() in c++11 + } + else { ErrorMsg << "In getGeomTypeFromName: malformed geometry name - " << geomName; throw Base::ValueError(ErrorMsg.str()); } @@ -170,7 +172,8 @@ double DrawUtil::simpleMinDist(TopoDS_Shape s1, TopoDS_Shape s2) int count = extss.NbSolution(); if (count != 0) { return extss.Value(); - } else { + } + else { return -1; } } @@ -179,19 +182,20 @@ double DrawUtil::simpleMinDist(TopoDS_Shape s1, TopoDS_Shape s2) //! quick angle for straight edges double DrawUtil::angleWithX(TopoDS_Edge e, bool reverse) { - gp_Pnt gstart = BRep_Tool::Pnt(TopExp::FirstVertex(e)); + gp_Pnt gstart = BRep_Tool::Pnt(TopExp::FirstVertex(e)); Base::Vector3d start(gstart.X(), gstart.Y(), gstart.Z()); - gp_Pnt gend = BRep_Tool::Pnt(TopExp::LastVertex(e)); + gp_Pnt gend = BRep_Tool::Pnt(TopExp::LastVertex(e)); Base::Vector3d end(gend.X(), gend.Y(), gend.Z()); Base::Vector3d u; if (reverse) { u = start - end; - } else { + } + else { u = end - start; } double result = atan2(u.y, u.x); if (result < 0) { - result += 2.0 * M_PI; + result += 2.0 * M_PI; } return result; @@ -203,11 +207,13 @@ double DrawUtil::angleWithX(TopoDS_Edge e, TopoDS_Vertex v, double tolerance) double param = 0; BRepAdaptor_Curve adapt(e); - if (isFirstVert(e, v,tolerance)) { + if (isFirstVert(e, v, tolerance)) { param = adapt.FirstParameter(); - } else if (isLastVert(e, v,tolerance)) { + } + else if (isLastVert(e, v, tolerance)) { param = adapt.LastParameter(); - } else { + } + else { //TARFU Base::Console().Message("Error: DU::angleWithX - v is neither first nor last \n"); } @@ -216,8 +222,8 @@ double DrawUtil::angleWithX(TopoDS_Edge e, TopoDS_Vertex v, double tolerance) const Handle(Geom_Curve) c = adapt.Curve().Curve(); c->D1(param, paramPoint, derivative); double angle = atan2(derivative.Y(), derivative.X()); - if (angle < 0) { //map from [-PI:PI] to [0:2PI] - angle += 2.0 * M_PI; + if (angle < 0) {//map from [-PI:PI] to [0:2PI] + angle += 2.0 * M_PI; } return angle; } @@ -234,19 +240,23 @@ double DrawUtil::incidenceAngleAtVertex(TopoDS_Edge e, TopoDS_Vertex v, double t Base::Vector3d anglePoint = DrawUtil::vertex2Vector(v); Base::Vector3d offsetPoint, incidenceVec; int noTangents = 0; - if (isFirstVert(e,v,tolerance)) { + if (isFirstVert(e, v, tolerance)) { vertexParam = adapt.FirstParameter(); - BRepLProp_CLProps prop(adapt, vertexParam + paramOffset, noTangents, Precision::Confusion()); + BRepLProp_CLProps prop(adapt, vertexParam + paramOffset, noTangents, + Precision::Confusion()); const gp_Pnt& gOffsetPoint = prop.Value(); offsetPoint = Base::Vector3d(gOffsetPoint.X(), gOffsetPoint.Y(), gOffsetPoint.Z()); - } else if (isLastVert(e,v,tolerance)) { + } + else if (isLastVert(e, v, tolerance)) { vertexParam = adapt.LastParameter(); - BRepLProp_CLProps prop(adapt, vertexParam - paramOffset, noTangents, Precision::Confusion()); + BRepLProp_CLProps prop(adapt, vertexParam - paramOffset, noTangents, + Precision::Confusion()); const gp_Pnt& gOffsetPoint = prop.Value(); offsetPoint = Base::Vector3d(gOffsetPoint.X(), gOffsetPoint.Y(), gOffsetPoint.Z()); - } else { + } + else { //TARFU -// Base::Console().Message("DU::incidenceAngle - v is neither first nor last \n"); + // Base::Console().Message("DU::incidenceAngle - v is neither first nor last \n"); } incidenceVec = anglePoint - offsetPoint; incidenceAngle = atan2(incidenceVec.y, incidenceVec.x); @@ -286,10 +296,8 @@ bool DrawUtil::fpCompare(const double& d1, const double& d2, double tolerance) } //brute force intersection points of line(point, dir) with box(xRange, yRange) -std::pair DrawUtil::boxIntersect2d(Base::Vector3d point, - Base::Vector3d dirIn, - double xRange, - double yRange) +std::pair +DrawUtil::boxIntersect2d(Base::Vector3d point, Base::Vector3d dirIn, double xRange, double yRange) { std::pair result; Base::Vector3d p1, p2; @@ -297,39 +305,43 @@ std::pair DrawUtil::boxIntersect2d(Base::Vector3 dir.Normalize(); // y = mx + b // m = (y1 - y0) / (x1 - x0) - if (DrawUtil::fpCompare(dir.x, 0.0) ) { //vertical case + if (DrawUtil::fpCompare(dir.x, 0.0)) {//vertical case p1 = Base::Vector3d(point.x, point.y - (yRange / 2.0), 0.0); p2 = Base::Vector3d(point.x, point.y + (yRange / 2.0), 0.0); - } else { + } + else { double slope = dir.y / dir.x; double left = -xRange / 2.0; double right = xRange / 2.0; - if (DrawUtil::fpCompare(slope, 0.0)) { //horizontal case + if (DrawUtil::fpCompare(slope, 0.0)) {//horizontal case p1 = Base::Vector3d(point.x - (xRange / 2.0), point.y); p2 = Base::Vector3d(point.x + (xRange / 2.0), point.y); - } else { //normal case + } + else {//normal case double top = yRange / 2.0; double bottom = -yRange / 2.0; - double yLeft = point.y - slope * (point.x - left) ; - double yRight = point.y - slope * (point.x - right); - double xTop = point.x - ( (point.y - top) / slope ); - double xBottom = point.x - ( (point.y - bottom) / slope ); + double yLeft = point.y - slope * (point.x - left); + double yRight = point.y - slope * (point.x - right); + double xTop = point.x - ((point.y - top) / slope); + double xBottom = point.x - ((point.y - bottom) / slope); - if ( (bottom < yLeft) && - (top > yLeft) ) { + if ((bottom < yLeft) && (top > yLeft)) { p1 = Base::Vector3d(left, yLeft); - } else if (yLeft <= bottom) { + } + else if (yLeft <= bottom) { p1 = Base::Vector3d(xBottom, bottom); - } else if (yLeft >= top) { + } + else if (yLeft >= top) { p1 = Base::Vector3d(xTop, top); } - if ( (bottom < yRight) && - (top > yRight) ) { + if ((bottom < yRight) && (top > yRight)) { p2 = Base::Vector3d(right, yRight); - } else if (yRight <= bottom) { + } + else if (yRight <= bottom) { p2 = Base::Vector3d(xBottom, bottom); - } else if (yRight >= top) { + } + else if (yRight >= top) { p2 = Base::Vector3d(xTop, top); } } @@ -348,13 +360,13 @@ std::pair DrawUtil::boxIntersect2d(Base::Vector3 //find the apparent intersection of 2 3d curves. We are only interested in curves that are lines, so we will have either 0 or 1 //apparent intersection. The intersection is "apparent" because the curve's progenator is a trimmed curve (line segment) +//NOTE: these curves do not have a location, so the intersection point does not respect the +//placement of the edges which spawned the curves. Use the apparentIntersection(edge, edge) method instead. bool DrawUtil::apparentIntersection(const Handle(Geom_Curve) curve1, - const Handle(Geom_Curve) curve2, - Base::Vector3d& result) + const Handle(Geom_Curve) curve2, Base::Vector3d& result) { GeomAPI_ExtremaCurveCurve intersector(curve1, curve2); - if (intersector.NbExtrema() == 0 || - intersector.LowerDistance() > EWTOLERANCE ) { + if (intersector.NbExtrema() == 0 || intersector.LowerDistance() > EWTOLERANCE) { //no intersection return false; } @@ -365,9 +377,92 @@ bool DrawUtil::apparentIntersection(const Handle(Geom_Curve) curve1, return true; } +bool DrawUtil::apparentIntersection(TopoDS_Edge& edge0, TopoDS_Edge& edge1, gp_Pnt& intersect) +{ + gp_Pnt gStart0 = BRep_Tool::Pnt(TopExp::FirstVertex(edge0)); + gp_Pnt gEnd0 = BRep_Tool::Pnt(TopExp::LastVertex(edge0)); + gp_Pnt gStart1 = BRep_Tool::Pnt(TopExp::FirstVertex(edge1)); + gp_Pnt gEnd1 = BRep_Tool::Pnt(TopExp::LastVertex(edge1)); + + //intersection of 2 3d lines in point&direction form + //https://math.stackexchange.com/questions/270767/find-intersection-of-two-3d-lines + gp_Vec C(gStart0.XYZ()); + gp_Vec D(gStart1.XYZ()); + gp_Vec e(gEnd0.XYZ() - gStart0.XYZ());//direction of line0 + gp_Vec f(gEnd1.XYZ() - gStart1.XYZ());//direction of line1 + Base::Console().Message("DU::apparentInter - e: %s f: %s\n", formatVector(e).c_str(), + formatVector(f).c_str()); + + //check for cases the algorithm doesn't handle well + gp_Vec C1(gEnd0.XYZ()); + gp_Vec D1(gEnd1.XYZ()); + if (C.IsEqual(D, EWTOLERANCE, EWTOLERANCE) || C.IsEqual(D1, EWTOLERANCE, EWTOLERANCE)) { + intersect = gp_Pnt(C.XYZ()); + return true; + } + if (C1.IsEqual(D, EWTOLERANCE, EWTOLERANCE) || C1.IsEqual(D1, EWTOLERANCE, EWTOLERANCE)) { + intersect = gp_Pnt(C1.XYZ()); + return true; + } + + gp_Vec g(D - C);//betwen a point on each line + Base::Console().Message("DU::apparentInter - C: %s D: %s g: %s\n", formatVector(C).c_str(), + formatVector(D).c_str(), formatVector(g).c_str()); + + gp_Vec fxg = f.Crossed(g); + double h = fxg.Magnitude(); + gp_Vec fxe = f.Crossed(e); + double k = fxe.Magnitude(); + Base::Console().Message("DU::apparentInter - h: %.3f k: %.3f\n", h, k); + if (fpCompare(k, 0.0)) { + //no intersection + return false; + } + gp_Vec el = e * (h / k); + double pm = 1.0; + if (fpCompare(fxg.Dot(fxe), -1.0)) { + //opposite directions + pm = -1.0; + } + intersect = gp_Pnt(C.XYZ() + el.XYZ() * pm); + return true; +} + +//find the intersection of 2 lines (point0, dir0) and (point1, dir1) +//existence of an intersection is not checked +bool DrawUtil::intersect2Lines3d(Base::Vector3d point0, Base::Vector3d dir0, Base::Vector3d point1, + Base::Vector3d dir1, Base::Vector3d& intersect) +{ + Base::Vector3d g = point1 - point0; + Base::Vector3d fxg = dir1.Cross(g); + Base::Vector3d fxgn = fxg; + fxgn.Normalize(); + Base::Vector3d fxe = dir1.Cross(dir0); + Base::Vector3d fxen = fxe; + fxen.Normalize(); + Base::Vector3d dir0n = dir0; + dir0n.Normalize(); + Base::Vector3d dir1n = dir1; + dir1n.Normalize(); + if (fabs(dir0n.Dot(dir1n)) == 1.0) { + //parallel lines, no intersection + Base::Console().Message("DU::intersect2 - parallel lines, no intersection\n"); + return false; + } + + double scaler = fxg.Length() / fxe.Length(); + double direction = -1.0; + if (fxgn == fxen) { + direction = 1.0; + } + + intersect = point0 + dir0 * scaler * direction; + return true; +} + Base::Vector3d DrawUtil::vertex2Vector(const TopoDS_Vertex& v) { - gp_Pnt gp = BRep_Tool::Pnt(v); + gp_Pnt gp = BRep_Tool::Pnt(v); return Base::Vector3d(gp.X(), gp.Y(), gp.Z()); } @@ -375,55 +470,55 @@ Base::Vector3d DrawUtil::vertex2Vector(const TopoDS_Vertex& v) std::string DrawUtil::formatVector(const Base::Vector3d& v) { std::stringstream builder; - builder << std::fixed << std::setprecision(Base::UnitsApi::getDecimals()) ; - builder << " (" << v.x << ", " << v.y << ", " << v.z << ") "; + builder << std::fixed << std::setprecision(Base::UnitsApi::getDecimals()); + builder << " (" << v.x << ", " << v.y << ", " << v.z << ") "; return builder.str(); } std::string DrawUtil::formatVector(const gp_Dir& v) { std::stringstream builder; - builder << std::fixed << std::setprecision(Base::UnitsApi::getDecimals()) ; - builder << " (" << v.X() << ", " << v.Y() << ", " << v.Z() << ") "; + builder << std::fixed << std::setprecision(Base::UnitsApi::getDecimals()); + builder << " (" << v.X() << ", " << v.Y() << ", " << v.Z() << ") "; return builder.str(); } std::string DrawUtil::formatVector(const gp_Dir2d& v) { std::stringstream builder; - builder << std::fixed << std::setprecision(Base::UnitsApi::getDecimals()) ; - builder << " (" << v.X() << ", " << v.Y() << ") "; + builder << std::fixed << std::setprecision(Base::UnitsApi::getDecimals()); + builder << " (" << v.X() << ", " << v.Y() << ") "; return builder.str(); } std::string DrawUtil::formatVector(const gp_Vec& v) { std::stringstream builder; - builder << std::fixed << std::setprecision(Base::UnitsApi::getDecimals()) ; - builder << " (" << v.X() << ", " << v.Y() << ", " << v.Z() << ") "; + builder << std::fixed << std::setprecision(Base::UnitsApi::getDecimals()); + builder << " (" << v.X() << ", " << v.Y() << ", " << v.Z() << ") "; return builder.str(); } std::string DrawUtil::formatVector(const gp_Pnt& v) { std::stringstream builder; - builder << std::fixed << std::setprecision(Base::UnitsApi::getDecimals()) ; - builder << " (" << v.X() << ", " << v.Y() << ", " << v.Z() << ") "; + builder << std::fixed << std::setprecision(Base::UnitsApi::getDecimals()); + builder << " (" << v.X() << ", " << v.Y() << ", " << v.Z() << ") "; return builder.str(); } std::string DrawUtil::formatVector(const gp_Pnt2d& v) { std::stringstream builder; - builder << std::fixed << std::setprecision(Base::UnitsApi::getDecimals()) ; - builder << " (" << v.X() << ", " << v.Y() << ") "; + builder << std::fixed << std::setprecision(Base::UnitsApi::getDecimals()); + builder << " (" << v.X() << ", " << v.Y() << ") "; return builder.str(); } std::string DrawUtil::formatVector(const QPointF& v) { std::stringstream builder; - builder << std::fixed << std::setprecision(Base::UnitsApi::getDecimals()) ; - builder << " (" << v.x() << ", " << v.y() << ") "; + builder << std::fixed << std::setprecision(Base::UnitsApi::getDecimals()); + builder << " (" << v.x() << ", " << v.y() << ") "; return builder.str(); } @@ -431,12 +526,14 @@ std::string DrawUtil::formatVector(const QPointF& v) //! precision::Confusion() is too strict for vertex - vertex comparisons bool DrawUtil::vectorLess(const Base::Vector3d& v1, const Base::Vector3d& v2) { - if ((v1 - v2).Length() > EWTOLERANCE) { //ie v1 != v2 + if ((v1 - v2).Length() > EWTOLERANCE) {//ie v1 != v2 if (!DrawUtil::fpCompare(v1.x, v2.x, 2.0 * EWTOLERANCE)) { return (v1.x < v2.x); - } else if (!DrawUtil::fpCompare(v1.y, v2.y, 2.0 * EWTOLERANCE)) { + } + else if (!DrawUtil::fpCompare(v1.y, v2.y, 2.0 * EWTOLERANCE)) { return (v1.y < v2.y); - } else { + } + else { return (v1.z < v2.z); } } @@ -458,9 +555,9 @@ bool DrawUtil::vertexEqual(TopoDS_Vertex& v1, TopoDS_Vertex& v2) //! in sorts and containers bool DrawUtil::vectorEqual(Base::Vector3d& v1, Base::Vector3d& v2) { - bool less1 = vectorLess(v1, v2); - bool less2 = vectorLess(v2, v1); - return !less1 && !less2; + bool less1 = vectorLess(v1, v2); + bool less2 = vectorLess(v2, v1); + return !less1 && !less2; } //TODO: the next 2 could be templated @@ -493,7 +590,7 @@ std::vector DrawUtil::shapeToVector(TopoDS_Shape shapeIn) { std::vector vectorOut; TopExp_Explorer expl(shapeIn, TopAbs_EDGE); - for ( ; expl.More(); expl.Next()) { + for (; expl.More(); expl.Next()) { vectorOut.push_back(TopoDS::Edge(expl.Current())); } return vectorOut; @@ -506,7 +603,7 @@ Base::Vector3d DrawUtil::toR3(const gp_Ax2& fromSystem, const Base::Vector3d& fr gp_Trsf T; gp_Ax3 gRef; gp_Ax3 gFrom(fromSystem); - T.SetTransformation (gFrom, gRef); + T.SetTransformation(gFrom, gRef); gp_Pnt gToPoint = gFromPoint.Transformed(T); Base::Vector3d toPoint(gToPoint.X(), gToPoint.Y(), gToPoint.Z()); return toPoint; @@ -524,9 +621,7 @@ bool DrawUtil::checkParallel(const Base::Vector3d v1, Base::Vector3d v2, double } //! rotate vector by angle radians around axis through org -Base::Vector3d DrawUtil::vecRotate(Base::Vector3d vec, - double angle, - Base::Vector3d axis, +Base::Vector3d DrawUtil::vecRotate(Base::Vector3d vec, double angle, Base::Vector3d axis, Base::Vector3d org) { Base::Matrix4D xForm; @@ -539,25 +634,21 @@ gp_Vec DrawUtil::closestBasis(gp_Vec inVec) return gp_Vec(togp_Dir(closestBasis(toVector3d(inVec)))); } -Base::Vector3d DrawUtil::closestBasis(Base::Vector3d v) +Base::Vector3d DrawUtil::closestBasis(Base::Vector3d v) { Base::Vector3d result(0.0, -1, 0); - Base::Vector3d stdX(1.0, 0.0, 0.0); - Base::Vector3d stdY(0.0, 1.0, 0.0); - Base::Vector3d stdZ(0.0, 0.0, 1.0); - Base::Vector3d stdXr(-1.0, 0.0, 0.0); - Base::Vector3d stdYr(0.0, -1.0, 0.0); - Base::Vector3d stdZr(0.0, 0.0, -1.0); + Base::Vector3d stdX(1.0, 0.0, 0.0); + Base::Vector3d stdY(0.0, 1.0, 0.0); + Base::Vector3d stdZ(0.0, 0.0, 1.0); + Base::Vector3d stdXr(-1.0, 0.0, 0.0); + Base::Vector3d stdYr(0.0, -1.0, 0.0); + Base::Vector3d stdZr(0.0, 0.0, -1.0); //first check if already a basis - if (v.Dot(stdX) == 1.0 || - v.Dot(stdY) == 1.0 || - v.Dot(stdZ) == 1.0) { + if (v.Dot(stdX) == 1.0 || v.Dot(stdY) == 1.0 || v.Dot(stdZ) == 1.0) { return v; } - if (v.Dot(stdX) == -1.0 || - v.Dot(stdY) == -1.0 || - v.Dot(stdZ) == -1.0) { + if (v.Dot(stdX) == -1.0 || v.Dot(stdY) == -1.0 || v.Dot(stdZ) == -1.0) { return -v; } @@ -597,34 +688,30 @@ Base::Vector3d DrawUtil::closestBasis(Base::Vector3d v) //should not get to here return Base::Vector3d(1.0, 0.0, 0.0); - } -Base::Vector3d DrawUtil::closestBasis(Base::Vector3d vDir, gp_Ax2 coordSys) +Base::Vector3d DrawUtil::closestBasis(Base::Vector3d vDir, gp_Ax2 coordSys) { gp_Dir gDir(vDir.x, vDir.y, vDir.z); return closestBasis(gDir, coordSys); } -Base::Vector3d DrawUtil::closestBasis(gp_Dir gDir, gp_Ax2 coordSys) +Base::Vector3d DrawUtil::closestBasis(gp_Dir gDir, gp_Ax2 coordSys) { gp_Dir xCS = coordSys.XDirection(); gp_Dir yCS = coordSys.YDirection(); gp_Dir zCS = coordSys.Direction(); //first check if already a basis - if (gDir.Dot(xCS) == 1.0 || - gDir.Dot(yCS) == 1.0 || - gDir.Dot(zCS) == 1.0 ) { + if (gDir.Dot(xCS) == 1.0 || gDir.Dot(yCS) == 1.0 || gDir.Dot(zCS) == 1.0) { //gDir is parallel with a basis - return Base::Vector3d( gDir.X(), gDir.Y(), gDir.Z()) ; + return Base::Vector3d(gDir.X(), gDir.Y(), gDir.Z()); } - if (gDir.Dot(xCS.Reversed()) == 1.0 || - gDir.Dot(yCS.Reversed()) == 1.0 || - gDir.Dot(zCS.Reversed()) == 1.0 ) { + if (gDir.Dot(xCS.Reversed()) == 1.0 || gDir.Dot(yCS.Reversed()) == 1.0 + || gDir.Dot(zCS.Reversed()) == 1.0) { //gDir is anti-parallel with a basis - return Base::Vector3d( -gDir.X(), -gDir.Y(), -gDir.Z()); + return Base::Vector3d(-gDir.X(), -gDir.Y(), -gDir.Z()); } //not a basis. find smallest angle with a basis. @@ -646,7 +733,7 @@ Base::Vector3d DrawUtil::closestBasis(gp_Dir gDir, gp_Ax2 coordSys) } if (angleZ == angleMin) { - return Base::Vector3d(zCS.X(), zCS.Y(), zCS.Z()) ; + return Base::Vector3d(zCS.X(), zCS.Y(), zCS.Z()); } if (angleXr == angleMin) { @@ -667,12 +754,12 @@ Base::Vector3d DrawUtil::closestBasis(gp_Dir gDir, gp_Ax2 coordSys) double DrawUtil::getWidthInDirection(gp_Dir direction, TopoDS_Shape& shape) { - Base::Vector3d stdX(1.0, 0.0, 0.0); - Base::Vector3d stdY(0.0, 1.0, 0.0); - Base::Vector3d stdZ(0.0, 0.0, 1.0); - Base::Vector3d stdXr(-1.0, 0.0, 0.0); - Base::Vector3d stdYr(0.0, -1.0, 0.0); - Base::Vector3d stdZr(0.0, 0.0, -1.0); + Base::Vector3d stdX(1.0, 0.0, 0.0); + Base::Vector3d stdY(0.0, 1.0, 0.0); + Base::Vector3d stdZ(0.0, 0.0, 1.0); + Base::Vector3d stdXr(-1.0, 0.0, 0.0); + Base::Vector3d stdYr(0.0, -1.0, 0.0); + Base::Vector3d stdZr(0.0, 0.0, -1.0); Base::Vector3d vClosestBasis = closestBasis(toVector3d(direction)); Bnd_Box shapeBox; @@ -687,18 +774,15 @@ double DrawUtil::getWidthInDirection(gp_Dir direction, TopoDS_Shape& shape) } shapeBox.Get(xMin, yMin, zMin, xMax, yMax, zMax); - if (vClosestBasis.IsEqual(stdX, EWTOLERANCE) || - vClosestBasis.IsEqual(stdXr, EWTOLERANCE) ) { + if (vClosestBasis.IsEqual(stdX, EWTOLERANCE) || vClosestBasis.IsEqual(stdXr, EWTOLERANCE)) { return xMax - xMin; } - if (vClosestBasis.IsEqual(stdY, EWTOLERANCE) || - vClosestBasis.IsEqual(stdYr, EWTOLERANCE) ) { + if (vClosestBasis.IsEqual(stdY, EWTOLERANCE) || vClosestBasis.IsEqual(stdYr, EWTOLERANCE)) { return yMax - yMin; } - if (vClosestBasis.IsEqual(stdZ, EWTOLERANCE) || - vClosestBasis.IsEqual(stdZr, EWTOLERANCE) ) { + if (vClosestBasis.IsEqual(stdZ, EWTOLERANCE) || vClosestBasis.IsEqual(stdZr, EWTOLERANCE)) { return zMax - zMin; } @@ -714,19 +798,21 @@ double DrawUtil::sensibleScale(double working_scale) //which gives the largest scale for which the min_space requirements can be met, but we want a 'sensible' scale, rather than 0.28457239... //eg if working_scale = 0.115, then we want to use 0.1, similarly 7.65 -> 5, and 76.5 -> 50 - float exponent = std::floor(std::log10(working_scale)); //if working_scale = a * 10^b, what is b? - working_scale *= std::pow(10, -exponent); //now find what 'a' is. + float exponent = std::floor(std::log10(working_scale));//if working_scale = a * 10^b, what is b? + working_scale *= std::pow(10, -exponent); //now find what 'a' is. //int choices = 10; - float valid_scales[2][10] = - {{1.0, 1.25, 2.0, 2.5, 3.75, 5.0, 7.5, 10.0, 50.0, 100.0}, //equate to 1:10, 1:8, 1:5, 1:4, 3:8, 1:2, 3:4, 1:1 - // .1 .125 .375 .75 - {1.0, 1.5 , 2.0, 3.0, 4.0 , 5.0, 8.0, 10.0, 50.0, 100.0}}; //equate to 1:1, 3:2, 2:1, 3:1, 4:1, 5:1, 8:1, 10:1 - // 1.5:1 + float valid_scales[2][10] = {{1.0, 1.25, 2.0, 2.5, 3.75, 5.0, 7.5, 10.0, 50.0, + 100.0},//equate to 1:10, 1:8, 1:5, 1:4, 3:8, 1:2, 3:4, 1:1 + // .1 .125 .375 .75 + {1.0, 1.5, 2.0, 3.0, 4.0, 5.0, 8.0, 10.0, 50.0, + 100.0}};//equate to 1:1, 3:2, 2:1, 3:1, 4:1, 5:1, 8:1, 10:1 + // 1.5:1 //int i = choices - 1; int i = 9; - while (valid_scales[(exponent >= 0)][i] > working_scale) //choose closest value smaller than 'a' from list. - i -= 1; //choosing top list if exponent -ve, bottom list for +ve exponent + while (valid_scales[(exponent >= 0)][i] + > working_scale)//choose closest value smaller than 'a' from list. + i -= 1; //choosing top list if exponent -ve, bottom list for +ve exponent //now have the appropriate scale, reapply the *10^b return valid_scales[(exponent >= 0)][i] * pow(10, exponent); @@ -737,62 +823,63 @@ double DrawUtil::getDefaultLineWeight(std::string lineType) return TechDraw::LineGroup::getDefaultWidth(lineType); } -bool DrawUtil::isBetween(const Base::Vector3d pt, const Base::Vector3d end1, const Base::Vector3d end2) +bool DrawUtil::isBetween(const Base::Vector3d pt, const Base::Vector3d end1, + const Base::Vector3d end2) { double segLength = (end2 - end1).Length(); - double l1 = (pt - end1).Length(); - double l2 = (pt - end2).Length(); + double l1 = (pt - end1).Length(); + double l2 = (pt - end2).Length(); if (fpCompare(segLength, l1 + l2)) { return true; } return false; } -Base::Vector3d DrawUtil::Intersect2d(Base::Vector3d p1, Base::Vector3d d1, - Base::Vector3d p2, Base::Vector3d d2) +Base::Vector3d DrawUtil::Intersect2d(Base::Vector3d p1, Base::Vector3d d1, Base::Vector3d p2, + Base::Vector3d d2) { - Base::Vector3d p12(p1.x+d1.x, p1.y+d1.y, 0.0); + Base::Vector3d p12(p1.x + d1.x, p1.y + d1.y, 0.0); double A1 = d1.y; double B1 = -d1.x; - double C1 = A1*p1.x + B1*p1.y; + double C1 = A1 * p1.x + B1 * p1.y; - Base::Vector3d p22(p2.x+d2.x, p2.y+d2.y, 0.0); + Base::Vector3d p22(p2.x + d2.x, p2.y + d2.y, 0.0); double A2 = d2.y; double B2 = -d2.x; - double C2 = A2*p2.x + B2*p2.y; + double C2 = A2 * p2.x + B2 * p2.y; - double det = A1*B2 - A2*B1; + double det = A1 * B2 - A2 * B1; if (fpCompare(det, 0.0, Precision::Confusion())) { Base::Console().Message("Lines are parallel\n"); return Base::Vector3d(0.0, 0.0, 0.0); } - double x = (B2*C1 - B1*C2)/det; - double y = (A1*C2 - A2*C1)/det; + double x = (B2 * C1 - B1 * C2) / det; + double y = (A1 * C2 - A2 * C1) / det; return Base::Vector3d(x, y, 0.0); } -Base::Vector2d DrawUtil::Intersect2d(Base::Vector2d p1, Base::Vector2d d1, - Base::Vector2d p2, Base::Vector2d d2) +Base::Vector2d DrawUtil::Intersect2d(Base::Vector2d p1, Base::Vector2d d1, Base::Vector2d p2, + Base::Vector2d d2) { - Base::Vector2d p12(p1.x+d1.x, p1.y+d1.y); + Base::Vector2d p12(p1.x + d1.x, p1.y + d1.y); double A1 = d1.y; double B1 = -d1.x; - double C1 = A1*p1.x + B1*p1.y; + double C1 = A1 * p1.x + B1 * p1.y; - Base::Vector2d p22(p2.x+d2.x, p2.y+d2.y); + Base::Vector2d p22(p2.x + d2.x, p2.y + d2.y); double A2 = d2.y; double B2 = -d2.x; - double C2 = A2*p2.x + B2*p2.y; + double C2 = A2 * p2.x + B2 * p2.y; - double det = A1*B2 - A2*B1; + double det = A1 * B2 - A2 * B1; if (fpCompare(det, 0.0, Precision::Confusion())) { Base::Console().Message("Lines are parallel\n"); return Base::Vector2d(0.0, 0.0); } - double x = (B2*C1 - B1*C2)/det; - double y = (A1*C2 - A2*C1)/det; + double x = (B2 * C1 - B1 * C2) / det; + double y = (A1 * C2 - A2 * C1) / det; return Base::Vector2d(x, y); } @@ -813,27 +900,20 @@ TopoDS_Shape DrawUtil::shapeFromString(std::string s) return result; } -Base::Vector3d DrawUtil::invertY(Base::Vector3d v) -{ - return Base::Vector3d (v.x, -v.y, v.z); -} +Base::Vector3d DrawUtil::invertY(Base::Vector3d v) { return Base::Vector3d(v.x, -v.y, v.z); } -QPointF DrawUtil::invertY(QPointF v) -{ - return QPointF(v.x(), -v.y()); -} +QPointF DrawUtil::invertY(QPointF v) { return QPointF(v.x(), -v.y()); } //obs? was used in CSV prototype of Cosmetics std::vector DrawUtil::split(std::string csvLine) { -// Base::Console().Message("DU::split - csvLine: %s\n", csvLine.c_str()); - std::vector result; - std::stringstream lineStream(csvLine); - std::string cell; + // Base::Console().Message("DU::split - csvLine: %s\n", csvLine.c_str()); + std::vector result; + std::stringstream lineStream(csvLine); + std::string cell; - while(std::getline(lineStream, cell, ',')) - { + while (std::getline(lineStream, cell, ',')) { result.push_back(cell); } return result; @@ -842,7 +922,7 @@ std::vector DrawUtil::split(std::string csvLine) //obs? was used in CSV prototype of Cosmetics std::vector DrawUtil::tokenize(std::string csvLine, std::string delimiter) { -// Base::Console().Message("DU::tokenize - csvLine: %s delimit: %s\n", csvLine.c_str(), delimiter.c_str()); + // Base::Console().Message("DU::tokenize - csvLine: %s delimit: %s\n", csvLine.c_str(), delimiter.c_str()); std::string s(csvLine); size_t pos = 0; std::vector tokens; @@ -858,13 +938,13 @@ std::vector DrawUtil::tokenize(std::string csvLine, std::string del App::Color DrawUtil::pyTupleToColor(PyObject* pColor) { -// Base::Console().Message("DU::pyTupleToColor()\n"); + // Base::Console().Message("DU::pyTupleToColor()\n"); double red = 0.0, green = 0.0, blue = 0.0, alpha = 0.0; if (!PyTuple_Check(pColor)) { return App::Color(red, green, blue, alpha); } - int tSize = (int) PyTuple_Size(pColor); + int tSize = (int)PyTuple_Size(pColor); if (tSize > 2) { PyObject* pRed = PyTuple_GetItem(pColor, 0); red = PyFloat_AsDouble(pRed); @@ -882,7 +962,7 @@ App::Color DrawUtil::pyTupleToColor(PyObject* pColor) PyObject* DrawUtil::colorToPyTuple(App::Color color) { -// Base::Console().Message("DU::pyTupleToColor()\n"); + // Base::Console().Message("DU::pyTupleToColor()\n"); PyObject* pTuple = PyTuple_New(4); PyObject* pRed = PyFloat_FromDouble(color.r); PyObject* pGreen = PyFloat_FromDouble(color.g); @@ -898,15 +978,18 @@ PyObject* DrawUtil::colorToPyTuple(App::Color color) } //check for crazy edge. This is probably a geometry error of some sort. -bool DrawUtil::isCrazy(TopoDS_Edge e) +bool DrawUtil::isCrazy(TopoDS_Edge e) { if (e.IsNull()) { return true; } - Base::Reference hGrp = App::GetApplication().GetUserParameter().GetGroup("BaseApp")-> - GetGroup("Preferences")->GetGroup("Mod/TechDraw/debug"); + Base::Reference hGrp = App::GetApplication() + .GetUserParameter() + .GetGroup("BaseApp") + ->GetGroup("Preferences") + ->GetGroup("Mod/TechDraw/debug"); bool crazyOK = hGrp->GetBool("allowCrazyEdge", false); if (crazyOK) { return false; @@ -915,42 +998,42 @@ bool DrawUtil::isCrazy(TopoDS_Edge e) BRepAdaptor_Curve adapt(e); double edgeLength = GCPnts_AbscissaPoint::Length(adapt, Precision::Confusion()); - if (edgeLength < 0.00001) { //edge is scaled. this is 0.00001 mm on paper + if (edgeLength < 0.00001) {//edge is scaled. this is 0.00001 mm on paper Base::Console().Log("DU::isCrazy - edge crazy short: %.7f\n", edgeLength); return true; } - if (edgeLength > 9999.9) { //edge is scaled. this is 10 m on paper. can't be right? + if (edgeLength > 9999.9) {//edge is scaled. this is 10 m on paper. can't be right? Base::Console().Log("DU::isCrazy - edge crazy long: %.3f\n", edgeLength); return true; } double start = BRepLProp_CurveTool::FirstParameter(adapt); double end = BRepLProp_CurveTool::LastParameter(adapt); - BRepLProp_CLProps propStart(adapt, start, 0,Precision::Confusion()); + BRepLProp_CLProps propStart(adapt, start, 0, Precision::Confusion()); const gp_Pnt& vStart = propStart.Value(); - BRepLProp_CLProps propEnd(adapt, end, 0,Precision::Confusion()); + BRepLProp_CLProps propEnd(adapt, end, 0, Precision::Confusion()); const gp_Pnt& vEnd = propEnd.Value(); double distance = vStart.Distance(vEnd); double ratio = edgeLength / distance; - if (adapt.GetType() == GeomAbs_BSplineCurve && - distance > 0.001 && // not a closed loop - ratio > 9999.9) { // 10, 000x - return true; //this is crazy edge + if (adapt.GetType() == GeomAbs_BSplineCurve && distance > 0.001 &&// not a closed loop + ratio > 9999.9) { // 10, 000x + return true; //this is crazy edge } else if (adapt.GetType() == GeomAbs_Ellipse) { gp_Elips ellp = adapt.Ellipse(); double major = ellp.MajorRadius(); double minor = ellp.MinorRadius(); - if (minor < 0.001) { //too narrow + if (minor < 0.001) {//too narrow Base::Console().Log("DU::isCrazy - ellipse is crazy narrow: %.7f\n", minor); return true; - } else if (major > 9999.9) { //too big + } + else if (major > 9999.9) {//too big Base::Console().Log("DU::isCrazy - ellipse is crazy wide: %.3f\n", major); return true; } } -// Base::Console().Message("DU::isCrazy - returns: %d ratio: %.3f\n", false, ratio); + // Base::Console().Message("DU::isCrazy - returns: %d ratio: %.3f\n", false, ratio); return false; } @@ -964,7 +1047,7 @@ Base::Vector3d DrawUtil::getFaceCenter(TopoDS_Face f) double v1 = adapt.FirstVParameter(); double v2 = adapt.LastVParameter(); double mv = (v1 + v2) / 2.0; - BRepLProp_SLProps prop(adapt, mu, mv, 0,Precision::Confusion()); + BRepLProp_SLProps prop(adapt, mu, mv, 0, Precision::Confusion()); const gp_Pnt gv = prop.Value(); return Base::Vector3d(gv.X(), gv.Y(), gv.Z()); } @@ -982,8 +1065,7 @@ int DrawUtil::countSubShapes(TopoDS_Shape shape, TopAbs_ShapeEnum subShape) { int count = 0; TopExp_Explorer Ex(shape, subShape); - while (Ex.More()) - { + while (Ex.More()) { count++; Ex.Next(); } @@ -997,8 +1079,8 @@ void DrawUtil::encodeXmlSpecialChars(std::string& inoutText) { std::string buffer; buffer.reserve(inoutText.size()); - for(size_t cursor = 0; cursor != inoutText.size(); ++cursor) { - switch(inoutText.at(cursor)) { + for (size_t cursor = 0; cursor != inoutText.size(); ++cursor) { + switch (inoutText.at(cursor)) { case '&': buffer.append("&"); break; @@ -1015,7 +1097,7 @@ void DrawUtil::encodeXmlSpecialChars(std::string& inoutText) buffer.append(">"); break; default: - buffer.append(&inoutText.at(cursor), 1); //not a special character + buffer.append(&inoutText.at(cursor), 1);//not a special character break; } } @@ -1032,11 +1114,11 @@ void DrawUtil::encodeXmlSpecialChars(std::string& inoutText) std::list DrawUtil::sort_Edges(double tol3d, std::list& edges) { tol3d = tol3d * tol3d; - std::list edge_points; + std::list edge_points; TopExp_Explorer xp; for (std::list::iterator it = edges.begin(); it != edges.end(); ++it) { EdgePoints ep; - xp.Init(*it,TopAbs_VERTEX); + xp.Init(*it, TopAbs_VERTEX); ep.v1 = BRep_Tool::Pnt(TopoDS::Vertex(xp.Current())); xp.Next(); ep.v2 = BRep_Tool::Pnt(TopoDS::Vertex(xp.Current())); @@ -1051,7 +1133,7 @@ std::list DrawUtil::sort_Edges(double tol3d, std::list std::list sorted; gp_Pnt gpChainFirst, gpChainLast; gpChainFirst = edge_points.front().v1; - gpChainLast = edge_points.front().v2; + gpChainLast = edge_points.front().v2; sorted.push_back(edge_points.front().edge); edges.erase(edge_points.front().it); @@ -1083,10 +1165,12 @@ std::list DrawUtil::sort_Edges(double tol3d, std::list //found a connection from end of chain to end of edge gpChainLast = itEdgePoint->v1; Standard_Real firstParam, lastParam; - const Handle(Geom_Curve) & curve = BRep_Tool::Curve(itEdgePoint->edge, firstParam, lastParam); + const Handle(Geom_Curve)& curve = + BRep_Tool::Curve(itEdgePoint->edge, firstParam, lastParam); firstParam = curve->ReversedParameter(firstParam); lastParam = curve->ReversedParameter(lastParam); - TopoDS_Edge edgeReversed = BRepBuilderAPI_MakeEdge(curve->Reversed(), firstParam, lastParam); + TopoDS_Edge edgeReversed = + BRepBuilderAPI_MakeEdge(curve->Reversed(), firstParam, lastParam); sorted.push_back(edgeReversed); edges.erase(itEdgePoint->it); edge_points.erase(itEdgePoint); @@ -1097,10 +1181,12 @@ std::list DrawUtil::sort_Edges(double tol3d, std::list //found a connection from start of chain to start of edge gpChainFirst = itEdgePoint->v2; Standard_Real firstParam, lastParam; - const Handle(Geom_Curve) & curve = BRep_Tool::Curve(itEdgePoint->edge, firstParam, lastParam); + const Handle(Geom_Curve)& curve = + BRep_Tool::Curve(itEdgePoint->edge, firstParam, lastParam); firstParam = curve->ReversedParameter(firstParam); lastParam = curve->ReversedParameter(lastParam); - TopoDS_Edge edgeReversed = BRepBuilderAPI_MakeEdge(curve->Reversed(), firstParam, lastParam); + TopoDS_Edge edgeReversed = + BRepBuilderAPI_MakeEdge(curve->Reversed(), firstParam, lastParam); sorted.push_front(edgeReversed); edges.erase(itEdgePoint->it); edge_points.erase(itEdgePoint); @@ -1109,7 +1195,8 @@ std::list DrawUtil::sort_Edges(double tol3d, std::list } } - if ((itEdgePoint == edge_points.end()) || (gpChainLast.SquareDistance(gpChainFirst) <= tol3d)) { + if ((itEdgePoint == edge_points.end()) + || (gpChainLast.SquareDistance(gpChainFirst) <= tol3d)) { // no adjacent edge found or polyline is closed return sorted; } @@ -1126,15 +1213,12 @@ int DrawUtil::sgn(double x) return (x > +Precision::Confusion()) - (x < -Precision::Confusion()); } -double DrawUtil::sqr(double x) -{ - return x*x; -} +double DrawUtil::sqr(double x) { return x * x; } -void DrawUtil::angleNormalize(double &fi) +void DrawUtil::angleNormalize(double& fi) { while (fi <= -M_PI) { - fi += M_2PI; + fi += M_2PI; } while (fi > M_PI) { fi -= M_2PI; @@ -1166,8 +1250,8 @@ double DrawUtil::angleDifference(double fi1, double fi2, bool reflex) // Interval marking functions // ========================== -unsigned int DrawUtil::intervalMerge(std::vector> &marking, - double boundary, bool wraps) +unsigned int DrawUtil::intervalMerge(std::vector>& marking, double boundary, + bool wraps) { // We will be returning the placement index instead of an iterator, because indices // are still valid after we insert on higher positions, while iterators may be invalidated @@ -1199,8 +1283,8 @@ unsigned int DrawUtil::intervalMerge(std::vector> &marki return i; } -void DrawUtil::intervalMarkLinear(std::vector> &marking, - double start, double length, bool value) +void DrawUtil::intervalMarkLinear(std::vector>& marking, double start, + double length, bool value) { if (length == 0.0) { return; @@ -1219,8 +1303,8 @@ void DrawUtil::intervalMarkLinear(std::vector> &marking, } } -void DrawUtil::intervalMarkCircular(std::vector> &marking, - double start, double length, bool value) +void DrawUtil::intervalMarkCircular(std::vector>& marking, double start, + double length, bool value) { if (length == 0.0) { return; @@ -1249,8 +1333,7 @@ void DrawUtil::intervalMarkCircular(std::vector> &markin marking[startIndex].second = value; ++startIndex; startIndex %= marking.size(); - } - while (startIndex != endIndex); + } while (startIndex != endIndex); } // Supplementary 2D analytic geometry functions @@ -1265,13 +1348,13 @@ int DrawUtil::findRootForValue(double Ax2, double Bxy, double Cy2, double Dx, do if (findX) { qA = Ax2; - qB = Bxy*value + Dx; - qC = Cy2*value*value + Ey*value + F; + qB = Bxy * value + Dx; + qC = Cy2 * value * value + Ey * value + F; } else { qA = Cy2; - qB = Bxy*value + Ey; - qC = Ax2*value*value + Dx*value + F; + qB = Bxy * value + Ey; + qC = Ax2 * value * value + Dx * value + F; } if (fabs(qA) < Precision::Confusion()) { @@ -1288,32 +1371,32 @@ int DrawUtil::findRootForValue(double Ax2, double Bxy, double Cy2, double Dx, do } } else { - roots[0] = -qC/qB; + roots[0] = -qC / qB; return 1; } } else { - double qD = sqr(qB) - 4.0*qA*qC; + double qD = sqr(qB) - 4.0 * qA * qC; if (qD < -Precision::Confusion()) { // Negative discriminant => no real roots return 0; } else if (qD > +Precision::Confusion()) { // Two distinctive roots - roots[0] = (-qB + sqrt(qD))*0.5/qA; - roots[1] = (-qB - sqrt(qD))*0.5/qA; + roots[0] = (-qB + sqrt(qD)) * 0.5 / qA; + roots[1] = (-qB - sqrt(qD)) * 0.5 / qA; return 2; } else { // Double root - roots[0] = -qB*0.5/qA; + roots[0] = -qB * 0.5 / qA; return 1; } } } -bool DrawUtil::mergeBoundedPoint(const Base::Vector2d &point, const Base::BoundBox2d &boundary, - std::vector &storage) +bool DrawUtil::mergeBoundedPoint(const Base::Vector2d& point, const Base::BoundBox2d& boundary, + std::vector& storage) { if (!boundary.Contains(point, Precision::Confusion())) { return false; @@ -1331,8 +1414,8 @@ bool DrawUtil::mergeBoundedPoint(const Base::Vector2d &point, const Base::BoundB void DrawUtil::findConicRectangleIntersections(double conicAx2, double conicBxy, double conicCy2, double conicDx, double conicEy, double conicF, - const Base::BoundBox2d &rectangle, - std::vector &intersections) + const Base::BoundBox2d& rectangle, + std::vector& intersections) { double roots[2]; int rootCount; @@ -1386,29 +1469,31 @@ void DrawUtil::findConicRectangleIntersections(double conicAx2, double conicBxy, } } -void DrawUtil::findLineRectangleIntersections(const Base::Vector2d &linePoint, double lineAngle, - const Base::BoundBox2d &rectangle, - std::vector &intersections) +void DrawUtil::findLineRectangleIntersections(const Base::Vector2d& linePoint, double lineAngle, + const Base::BoundBox2d& rectangle, + std::vector& intersections) { Base::Vector2d lineDirection(Base::Vector2d::FromPolar(1.0, lineAngle)); findConicRectangleIntersections(0.0, 0.0, 0.0, +lineDirection.y, -lineDirection.x, - lineDirection.x*linePoint.y - lineDirection.y*linePoint.x, + lineDirection.x * linePoint.y - lineDirection.y * linePoint.x, rectangle, intersections); } -void DrawUtil::findCircleRectangleIntersections(const Base::Vector2d &circleCenter, double circleRadius, - const Base::BoundBox2d &rectangle, - std::vector &intersections) +void DrawUtil::findCircleRectangleIntersections(const Base::Vector2d& circleCenter, + double circleRadius, + const Base::BoundBox2d& rectangle, + std::vector& intersections) { - findConicRectangleIntersections(1.0, 0.0, 1.0, -2.0*circleCenter.x, -2.0*circleCenter.y, + findConicRectangleIntersections(1.0, 0.0, 1.0, -2.0 * circleCenter.x, -2.0 * circleCenter.y, sqr(circleCenter.x) + sqr(circleCenter.y) - sqr(circleRadius), rectangle, intersections); } -void DrawUtil::findLineSegmentRectangleIntersections(const Base::Vector2d &linePoint, double lineAngle, - double segmentBasePosition, double segmentLength, - const Base::BoundBox2d &rectangle, - std::vector &intersections) +void DrawUtil::findLineSegmentRectangleIntersections(const Base::Vector2d& linePoint, + double lineAngle, double segmentBasePosition, + double segmentLength, + const Base::BoundBox2d& rectangle, + std::vector& intersections) { findLineRectangleIntersections(linePoint, lineAngle, rectangle, intersections); @@ -1419,8 +1504,8 @@ void DrawUtil::findLineSegmentRectangleIntersections(const Base::Vector2d &lineP // Dispose the points on rectangle but not within the line segment boundaries Base::Vector2d segmentDirection(Base::Vector2d::FromPolar(1.0, lineAngle)); - for (unsigned int i = 0; i < intersections.size(); ) { - double pointPosition = segmentDirection*(intersections[i] - linePoint); + for (unsigned int i = 0; i < intersections.size();) { + double pointPosition = segmentDirection * (intersections[i] - linePoint); if (pointPosition < segmentBasePosition - Precision::Confusion() || pointPosition > segmentBasePosition + segmentLength + Precision::Confusion()) { @@ -1432,16 +1517,16 @@ void DrawUtil::findLineSegmentRectangleIntersections(const Base::Vector2d &lineP } // Try to add the line segment end points - mergeBoundedPoint(linePoint + segmentBasePosition*segmentDirection, - rectangle, intersections); - mergeBoundedPoint(linePoint + (segmentBasePosition + segmentLength)*segmentDirection, + mergeBoundedPoint(linePoint + segmentBasePosition * segmentDirection, rectangle, intersections); + mergeBoundedPoint(linePoint + (segmentBasePosition + segmentLength) * segmentDirection, rectangle, intersections); } -void DrawUtil::findCircularArcRectangleIntersections(const Base::Vector2d &circleCenter, double circleRadius, - double arcBaseAngle, double arcRotation, - const Base::BoundBox2d &rectangle, - std::vector &intersections) +void DrawUtil::findCircularArcRectangleIntersections(const Base::Vector2d& circleCenter, + double circleRadius, double arcBaseAngle, + double arcRotation, + const Base::BoundBox2d& rectangle, + std::vector& intersections) { findCircleRectangleIntersections(circleCenter, circleRadius, rectangle, intersections); @@ -1454,7 +1539,7 @@ void DrawUtil::findCircularArcRectangleIntersections(const Base::Vector2d &circl } // Dispose the points on rectangle but not within the circular arc boundaries - for (unsigned int i = 0; i < intersections.size(); ) { + for (unsigned int i = 0; i < intersections.size();) { double pointAngle = (intersections[i] - circleCenter).Angle(); if (pointAngle < arcBaseAngle - Precision::Confusion()) { pointAngle += M_2PI; @@ -1471,7 +1556,8 @@ void DrawUtil::findCircularArcRectangleIntersections(const Base::Vector2d &circl // Try to add the circular arc end points mergeBoundedPoint(circleCenter + Base::Vector2d::FromPolar(circleRadius, arcBaseAngle), rectangle, intersections); - mergeBoundedPoint(circleCenter + Base::Vector2d::FromPolar(circleRadius, arcBaseAngle + arcRotation), + mergeBoundedPoint(circleCenter + + Base::Vector2d::FromPolar(circleRadius, arcBaseAngle + arcRotation), rectangle, intersections); } @@ -1479,7 +1565,7 @@ void DrawUtil::findCircularArcRectangleIntersections(const Base::Vector2d &circl //create empty outSpec file if inSpec void DrawUtil::copyFile(std::string inSpec, std::string outSpec) { -// Base::Console().Message("DU::copyFile(%s, %s)\n", inSpec.c_str(), outSpec.c_str()); + // Base::Console().Message("DU::copyFile(%s, %s)\n", inSpec.c_str(), outSpec.c_str()); if (inSpec.empty()) { // create an empty file Base::FileInfo fi(outSpec); @@ -1492,7 +1578,8 @@ void DrawUtil::copyFile(std::string inSpec, std::string outSpec) } bool rc = fi.copyTo(outSpec.c_str()); if (!rc) { - Base::Console().Message("DU::copyFile - failed - in: %s out:%s\n", inSpec.c_str(), outSpec.c_str()); + Base::Console().Message("DU::copyFile - failed - in: %s out:%s\n", inSpec.c_str(), + outSpec.c_str()); } } @@ -1503,10 +1590,10 @@ void DrawUtil::dumpVertexes(const char* text, const TopoDS_Shape& s) { Base::Console().Message("DUMP - %s\n", text); TopExp_Explorer expl(s, TopAbs_VERTEX); - for (int i = 1 ; expl.More(); expl.Next(), i++) { + for (int i = 1; expl.More(); expl.Next(), i++) { const TopoDS_Vertex& v = TopoDS::Vertex(expl.Current()); gp_Pnt pnt = BRep_Tool::Pnt(v); - Base::Console().Message("v%d: (%.3f, %.3f, %.3f)\n", i,pnt.X(), pnt.Y(), pnt.Z()); + Base::Console().Message("v%d: (%.3f, %.3f, %.3f)\n", i, pnt.X(), pnt.Y(), pnt.Z()); } } @@ -1539,7 +1626,7 @@ void DrawUtil::dumpEdges(const char* text, const TopoDS_Shape& s) { Base::Console().Message("DUMP - %s\n", text); TopExp_Explorer expl(s, TopAbs_EDGE); - for (int i = 1 ; expl.More(); expl.Next(), i++) { + for (int i = 1; expl.More(); expl.Next(), i++) { const TopoDS_Edge& e = TopoDS::Edge(expl.Current()); dumpEdge("dumpEdges", i, e); } @@ -1547,7 +1634,7 @@ void DrawUtil::dumpEdges(const char* text, const TopoDS_Shape& s) void DrawUtil::dump1Vertex(const char* text, const TopoDS_Vertex& v) { -// Base::Console().Message("DUMP - dump1Vertex - %s\n",text); + // Base::Console().Message("DUMP - dump1Vertex - %s\n",text); gp_Pnt pnt = BRep_Tool::Pnt(v); Base::Console().Message("%s: (%.3f, %.3f, %.3f)\n", text, pnt.X(), pnt.Y(), pnt.Z()); } @@ -1557,67 +1644,60 @@ void DrawUtil::dumpEdge(const char* label, int i, TopoDS_Edge e) BRepAdaptor_Curve adapt(e); double start = BRepLProp_CurveTool::FirstParameter(adapt); double end = BRepLProp_CurveTool::LastParameter(adapt); - BRepLProp_CLProps propStart(adapt, start, 0,Precision::Confusion()); + BRepLProp_CLProps propStart(adapt, start, 0, Precision::Confusion()); const gp_Pnt& vStart = propStart.Value(); - BRepLProp_CLProps propEnd(adapt, end, 0,Precision::Confusion()); + BRepLProp_CLProps propEnd(adapt, end, 0, Precision::Confusion()); const gp_Pnt& vEnd = propEnd.Value(); //Base::Console().Message("%s edge:%d start:(%.3f, %.3f, %.3f)/%0.3f end:(%.2f, %.3f, %.3f)/%.3f\n", label, i, // vStart.X(), vStart.Y(), vStart.Z(), start, vEnd.X(), vEnd.Y(), vEnd.Z(), end); - Base::Console().Message("%s edge:%d start:(%.3f, %.3f, %.3f) end:(%.2f, %.3f, %.3f) Orient: %d\n", label, i, - vStart.X(), vStart.Y(), vStart.Z(), vEnd.X(), vEnd.Y(), vEnd.Z(), e.Orientation()); + Base::Console().Message( + "%s edge:%d start:(%.3f, %.3f, %.3f) end:(%.2f, %.3f, %.3f) Orient: %d\n", label, i, + vStart.X(), vStart.Y(), vStart.Z(), vEnd.X(), vEnd.Y(), vEnd.Z(), e.Orientation()); double edgeLength = GCPnts_AbscissaPoint::Length(adapt, Precision::Confusion()); - Base::Console().Message(">>>>>>> length: %.3f distance: %.3f ration: %.3f type: %d\n", edgeLength, - vStart.Distance(vEnd), edgeLength / vStart.Distance(vEnd), adapt.GetType()); + Base::Console().Message(">>>>>>> length: %.3f distance: %.3f ration: %.3f type: %d\n", + edgeLength, vStart.Distance(vEnd), edgeLength / vStart.Distance(vEnd), + adapt.GetType()); } -const char* DrawUtil::printBool(bool b) -{ - return (b ? "True" : "False"); -} +const char* DrawUtil::printBool(bool b) { return (b ? "True" : "False"); } -QString DrawUtil::qbaToDebug(const QByteArray & line) +QString DrawUtil::qbaToDebug(const QByteArray& line) { QString s; uchar c; - for ( int i=0 ; i < line.size() ; i++ ){ + for (int i = 0; i < line.size(); i++) { c = line[i]; - if (( c >= 0x20) && (c <= 126) ) { + if ((c >= 0x20) && (c <= 126)) { s.append(QChar::fromLatin1(c)); - } else { + } + else { s.append(QString::fromUtf8("<%1>").arg(c, 2, 16, QChar::fromLatin1('0'))); } } return s; } -void DrawUtil::dumpCS(const char* text, - const gp_Ax2& CS) +void DrawUtil::dumpCS(const char* text, const gp_Ax2& CS) { gp_Dir baseAxis = CS.Direction(); - gp_Dir baseX = CS.XDirection(); - gp_Dir baseY = CS.YDirection(); - gp_Pnt baseOrg = CS.Location(); - Base::Console().Message("DU::dumpCS - %s Loc: %s Axis: %s X: %s Y: %s\n", text, - DrawUtil::formatVector(baseOrg).c_str(), - DrawUtil::formatVector(baseAxis).c_str(), - DrawUtil::formatVector(baseX).c_str(), - DrawUtil::formatVector(baseY).c_str()); + gp_Dir baseX = CS.XDirection(); + gp_Dir baseY = CS.YDirection(); + gp_Pnt baseOrg = CS.Location(); + Base::Console().Message( + "DU::dumpCS - %s Loc: %s Axis: %s X: %s Y: %s\n", text, + DrawUtil::formatVector(baseOrg).c_str(), DrawUtil::formatVector(baseAxis).c_str(), + DrawUtil::formatVector(baseX).c_str(), DrawUtil::formatVector(baseY).c_str()); } -void DrawUtil::dumpCS3(const char* text, - const gp_Ax3& CS) +void DrawUtil::dumpCS3(const char* text, const gp_Ax3& CS) { gp_Dir baseAxis = CS.Direction(); - gp_Dir baseX = CS.XDirection(); - gp_Dir baseY = CS.YDirection(); - gp_Pnt baseOrg = CS.Location(); - Base::Console().Message("DU::dumpCS3 - %s Loc: %s Axis: %s X: %s Y: %s\n", text, - DrawUtil::formatVector(baseOrg).c_str(), - DrawUtil::formatVector(baseAxis).c_str(), - DrawUtil::formatVector(baseX).c_str(), - DrawUtil::formatVector(baseY).c_str()); + gp_Dir baseX = CS.XDirection(); + gp_Dir baseY = CS.YDirection(); + gp_Pnt baseOrg = CS.Location(); + Base::Console().Message( + "DU::dumpCS3 - %s Loc: %s Axis: %s X: %s Y: %s\n", text, + DrawUtil::formatVector(baseOrg).c_str(), DrawUtil::formatVector(baseAxis).c_str(), + DrawUtil::formatVector(baseX).c_str(), DrawUtil::formatVector(baseY).c_str()); } - - -//================================== diff --git a/src/Mod/TechDraw/App/DrawUtil.h b/src/Mod/TechDraw/App/DrawUtil.h index ee1e098344..0af9da39e0 100644 --- a/src/Mod/TechDraw/App/DrawUtil.h +++ b/src/Mod/TechDraw/App/DrawUtil.h @@ -30,18 +30,18 @@ #include #include -#include -#include -#include -#include -#include -#include #include #include #include #include #include #include +#include +#include +#include +#include +#include +#include #include #include @@ -49,13 +49,13 @@ #ifndef M_2PI -# define M_2PI ((M_PI) * 2.0) +#define M_2PI ((M_PI)*2.0) #endif #define VERTEXTOLERANCE (2.0 * Precision::Confusion()) #define VECTORTOLERANCE (Precision::Confusion()) -#define SVG_NS_URI "http://www.w3.org/2000/svg" +#define SVG_NS_URI "http://www.w3.org/2000/svg" #define FREECAD_SVG_NS_URI "http://www.freecadweb.org/wiki/index.php?title=Svg_Namespace" //some shapes are being passed in where edges that should be connected are in fact @@ -79,161 +79,177 @@ struct EdgePoints { }; /// Convenient utility functions for TechDraw Module -class TechDrawExport DrawUtil { - public: - static int getIndexFromName(std::string geomName); - static std::string getGeomTypeFromName(std::string geomName); - static std::string makeGeomName(std::string geomType, int index); - static bool isSamePoint(TopoDS_Vertex v1, TopoDS_Vertex v2, double tolerance = VERTEXTOLERANCE); - static bool isZeroEdge(TopoDS_Edge e, double tolerance = VERTEXTOLERANCE); - static double simpleMinDist(TopoDS_Shape s1, TopoDS_Shape s2); - static double sensibleScale(double working_scale); - static double angleWithX(TopoDS_Edge e, bool reverse); - static double angleWithX(TopoDS_Edge e, TopoDS_Vertex v, double tolerance = VERTEXTOLERANCE); - static double incidenceAngleAtVertex(TopoDS_Edge e, TopoDS_Vertex v, double tolerance); +class TechDrawExport DrawUtil +{ +public: + static int getIndexFromName(std::string geomName); + static std::string getGeomTypeFromName(std::string geomName); + static std::string makeGeomName(std::string geomType, int index); + static bool isSamePoint(TopoDS_Vertex v1, TopoDS_Vertex v2, double tolerance = VERTEXTOLERANCE); + static bool isZeroEdge(TopoDS_Edge e, double tolerance = VERTEXTOLERANCE); + static double simpleMinDist(TopoDS_Shape s1, TopoDS_Shape s2); + static double sensibleScale(double working_scale); + static double angleWithX(TopoDS_Edge e, bool reverse); + static double angleWithX(TopoDS_Edge e, TopoDS_Vertex v, double tolerance = VERTEXTOLERANCE); + static double incidenceAngleAtVertex(TopoDS_Edge e, TopoDS_Vertex v, double tolerance); - static bool isFirstVert(TopoDS_Edge e, TopoDS_Vertex v, double tolerance = VERTEXTOLERANCE); - static bool isLastVert(TopoDS_Edge e, TopoDS_Vertex v, double tolerance = VERTEXTOLERANCE); - static bool fpCompare(const double& d1, const double& d2, double tolerance = FLT_EPSILON); - static std::pair boxIntersect2d(Base::Vector3d point, - Base::Vector3d dir, - double xRange, - double yRange) ; - static bool apparentIntersection(const Handle(Geom_Curve) curve1, - const Handle(Geom_Curve) curve2, - Base::Vector3d& result); + static bool isFirstVert(TopoDS_Edge e, TopoDS_Vertex v, double tolerance = VERTEXTOLERANCE); + static bool isLastVert(TopoDS_Edge e, TopoDS_Vertex v, double tolerance = VERTEXTOLERANCE); + static bool fpCompare(const double& d1, const double& d2, double tolerance = FLT_EPSILON); + static std::pair + boxIntersect2d(Base::Vector3d point, Base::Vector3d dir, double xRange, double yRange); + static bool apparentIntersection(const Handle(Geom_Curve) curve1, + const Handle(Geom_Curve) curve2, Base::Vector3d& result); + static bool apparentIntersection(TopoDS_Edge& edge0, TopoDS_Edge& edge1, gp_Pnt& intersect); + static bool intersect2Lines3d(Base::Vector3d p0, Base::Vector3d d0, Base::Vector3d p1, + Base::Vector3d d1, Base::Vector3d& intersect); + static Base::Vector3d vertex2Vector(const TopoDS_Vertex& v); - static Base::Vector3d vertex2Vector(const TopoDS_Vertex& v); + static std::string formatVector(const Base::Vector3d& v); + static std::string formatVector(const gp_Dir& v); + static std::string formatVector(const gp_Dir2d& v); + static std::string formatVector(const gp_Vec& v); + static std::string formatVector(const gp_Pnt& v); + static std::string formatVector(const gp_Pnt2d& v); + static std::string formatVector(const QPointF& v); - static std::string formatVector(const Base::Vector3d& v); - static std::string formatVector(const gp_Dir& v); - static std::string formatVector(const gp_Dir2d& v); - static std::string formatVector(const gp_Vec& v); - static std::string formatVector(const gp_Pnt& v); - static std::string formatVector(const gp_Pnt2d& v); - static std::string formatVector(const QPointF& v); + static bool vectorLess(const Base::Vector3d& v1, const Base::Vector3d& v2); + //!std::map require comparator to be a type not a function + struct vectorLessType { + bool operator()(const Base::Vector3d& a, const Base::Vector3d& b) const + { + return DrawUtil::vectorLess(a, b); + } + }; + static bool vertexEqual(TopoDS_Vertex& v1, TopoDS_Vertex& v2); + static bool vectorEqual(Base::Vector3d& v1, Base::Vector3d& v2); - static bool vectorLess(const Base::Vector3d& v1, const Base::Vector3d& v2); - //!std::map require comparator to be a type not a function - struct vectorLessType { - bool operator()(const Base::Vector3d& a, const Base::Vector3d& b) const { - return DrawUtil::vectorLess(a, b); - } - }; - static bool vertexEqual(TopoDS_Vertex& v1, TopoDS_Vertex& v2); - static bool vectorEqual(Base::Vector3d& v1, Base::Vector3d& v2); + static TopoDS_Shape vectorToCompound(std::vector vecIn); + static TopoDS_Shape vectorToCompound(std::vector vecIn); + static std::vector shapeToVector(TopoDS_Shape shapeIn); - static TopoDS_Shape vectorToCompound(std::vector vecIn); - static TopoDS_Shape vectorToCompound(std::vector vecIn); - static std::vector shapeToVector(TopoDS_Shape shapeIn); + static Base::Vector3d toR3(const gp_Ax2& fromSystem, const Base::Vector3d& fromPoint); + static bool checkParallel(const Base::Vector3d v1, const Base::Vector3d v2, + double tolerance = FLT_EPSILON); + //! rotate vector by angle radians around axis through org + static Base::Vector3d vecRotate(Base::Vector3d vec, double angle, Base::Vector3d axis, + Base::Vector3d org = Base::Vector3d(0.0, 0.0, 0.0)); - static Base::Vector3d toR3(const gp_Ax2& fromSystem, const Base::Vector3d& fromPoint); - static bool checkParallel(const Base::Vector3d v1, const Base::Vector3d v2, double tolerance = FLT_EPSILON); - //! rotate vector by angle radians around axis through org - static Base::Vector3d vecRotate(Base::Vector3d vec, - double angle, - Base::Vector3d axis, - Base::Vector3d org = Base::Vector3d(0.0, 0.0, 0.0)); + static Base::Vector3d closestBasis(Base::Vector3d v); + static gp_Vec closestBasis(gp_Vec inVec); + static Base::Vector3d closestBasis(Base::Vector3d vDir, gp_Ax2 coordSys); + static Base::Vector3d closestBasis(gp_Dir gDir, gp_Ax2 coordSys); - static Base::Vector3d closestBasis(Base::Vector3d v); - static gp_Vec closestBasis(gp_Vec inVec); - static Base::Vector3d closestBasis(Base::Vector3d vDir, gp_Ax2 coordSys); - static Base::Vector3d closestBasis(gp_Dir gDir, gp_Ax2 coordSys); + static double getWidthInDirection(gp_Dir direction, TopoDS_Shape& shape); - static double getWidthInDirection(gp_Dir direction, TopoDS_Shape& shape); + static double getDefaultLineWeight(std::string s); + //! is pt between end1 and end2? + static bool isBetween(const Base::Vector3d pt, const Base::Vector3d end1, + const Base::Vector3d end2); + //! find intersection in 2d for 2 lines in point+direction form + static Base::Vector3d Intersect2d(Base::Vector3d p1, Base::Vector3d d1, Base::Vector3d p2, + Base::Vector3d d2); + static Base::Vector2d Intersect2d(Base::Vector2d p1, Base::Vector2d d1, Base::Vector2d p2, + Base::Vector2d d2); - static double getDefaultLineWeight(std::string s); - //! is pt between end1 and end2? - static bool isBetween(const Base::Vector3d pt, const Base::Vector3d end1, const Base::Vector3d end2); - //! find intersection in 2d for 2 lines in point+direction form - static Base::Vector3d Intersect2d(Base::Vector3d p1, Base::Vector3d d1, - Base::Vector3d p2, Base::Vector3d d2); - static Base::Vector2d Intersect2d(Base::Vector2d p1, Base::Vector2d d1, - Base::Vector2d p2, Base::Vector2d d2); + static Base::Vector3d toVector3d(const gp_Pnt gp) + { + return Base::Vector3d(gp.X(), gp.Y(), gp.Z()); + } + static Base::Vector3d toVector3d(const gp_Dir gp) + { + return Base::Vector3d(gp.X(), gp.Y(), gp.Z()); + } + static Base::Vector3d toVector3d(const gp_Vec gp) + { + return Base::Vector3d(gp.X(), gp.Y(), gp.Z()); + } + static Base::Vector3d toVector3d(const QPointF gp) + { + return Base::Vector3d(gp.x(), gp.y(), 0.0); + } - static Base::Vector3d toVector3d(const gp_Pnt gp) { return Base::Vector3d(gp.X(), gp.Y(), gp.Z()); } - static Base::Vector3d toVector3d(const gp_Dir gp) { return Base::Vector3d(gp.X(), gp.Y(), gp.Z()); } - static Base::Vector3d toVector3d(const gp_Vec gp) { return Base::Vector3d(gp.X(), gp.Y(), gp.Z()); } - static Base::Vector3d toVector3d(const QPointF gp) { return Base::Vector3d(gp.x(), gp.y(), 0.0); } + static gp_Pnt togp_Pnt(const Base::Vector3d v) { return gp_Pnt(v.x, v.y, v.z); } + static gp_Dir togp_Dir(const Base::Vector3d v) { return gp_Dir(v.x, v.y, v.z); } + static gp_Vec togp_Vec(const Base::Vector3d v) { return gp_Vec(v.x, v.y, v.z); } + static QPointF toQPointF(const Base::Vector3d v) { return QPointF(v.x, v.y); } - static gp_Pnt togp_Pnt(const Base::Vector3d v) { return gp_Pnt(v.x, v.y, v.z); } - static gp_Dir togp_Dir(const Base::Vector3d v) { return gp_Dir(v.x, v.y, v.z); } - static gp_Vec togp_Vec(const Base::Vector3d v) { return gp_Vec(v.x, v.y, v.z); } - static QPointF toQPointF(const Base::Vector3d v) { return QPointF(v.x, v.y); } + static std::string shapeToString(TopoDS_Shape s); + static TopoDS_Shape shapeFromString(std::string s); + static Base::Vector3d invertY(Base::Vector3d v); + static QPointF invertY(QPointF p); + static std::vector split(std::string csvLine); + static std::vector tokenize(std::string csvLine, + std::string delimiter = ", $$$, "); + static App::Color pyTupleToColor(PyObject* pColor); + static PyObject* colorToPyTuple(App::Color color); + static bool isCrazy(TopoDS_Edge e); + static Base::Vector3d getFaceCenter(TopoDS_Face f); + static bool circulation(Base::Vector3d A, Base::Vector3d B, Base::Vector3d C); + static int countSubShapes(TopoDS_Shape shape, TopAbs_ShapeEnum subShape); + static void encodeXmlSpecialChars(std::string& inoutText); + static std::list sort_Edges(double tol3d, std::list& edges); - static std::string shapeToString(TopoDS_Shape s); - static TopoDS_Shape shapeFromString(std::string s); - static Base::Vector3d invertY(Base::Vector3d v); - static QPointF invertY(QPointF p); - static std::vector split(std::string csvLine); - static std::vector tokenize(std::string csvLine, std::string delimiter = ", $$$, "); - static App::Color pyTupleToColor(PyObject* pColor); - static PyObject* colorToPyTuple(App::Color color); - static bool isCrazy(TopoDS_Edge e); - static Base::Vector3d getFaceCenter(TopoDS_Face f); - static bool circulation(Base::Vector3d A, Base::Vector3d B, Base::Vector3d C); - static int countSubShapes(TopoDS_Shape shape, TopAbs_ShapeEnum subShape); - static void encodeXmlSpecialChars(std::string& inoutText); - static std::list sort_Edges(double tol3d, std::list& edges); + // Supplementary mathematical functions + static int sgn(double x); + static double sqr(double x); + static void angleNormalize(double& fi); + static double angleComposition(double fi, double delta); + static double angleDifference(double fi1, double fi2, bool reflex = false); - // Supplementary mathematical functions - static int sgn(double x); - static double sqr(double x); - static void angleNormalize(double &fi); - static double angleComposition(double fi, double delta); - static double angleDifference(double fi1, double fi2, bool reflex = false); + // Interval marking functions + static unsigned int intervalMerge(std::vector>& marking, + double boundary, bool wraps); + static void intervalMarkLinear(std::vector>& marking, double start, + double length, bool value); + static void intervalMarkCircular(std::vector>& marking, double start, + double length, bool value); - // Interval marking functions - static unsigned int intervalMerge(std::vector> &marking, - double boundary, bool wraps); - static void intervalMarkLinear(std::vector> &marking, - double start, double length, bool value); - static void intervalMarkCircular(std::vector> &marking, - double start, double length, bool value); + // Supplementary 2D analytic geometry functions + static int findRootForValue(double Ax2, double Bxy, double Cy2, double Dx, double Ey, double F, + double value, bool findX, double roots[]); + static bool mergeBoundedPoint(const Base::Vector2d& point, const Base::BoundBox2d& boundary, + std::vector& storage); - // Supplementary 2D analytic geometry functions - static int findRootForValue(double Ax2, double Bxy, double Cy2, double Dx, double Ey, double F, - double value, bool findX, double roots[]); - static bool mergeBoundedPoint(const Base::Vector2d &point, const Base::BoundBox2d &boundary, - std::vector &storage); + static void findConicRectangleIntersections(double conicAx2, double conicBxy, double conicCy2, + double conicDx, double conicEy, double conicF, + const Base::BoundBox2d& rectangle, + std::vector& intersections); + static void findLineRectangleIntersections(const Base::Vector2d& linePoint, double lineAngle, + const Base::BoundBox2d& rectangle, + std::vector& intersections); + static void findCircleRectangleIntersections(const Base::Vector2d& circleCenter, + double circleRadius, + const Base::BoundBox2d& rectangle, + std::vector& intersections); - static void findConicRectangleIntersections(double conicAx2, double conicBxy, double conicCy2, - double conicDx, double conicEy, double conicF, - const Base::BoundBox2d &rectangle, - std::vector &intersections); - static void findLineRectangleIntersections(const Base::Vector2d &linePoint, double lineAngle, - const Base::BoundBox2d &rectangle, - std::vector &intersections); - static void findCircleRectangleIntersections(const Base::Vector2d &circleCenter, double circleRadius, - const Base::BoundBox2d &rectangle, - std::vector &intersections); - - static void findLineSegmentRectangleIntersections(const Base::Vector2d &linePoint, double lineAngle, - double segmentBasePosition, double segmentLength, - const Base::BoundBox2d &rectangle, - std::vector &intersections); - static void findCircularArcRectangleIntersections(const Base::Vector2d &circleCenter, double circleRadius, - double arcBaseAngle, double arcRotation, - const Base::BoundBox2d &rectangle, - std::vector &intersections); - static void copyFile(std::string inSpec, std::string outSpec); - - //debugging routines - static void dumpVertexes(const char* text, const TopoDS_Shape& s); - static void dumpEdge(const char* label, int i, TopoDS_Edge e); - static void dump1Vertex(const char* label, const TopoDS_Vertex& v); - static void countFaces(const char* label, const TopoDS_Shape& s); - static void countWires(const char* label, const TopoDS_Shape& s); - static void countEdges(const char* label, const TopoDS_Shape& s); - static const char* printBool(bool b); - static QString qbaToDebug(const QByteArray& line); - static void dumpCS(const char* text, const gp_Ax2& CS); - static void dumpCS3(const char* text, const gp_Ax3& CS); - static void dumpEdges(const char* text, const TopoDS_Shape& s); + static void findLineSegmentRectangleIntersections(const Base::Vector2d& linePoint, + double lineAngle, double segmentBasePosition, + double segmentLength, + const Base::BoundBox2d& rectangle, + std::vector& intersections); + static void findCircularArcRectangleIntersections(const Base::Vector2d& circleCenter, + double circleRadius, double arcBaseAngle, + double arcRotation, + const Base::BoundBox2d& rectangle, + std::vector& intersections); + static void copyFile(std::string inSpec, std::string outSpec); + //debugging routines + static void dumpVertexes(const char* text, const TopoDS_Shape& s); + static void dumpEdge(const char* label, int i, TopoDS_Edge e); + static void dump1Vertex(const char* label, const TopoDS_Vertex& v); + static void countFaces(const char* label, const TopoDS_Shape& s); + static void countWires(const char* label, const TopoDS_Shape& s); + static void countEdges(const char* label, const TopoDS_Shape& s); + static const char* printBool(bool b); + static QString qbaToDebug(const QByteArray& line); + static void dumpCS(const char* text, const gp_Ax2& CS); + static void dumpCS3(const char* text, const gp_Ax3& CS); + static void dumpEdges(const char* text, const TopoDS_Shape& s); }; -} //end namespace TechDraw +}//end namespace TechDraw #endif diff --git a/src/Mod/TechDraw/App/DrawViewDimension.cpp b/src/Mod/TechDraw/App/DrawViewDimension.cpp index 7e71cdd141..1b277a5c1f 100644 --- a/src/Mod/TechDraw/App/DrawViewDimension.cpp +++ b/src/Mod/TechDraw/App/DrawViewDimension.cpp @@ -24,27 +24,28 @@ #include "PreCompiled.h" #ifndef _PreComp_ -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include #include #include #include #include #include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include #include #include +#include +#include +#include +#include +#include +#include +#include +#include #endif #include @@ -57,11 +58,11 @@ #include //#include -#include // generated from DrawViewDimensionPy.xml +#include // generated from DrawViewDimensionPy.xml -#include "DrawViewDimension.h" #include "DimensionFormatter.h" #include "DrawUtil.h" +#include "DrawViewDimension.h" #include "DrawViewPart.h" #include "Geometry.h" #include "Preferences.h" @@ -75,57 +76,64 @@ using DU = DrawUtil; PROPERTY_SOURCE(TechDraw::DrawViewDimension, TechDraw::DrawView) -const char* DrawViewDimension::TypeEnums[]= {"Distance", - "DistanceX", - "DistanceY", - "DistanceZ", - "Radius", - "Diameter", - "Angle", - "Angle3Pt", - nullptr}; +const char* DrawViewDimension::TypeEnums[] = {"Distance", "DistanceX", "DistanceY", + "DistanceZ", "Radius", "Diameter", + "Angle", "Angle3Pt", nullptr}; -const char* DrawViewDimension::MeasureTypeEnums[]= {"True", - "Projected", - nullptr}; +const char* DrawViewDimension::MeasureTypeEnums[] = {"True", "Projected", nullptr}; // constraint to set the step size to 0.1 -static const App::PropertyQuantityConstraint::Constraints ToleranceConstraint = { -DBL_MAX, DBL_MAX, 0.1 }; +static const App::PropertyQuantityConstraint::Constraints ToleranceConstraint = {-DBL_MAX, DBL_MAX, + 0.1}; // constraint to force positive values -static const App::PropertyQuantityConstraint::Constraints PositiveConstraint = { 0.0, DBL_MAX, 0.1 }; +static const App::PropertyQuantityConstraint::Constraints PositiveConstraint = {0.0, DBL_MAX, 0.1}; DrawViewDimension::DrawViewDimension() { //create the formatter since it will be needed to set default property values m_formatter = new DimensionFormatter(this); - ADD_PROPERTY_TYPE(References2D, (nullptr, nullptr), "", (App::Prop_None), "Projected Geometry References"); + ADD_PROPERTY_TYPE(References2D, (nullptr, nullptr), "", (App::Prop_None), + "Projected Geometry References"); References2D.setScope(App::LinkScope::Global); - ADD_PROPERTY_TYPE(References3D, (nullptr, nullptr), "", (App::Prop_None), "3D Geometry References"); + ADD_PROPERTY_TYPE(References3D, (nullptr, nullptr), "", (App::Prop_None), + "3D Geometry References"); References3D.setScope(App::LinkScope::Global); - ADD_PROPERTY_TYPE(FormatSpec, (getDefaultFormatSpec()), "Format", App::Prop_Output, "Dimension format"); - ADD_PROPERTY_TYPE(FormatSpecOverTolerance, (getDefaultFormatSpec(true)), "Format", App::Prop_Output, "Dimension overtolerance format"); - ADD_PROPERTY_TYPE(FormatSpecUnderTolerance, (getDefaultFormatSpec(true)), "Format", App::Prop_Output, "Dimension undertolerance format"); + ADD_PROPERTY_TYPE(FormatSpec, (getDefaultFormatSpec()), "Format", App::Prop_Output, + "Dimension format"); + ADD_PROPERTY_TYPE(FormatSpecOverTolerance, (getDefaultFormatSpec(true)), "Format", + App::Prop_Output, "Dimension overtolerance format"); + ADD_PROPERTY_TYPE(FormatSpecUnderTolerance, (getDefaultFormatSpec(true)), "Format", + App::Prop_Output, "Dimension undertolerance format"); ADD_PROPERTY_TYPE(Arbitrary, (false), "Format", App::Prop_Output, "Value overridden by user"); - ADD_PROPERTY_TYPE(ArbitraryTolerances, (false), "Format", App::Prop_Output, "Tolerance values overridden by user"); + ADD_PROPERTY_TYPE(ArbitraryTolerances, (false), "Format", App::Prop_Output, + "Tolerance values overridden by user"); - Type.setEnums(TypeEnums); //dimension type: length, radius etc + Type.setEnums(TypeEnums);//dimension type: length, radius etc ADD_PROPERTY(Type, ((long)0)); MeasureType.setEnums(MeasureTypeEnums); - ADD_PROPERTY(MeasureType, ((long)1)); //Projected (or True) measurement - ADD_PROPERTY_TYPE(TheoreticalExact, (false), "", App::Prop_Output, "If theoretical exact (basic) dimension"); - ADD_PROPERTY_TYPE(EqualTolerance, (true), "", App::Prop_Output, "If over- and undertolerance are equal"); + ADD_PROPERTY(MeasureType, ((long)1));//Projected (or True) measurement + ADD_PROPERTY_TYPE(TheoreticalExact, (false), "", App::Prop_Output, + "If theoretical exact (basic) dimension"); + ADD_PROPERTY_TYPE(EqualTolerance, (true), "", App::Prop_Output, + "If over- and undertolerance are equal"); - ADD_PROPERTY_TYPE(OverTolerance, (0.0), "", App::Prop_Output, "Overtolerance value\nIf 'Equal Tolerance' is true this is also\nthe negated value for 'Under Tolerance'"); + ADD_PROPERTY_TYPE(OverTolerance, (0.0), "", App::Prop_Output, + "Overtolerance value\nIf 'Equal Tolerance' is true this is also\nthe negated " + "value for 'Under Tolerance'"); OverTolerance.setUnit(Base::Unit::Length); OverTolerance.setConstraints(&ToleranceConstraint); - ADD_PROPERTY_TYPE(UnderTolerance, (0.0), "", App::Prop_Output, "Undertolerance value\nIf 'Equal Tolerance' is true it will be replaced\nby negative value of 'Over Tolerance'"); + ADD_PROPERTY_TYPE(UnderTolerance, (0.0), "", App::Prop_Output, + "Undertolerance value\nIf 'Equal Tolerance' is true it will be replaced\nby " + "negative value of 'Over Tolerance'"); UnderTolerance.setUnit(Base::Unit::Length); UnderTolerance.setConstraints(&ToleranceConstraint); - ADD_PROPERTY_TYPE(Inverted, (false), "", App::Prop_Output, "The dimensional value is displayed inverted"); + ADD_PROPERTY_TYPE(Inverted, (false), "", App::Prop_Output, + "The dimensional value is displayed inverted"); - ADD_PROPERTY_TYPE(AngleOverride, (false), "Override", App::Prop_Output, "User specified angles"); + ADD_PROPERTY_TYPE(AngleOverride, (false), "Override", App::Prop_Output, + "User specified angles"); ADD_PROPERTY_TYPE(LineAngle, (0.0), "Override", App::Prop_Output, "Dimension line angle"); ADD_PROPERTY_TYPE(ExtensionAngle, (0.0), "Override", App::Prop_Output, "Extension line angle"); @@ -172,7 +180,7 @@ void DrawViewDimension::resetAngular() { m_anglePoints.first(Base::Vector3d(0, 0, 0)); m_anglePoints.second(Base::Vector3d(0, 0, 0)); - m_anglePoints.vertex(Base::Vector3d(0, 0,0)); + m_anglePoints.vertex(Base::Vector3d(0, 0, 0)); } void DrawViewDimension::resetArc() @@ -191,7 +199,7 @@ void DrawViewDimension::onChanged(const App::Property* prop) { if (prop == &References3D) { //have to rebuild the Measurement object - clear3DMeasurements(); //Measurement object + clear3DMeasurements();//Measurement object if (!(References3D.getValues()).empty()) { setAll3DMeasurement(); } @@ -202,15 +210,17 @@ void DrawViewDimension::onChanged(const App::Property* prop) return; } - if (prop == &References3D) { //have to rebuild the Measurement object - clear3DMeasurements(); //Measurement object + if (prop == &References3D) {//have to rebuild the Measurement object + clear3DMeasurements(); //Measurement object if (!(References3D.getValues()).empty()) { setAll3DMeasurement(); - } else if (MeasureType.isValue("True")) { //empty 3dRefs, but True - MeasureType.touch(); //run MeasureType logic for this case + } + else if (MeasureType.isValue("True")) {//empty 3dRefs, but True + MeasureType.touch(); //run MeasureType logic for this case } return; - } else if (prop == &Type) { //why?? + } + else if (prop == &Type) {//why?? FormatSpec.setValue(getDefaultFormatSpec().c_str()); DimensionType type = static_cast(Type.getValue()); @@ -223,7 +233,8 @@ void DrawViewDimension::onChanged(const App::Property* prop) UnderTolerance.setUnit(Base::Unit::Length); } return; - } else if (prop == &TheoreticalExact) { + } + else if (prop == &TheoreticalExact) { // if TheoreticalExact disable tolerances and set them to zero if (TheoreticalExact.getValue()) { OverTolerance.setValue(0.0); @@ -234,7 +245,8 @@ void DrawViewDimension::onChanged(const App::Property* prop) FormatSpecUnderTolerance.setReadOnly(true); ArbitraryTolerances.setValue(false); ArbitraryTolerances.setReadOnly(true); - } else { + } + else { OverTolerance.setReadOnly(false); FormatSpecOverTolerance.setReadOnly(false); ArbitraryTolerances.setReadOnly(false); @@ -244,7 +256,8 @@ void DrawViewDimension::onChanged(const App::Property* prop) } } return; - } else if (prop == &EqualTolerance) { + } + else if (prop == &EqualTolerance) { // if EqualTolerance set negated overtolerance for untertolerance // then also the OverTolerance must be positive if (EqualTolerance.getValue()) { @@ -258,7 +271,8 @@ void DrawViewDimension::onChanged(const App::Property* prop) UnderTolerance.setReadOnly(true); FormatSpecUnderTolerance.setValue(FormatSpecOverTolerance.getValue()); FormatSpecUnderTolerance.setReadOnly(true); - } else { + } + else { OverTolerance.setConstraints(&ToleranceConstraint); if (!TheoreticalExact.getValue()) { UnderTolerance.setReadOnly(false); @@ -266,19 +280,22 @@ void DrawViewDimension::onChanged(const App::Property* prop) } } return; - } else if (prop == &OverTolerance) { + } + else if (prop == &OverTolerance) { // if EqualTolerance set negated overtolerance for untertolerance if (EqualTolerance.getValue()) { UnderTolerance.setValue(-1.0 * OverTolerance.getValue()); UnderTolerance.setUnit(OverTolerance.getUnit()); } return; - } else if (prop == &FormatSpecOverTolerance) { + } + else if (prop == &FormatSpecOverTolerance) { if (!ArbitraryTolerances.getValue()) { FormatSpecUnderTolerance.setValue(FormatSpecOverTolerance.getValue()); } return; - } else if (prop == &FormatSpecUnderTolerance) { + } + else if (prop == &FormatSpecUnderTolerance) { if (!ArbitraryTolerances.getValue()) { FormatSpecOverTolerance.setValue(FormatSpecUnderTolerance.getValue()); } @@ -310,7 +327,8 @@ void DrawViewDimension::onDocumentRestored() } } -void DrawViewDimension::handleChangedPropertyType(Base::XMLReader &reader, const char * TypeName, App::Property * prop) +void DrawViewDimension::handleChangedPropertyType(Base::XMLReader& reader, const char* TypeName, + App::Property* prop) { if (prop == &OverTolerance && strcmp(TypeName, "App::PropertyFloat") == 0) { App::PropertyFloat v; @@ -344,9 +362,7 @@ void DrawViewDimension::handleChangedPropertyType(Base::XMLReader &reader, const short DrawViewDimension::mustExecute() const { if (!isRestoring()) { - if (References2D.isTouched() || - References3D.isTouched() || - Type.isTouched() ) { + if (References2D.isTouched() || References3D.isTouched() || Type.isTouched()) { return true; } } @@ -354,9 +370,9 @@ short DrawViewDimension::mustExecute() const return DrawView::mustExecute(); } -App::DocumentObjectExecReturn *DrawViewDimension::execute() +App::DocumentObjectExecReturn* DrawViewDimension::execute() { -// Base::Console().Message("DVD::execute() - %s\n", getNameInDocument()); + // Base::Console().Message("DVD::execute() - %s\n", getNameInDocument()); if (!keepUpdated()) { return App::DocumentObject::StdReturn; } @@ -374,9 +390,9 @@ App::DocumentObjectExecReturn *DrawViewDimension::execute() return App::DocumentObject::StdReturn; } - if (References3D.getValues().empty() && - !checkReferences2D()) { - Base::Console().Warning("DVD::execute - %s has invalid 2D References\n", getNameInDocument()); + if (References3D.getValues().empty() && !checkReferences2D()) { + Base::Console().Warning("DVD::execute - %s has invalid 2D References\n", + getNameInDocument()); return App::DocumentObject::StdReturn; } @@ -387,32 +403,37 @@ App::DocumentObjectExecReturn *DrawViewDimension::execute() resetAngular(); resetArc(); - if ( Type.isValue("Distance") || - Type.isValue("DistanceX") || - Type.isValue("DistanceY") ) { + if (Type.isValue("Distance") || Type.isValue("DistanceX") || Type.isValue("DistanceY")) { if (getRefType() == oneEdge) { m_linearPoints = getPointsOneEdge(references); - } else if (getRefType() == twoEdge) { + } + else if (getRefType() == twoEdge) { m_linearPoints = getPointsTwoEdges(references); - } else if (getRefType() == twoVertex) { + } + else if (getRefType() == twoVertex) { m_linearPoints = getPointsTwoVerts(references); - } else if (getRefType() == vertexEdge) { + } + else if (getRefType() == vertexEdge) { m_linearPoints = getPointsEdgeVert(references); } m_hasGeometry = true; - } else if (Type.isValue("Radius")){ + } + else if (Type.isValue("Radius")) { m_arcPoints = getArcParameters(references); m_hasGeometry = true; - } else if (Type.isValue("Diameter")){ + } + else if (Type.isValue("Diameter")) { m_arcPoints = getArcParameters(references); m_hasGeometry = true; - } else if (Type.isValue("Angle")){ + } + else if (Type.isValue("Angle")) { if (getRefType() != twoEdge) { throw Base::RuntimeError("Angle dimension has non-edge references"); } m_anglePoints = getAnglePointsTwoEdges(references); m_hasGeometry = true; - } else if (Type.isValue("Angle3Pt")){ + } + else if (Type.isValue("Angle3Pt")) { if (getRefType() != threeVertex) { throw Base::RuntimeError("3 point angle dimension has non-vertex references"); } @@ -425,29 +446,21 @@ App::DocumentObjectExecReturn *DrawViewDimension::execute() } ////TODO: schema not report their multiValue status -bool DrawViewDimension::isMultiValueSchema() const -{ - return m_formatter->isMultiValueSchema(); -} +bool DrawViewDimension::isMultiValueSchema() const { return m_formatter->isMultiValueSchema(); } -std::string DrawViewDimension::formatValue(qreal value, - QString qFormatSpec, - int partial, +std::string DrawViewDimension::formatValue(qreal value, QString qFormatSpec, int partial, bool isDim) { - return m_formatter->formatValue(value, - qFormatSpec, - partial, - isDim); + return m_formatter->formatValue(value, qFormatSpec, partial, isDim); } bool DrawViewDimension::haveTolerance() { //if a numeric tolerance is specified AND //tolerances are NOT arbitrary - if ((!DrawUtil::fpCompare(OverTolerance.getValue(), 0.0) || - !DrawUtil::fpCompare(UnderTolerance.getValue(), 0.0)) && - !ArbitraryTolerances.getValue()){ + if ((!DrawUtil::fpCompare(OverTolerance.getValue(), 0.0) + || !DrawUtil::fpCompare(UnderTolerance.getValue(), 0.0)) + && !ArbitraryTolerances.getValue()) { return true; } return false; @@ -478,69 +491,77 @@ QStringList DrawViewDimension::getPrefixSuffixSpec(QString fSpec) //!NOTE: this returns the Dimension value in internal units (ie mm)!!!! double DrawViewDimension::getDimValue() { -// Base::Console().Message("DVD::getDimValue()\n"); + // Base::Console().Message("DVD::getDimValue()\n"); double result = 0.0; if (!has2DReferences() && !has3DReferences()) { //nothing to measure return result; } - if (!getViewPart()) + if (!getViewPart()) return result; - if (!getViewPart()->hasGeometry()) //happens when loading saved document + if (!getViewPart()->hasGeometry())//happens when loading saved document return result; if (MeasureType.isValue("True")) { // True Values if (!measurement->has3DReferences()) { - Base::Console().Warning("%s - True dimension has no 3D References\n", getNameInDocument()); + Base::Console().Warning("%s - True dimension has no 3D References\n", + getNameInDocument()); return result; } - if ( Type.isValue("Distance") || - Type.isValue("DistanceX") || - Type.isValue("DistanceY") ) { + if (Type.isValue("Distance") || Type.isValue("DistanceX") || Type.isValue("DistanceY")) { result = measurement->length(); - } else if (Type.isValue("Radius")){ + } + else if (Type.isValue("Radius")) { result = measurement->radius(); - } else if (Type.isValue("Diameter")){ + } + else if (Type.isValue("Diameter")) { result = 2.0 * measurement->radius(); - } else if (Type.isValue("Angle") || - Type.isValue("Angle3Pt") ) { + } + else if (Type.isValue("Angle") || Type.isValue("Angle3Pt")) { result = measurement->angle(); - } else { //tarfu + } + else {//tarfu throw Base::ValueError("getDimValue() - Unknown Dimension Type (3)"); } - } else { + } + else { // Projected Values if (!checkReferences2D()) { - Base::Console().Warning("DVD::getDimValue - %s - 2D references are corrupt (5)\n", getNameInDocument()); + Base::Console().Warning("DVD::getDimValue - %s - 2D references are corrupt (5)\n", + getNameInDocument()); return result; } - if ( Type.isValue("Distance") || - Type.isValue("DistanceX") || - Type.isValue("DistanceY") ) { + if (Type.isValue("Distance") || Type.isValue("DistanceX") || Type.isValue("DistanceY")) { pointPair pts = getLinearPoints(); Base::Vector3d dimVec = pts.first() - pts.second(); if (Type.isValue("Distance")) { result = dimVec.Length() / getViewPart()->getScale(); - } else if (Type.isValue("DistanceX")) { + } + else if (Type.isValue("DistanceX")) { result = fabs(dimVec.x) / getViewPart()->getScale(); - } else { + } + else { result = fabs(dimVec.y) / getViewPart()->getScale(); } - } else if (Type.isValue("Radius")){ + } + else if (Type.isValue("Radius")) { arcPoints pts = m_arcPoints; - result = pts.radius / getViewPart()->getScale(); //Projected BaseGeom is scaled for drawing - } else if (Type.isValue("Diameter")){ + result = + pts.radius / getViewPart()->getScale();//Projected BaseGeom is scaled for drawing + } + else if (Type.isValue("Diameter")) { arcPoints pts = m_arcPoints; - result = (pts.radius * 2.0) / getViewPart()->getScale(); //Projected BaseGeom is scaled for drawing - } else if (Type.isValue("Angle") || - Type.isValue("Angle3Pt")) { //same as case "Angle"? + result = (pts.radius * 2.0) + / getViewPart()->getScale();//Projected BaseGeom is scaled for drawing + } + else if (Type.isValue("Angle") || Type.isValue("Angle3Pt")) {//same as case "Angle"? anglePoints pts = m_anglePoints; Base::Vector3d vertex = pts.vertex(); Base::Vector3d leg0 = pts.first() - vertex; Base::Vector3d leg1 = pts.second() - vertex; - double legAngle = leg0.GetAngle(leg1) * 180.0 / M_PI; + double legAngle = leg0.GetAngle(leg1) * 180.0 / M_PI; result = legAngle; } } @@ -559,11 +580,11 @@ double DrawViewDimension::getDimValue() pointPair DrawViewDimension::getPointsOneEdge(ReferenceVector references) { -// Base::Console().Message("DVD::getPointsOneEdge()\n"); + // Base::Console().Message("DVD::getPointsOneEdge()\n"); App::DocumentObject* refObject = references.front().getObject(); int iSubelement = DrawUtil::getIndexFromName(references.front().getSubName()); - if (refObject->isDerivedFrom(TechDraw::DrawViewPart::getClassTypeId()) && - !references.at(0).getSubName().empty()) { + if (refObject->isDerivedFrom(TechDraw::DrawViewPart::getClassTypeId()) + && !references.at(0).getSubName().empty()) { //TODO: Notify if not straight line Edge? //this is a 2d object (a DVP + subelements) TechDraw::BaseGeomPtr geom = getViewPart()->getGeomByIndex(iSubelement); @@ -578,38 +599,36 @@ pointPair DrawViewDimension::getPointsOneEdge(ReferenceVector references) throw Base::RuntimeError(ssMessage.str()); } TechDraw::GenericPtr generic = std::static_pointer_cast(geom); - return { generic->points[0], generic->points[1] }; + return {generic->points[0], generic->points[1]}; } //this is a 3d object //get the endpoints of the edge in the DVP's coordinates Base::Vector3d edgeEnd0, edgeEnd1; TopoDS_Shape geometry = references.front().getGeometry(); - if (geometry.IsNull() || - geometry.ShapeType() != TopAbs_EDGE) { + if (geometry.IsNull() || geometry.ShapeType() != TopAbs_EDGE) { throw Base::RuntimeError("Geometry for dimension reference is null."); } const TopoDS_Edge& edge = TopoDS::Edge(geometry); gp_Pnt gEnd0 = BRep_Tool::Pnt(TopExp::FirstVertex(edge)); gp_Pnt gEnd1 = BRep_Tool::Pnt(TopExp::LastVertex(edge)); - gp_Pnt gCentroid = DrawUtil::togp_Pnt(getViewPart()->getOriginalCentroid()); - gEnd0 = gp_Pnt(gEnd0.XYZ() - gCentroid.XYZ()); - gEnd1 = gp_Pnt(gEnd1.XYZ() - gCentroid.XYZ()); - //project points onto paperplane, centered, scaled, rotated and inverted - edgeEnd0 = getViewPart()->projectPoint(DrawUtil::toVector3d(gEnd0)) * getViewPart()->getScale(); - edgeEnd1 = getViewPart()->projectPoint(DrawUtil::toVector3d(gEnd1)) * getViewPart()->getScale(); - return { edgeEnd0, edgeEnd1 }; + pointPair pts(DU::toVector3d(gEnd0), DU::toVector3d(gEnd1)); + pts.move(getViewPart()->getOriginalCentroid()); + pts.project(getViewPart()); + pts.mapToPage(getViewPart()); + pts.invertY(); + return pts; } pointPair DrawViewDimension::getPointsTwoEdges(ReferenceVector references) { -// Base::Console().Message("DVD::getPointsTwoEdges() - %s\n", getNameInDocument()); + // Base::Console().Message("DVD::getPointsTwoEdges() - %s\n", getNameInDocument()); App::DocumentObject* refObject = references.front().getObject(); int iSubelement0 = DrawUtil::getIndexFromName(references.at(0).getSubName()); int iSubelement1 = DrawUtil::getIndexFromName(references.at(1).getSubName()); - if (refObject->isDerivedFrom(TechDraw::DrawViewPart::getClassTypeId()) && - !references.at(0).getSubName().empty()) { + if (refObject->isDerivedFrom(TechDraw::DrawViewPart::getClassTypeId()) + && !references.at(0).getSubName().empty()) { //this is a 2d object (a DVP + subelements) TechDraw::BaseGeomPtr geom0 = getViewPart()->getGeomByIndex(iSubelement0); TechDraw::BaseGeomPtr geom1 = getViewPart()->getGeomByIndex(iSubelement1); @@ -624,8 +643,8 @@ pointPair DrawViewDimension::getPointsTwoEdges(ReferenceVector references) //this is a 3d object TopoDS_Shape geometry0 = references.at(0).getGeometry(); TopoDS_Shape geometry1 = references.at(1).getGeometry(); - if (geometry0.IsNull() || geometry1.IsNull() || - geometry0.ShapeType() != TopAbs_EDGE || geometry1.ShapeType() != TopAbs_EDGE) { + if (geometry0.IsNull() || geometry1.IsNull() || geometry0.ShapeType() != TopAbs_EDGE + || geometry1.ShapeType() != TopAbs_EDGE) { throw Base::RuntimeError("Geometry for dimension reference is null."); } @@ -639,12 +658,12 @@ pointPair DrawViewDimension::getPointsTwoEdges(ReferenceVector references) pointPair DrawViewDimension::getPointsTwoVerts(ReferenceVector references) { -// Base::Console().Message("DVD::getPointsTwoVerts() - %s\n", getNameInDocument()); + // Base::Console().Message("DVD::getPointsTwoVerts() - %s\n", getNameInDocument()); App::DocumentObject* refObject = references.front().getObject(); int iSubelement0 = DrawUtil::getIndexFromName(references.at(0).getSubName()); int iSubelement1 = DrawUtil::getIndexFromName(references.at(1).getSubName()); - if (refObject->isDerivedFrom(TechDraw::DrawViewPart::getClassTypeId()) && - !references.at(0).getSubName().empty()) { + if (refObject->isDerivedFrom(TechDraw::DrawViewPart::getClassTypeId()) + && !references.at(0).getSubName().empty()) { //this is a 2d object (a DVP + subelements) TechDraw::VertexPtr v0 = getViewPart()->getProjVertexByIndex(iSubelement0); TechDraw::VertexPtr v1 = getViewPart()->getProjVertexByIndex(iSubelement1); @@ -654,44 +673,45 @@ pointPair DrawViewDimension::getPointsTwoVerts(ReferenceVector references) throw Base::RuntimeError(ssMessage.str()); } - return { v0->pnt, v1->pnt }; + return {v0->pnt, v1->pnt}; } //this is a 3d object TopoDS_Shape geometry0 = references.at(0).getGeometry(); TopoDS_Shape geometry1 = references.at(1).getGeometry(); - if (geometry0.IsNull() || geometry1.IsNull() || - geometry0.ShapeType() != TopAbs_VERTEX || geometry1.ShapeType() != TopAbs_VERTEX) { + if (geometry0.IsNull() || geometry1.IsNull() || geometry0.ShapeType() != TopAbs_VERTEX + || geometry1.ShapeType() != TopAbs_VERTEX) { throw Base::RuntimeError("Geometry for dimension reference is null."); } const TopoDS_Vertex& vertex0 = TopoDS::Vertex(geometry0); const TopoDS_Vertex& vertex1 = TopoDS::Vertex(geometry1); gp_Pnt gPoint0 = BRep_Tool::Pnt(vertex0); gp_Pnt gPoint1 = BRep_Tool::Pnt(vertex1); - gp_Pnt gCentroid = DrawUtil::togp_Pnt(getViewPart()->getOriginalCentroid()); - gPoint0 = gp_Pnt(gPoint0.XYZ() - gCentroid.XYZ()); - gPoint1 = gp_Pnt(gPoint1.XYZ() - gCentroid.XYZ()); - //project points onto paperplane, centered, scaled, rotated and inverted - Base::Vector3d vPoint0 = getViewPart()->projectPoint(DrawUtil::toVector3d(gPoint0)) * getViewPart()->getScale(); - Base::Vector3d vPoint1 = getViewPart()->projectPoint(DrawUtil::toVector3d(gPoint1)) * getViewPart()->getScale(); - return { vPoint0, vPoint1 }; + + pointPair pts(DU::toVector3d(gPoint0), DU::toVector3d(gPoint1)); + pts.move(getViewPart()->getOriginalCentroid()); + pts.project(getViewPart()); + pts.mapToPage(getViewPart()); + pts.invertY(); + return pts; } pointPair DrawViewDimension::getPointsEdgeVert(ReferenceVector references) { -// Base::Console().Message("DVD::getPointsEdgeVert() - %s\n", getNameInDocument()); + // Base::Console().Message("DVD::getPointsEdgeVert() - %s\n", getNameInDocument()); App::DocumentObject* refObject = references.front().getObject(); int iSubelement0 = DrawUtil::getIndexFromName(references.at(0).getSubName()); int iSubelement1 = DrawUtil::getIndexFromName(references.at(1).getSubName()); - if (refObject->isDerivedFrom(TechDraw::DrawViewPart::getClassTypeId()) && - !references.at(0).getSubName().empty()) { + if (refObject->isDerivedFrom(TechDraw::DrawViewPart::getClassTypeId()) + && !references.at(0).getSubName().empty()) { //this is a 2d object (a DVP + subelements) TechDraw::BaseGeomPtr edge; TechDraw::VertexPtr vertex; if (DrawUtil::getGeomTypeFromName(references.at(0).getSubName()) == "Edge") { edge = getViewPart()->getGeomByIndex(iSubelement0); vertex = getViewPart()->getProjVertexByIndex(iSubelement1); - } else { + } + else { edge = getViewPart()->getGeomByIndex(iSubelement1); vertex = getViewPart()->getProjVertexByIndex(iSubelement0); } @@ -704,8 +724,8 @@ pointPair DrawViewDimension::getPointsEdgeVert(ReferenceVector references) //this is a 3d object TopoDS_Shape geometry0 = references.at(0).getGeometry(); TopoDS_Shape geometry1 = references.at(1).getGeometry(); - if (geometry0.IsNull() || geometry1.IsNull() || - geometry0.ShapeType() != TopAbs_VERTEX || geometry1.ShapeType() != TopAbs_VERTEX) { + if (geometry0.IsNull() || geometry1.IsNull() || geometry0.ShapeType() != TopAbs_VERTEX + || geometry1.ShapeType() != TopAbs_VERTEX) { throw Base::RuntimeError("Geometry for dimension reference is null."); } @@ -719,11 +739,11 @@ pointPair DrawViewDimension::getPointsEdgeVert(ReferenceVector references) arcPoints DrawViewDimension::getArcParameters(ReferenceVector references) { -// Base::Console().Message("DVD::getArcParameters()\n"); + // Base::Console().Message("DVD::getArcParameters()\n"); App::DocumentObject* refObject = references.front().getObject(); int iSubelement = DrawUtil::getIndexFromName(references.front().getSubName()); - if (refObject->isDerivedFrom(TechDraw::DrawViewPart::getClassTypeId()) && - !references.at(0).getSubName().empty()) { + if (refObject->isDerivedFrom(TechDraw::DrawViewPart::getClassTypeId()) + && !references.at(0).getSubName().empty()) { //this is a 2d object (a DVP + subelements) TechDraw::BaseGeomPtr geom = getViewPart()->getGeomByIndex(iSubelement); if (!geom) { @@ -736,8 +756,7 @@ arcPoints DrawViewDimension::getArcParameters(ReferenceVector references) //this is a 3d reference TopoDS_Shape geometry = references.front().getGeometry(); - if (geometry.IsNull() || - geometry.ShapeType() != TopAbs_EDGE) { + if (geometry.IsNull() || geometry.ShapeType() != TopAbs_EDGE) { throw Base::RuntimeError("Geometry for dimension reference is null."); } const TopoDS_Edge& edge = TopoDS::Edge(geometry); @@ -755,58 +774,62 @@ arcPoints DrawViewDimension::arcPointsFromBaseGeom(TechDraw::BaseGeomPtr base) arcPoints pts; pts.center = Base::Vector3d(0.0, 0.0, 0.0); pts.radius = 0.0; - if ((base && base->geomType == TechDraw::GeomType::CIRCLE) || - (base && base->geomType == TechDraw::GeomType::ARCOFCIRCLE)) { - circle = std::static_pointer_cast (base); + if ((base && base->geomType == TechDraw::GeomType::CIRCLE) + || (base && base->geomType == TechDraw::GeomType::ARCOFCIRCLE)) { + circle = std::static_pointer_cast(base); pts.center = Base::Vector3d(circle->center.x, circle->center.y, 0.0); pts.radius = circle->radius; if (base->geomType == TechDraw::GeomType::ARCOFCIRCLE) { - TechDraw::AOCPtr aoc = std::static_pointer_cast (circle); + TechDraw::AOCPtr aoc = std::static_pointer_cast(circle); pts.isArc = true; pts.onCurve.first(Base::Vector3d(aoc->midPnt.x, aoc->midPnt.y, 0.0)); - pts.midArc = Base::Vector3d(aoc->midPnt.x, aoc->midPnt.y, 0.0); + pts.midArc = Base::Vector3d(aoc->midPnt.x, aoc->midPnt.y, 0.0); pts.arcEnds.first(Base::Vector3d(aoc->startPnt.x, aoc->startPnt.y, 0.0)); pts.arcEnds.second(Base::Vector3d(aoc->endPnt.x, aoc->endPnt.y, 0.0)); - pts.arcCW = aoc->cw; - } else { - pts.isArc = false; - pts.onCurve.first(pts.center + Base::Vector3d(1, 0,0) * circle->radius); //arbitrary point on edge - pts.onCurve.second(pts.center + Base::Vector3d(-1, 0,0) * circle->radius); //arbitrary point on edge + pts.arcCW = aoc->cw; } - } else if ( (base && base->geomType == TechDraw::GeomType::ELLIPSE) || - (base && base->geomType == TechDraw::GeomType::ARCOFELLIPSE) ) { - TechDraw::EllipsePtr ellipse = std::static_pointer_cast (base); + else { + pts.isArc = false; + pts.onCurve.first(pts.center + + Base::Vector3d(1, 0, 0) * circle->radius);//arbitrary point on edge + pts.onCurve.second( + pts.center + Base::Vector3d(-1, 0, 0) * circle->radius);//arbitrary point on edge + } + } + else if ((base && base->geomType == TechDraw::GeomType::ELLIPSE) + || (base && base->geomType == TechDraw::GeomType::ARCOFELLIPSE)) { + TechDraw::EllipsePtr ellipse = std::static_pointer_cast(base); if (ellipse->closed()) { double r1 = ellipse->minor; double r2 = ellipse->major; double rAvg = (r1 + r2) / 2.0; - pts.center = Base::Vector3d(ellipse->center.x, - ellipse->center.y, - 0.0); + pts.center = Base::Vector3d(ellipse->center.x, ellipse->center.y, 0.0); pts.radius = rAvg; pts.isArc = false; - pts.onCurve.first(pts.center + Base::Vector3d(1, 0,0) * rAvg); //arbitrary point on edge - pts.onCurve.second(pts.center + Base::Vector3d(-1, 0,0) * rAvg); //arbitrary point on edge - } else { - TechDraw::AOEPtr aoe = std::static_pointer_cast (base); + pts.onCurve.first(pts.center + Base::Vector3d(1, 0, 0) * rAvg);//arbitrary point on edge + pts.onCurve.second(pts.center + + Base::Vector3d(-1, 0, 0) * rAvg);//arbitrary point on edge + } + else { + TechDraw::AOEPtr aoe = std::static_pointer_cast(base); double r1 = aoe->minor; double r2 = aoe->major; double rAvg = (r1 + r2) / 2.0; pts.isArc = true; - pts.center = Base::Vector3d(aoe->center.x, - aoe->center.y, - 0.0); + pts.center = Base::Vector3d(aoe->center.x, aoe->center.y, 0.0); pts.radius = rAvg; pts.arcEnds.first(Base::Vector3d(aoe->startPnt.x, aoe->startPnt.y, 0.0)); pts.arcEnds.second(Base::Vector3d(aoe->endPnt.x, aoe->endPnt.y, 0.0)); - pts.midArc = Base::Vector3d(aoe->midPnt.x, aoe->midPnt.y, 0.0); - pts.arcCW = aoe->cw; - pts.onCurve.first(Base::Vector3d(aoe->midPnt.x, aoe->midPnt.y, 0.0)); //for radius -// pts.onCurve.first(pts.center + Base::Vector3d(1, 0,0) * rAvg); //for diameter - pts.onCurve.second(pts.center + Base::Vector3d(-1, 0,0) * rAvg); //arbitrary point on edge + pts.midArc = Base::Vector3d(aoe->midPnt.x, aoe->midPnt.y, 0.0); + pts.arcCW = aoe->cw; + pts.onCurve.first(Base::Vector3d(aoe->midPnt.x, aoe->midPnt.y, 0.0));//for radius + // pts.onCurve.first(pts.center + Base::Vector3d(1, 0,0) * rAvg); //for diameter + pts.onCurve.second(pts.center + + Base::Vector3d(-1, 0, 0) * rAvg);//arbitrary point on edge } - } else if (base && base->geomType == TechDraw::GeomType::BSPLINE) { - TechDraw::BSplinePtr spline = std::static_pointer_cast (base); + } + else if (base && base->geomType == TechDraw::GeomType::BSPLINE) { + TechDraw::BSplinePtr spline = std::static_pointer_cast(base); if (spline->isCircle()) { bool arc; double rad; @@ -817,22 +840,28 @@ arcPoints DrawViewDimension::arcPointsFromBaseGeom(TechDraw::BaseGeomPtr base) pts.radius = rad; pts.arcEnds.first(Base::Vector3d(spline->startPnt.x, spline->startPnt.y, 0.0)); pts.arcEnds.second(Base::Vector3d(spline->endPnt.x, spline->endPnt.y, 0.0)); - pts.midArc = Base::Vector3d(spline->midPnt.x, spline->midPnt.y, 0.0); + pts.midArc = Base::Vector3d(spline->midPnt.x, spline->midPnt.y, 0.0); pts.isArc = arc; - pts.arcCW = spline->cw; + pts.arcCW = spline->cw; if (arc) { pts.onCurve.first(Base::Vector3d(spline->midPnt.x, spline->midPnt.y, 0.0)); - } else { - pts.onCurve.first(pts.center + Base::Vector3d(1, 0,0) * rad); //arbitrary point on edge - pts.onCurve.second(pts.center + Base::Vector3d(-1, 0,0) * rad); //arbitrary point on edge } - } else { + else { + pts.onCurve.first(pts.center + + Base::Vector3d(1, 0, 0) * rad);//arbitrary point on edge + pts.onCurve.second(pts.center + + Base::Vector3d(-1, 0, 0) * rad);//arbitrary point on edge + } + } + else { //fubar - can't have non-circular spline as target of Diameter dimension, but this is already //checked, so something has gone badly wrong. - Base::Console().Error("%s: can not make a Circle from this BSpline edge\n", getNameInDocument()); + Base::Console().Error("%s: can not make a Circle from this BSpline edge\n", + getNameInDocument()); throw Base::RuntimeError("Bad BSpline geometry for arc dimension"); } - } else { + } + else { std::stringstream ssMessage; ssMessage << getNameInDocument() << " 2d reference is a " << base->geomTypeName(); throw Base::RuntimeError(ssMessage.str()); @@ -844,7 +873,7 @@ arcPoints DrawViewDimension::arcPointsFromEdge(TopoDS_Edge occEdge) { arcPoints pts; pts.isArc = !BRep_Tool::IsClosed(occEdge); - pts.arcCW = false; + pts.arcCW = false; BRepAdaptor_Curve adapt(occEdge); double pFirst = adapt.FirstParameter(); double pLast = adapt.LastParameter(); @@ -868,12 +897,16 @@ arcPoints DrawViewDimension::arcPointsFromEdge(TopoDS_Edge occEdge) gp_Vec endVec = DU::togp_Vec(pts.arcEnds.second() - pts.center); double angle = startVec.AngleWithRef(endVec, axis.Direction().XYZ()); pts.arcCW = (angle < 0.0); - } else { - //full circle - pts.onCurve.first(pts.center + Base::Vector3d(1, 0,0) * pts.radius); //arbitrary point on edge - pts.onCurve.second(pts.center + Base::Vector3d(-1, 0,0) * pts.radius); //arbitrary point on edge } - } else if (adapt.GetType() == GeomAbs_Ellipse) { + else { + //full circle + pts.onCurve.first(pts.center + + Base::Vector3d(1, 0, 0) * pts.radius);//arbitrary point on edge + pts.onCurve.second(pts.center + + Base::Vector3d(-1, 0, 0) * pts.radius);//arbitrary point on edge + } + } + else if (adapt.GetType() == GeomAbs_Ellipse) { gp_Elips ellipse = adapt.Ellipse(); pts.center = DU::toVector3d(ellipse.Location()); pts.radius = (ellipse.MajorRadius() + ellipse.MinorRadius()) / 2.0; @@ -884,12 +917,16 @@ arcPoints DrawViewDimension::arcPointsFromEdge(TopoDS_Edge occEdge) gp_Vec endVec = DU::togp_Vec(pts.arcEnds.second() - pts.center); double angle = startVec.AngleWithRef(endVec, axis.Direction().XYZ()); pts.arcCW = (angle < 0.0); - } else { - //full ellipse - pts.onCurve.first(pts.center + Base::Vector3d(1, 0,0) * pts.radius); //arbitrary point on edge - pts.onCurve.second(pts.center + Base::Vector3d(-1, 0,0) * pts.radius); //arbitrary point on edge } - } else if (adapt.GetType() == GeomAbs_BSplineCurve) { + else { + //full ellipse + pts.onCurve.first(pts.center + + Base::Vector3d(1, 0, 0) * pts.radius);//arbitrary point on edge + pts.onCurve.second(pts.center + + Base::Vector3d(-1, 0, 0) * pts.radius);//arbitrary point on edge + } + } + else if (adapt.GetType() == GeomAbs_BSplineCurve) { if (GeometryUtils::isCircle(occEdge)) { bool isArc(false); TopoDS_Edge circleEdge = GeometryUtils::asCircle(occEdge, isArc); @@ -909,15 +946,20 @@ arcPoints DrawViewDimension::arcPointsFromEdge(TopoDS_Edge occEdge) gp_Vec endVec = DU::togp_Vec(pts.arcEnds.second() - pts.center); double angle = startVec.AngleWithRef(endVec, axis.Direction().XYZ()); pts.arcCW = (angle < 0.0); - } else { - //full circle - pts.onCurve.first(pts.center + Base::Vector3d(1, 0,0) * pts.radius); //arbitrary point on edge - pts.onCurve.second(pts.center + Base::Vector3d(-1, 0,0) * pts.radius); //arbitrary point on edge } - } else { + else { + //full circle + pts.onCurve.first(pts.center + + Base::Vector3d(1, 0, 0) * pts.radius);//arbitrary point on edge + pts.onCurve.second( + pts.center + Base::Vector3d(-1, 0, 0) * pts.radius);//arbitrary point on edge + } + } + else { throw Base::RuntimeError("failed to make circle from bspline"); } - } else { + } + else { throw Base::RuntimeError("can not get arc points from this edge"); } @@ -926,12 +968,12 @@ arcPoints DrawViewDimension::arcPointsFromEdge(TopoDS_Edge occEdge) anglePoints DrawViewDimension::getAnglePointsTwoEdges(ReferenceVector references) { -// Base::Console().Message("DVD::getAnglePointsTwoEdges() - %s\n", getNameInDocument()); + //Base::Console().Message("DVD::getAnglePointsTwoEdges() - %s\n", getNameInDocument()); App::DocumentObject* refObject = references.front().getObject(); int iSubelement0 = DrawUtil::getIndexFromName(references.at(0).getSubName()); int iSubelement1 = DrawUtil::getIndexFromName(references.at(1).getSubName()); - if (refObject->isDerivedFrom(TechDraw::DrawViewPart::getClassTypeId()) && - !references.at(0).getSubName().empty()) { + if (refObject->isDerivedFrom(TechDraw::DrawViewPart::getClassTypeId()) + && !references.at(0).getSubName().empty()) { //this is a 2d object (a DVP + subelements) TechDraw::BaseGeomPtr geom0 = getViewPart()->getGeomByIndex(iSubelement0); TechDraw::BaseGeomPtr geom1 = getViewPart()->getGeomByIndex(iSubelement1); @@ -942,12 +984,14 @@ anglePoints DrawViewDimension::getAnglePointsTwoEdges(ReferenceVector references } if (geom0->geomType != TechDraw::GeomType::GENERIC) { std::stringstream ssMessage; - ssMessage << getNameInDocument() << " first 2d reference is a " << geom0->geomTypeName(); + ssMessage << getNameInDocument() << " first 2d reference is a " + << geom0->geomTypeName(); throw Base::RuntimeError(ssMessage.str()); } if (geom1->geomType != TechDraw::GeomType::GENERIC) { std::stringstream ssMessage; - ssMessage << getNameInDocument() << " second 2d reference is a " << geom0->geomTypeName(); + ssMessage << getNameInDocument() << " second 2d reference is a " + << geom0->geomTypeName(); throw Base::RuntimeError(ssMessage.str()); } TechDraw::GenericPtr generic0 = std::static_pointer_cast(geom0); @@ -955,17 +999,19 @@ anglePoints DrawViewDimension::getAnglePointsTwoEdges(ReferenceVector references Base::Vector3d apex = generic0->apparentInter(generic1); Base::Vector3d farPoint0, farPoint1; //pick the end of generic0 farthest from the apex - if ((generic0->getStartPoint() - apex).Length() > - (generic0->getEndPoint() - apex).Length()) { + if ((generic0->getStartPoint() - apex).Length() + > (generic0->getEndPoint() - apex).Length()) { farPoint0 = generic0->getStartPoint(); - } else { + } + else { farPoint0 = generic0->getEndPoint(); } //pick the end of generic1 farthest from the apex - if ((generic1->getStartPoint() - apex).Length() > - (generic1->getEndPoint() - apex).Length()) { + if ((generic1->getStartPoint() - apex).Length() + > (generic1->getEndPoint() - apex).Length()) { farPoint1 = generic1->getStartPoint(); - } else { + } + else { farPoint1 = generic1->getEndPoint(); } Base::Vector3d leg0Dir = (generic0->getStartPoint() - generic0->getEndPoint()).Normalize(); @@ -974,21 +1020,21 @@ anglePoints DrawViewDimension::getAnglePointsTwoEdges(ReferenceVector references //legs of the angle are parallel. throw Base::RuntimeError("Can not make angle from parallel edges"); } - Base::Vector3d extenPoint0 = farPoint0; //extension line points + Base::Vector3d extenPoint0 = farPoint0;//extension line points Base::Vector3d extenPoint1 = farPoint1; if (DU::fpCompare(fabs(leg0Dir.Dot(leg1Dir)), 0.0)) { //legs of angle are perpendicular farPoints will do - } else { + } + else { //legs of the angle are skew //project farthest points onto opposite edge Base::Vector3d projFar0OnLeg1 = farPoint0.Perpendicular(apex, leg1Dir); Base::Vector3d projFar1OnLeg0 = farPoint1.Perpendicular(apex, leg0Dir); - if (DrawUtil::isBetween(projFar0OnLeg1, - generic1->getStartPoint(), + if (DrawUtil::isBetween(projFar0OnLeg1, generic1->getStartPoint(), generic1->getEndPoint())) { extenPoint1 = projFar0OnLeg1; - } else if (DrawUtil::isBetween(projFar1OnLeg0, - generic0->getStartPoint(), + } + else if (DrawUtil::isBetween(projFar1OnLeg0, generic0->getStartPoint(), generic0->getEndPoint())) { extenPoint0 = projFar1OnLeg0; } @@ -1004,61 +1050,43 @@ anglePoints DrawViewDimension::getAnglePointsTwoEdges(ReferenceVector references //this is a 3d object TopoDS_Shape geometry0 = references.at(0).getGeometry(); TopoDS_Shape geometry1 = references.at(1).getGeometry(); - if (geometry0.IsNull() || geometry1.IsNull() || - geometry0.ShapeType() != TopAbs_EDGE || geometry1.ShapeType() != TopAbs_EDGE) { + if (geometry0.IsNull() || geometry1.IsNull() || geometry0.ShapeType() != TopAbs_EDGE + || geometry1.ShapeType() != TopAbs_EDGE) { throw Base::RuntimeError("Geometry for dimension reference is null."); } TopoDS_Edge edge0 = TopoDS::Edge(geometry0); BRepAdaptor_Curve adapt0(edge0); - auto curve0 = adapt0.Curve().Curve(); TopoDS_Edge edge1 = TopoDS::Edge(geometry1); BRepAdaptor_Curve adapt1(edge1); - auto curve1 = adapt1.Curve().Curve(); - Base::Vector3d apex; - if (!DU::apparentIntersection(curve0, curve1, apex)) { - //no intersection - throw Base::RuntimeError("Edges for angle dimension can not intersect"); + + if (adapt0.GetType() != GeomAbs_Line || adapt1.GetType() != GeomAbs_Line) { + throw Base::RuntimeError("Geometry for angle dimension must be lines."); } gp_Pnt gStart0 = BRep_Tool::Pnt(TopExp::FirstVertex(edge0)); gp_Pnt gEnd0 = BRep_Tool::Pnt(TopExp::LastVertex(edge0)); - gp_Pnt gFar0 = gEnd0; - if (gStart0.Distance(DU::togp_Pnt(apex)) > gEnd0.Distance(DU::togp_Pnt(apex)) ) { - gFar0 = gStart0; - } + gp_Vec gDir0(gEnd0.XYZ() - gStart0.XYZ()); gp_Pnt gStart1 = BRep_Tool::Pnt(TopExp::FirstVertex(edge1)); gp_Pnt gEnd1 = BRep_Tool::Pnt(TopExp::LastVertex(edge1)); + gp_Vec gDir1(gEnd1.XYZ() - gStart1.XYZ()); + Base::Vector3d vApex; + bool haveIntersection = + DU::intersect2Lines3d(DU::toVector3d(gStart0), DU::toVector3d(gDir0), + DU::toVector3d(gStart1), DU::toVector3d(gDir1), vApex); + if (!haveIntersection) { + throw Base::RuntimeError("Geometry for 3d angle dimension does not intersect"); + } + gp_Pnt gApex = DU::togp_Pnt(vApex); + + gp_Pnt gFar0 = gEnd0; + if (gStart0.Distance(gApex) > gEnd0.Distance(gApex)) { + gFar0 = gStart0; + } + gp_Pnt gFar1 = gEnd1; - if (gStart1.Distance(DU::togp_Pnt(apex)) > gEnd1.Distance(DU::togp_Pnt(apex)) ) { + if (gStart1.Distance(gApex) > gEnd1.Distance(gApex)) { gFar1 = gStart1; } - Base::Vector3d farPoint0 = DU::toVector3d(gFar0); - Base::Vector3d farPoint1 = DU::toVector3d(gFar1); - Base::Vector3d leg0Dir = DU::toVector3d(gFar0) - apex; - Base::Vector3d leg1Dir = DU::toVector3d(gFar1) - apex; - if (DU::fpCompare(fabs(leg0Dir.Dot(leg1Dir)), 1.0)) { - //legs of the angle are parallel. - throw Base::RuntimeError("Can not make angle from parallel edges"); - } - Base::Vector3d extenPoint0 = DU::toVector3d(gFar0); //extension line points - Base::Vector3d extenPoint1 = DU::toVector3d(gFar1); - if (!DU::fpCompare(fabs(leg0Dir.Dot(leg1Dir)), 0.0)) { - //legs of the angle are skew - //project farthest points onto opposite edge - Base::Vector3d projFar0OnLeg1 = farPoint0.Perpendicular(apex, leg1Dir); - Base::Vector3d projFar1OnLeg0 = farPoint1.Perpendicular(apex, leg0Dir); - if (DrawUtil::isBetween(projFar0OnLeg1, - DU::toVector3d(gStart0), - DU::toVector3d(gEnd0)) ) { - extenPoint1 = projFar0OnLeg1; - } else if (DrawUtil::isBetween(projFar1OnLeg0, - DU::toVector3d(gStart1), - DU::toVector3d(gEnd1)) ) { - - extenPoint0 = projFar1OnLeg0; - } - } - - anglePoints pts(apex, extenPoint0, extenPoint1); + anglePoints pts(DU::toVector3d(gApex), DU::toVector3d(gFar0), DU::toVector3d(gFar1)); pts.move(getViewPart()->getOriginalCentroid()); pts.project(getViewPart()); pts.mapToPage(getViewPart()); @@ -1069,7 +1097,7 @@ anglePoints DrawViewDimension::getAnglePointsTwoEdges(ReferenceVector references //TODO: this makes assumptions about the order of references (p - v - p). is this checked somewhere? anglePoints DrawViewDimension::getAnglePointsThreeVerts(ReferenceVector references) { -// Base::Console().Message("DVD::getAnglePointsThreeVerts() - %s\n", getNameInDocument()); + // Base::Console().Message("DVD::getAnglePointsThreeVerts() - %s\n", getNameInDocument()); if (references.size() < 3) { throw Base::RuntimeError("Not enough references to make angle dimension"); } @@ -1077,8 +1105,8 @@ anglePoints DrawViewDimension::getAnglePointsThreeVerts(ReferenceVector referenc int iSubelement0 = DrawUtil::getIndexFromName(references.at(0).getSubName()); int iSubelement1 = DrawUtil::getIndexFromName(references.at(1).getSubName()); int iSubelement2 = DrawUtil::getIndexFromName(references.at(2).getSubName()); - if (refObject->isDerivedFrom(TechDraw::DrawViewPart::getClassTypeId()) && - !references.at(0).getSubName().empty()) { + if (refObject->isDerivedFrom(TechDraw::DrawViewPart::getClassTypeId()) + && !references.at(0).getSubName().empty()) { //this is a 2d object (a DVP + subelements) TechDraw::VertexPtr vert0 = getViewPart()->getProjVertexByIndex(iSubelement0); TechDraw::VertexPtr vert1 = getViewPart()->getProjVertexByIndex(iSubelement1); @@ -1094,8 +1122,9 @@ anglePoints DrawViewDimension::getAnglePointsThreeVerts(ReferenceVector referenc TopoDS_Shape geometry0 = references.at(0).getGeometry(); TopoDS_Shape geometry1 = references.at(1).getGeometry(); TopoDS_Shape geometry2 = references.at(2).getGeometry(); - if (geometry0.IsNull() || geometry1.IsNull() || geometry2.IsNull() || - geometry0.ShapeType() != TopAbs_VERTEX || geometry1.ShapeType() != TopAbs_VERTEX || geometry2.ShapeType() != TopAbs_VERTEX) { + if (geometry0.IsNull() || geometry1.IsNull() || geometry2.IsNull() + || geometry0.ShapeType() != TopAbs_VERTEX || geometry1.ShapeType() != TopAbs_VERTEX + || geometry2.ShapeType() != TopAbs_VERTEX) { throw Base::RuntimeError("Geometry for dimension reference is null."); } TopoDS_Vertex vertex0 = TopoDS::Vertex(geometry0); @@ -1117,7 +1146,7 @@ DrawViewPart* DrawViewDimension::getViewPart() const if (References2D.getValues().empty()) { return nullptr; } - return dynamic_cast(References2D.getValues().at(0)); + return dynamic_cast(References2D.getValues().at(0)); } @@ -1137,7 +1166,8 @@ ReferenceVector DrawViewDimension::getEffectiveReferences() const ReferenceEntry ref(objects3d.at(i), std::string(subElements3d.at(i))); effectiveRefs.push_back(ref); } - } else { + } + else { //use 2d references if necessary int refCount = objects.size(); for (int i = 0; i < refCount; i++) { @@ -1197,7 +1227,8 @@ int DrawViewDimension::getRefType() const if (subNames.empty()) { //something went wrong, there were no subNames. - Base::Console().Message("DVD::getRefType - %s - there are no subNames.\n", getNameInDocument()); + Base::Console().Message("DVD::getRefType - %s - there are no subNames.\n", + getNameInDocument()); return 0; } @@ -1206,21 +1237,35 @@ int DrawViewDimension::getRefType() const //TODO: Gui/DimensionValidators.cpp has almost the same code //decide what the reference configuration is by examining the names of the sub elements -int DrawViewDimension::getRefTypeSubElements(const std::vector &subElements) +int DrawViewDimension::getRefTypeSubElements(const std::vector& subElements) { int refType = invalidRef; int refEdges = 0, refVertices = 0; - for (const auto& se: subElements) { - if (DrawUtil::getGeomTypeFromName(se) == "Vertex") { refVertices++; } - if (DrawUtil::getGeomTypeFromName(se) == "Edge") { refEdges++; } + for (const auto& se : subElements) { + if (DrawUtil::getGeomTypeFromName(se) == "Vertex") { + refVertices++; + } + if (DrawUtil::getGeomTypeFromName(se) == "Edge") { + refEdges++; + } } - if (refEdges == 0 && refVertices == 2) { refType = twoVertex; } - if (refEdges == 0 && refVertices == 3) { refType = threeVertex; } - if (refEdges == 1 && refVertices == 0) { refType = oneEdge; } - if (refEdges == 1 && refVertices == 1) { refType = vertexEdge; } - if (refEdges == 2 && refVertices == 0) { refType = twoEdge; } + if (refEdges == 0 && refVertices == 2) { + refType = twoVertex; + } + if (refEdges == 0 && refVertices == 3) { + refType = threeVertex; + } + if (refEdges == 1 && refVertices == 0) { + refType = oneEdge; + } + if (refEdges == 1 && refVertices == 1) { + refType = vertexEdge; + } + if (refEdges == 2 && refVertices == 0) { + refType = twoEdge; + } return refType; } @@ -1228,25 +1273,24 @@ int DrawViewDimension::getRefTypeSubElements(const std::vector &sub //! validate 2D references - only checks if the target exists bool DrawViewDimension::checkReferences2D() const { -// Base::Console().Message("DVD::checkReferences2d() - %s\n", getNameInDocument()); - const std::vector &objects = References2D.getValues(); + // Base::Console().Message("DVD::checkReferences2d() - %s\n", getNameInDocument()); + const std::vector& objects = References2D.getValues(); if (objects.empty()) { return false; } - const std::vector &subElements = References2D.getSubValues(); + const std::vector& subElements = References2D.getSubValues(); if (subElements.empty()) { //must have at least 1 null string entry to balance DVP return false; } - if (subElements.front().empty() && - !References3D.getValues().empty()) { + if (subElements.front().empty() && !References3D.getValues().empty()) { //this is (probably) a dim with 3d refs return true; } - for (auto& s: subElements) { + for (auto& s : subElements) { if (s.empty()) { return false; } @@ -1257,7 +1301,8 @@ bool DrawViewDimension::checkReferences2D() const if (!geom) { return false; } - } else if (DrawUtil::getGeomTypeFromName(s) == "Vertex") { + } + else if (DrawUtil::getGeomTypeFromName(s) == "Vertex") { TechDraw::VertexPtr v = getViewPart()->getProjVertexByIndex(idx); if (!v) { return false; @@ -1268,8 +1313,7 @@ bool DrawViewDimension::checkReferences2D() const return true; } -pointPair DrawViewDimension::closestPoints(TopoDS_Shape s1, - TopoDS_Shape s2) const +pointPair DrawViewDimension::closestPoints(TopoDS_Shape s1, TopoDS_Shape s2) const { pointPair result; BRepExtrema_DistShapeShape extss(s1, s2); @@ -1282,7 +1326,7 @@ pointPair DrawViewDimension::closestPoints(TopoDS_Shape s1, result.first(Base::Vector3d(p.X(), p.Y(), p.Z())); p = extss.PointOnShape2(1); result.second(Base::Vector3d(p.X(), p.Y(), p.Z())); - } //TODO: else { explode } + }//TODO: else { explode } return result; } @@ -1292,11 +1336,11 @@ void DrawViewDimension::setReferences2d(ReferenceVector refs) { std::vector objects; std::vector subNames; - if ( objects.size() != subNames.size() ) { + if (objects.size() != subNames.size()) { throw Base::IndexError("DVD::setReferences2d - objects and subNames do not match."); } - for ( size_t iRef = 0; iRef < refs.size(); iRef++) { + for (size_t iRef = 0; iRef < refs.size(); iRef++) { objects.push_back(refs.at(iRef).getObject()); subNames.push_back(refs.at(iRef).getSubName()); } @@ -1309,11 +1353,11 @@ void DrawViewDimension::setReferences3d(ReferenceVector refs) { std::vector objects; std::vector subNames; - if ( objects.size() != subNames.size() ) { + if (objects.size() != subNames.size()) { throw Base::IndexError("DVD::setReferences3d - objects and subNames do not match."); } - for ( size_t iRef = 0; iRef < refs.size(); iRef++) { + for (size_t iRef = 0; iRef < refs.size(); iRef++) { objects.push_back(refs.at(iRef).getObject()); subNames.push_back(refs.at(iRef).getSubName()); } @@ -1324,14 +1368,14 @@ void DrawViewDimension::setReferences3d(ReferenceVector refs) //!add Dimension 3D references to measurement void DrawViewDimension::setAll3DMeasurement() { -// Base::Console().Message("DVD::setAll3dMeasurement()\n"); + // Base::Console().Message("DVD::setAll3dMeasurement()\n"); measurement->clear(); - const std::vector &Objs = References3D.getValues(); - const std::vector &Subs = References3D.getSubValues(); + const std::vector& Objs = References3D.getValues(); + const std::vector& Subs = References3D.getSubValues(); int end = Objs.size(); int i = 0; - for ( ; i < end; i++) { - static_cast (measurement->addReference3D(Objs.at(i), Subs.at(i))); + for (; i < end; i++) { + static_cast(measurement->addReference3D(Objs.at(i), Subs.at(i))); } } @@ -1345,20 +1389,19 @@ void DrawViewDimension::clear3DMeasurements() void DrawViewDimension::dumpRefs2D(const char* text) const { Base::Console().Message("DUMP - %s\n", text); - const std::vector &objects = References2D.getValues(); - const std::vector &subElements = References2D.getSubValues(); + const std::vector& objects = References2D.getValues(); + const std::vector& subElements = References2D.getSubValues(); std::vector::const_iterator objIt = objects.begin(); std::vector::const_iterator subIt = subElements.begin(); int i = 0; - for( ;objIt != objects.end();objIt++, subIt++, i++) { - Base::Console().Message("DUMP - ref: %d object: %s subElement: %s\n", i,(*objIt)->getNameInDocument(), (*subIt).c_str()); + for (; objIt != objects.end(); objIt++, subIt++, i++) { + Base::Console().Message("DUMP - ref: %d object: %s subElement: %s\n", i, + (*objIt)->getNameInDocument(), (*subIt).c_str()); } } -double DrawViewDimension::dist2Segs(Base::Vector3d s1, - Base::Vector3d e1, - Base::Vector3d s2, - Base::Vector3d e2) const +double DrawViewDimension::dist2Segs(Base::Vector3d s1, Base::Vector3d e1, Base::Vector3d s2, + Base::Vector3d e2) const { gp_Pnt start(s1.x, s1.y, 0.0); gp_Pnt end(e1.x, e1.y, 0.0); @@ -1382,23 +1425,25 @@ double DrawViewDimension::dist2Segs(Base::Vector3d s1, double minDist = 0.0; if (count != 0) { minDist = extss.Value(); - } //TODO: else { explode } + }//TODO: else { explode } return minDist; } -bool DrawViewDimension::leaderIntersectsArc(Base::Vector3d s, Base::Vector3d pointOnCircle) { +bool DrawViewDimension::leaderIntersectsArc(Base::Vector3d s, Base::Vector3d pointOnCircle) +{ bool result = false; - const std::vector &subElements = References2D.getSubValues(); + const std::vector& subElements = References2D.getSubValues(); int idx = DrawUtil::getIndexFromName(subElements[0]); TechDraw::BaseGeomPtr base = getViewPart()->getGeomByIndex(idx); - if ( base && base->geomType == TechDraw::GeomType::ARCOFCIRCLE ) { - TechDraw::AOCPtr aoc = std::static_pointer_cast (base); + if (base && base->geomType == TechDraw::GeomType::ARCOFCIRCLE) { + TechDraw::AOCPtr aoc = std::static_pointer_cast(base); if (aoc->intersectsArc(s, pointOnCircle)) { result = true; } - } else if ( base && base->geomType == TechDraw::GeomType::BSPLINE ) { - TechDraw::BSplinePtr spline = std::static_pointer_cast (base); + } + else if (base && base->geomType == TechDraw::GeomType::BSPLINE) { + TechDraw::BSplinePtr spline = std::static_pointer_cast(base); if (spline->isCircle()) { if (spline->intersectsArc(s, pointOnCircle)) { result = true; @@ -1414,7 +1459,8 @@ void DrawViewDimension::saveArrowPositions(const Base::Vector2d positions[]) if (!positions) { m_arrowPositions.first(Base::Vector3d(0.0, 0.0, 0.0)); m_arrowPositions.second(Base::Vector3d(0.0, 0.0, 0.0)); - } else { + } + else { double scale = getViewPart()->getScale(); m_arrowPositions.first(Base::Vector3d(positions[0].x, positions[0].y, 0.0) / scale); m_arrowPositions.second(Base::Vector3d(positions[1].x, positions[1].y, 0.0) / scale); @@ -1423,16 +1469,13 @@ void DrawViewDimension::saveArrowPositions(const Base::Vector2d positions[]) //return position within parent view of dimension arrow heads/dimline endpoints //note positions are in apparent coord (inverted y). -pointPair DrawViewDimension::getArrowPositions() -{ - return m_arrowPositions; -} +pointPair DrawViewDimension::getArrowPositions() { return m_arrowPositions; } bool DrawViewDimension::has2DReferences() const { -// Base::Console().Message("DVD::has2DReferences() - %s\n",getNameInDocument()); - const std::vector &objects = References2D.getValues(); - const std::vector& subNames = References2D.getSubValues(); + // Base::Console().Message("DVD::has2DReferences() - %s\n",getNameInDocument()); + const std::vector& objects = References2D.getValues(); + const std::vector& subNames = References2D.getSubValues(); if (objects.empty()) { //we don't even have a DVP return false; @@ -1448,17 +1491,13 @@ bool DrawViewDimension::has2DReferences() const } //there is no special structure to 3d references, so anything > 0 is good -bool DrawViewDimension::has3DReferences() const -{ - return (References3D.getSize() > 0); -} +bool DrawViewDimension::has3DReferences() const { return (References3D.getSize() > 0); } //has arbitrary or nonzero tolerance bool DrawViewDimension::hasOverUnderTolerance() const { - if (ArbitraryTolerances.getValue() || - !DrawUtil::fpCompare(OverTolerance.getValue(), 0.0) || - !DrawUtil::fpCompare(UnderTolerance.getValue(), 0.0)) { + if (ArbitraryTolerances.getValue() || !DrawUtil::fpCompare(OverTolerance.getValue(), 0.0) + || !DrawUtil::fpCompare(UnderTolerance.getValue(), 0.0)) { return true; } return false; @@ -1466,24 +1505,28 @@ bool DrawViewDimension::hasOverUnderTolerance() const bool DrawViewDimension::showUnits() const { - Base::Reference hGrp = App::GetApplication().GetUserParameter() - .GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("Mod/TechDraw/Dimensions"); + Base::Reference hGrp = App::GetApplication() + .GetUserParameter() + .GetGroup("BaseApp") + ->GetGroup("Preferences") + ->GetGroup("Mod/TechDraw/Dimensions"); return hGrp->GetBool("ShowUnits", false); } -bool DrawViewDimension::useDecimals() const -{ - return Preferences::useGlobalDecimals(); -} +bool DrawViewDimension::useDecimals() const { return Preferences::useGlobalDecimals(); } std::string DrawViewDimension::getPrefixForDimType() const { - if (Type.isValue("Radius")){ + if (Type.isValue("Radius")) { return "R"; - } else if (Type.isValue("Diameter")){ - Base::Reference hGrp = App::GetApplication().GetUserParameter() - .GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("Mod/TechDraw/Dimensions"); - return std::string(hGrp->GetASCII("DiameterSymbol", "\xe2\x8c\x80")); // Diameter symbol + } + else if (Type.isValue("Diameter")) { + Base::Reference hGrp = App::GetApplication() + .GetUserParameter() + .GetGroup("BaseApp") + ->GetGroup("Preferences") + ->GetGroup("Mod/TechDraw/Dimensions"); + return std::string(hGrp->GetASCII("DiameterSymbol", "\xe2\x8c\x80"));// Diameter symbol } return ""; @@ -1504,7 +1547,7 @@ bool DrawViewDimension::isExtentDim() const } -PyObject *DrawViewDimension::getPyObject() +PyObject* DrawViewDimension::getPyObject() { if (PythonObject.is(Py::_None())) { // ref counter is set to 1 diff --git a/src/Mod/TechDraw/App/DrawViewPart.cpp b/src/Mod/TechDraw/App/DrawViewPart.cpp index d277eb5ced..21e93cf9a0 100644 --- a/src/Mod/TechDraw/App/DrawViewPart.cpp +++ b/src/Mod/TechDraw/App/DrawViewPart.cpp @@ -318,34 +318,30 @@ void DrawViewPart::partExec(TopoDS_Shape& shape) //prepare the shape for HLR processing by centering, scaling and rotating it GeometryObjectPtr DrawViewPart::makeGeometryForShape(TopoDS_Shape& shape) { -// Base::Console().Message("DVP::makeGeometryForShape() - %s\n", getNameInDocument()); - gp_Pnt inputCenter = TechDraw::findCentroid(shape, - getProjectionCS()); + // Base::Console().Message("DVP::makeGeometryForShape() - %s\n", getNameInDocument()); + gp_Pnt inputCenter = TechDraw::findCentroid(shape, getProjectionCS()); m_saveCentroid = DU::toVector3d(inputCenter); m_saveShape = centerScaleRotate(this, shape, m_saveCentroid); - GeometryObjectPtr go = buildGeometryObject(shape, getProjectionCS()); + GeometryObjectPtr go = buildGeometryObject(shape, getProjectionCS()); return go; } //Modify a shape by centering, scaling and rotating and return the centered (but not rotated) shape -TopoDS_Shape DrawViewPart::centerScaleRotate(DrawViewPart* dvp, - TopoDS_Shape& inOutShape, +TopoDS_Shape DrawViewPart::centerScaleRotate(DrawViewPart* dvp, TopoDS_Shape& inOutShape, Base::Vector3d centroid) { -// Base::Console().Message("DVP::centerScaleRotate() - %s\n", dvp->getNameInDocument()); + // Base::Console().Message("DVP::centerScaleRotate() - %s\n", dvp->getNameInDocument()); gp_Ax2 viewAxis = dvp->getProjectionCS(); //center shape on origin - TopoDS_Shape centeredShape = TechDraw::moveShape(inOutShape, - centroid * -1.0); + TopoDS_Shape centeredShape = TechDraw::moveShape(inOutShape, centroid * -1.0); inOutShape = TechDraw::scaleShape(centeredShape, dvp->getScale()); if (!DrawUtil::fpCompare(dvp->Rotation.getValue(), 0.0)) { - inOutShape = TechDraw::rotateShape(inOutShape, - viewAxis, - dvp->Rotation.getValue()); //conventional rotation - } -// BRepTools::Write(inOutShape, "DVPScaled.brep"); //debug + inOutShape = TechDraw::rotateShape(inOutShape, viewAxis, + dvp->Rotation.getValue());//conventional rotation + } + // BRepTools::Write(inOutShape, "DVPScaled.brep"); //debug return centeredShape; } @@ -798,7 +794,7 @@ TechDraw::VertexPtr DrawViewPart::getVertex(std::string vertexName) const //! returns existing BaseGeom of 2D Edge TechDraw::BaseGeomPtr DrawViewPart::getEdge(std::string edgeName) const { - const std::vector &geoms = getEdgeGeometry(); + const std::vector& geoms = getEdgeGeometry(); if (geoms.empty()) { //should not happen throw Base::IndexError("DVP::getEdge - No edges found."); @@ -813,7 +809,7 @@ TechDraw::BaseGeomPtr DrawViewPart::getEdge(std::string edgeName) const //! returns existing 2d Face TechDraw::FacePtr DrawViewPart::getFace(std::string faceName) const { - const std::vector &faces = getFaceGeometry(); + const std::vector& faces = getFaceGeometry(); if (faces.empty()) { //should not happen throw Base::IndexError("DVP::getFace - No faces found."); @@ -980,7 +976,7 @@ TopoDS_Shape DrawViewPart::getShape() const builder.Add(result, geometryObject->getVisSmooth()); } } - //check for empty compound + //check for empty compound if (!result.IsNull() && TopoDS_Iterator(result).More()) { return result; } @@ -1141,6 +1137,16 @@ gp_Ax2 DrawViewPart::getProjectionCS(const Base::Vector3d pt) const return viewAxis; } +gp_Ax2 DrawViewPart::getRotatedCS(const Base::Vector3d basePoint) const +{ + // Base::Console().Message("DVP::getRotatedCS() - %s - %s\n", getNameInDocument(), Label.getValue()); + gp_Ax2 unrotated = getProjectionCS(basePoint); + gp_Ax1 rotationAxis(DU::togp_Pnt(basePoint), unrotated.Direction()); + double angleRad = Rotation.getValue() * M_PI / 180.0; + gp_Ax2 rotated = unrotated.Rotated(rotationAxis, -angleRad); + return rotated; +} + gp_Ax2 DrawViewPart::getViewAxis(const Base::Vector3d& pt, const Base::Vector3d& direction, const bool flip) const { diff --git a/src/Mod/TechDraw/App/DrawViewPart.h b/src/Mod/TechDraw/App/DrawViewPart.h index 9ee79b3033..24c08c0fc3 100644 --- a/src/Mod/TechDraw/App/DrawViewPart.h +++ b/src/Mod/TechDraw/App/DrawViewPart.h @@ -114,9 +114,8 @@ public: const char* getViewProviderName() const override { return "TechDrawGui::ViewProviderViewPart"; } PyObject* getPyObject() override; - static TopoDS_Shape centerScaleRotate(DrawViewPart* dvp, - TopoDS_Shape& inOutShape, - Base::Vector3d centroid); + static TopoDS_Shape centerScaleRotate(DrawViewPart* dvp, TopoDS_Shape& inOutShape, + Base::Vector3d centroid); std::vector getHatches() const; std::vector getGeomHatches() const; std::vector getDimensions() const; @@ -134,8 +133,10 @@ public: TechDraw::BaseGeomPtr getEdge(std::string edgeName) const; TechDraw::FacePtr getFace(std::string faceName) const; - TechDraw::BaseGeomPtr getGeomByIndex(int idx) const; //get existing geom for edge idx in projection - TechDraw::VertexPtr getProjVertexByIndex(int idx) const; //get existing geom for vertex idx in projection + TechDraw::BaseGeomPtr + getGeomByIndex(int idx) const;//get existing geom for edge idx in projection + TechDraw::VertexPtr + getProjVertexByIndex(int idx) const;//get existing geom for vertex idx in projection TechDraw::VertexPtr getProjVertexByCosTag(std::string cosTag); std::vector @@ -157,6 +158,7 @@ public: virtual gp_Ax2 getViewAxis(const Base::Vector3d& pt, const Base::Vector3d& direction, const bool flip = true) const; virtual gp_Ax2 getProjectionCS(Base::Vector3d pt = Base::Vector3d(0.0, 0.0, 0.0)) const; + virtual gp_Ax2 getRotatedCS(Base::Vector3d basePoint = Base::Vector3d(0.0, 0.0, 0.0)) const; virtual Base::Vector3d getXDirection() const;//don't use XDirection.getValue() virtual Base::Vector3d getOriginalCentroid() const; virtual Base::Vector3d getCurrentCentroid() const;