File size: 12,426 Bytes
a5ffdcd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | /****************************************************************************
**
** 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<iostream>
#include "rs_dimlinear.h"
#include "rs_constructionline.h"
#include "rs_debug.h"
#include "rs_graphic.h"
#include "rs_line.h"
#include "rs_math.h"
#include "rs_settings.h"
#include "rs_units.h"
RS_DimLinearData::RS_DimLinearData():
extensionPoint1(false),
extensionPoint2(false),
angle(0.0),
oblique(0.0)
{}
RS_DimLinearData::RS_DimLinearData(const RS_DimLinearData& other):
extensionPoint1{other.extensionPoint1},
extensionPoint2{other.extensionPoint2},
angle{other.angle},
oblique{other.oblique} {
}
RS_DimLinearData::RS_DimLinearData(const RS_Vector& _extensionPoint1,
const RS_Vector& _extensionPoint2,
double _angle, double _oblique):
extensionPoint1(_extensionPoint1)
,extensionPoint2(_extensionPoint2)
,angle(_angle)
,oblique(_oblique){
}
RS_DimLinearData::~RS_DimLinearData() {
}
std::ostream& operator << (std::ostream& os,
const RS_DimLinearData& dd) {
os << "(" << dd.extensionPoint1 << ","
<< dd.extensionPoint1 <<','
<< dd.angle <<','
<< dd.oblique <<','
<<")";
return os;
}
/**
* Constructor.
*
* @para parent Parent Entity Container.
* @para d Common dimension geometrical data.
* @para ed Extended geometrical data for linear dimension.
*/
RS_DimLinear::RS_DimLinear(RS_EntityContainer* parent, const RS_DimensionData& d, const RS_DimLinearData& ed)
: RS_Dimension(parent, d), m_dimLinearData(ed) {
}
RS_DimLinear::RS_DimLinear(const RS_DimLinear& entity)
: RS_Dimension(entity), m_dimLinearData(entity.m_dimLinearData) {
}
RS_Entity* RS_DimLinear::clone() const {
auto* d = new RS_DimLinear(*this);
return d;
}
RS_VectorSolutions RS_DimLinear::getRefPoints() const{
return RS_VectorSolutions({m_dimLinearData.extensionPoint1, m_dimLinearData.extensionPoint2,
m_dimGenericData.definitionPoint, m_dimGenericData.middleOfText});
}
void RS_DimLinear::setAngle(double a) {
m_dimLinearData.angle = RS_Math::correctAngle(a);
}
/**
* Updates the sub entities of this dimension. Called when the
* text or the position, alignment, .. changes.
*
* @param autoText Automatically reposition the text label
*/
void RS_DimLinear::doUpdateDim() {
RS_DEBUG->print("RS_DimLinear::update");
double dimscale = getGeneralScale();
double dimexo = getExtensionLineOffset()*dimscale;
double dimexe = getExtensionLineExtension()*dimscale;
// direction of dimension line
RS_Vector dirDim = RS_Vector::polar(100.0, m_dimLinearData.angle);
// construction line for dimension line
RS_ConstructionLine dimLine(m_dimGenericData.definitionPoint, m_dimGenericData.definitionPoint + dirDim);
RS_Vector dimP1 = dimLine.getNearestPointOnEntity(m_dimLinearData.extensionPoint1);
RS_Vector dimP2 = dimLine.getNearestPointOnEntity(m_dimLinearData.extensionPoint2);
auto extLineStyle = m_dimStyleTransient->extensionLine();
bool showExtLine1 = extLineStyle->suppressFirstLine() == LC_DimStyle::ExtensionLine::DONT_SUPPRESS;
bool showExtLine2 = extLineStyle->suppressSecondLine() == LC_DimStyle::ExtensionLine::DONT_SUPPRESS;
auto dimLineStyle = m_dimStyleTransient->dimensionLine();
bool showDimLine1 = dimLineStyle->suppressFirstLine() == LC_DimStyle::DimensionLine::DONT_SUPPRESS;
bool showArrow1 = showExtLine1 && showDimLine1;
bool showDimLine2 = dimLineStyle->suppressSecondLine() == LC_DimStyle::DimensionLine::DONT_SUPPRESS;
bool showArrow2 = showExtLine2 && showDimLine2;
// Definitive dimension line:
createDimensionLine(dimP1, dimP2, showArrow1, showArrow2, showDimLine1, showDimLine2, m_dimGenericData.autoText);
double extAngle1, extAngle2;
double extensionLine1Length = (m_dimLinearData.extensionPoint1-dimP1).magnitude();
bool extensionLine1NonZero = extensionLine1Length<1e-6;
bool extensionLine2NonZero = (m_dimLinearData.extensionPoint2-dimP2).magnitude()<1e-6;
if (extensionLine1NonZero) {
if (extensionLine2NonZero) {
//boot extension points are in dimension line only rotate 90
extAngle2 = m_dimLinearData.angle + (M_PI_2);
} else {
//first extension point are in dimension line use second
extAngle2 = m_dimLinearData.extensionPoint2.angleTo(dimP2);
}
extAngle1 = extAngle2;
} else {
//first extension point not are in dimension line use it
extAngle1 = m_dimLinearData.extensionPoint1.angleTo(dimP1);
if (extensionLine2NonZero) {
extAngle2 = extAngle1;
}
else {
extAngle2 = m_dimLinearData.extensionPoint2.angleTo(dimP2);
}
}
RS_Vector vDimexe1 = RS_Vector::polar(dimexe, extAngle1);
RS_Vector vDimexe2 = RS_Vector::polar(dimexe, extAngle2);
RS_Vector vDimexo1, vDimexo2;
if (getFixedLengthOn()){
double dimfxl = getFixedLength()*dimscale;
double extLength = extensionLine1Length;
if (extLength-dimexo > dimfxl) {
vDimexo1.setPolar(extLength - dimfxl, extAngle1);
}
extLength = (m_dimLinearData.extensionPoint2-dimP2).magnitude();
if (extLength-dimexo > dimfxl) {
vDimexo2.setPolar(extLength - dimfxl, extAngle2);
}
} else {
vDimexo1.setPolar(dimexo, extAngle1);
vDimexo2.setPolar(dimexo, extAngle2);
}
// extension lines:
if (showExtLine1) {
addDimExtensionLine(m_dimLinearData.extensionPoint1+vDimexo1, dimP1+vDimexe1, true);
}
if (showExtLine2) {
addDimExtensionLine(m_dimLinearData.extensionPoint2+vDimexo2, dimP2+vDimexe2, false);
}
}
/**
* @return Automatically created label for the default
* measurement of this dimension.
*/
QString RS_DimLinear::getMeasuredLabel() {
// direction of dimension line
RS_Vector dirDim = RS_Vector::polar(100.0, m_dimLinearData.angle);
// construction line for dimension line
RS_ConstructionLine dimLine(nullptr,
RS_ConstructionLineData(m_dimGenericData.definitionPoint,
m_dimGenericData.definitionPoint + dirDim));
RS_Vector dimP1 = dimLine.getNearestPointOnEntity(m_dimLinearData.extensionPoint1);
RS_Vector dimP2 = dimLine.getNearestPointOnEntity(m_dimLinearData.extensionPoint2);
// Definitive dimension line:
double distance = dimP1.distanceTo(dimP2);
double dist = prepareLabelLinearDistance(distance);
QString distanceLabel = createLinearMeasuredLabel(dist);
return distanceLabel;
}
bool RS_DimLinear::hasEndpointsWithinWindow(const RS_Vector& v1, const RS_Vector& v2) const {
return (m_dimLinearData.extensionPoint1.isInWindow(v1, v2) ||
m_dimLinearData.extensionPoint2.isInWindow(v1, v2));
}
void RS_DimLinear::move(const RS_Vector& offset) {
RS_Dimension::move(offset);
m_dimLinearData.extensionPoint1.move(offset);
m_dimLinearData.extensionPoint2.move(offset);
update();
}
void RS_DimLinear::rotate(const RS_Vector& center, double angle) {
RS_Vector angleVector(angle);
RS_Dimension::rotate(center, angleVector);
m_dimLinearData.extensionPoint1.rotate(center, angleVector);
m_dimLinearData.extensionPoint2.rotate(center, angleVector);
m_dimLinearData.angle = RS_Math::correctAngle(m_dimLinearData.angle+angle);
update();
}
void RS_DimLinear::rotate(const RS_Vector& center, const RS_Vector& angleVector) {
RS_Dimension::rotate(center, angleVector);
m_dimLinearData.extensionPoint1.rotate(center, angleVector);
m_dimLinearData.extensionPoint2.rotate(center, angleVector);
m_dimLinearData.angle = RS_Math::correctAngle(m_dimLinearData.angle+angleVector.angle());
update();
}
void RS_DimLinear::scale(const RS_Vector& center, const RS_Vector& factor) {
RS_Dimension::scale(center, factor);
m_dimLinearData.extensionPoint1.scale(center, factor);
m_dimLinearData.extensionPoint2.scale(center, factor);
update();
}
void RS_DimLinear::getDimPoints(RS_Vector& dimP1, RS_Vector& dimP2){
RS_Vector dirDim = RS_Vector::polar(100.0, m_dimLinearData.angle);
// construction line for dimension line
RS_ConstructionLine dimLine(nullptr,RS_ConstructionLineData(m_dimGenericData.definitionPoint,m_dimGenericData.definitionPoint + dirDim));
dimP1 = dimLine.getNearestPointOnEntity(m_dimLinearData.extensionPoint1);
dimP2 = dimLine.getNearestPointOnEntity(m_dimLinearData.extensionPoint2);
}
void RS_DimLinear::mirror(const RS_Vector& axisPoint1, const RS_Vector& axisPoint2) {
RS_Dimension::mirror(axisPoint1, axisPoint2);
m_dimLinearData.extensionPoint1.mirror(axisPoint1, axisPoint2);
m_dimLinearData.extensionPoint2.mirror(axisPoint1, axisPoint2);
RS_Vector vec;
vec.setPolar(1.0, m_dimLinearData.angle);
vec.mirror(RS_Vector(0.0,0.0), axisPoint2-axisPoint1);
m_dimLinearData.angle = vec.angle();
update();
}
void RS_DimLinear::stretch(const RS_Vector& firstCorner,
const RS_Vector& secondCorner,
const RS_Vector& offset) {
//e->calculateBorders();
if (getMin().isInWindow(firstCorner, secondCorner) && getMax().isInWindow(firstCorner, secondCorner)) {
move(offset);
} else {
//RS_Vector v = data.definitionPoint - edata.extensionPoint2;
//double len = edata.extensionPoint2.distanceTo(data.definitionPoint);
//double ang1 = edata.extensionPoint1.angleTo(edata.extensionPoint2)
// + M_PI_2;
if (m_dimLinearData.extensionPoint1.isInWindow(firstCorner,secondCorner)) {
m_dimLinearData.extensionPoint1.move(offset);
}
if (m_dimLinearData.extensionPoint2.isInWindow(firstCorner,secondCorner)) {
m_dimLinearData.extensionPoint2.move(offset);
}
/*
double ang2 = edata.extensionPoint1.angleTo(edata.extensionPoint2)
+ M_PI_2;
double diff = RS_Math::getAngleDifference(ang1, ang2);
if (diff>M_PI) {
diff-=2*M_PI;
}
if (fabs(diff)>M_PI_2) {
ang2 = RS_Math::correctAngle(ang2+M_PI);
}
RS_Vector v;
v.setPolar(len, ang2);
data.definitionPoint = edata.extensionPoint2 + v;
*/
}
updateDim(true);
}
void RS_DimLinear::moveRef(const RS_Vector& ref, const RS_Vector& offset) {
if (ref.distanceTo(m_dimGenericData.definitionPoint)<1.0e-4) {
m_dimGenericData.definitionPoint += offset;
updateDim(true);
}
else if (ref.distanceTo(m_dimGenericData.middleOfText)<1.0e-4) {
m_dimGenericData.middleOfText += offset;
updateDim(false);
}
else if (ref.distanceTo(m_dimLinearData.extensionPoint1)<1.0e-4) {
m_dimLinearData.extensionPoint1 += offset;
updateDim(true);
}
else if (ref.distanceTo(m_dimLinearData.extensionPoint2)<1.0e-4) {
m_dimLinearData.extensionPoint2 += offset;
updateDim(true);
}
}
/**
* Dumps the point's data to stdout.
*/
std::ostream& operator << (std::ostream& os, const RS_DimLinear& d) {
os << " DimLinear: " << d.getData() << "\n" << d.getEData() << "\n";
return os;
}
|