/**************************************************************************** ** ** This file is part of the LibreCAD project, a 2D CAD program ** ** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl) ** Copyright (C) 2001-2003 RibbonSoft. All rights reserved. ** ** ** This file may be distributed and/or modified under the terms of the ** GNU General Public License version 2 as published by the Free Software ** Foundation and appearing in the file gpl-2.0.txt included in the ** packaging of this file. ** ** This program is distributed in the hope that it will be useful, ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** GNU General Public License for more details. ** ** You should have received a copy of the GNU General Public License ** along with this program; if not, write to the Free Software ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA ** ** This copyright notice MUST APPEAR in all copies of the script! ** **********************************************************************/ #include "rs_dimangular.h" #include "rs_constructionline.h" #include "rs_filterdxf.h" #include "rs_graphic.h" #include "rs_information.h" #include "rs_solid.h" #include "rs_text.h" /** * Constructor. * * @para parent Parent Entity Container. * @para d Common dimension geometrical data. * @para ed Extended geometrical data for angular dimension. */ RS_DimAngular::RS_DimAngular(RS_EntityContainer* parent, const RS_DimensionData& d, const RS_DimAngularData& ed) : RS_Dimension(parent, d), edata(ed) { calculateBorders(); } /** * @return Automatically created label for the default * measurement of this dimension. */ QString RS_DimAngular::getMeasuredLabel() { QString ret; int dimaunit = getGraphicVariableInt("$DIMAUNIT", 0); int dimadec = getGraphicVariableInt("$DIMADEC", 0); ret = RS_Units::formatAngle(getAngle(), RS_FilterDXF::numberToAngleFormat(dimaunit), dimadec); /* ret = QString("%1%2") .arg(RS_Math::rad2deg(getAngle())) .arg(QChar(0xB0)); */ return ret; } /** * @return Angle of the measured dimension. */ double RS_DimAngular::getAngle() { double ang1 = 0.0; double ang2 = 0.0; bool reversed = false; RS_Vector p1; RS_Vector p2; getAngles(ang1, ang2, reversed, p1, p2); return reversed ? RS_Math::correctAngle(ang1 - ang2) : RS_Math::correctAngle(ang2 - ang1); // // if (!reversed) { // if (ang2= a1 |p1|^2 + a2 // = a1 + a2 |p2|^2 // a1 = ( |p2|^2 - ) /( |p1|^2 |p2|^2 - ^2) // a2 = ( |p1|^2 - ) /( |p1|^2 |p2|^2 - ^2) double rp1=vp1.squared(); double rp2=vp2.squared(); double p0p1=RS_Vector::dotP(vp0,vp1); double p0p2=RS_Vector::dotP(vp0,vp2); double p1p2=RS_Vector::dotP(vp1,vp2); double d= rp1*rp2 - p1p2*p1p2; double a1=d*(rp2*p0p1-p1p2*p0p2); // we only need the sign, so, use multiply instead of division to avoid divid by zero; if ( a1 >= 0. ) { p1 = edata.definitionPoint2; } else { vp1 *= -1; p1 = edata.definitionPoint1; } a1 = d*(rp1*p0p2-p1p2*p0p1); if ( a1 >= 0. ) { p2 = data.definitionPoint; } else { vp2 *= -1; p2 = edata.definitionPoint3; } RS_Vector center = getCenter(); double ang = center.angleTo(edata.definitionPoint4); ang1=vp1.angle(); ang2=vp2.angle(); if ( ! RS_Math::isAngleBetween(ang, ang1,ang2,false) ) { reversed = true; } return true; } // //std::cout<<"RS_DimAngular::getAngles(): new algorithm, angle1= "<setPen(RS_Pen(RS2::FlagInvalid)); line->setLayer(NULL); addEntity(line); // 2nd extension line: dist = center.distanceTo(p2); len = rad - dist + dimexe; dir.setPolar(1.0, ang2); line = new RS_Line(this, RS_LineData(center + dir*dist + dir*dimexo, center + dir*dist + dir*len)); line->setPen(RS_Pen(RS2::FlagInvalid)); line->setLayer(NULL); addEntity(line); // Create dimension line (arc): RS_Arc* arc = new RS_Arc(this, RS_ArcData(center, rad, ang1, ang2, reversed)); arc->setPen(RS_Pen(RS2::FlagInvalid)); arc->setLayer(NULL); addEntity(arc); // length of dimension arc: double distance = arc->getLength(); // do we have to put the arrows outside of the arc? bool outsideArrows = (distance1.0e-6) { arrowAng = getArrowSize() / rad; } else { arrowAng = 0.0; } RS_Vector v1, v2; if (!arc->isReversed()) { v1.setPolar(rad, arc->getAngle1()+arrowAng); } else { v1.setPolar(rad, arc->getAngle1()-arrowAng); } v1+=arc->getCenter(); arrowAngle1 = arc->getStartpoint().angleTo(v1); if (!arc->isReversed()) { v2.setPolar(rad, arc->getAngle2()-arrowAng); } else { v2.setPolar(rad, arc->getAngle2()+arrowAng); } v2+=arc->getCenter(); arrowAngle2 = arc->getEndpoint().angleTo(v2); if (!outsideArrows) { arrowAngle1 = arrowAngle1+M_PI; arrowAngle2 = arrowAngle2+M_PI; } // Arrows: RS_SolidData sd; RS_Solid* arrow; // arrow 1 arrow = new RS_Solid(this, sd); arrow->shapeArrow(arc->getStartpoint(), arrowAngle1, getArrowSize()); arrow->setPen(RS_Pen(RS2::FlagInvalid)); arrow->setLayer(NULL); addEntity(arrow); // arrow 2: arrow = new RS_Solid(this, sd); arrow->shapeArrow(arc->getEndpoint(), arrowAngle2, getArrowSize()); arrow->setPen(RS_Pen(RS2::FlagInvalid)); arrow->setLayer(NULL); addEntity(arrow); // text label: RS_TextData textData; RS_Vector textPos = arc->getMiddlePoint(); RS_Vector distV; double textAngle; double dimAngle1 = textPos.angleTo(arc->getCenter())-M_PI/2.0; // rotate text so it's readable from the bottom or right (ISO) // quadrant 1 & 4 if (dimAngle1>M_PI/2.0*3.0+0.001 || dimAngle1setPen(RS_Pen(RS2::FlagInvalid)); text->setLayer(NULL); addEntity(text); calculateBorders(); } void RS_DimAngular::move(const RS_Vector& offset) { RS_Dimension::move(offset); edata.definitionPoint1.move(offset); edata.definitionPoint2.move(offset); edata.definitionPoint3.move(offset); edata.definitionPoint4.move(offset); update(); } void RS_DimAngular::rotate(const RS_Vector& center, const double& angle) { rotate(center, RS_Vector(angle)); } void RS_DimAngular::rotate(const RS_Vector& center, const RS_Vector& angleVector) { RS_Dimension::rotate(center, angleVector); edata.definitionPoint1.rotate(center, angleVector); edata.definitionPoint2.rotate(center, angleVector); edata.definitionPoint3.rotate(center, angleVector); edata.definitionPoint4.rotate(center, angleVector); update(); } void RS_DimAngular::scale(const RS_Vector& center, const RS_Vector& factor) { RS_Dimension::scale(center, factor); edata.definitionPoint1.scale(center, factor); edata.definitionPoint2.scale(center, factor); edata.definitionPoint3.scale(center, factor); edata.definitionPoint4.scale(center, factor); update(); } void RS_DimAngular::mirror(const RS_Vector& axisPoint1, const RS_Vector& axisPoint2) { RS_Dimension::mirror(axisPoint1, axisPoint2); edata.definitionPoint1.mirror(axisPoint1, axisPoint2); edata.definitionPoint2.mirror(axisPoint1, axisPoint2); edata.definitionPoint3.mirror(axisPoint1, axisPoint2); edata.definitionPoint4.mirror(axisPoint1, axisPoint2); update(); } /** * Dumps the point's data to stdout. */ std::ostream& operator << (std::ostream& os, const RS_DimAngular& d) { os << " DimAngular: " << d.getData() << "\n" << d.getEData() << "\n"; return os; }