1 /****************************************************************************
2 **
3 ** This file is part of the LibreCAD project, a 2D CAD program
4 **
5 ** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
6 ** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
7 **
8 **
9 ** This file may be distributed and/or modified under the terms of the
10 ** GNU General Public License version 2 as published by the Free Software
11 ** Foundation and appearing in the file gpl-2.0.txt included in the
12 ** packaging of this file.
13 **
14 ** This program is distributed in the hope that it will be useful,
15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ** GNU General Public License for more details.
18 **
19 ** You should have received a copy of the GNU General Public License
20 ** along with this program; if not, write to the Free Software
21 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 **
23 ** This copyright notice MUST APPEAR in all copies of the script!
24 **
25 **********************************************************************/
26 
27 #include<iostream>
28 #include<cmath>
29 #include "rs_dimlinear.h"
30 #include "rs_line.h"
31 #include "rs_constructionline.h"
32 #include "rs_mtext.h"
33 #include "rs_solid.h"
34 #include "rs_graphic.h"
35 #include "rs_math.h"
36 #include "rs_debug.h"
37 
38 
RS_DimLinearData()39 RS_DimLinearData::RS_DimLinearData():
40 	extensionPoint1(false),
41 	extensionPoint2(false),
42 	angle(0.0),
43 	oblique(0.0)
44 {}
45 
RS_DimLinearData(const RS_Vector & _extensionPoint1,const RS_Vector & _extensionPoint2,double _angle,double _oblique)46 RS_DimLinearData::RS_DimLinearData(const RS_Vector& _extensionPoint1,
47 				 const RS_Vector& _extensionPoint2,
48 				 double _angle, double _oblique):
49 	extensionPoint1(_extensionPoint1)
50 	,extensionPoint2(_extensionPoint2)
51 	,angle(_angle)
52 	,oblique(_oblique)
53 {
54 }
55 
operator <<(std::ostream & os,const RS_DimLinearData & dd)56 std::ostream& operator << (std::ostream& os,
57 								  const RS_DimLinearData& dd) {
58 	os << "(" << dd.extensionPoint1 << ","
59 	   << dd.extensionPoint1 <<','
60 	   << dd.angle <<','
61 	   << dd.oblique <<','
62 		  <<")";
63 	return os;
64 }
65 
66 /**
67  * Constructor.
68  *
69  * @para parent Parent Entity Container.
70  * @para d Common dimension geometrical data.
71  * @para ed Extended geometrical data for linear dimension.
72  */
RS_DimLinear(RS_EntityContainer * parent,const RS_DimensionData & d,const RS_DimLinearData & ed)73 RS_DimLinear::RS_DimLinear(RS_EntityContainer* parent,
74                            const RS_DimensionData& d,
75                            const RS_DimLinearData& ed)
76         : RS_Dimension(parent, d), edata(ed) {
77 
78     calculateBorders();
79 }
80 
clone() const81 RS_Entity* RS_DimLinear::clone() const {
82 	RS_DimLinear* d = new RS_DimLinear(*this);
83 	d->setOwner(isOwner());
84 	d->initId();
85 	d->detach();
86 	return d;
87 }
88 
getRefPoints() const89 RS_VectorSolutions RS_DimLinear::getRefPoints() const
90 {
91 		return RS_VectorSolutions({edata.extensionPoint1, edata.extensionPoint2,
92 												data.definitionPoint, data.middleOfText});
93 }
setAngle(double a)94 void RS_DimLinear::setAngle(double a) {
95 	edata.angle = RS_Math::correctAngle(a);
96 }
97 
98 
99 /**
100  * @return Automatically created label for the default
101  * measurement of this dimension.
102  */
getMeasuredLabel()103 QString RS_DimLinear::getMeasuredLabel() {
104     // direction of dimension line
105 	RS_Vector dirDim = RS_Vector::polar(100.0, edata.angle);
106 
107     // construction line for dimension line
108 	RS_ConstructionLine dimLine(nullptr,
109 								RS_ConstructionLineData(data.definitionPoint,
110 														data.definitionPoint + dirDim));
111 
112     RS_Vector dimP1 = dimLine.getNearestPointOnEntity(edata.extensionPoint1);
113     RS_Vector dimP2 = dimLine.getNearestPointOnEntity(edata.extensionPoint2);
114 
115     // Definitive dimension line:
116     double dist = dimP1.distanceTo(dimP2) * getGeneralFactor();
117 
118         RS_Graphic* graphic = getGraphic();
119 
120     QString ret;
121         if (graphic) {
122             int dimlunit = getGraphicVariableInt("$DIMLUNIT", 2);
123             int dimdec = getGraphicVariableInt("$DIMDEC", 4);
124             int dimzin = getGraphicVariableInt("$DIMZIN", 1);
125             RS2::LinearFormat format = graphic->getLinearFormat(dimlunit);
126             ret = RS_Units::formatLinear(dist, RS2::None, format, dimdec);
127             if (format == RS2::Decimal)
128                 ret = stripZerosLinear(ret, dimzin);
129             //verify if units are decimal and comma separator
130             if (format == RS2::Decimal || format == RS2::ArchitecturalMetric){
131                 if (getGraphicVariableInt("$DIMDSEP", 0) == 44)
132                     ret.replace(QChar('.'), QChar(','));
133             }
134         }
135         else {
136         ret = QString("%1").arg(dist);
137         }
138 
139     return ret;
140 }
141 
142 
143 
hasEndpointsWithinWindow(const RS_Vector & v1,const RS_Vector & v2)144 bool RS_DimLinear::hasEndpointsWithinWindow(const RS_Vector& v1, const RS_Vector& v2) {
145         return (edata.extensionPoint1.isInWindow(v1, v2) ||
146                 edata.extensionPoint2.isInWindow(v1, v2));
147 }
148 
149 
150 
151 /**
152  * Updates the sub entities of this dimension. Called when the
153  * text or the position, alignment, .. changes.
154  *
155  * @param autoText Automatically reposition the text label
156  */
updateDim(bool autoText)157 void RS_DimLinear::updateDim(bool autoText) {
158 
159     RS_DEBUG->print("RS_DimLinear::update");
160 
161     clear();
162 
163     if (isUndone()) {
164         return;
165     }
166 
167     // general scale (DIMSCALE)
168     double dimscale = getGeneralScale();
169     // distance from entities (DIMEXO)
170     double dimexo = getExtensionLineOffset()*dimscale;
171     // extension line extension (DIMEXE)
172     double dimexe = getExtensionLineExtension()*dimscale;
173 
174     // direction of dimension line
175 	RS_Vector dirDim = RS_Vector::polar(100.0, edata.angle);
176 
177     // construction line for dimension line
178     RS_ConstructionLine dimLine(
179 		nullptr,
180 		RS_ConstructionLineData(data.definitionPoint,
181 								data.definitionPoint + dirDim));
182 
183     RS_Vector dimP1 = dimLine.getNearestPointOnEntity(edata.extensionPoint1);
184     RS_Vector dimP2 = dimLine.getNearestPointOnEntity(edata.extensionPoint2);
185 
186     // Definitive dimension line:
187     updateCreateDimensionLine(dimP1, dimP2, true, true, autoText);
188     /*
189     ld = RS_LineData(data.definitionPoint, dimP1);
190     RS_Line* dimensionLine = new RS_Line(this, ld);
191        addEntity(dimensionLine);
192     */
193 
194     double extAngle1, extAngle2;
195 
196     if ((edata.extensionPoint1-dimP1).magnitude()<1e-6) {
197         if ((edata.extensionPoint2-dimP2).magnitude()<1e-6) {
198             //boot extension points are in dimension line only rotate 90
199 			extAngle2 = edata.angle + (M_PI_2);
200         } else {
201             //first extension point are in dimension line use second
202             extAngle2 = edata.extensionPoint2.angleTo(dimP2);
203         }
204             extAngle1 = extAngle2;
205     } else {
206         //first extension point not are in dimension line use it
207         extAngle1 = edata.extensionPoint1.angleTo(dimP1);
208         if ((edata.extensionPoint2-dimP2).magnitude()<1e-6)
209             extAngle2 = extAngle1;
210         else
211             extAngle2 = edata.extensionPoint2.angleTo(dimP2);
212     }
213 
214     RS_Vector vDimexe1 = RS_Vector::polar(dimexe, extAngle1);
215     RS_Vector vDimexe2 = RS_Vector::polar(dimexe, extAngle2);
216 
217     RS_Vector vDimexo1, vDimexo2;
218     if (getFixedLengthOn()){
219         double dimfxl = getFixedLength()*dimscale;
220         double extLength = (edata.extensionPoint1-dimP1).magnitude();
221         if (extLength-dimexo > dimfxl)
222             vDimexo1.setPolar(extLength - dimfxl, extAngle1);
223         extLength = (edata.extensionPoint2-dimP2).magnitude();
224         if (extLength-dimexo > dimfxl)
225             vDimexo2.setPolar(extLength - dimfxl, extAngle2);
226     } else {
227         vDimexo1.setPolar(dimexo, extAngle1);
228         vDimexo2.setPolar(dimexo, extAngle2);
229     }
230 
231     RS_Pen pen(getExtensionLineColor(),
232            getExtensionLineWidth(),
233            RS2::LineByBlock);
234 
235     // extension lines:
236 	RS_Line* line = new RS_Line{this,
237 			edata.extensionPoint1+vDimexo1, dimP1+vDimexe1};
238     line->setPen(pen);
239 //    line->setPen(RS_Pen(RS2::FlagInvalid));
240 	line->setLayer(nullptr);
241     addEntity(line);
242     //data.definitionPoint+vDimexe2);
243 	line = new RS_Line{this,
244 			edata.extensionPoint2+vDimexo2, dimP2+vDimexe2};
245     line->setPen(pen);
246 //    line->setPen(RS_Pen(RS2::FlagInvalid));
247 	line->setLayer(nullptr);
248     addEntity(line);
249 
250     calculateBorders();
251 }
252 
move(const RS_Vector & offset)253 void RS_DimLinear::move(const RS_Vector& offset) {
254     RS_Dimension::move(offset);
255 
256     edata.extensionPoint1.move(offset);
257     edata.extensionPoint2.move(offset);
258     update();
259 }
260 
261 
262 
rotate(const RS_Vector & center,const double & angle)263 void RS_DimLinear::rotate(const RS_Vector& center, const double& angle) {
264     RS_Vector angleVector(angle);
265     RS_Dimension::rotate(center, angleVector);
266 
267     edata.extensionPoint1.rotate(center, angleVector);
268     edata.extensionPoint2.rotate(center, angleVector);
269     edata.angle = RS_Math::correctAngle(edata.angle+angle);
270     update();
271 }
272 
273 
rotate(const RS_Vector & center,const RS_Vector & angleVector)274 void RS_DimLinear::rotate(const RS_Vector& center, const RS_Vector& angleVector) {
275     RS_Dimension::rotate(center, angleVector);
276 
277     edata.extensionPoint1.rotate(center, angleVector);
278     edata.extensionPoint2.rotate(center, angleVector);
279     edata.angle = RS_Math::correctAngle(edata.angle+angleVector.angle());
280     update();
281 }
282 
283 
284 
scale(const RS_Vector & center,const RS_Vector & factor)285 void RS_DimLinear::scale(const RS_Vector& center, const RS_Vector& factor) {
286     RS_Dimension::scale(center, factor);
287 
288     edata.extensionPoint1.scale(center, factor);
289     edata.extensionPoint2.scale(center, factor);
290     update();
291 }
292 
293 
294 
mirror(const RS_Vector & axisPoint1,const RS_Vector & axisPoint2)295 void RS_DimLinear::mirror(const RS_Vector& axisPoint1, const RS_Vector& axisPoint2) {
296     RS_Dimension::mirror(axisPoint1, axisPoint2);
297 
298     edata.extensionPoint1.mirror(axisPoint1, axisPoint2);
299     edata.extensionPoint2.mirror(axisPoint1, axisPoint2);
300 
301     RS_Vector vec;
302     vec.setPolar(1.0, edata.angle);
303     vec.mirror(RS_Vector(0.0,0.0), axisPoint2-axisPoint1);
304     edata.angle = vec.angle();
305 
306     update();
307 }
308 
309 
310 
stretch(const RS_Vector & firstCorner,const RS_Vector & secondCorner,const RS_Vector & offset)311 void RS_DimLinear::stretch(const RS_Vector& firstCorner,
312                            const RS_Vector& secondCorner,
313                            const RS_Vector& offset) {
314 
315     //e->calculateBorders();
316     if (getMin().isInWindow(firstCorner, secondCorner) &&
317             getMax().isInWindow(firstCorner, secondCorner)) {
318 
319         move(offset);
320     } else {
321         //RS_Vector v = data.definitionPoint - edata.extensionPoint2;
322         //double len = edata.extensionPoint2.distanceTo(data.definitionPoint);
323         //double ang1 = edata.extensionPoint1.angleTo(edata.extensionPoint2)
324 		//              + M_PI_2;
325 
326         if (edata.extensionPoint1.isInWindow(firstCorner,
327                                             secondCorner)) {
328             edata.extensionPoint1.move(offset);
329         }
330         if (edata.extensionPoint2.isInWindow(firstCorner,
331                                             secondCorner)) {
332             edata.extensionPoint2.move(offset);
333         }
334 
335                 /*
336         double ang2 = edata.extensionPoint1.angleTo(edata.extensionPoint2)
337 					  + M_PI_2;
338 
339         double diff = RS_Math::getAngleDifference(ang1, ang2);
340         if (diff>M_PI) {
341             diff-=2*M_PI;
342         }
343 
344 		if (fabs(diff)>M_PI_2) {
345             ang2 = RS_Math::correctAngle(ang2+M_PI);
346         }
347 
348         RS_Vector v;
349         v.setPolar(len, ang2);
350         data.definitionPoint = edata.extensionPoint2 + v;
351                 */
352     }
353     updateDim(true);
354 }
355 
356 
357 
moveRef(const RS_Vector & ref,const RS_Vector & offset)358 void RS_DimLinear::moveRef(const RS_Vector& ref, const RS_Vector& offset) {
359 
360 	if (ref.distanceTo(data.definitionPoint)<1.0e-4) {
361 		data.definitionPoint += offset;
362                 updateDim(true);
363     }
364         else if (ref.distanceTo(data.middleOfText)<1.0e-4) {
365         data.middleOfText += offset;
366                 updateDim(false);
367     }
368         else if (ref.distanceTo(edata.extensionPoint1)<1.0e-4) {
369         edata.extensionPoint1 += offset;
370                 updateDim(true);
371     }
372         else if (ref.distanceTo(edata.extensionPoint2)<1.0e-4) {
373         edata.extensionPoint2 += offset;
374                 updateDim(true);
375     }
376 }
377 
378 /**
379  * Dumps the point's data to stdout.
380  */
operator <<(std::ostream & os,const RS_DimLinear & d)381 std::ostream& operator << (std::ostream& os, const RS_DimLinear& d) {
382     os << " DimLinear: " << d.getData() << "\n" << d.getEData() << "\n";
383     return os;
384 }
385