File size: 10,788 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 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 | /*
* ********************************************************************************
* This file is part of the LibreCAD project, a 2D CAD program
*
* Copyright (C) 2025 LibreCAD.org
* Copyright (C) 2025 sand1024
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* 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.
* ********************************************************************************
*/
#include<iostream>
#include "lc_tolerance.h"
#include <QRegularExpression>
#include "rs_color.h"
#include "rs_debug.h"
#include "rs_document.h"
#include "rs_filterdxfrw.h"
#include "rs_line.h"
#include "rs_text.h"
#include "rs_units.h"
LC_ToleranceData::~LC_ToleranceData() = default;
LC_Tolerance::LC_Tolerance(RS_EntityContainer* parent, const LC_ToleranceData& d)
:RS_EntityContainer(parent), m_toleranceData(d){
RS_EntityContainer::calculateBorders();
}
RS_Entity* LC_Tolerance::clone() const {
auto* d = new LC_Tolerance(*this);
d->setOwner(isOwner());
d->detach();
return d;
}
void LC_Tolerance::update() {
clear();
if (isUndone()) {
return;
}
doUpdateDim();
calculateBorders();
}
RS_VectorSolutions LC_Tolerance::getRefPoints() const {
return {m_toleranceData.m_insertionPoint};
}
void LC_Tolerance::move(const RS_Vector& offset) {
m_toleranceData.m_insertionPoint.move(offset);
update();
}
void LC_Tolerance::rotate(const RS_Vector& center, double angle) {
m_toleranceData.m_insertionPoint.rotate(center, angle);
update();
}
void LC_Tolerance::rotate(const RS_Vector& center, const RS_Vector& angleVector) {
m_toleranceData.m_insertionPoint.rotate(center, angleVector);
update();
}
void LC_Tolerance::scale(const RS_Vector& center, const RS_Vector& factor) {
m_toleranceData.m_insertionPoint.scale(center, factor);
update();
}
void LC_Tolerance::mirror(const RS_Vector& axisPoint1, const RS_Vector& axisPoint2) {
m_toleranceData.m_insertionPoint.mirror(axisPoint1, axisPoint2);
update();
}
void LC_Tolerance::moveRef(const RS_Vector& ref, const RS_Vector& offset) {
if (ref.distanceTo(m_toleranceData.m_insertionPoint)<1.0e-4) {
m_toleranceData.m_insertionPoint += offset;
update();
}
}
std::ostream& operator<<(std::ostream& os, const LC_ToleranceData& td) {
os << "("
<< td.m_insertionPoint << ','
<< td.m_directionVector << ','
<< td.m_dimStyleName.toLatin1().data() << ','
<< td.m_textCode.toLatin1().data() << ")";
return os;
}
std::ostream& operator<<(std::ostream& os, const LC_Tolerance& d) {
os << " Tolerance: " << d.getData() << "\n";
return os;
}
void LC_Tolerance::doUpdate() {
}
QList<QStringList> LC_Tolerance::getFields() const {
// create list of string lists with field texts:
QList<QStringList> ret;
QStringList lines = m_toleranceData.m_textCode.split("^J");
for (int k=0; k<lines.length(); k++) {
QString line = lines[k];
//qDebug() << "line:" << line;
QStringList lineFields = line.split("%%v");
ret.append(lineFields);
}
return ret;
}
double LC_Tolerance::getTextHeight() {
return getGraphicVariable("$DIMTXT", 2.5, 40);
}
// fixme - sand - temporary method, move to entity?
double LC_Tolerance::getGraphicVariable(const QString& key, double defMM, int code) {
double v = getGraphicVariableDouble(key, RS_MINDOUBLE);
if (v <= RS_MINDOUBLE) {
addGraphicVariable(key, RS_Units::convert(defMM, RS2::Millimeter, getGraphicUnit()), code);
v = getGraphicVariableDouble(key, 1.0);
}
return v;
}
double LC_Tolerance::getDimtxt(bool scale) {
double v = 2.5;
// get value from override:
if (dimtxt>0.0) {
v = dimtxt;
}
else {
v = getTextHeight();
}
if (scale) {
v *= getDimscale();
}
return v;
}
void LC_Tolerance::setDimtxt(double f) {
dimtxt = f;
update();
}
double LC_Tolerance::getGeneralScale() {
return getGraphicVariable("$DIMSCALE", 1.0, 40);
}
double LC_Tolerance::getDimscale() {
// get value from override:
if (dimscale>0.0) {
return dimscale;
}
double v = getGeneralScale();
return v;
}
QString LC_Tolerance::getTextStyle() {
return getGraphicVariableString("$DIMTXSTY", "standard");
}
RS_Color LC_Tolerance::getTextColor() {
return RS_FilterDXFRW::numberToColor(getGraphicVariableInt("$DIMCLRT", 0));
}
RS_Pen LC_Tolerance::getPenForText() {
RS_Pen result(getTextColor(), RS2::WidthByBlock, RS2::SolidLine);
return result;
}
RS_Color LC_Tolerance::getDimensionLineColor() {
return RS_FilterDXFRW::numberToColor(getGraphicVariableInt("$DIMCLRD", 0));
}
RS_Pen LC_Tolerance::getPenForLines() {
// RS_Pen result(RS_Color(RS2::FlagByBlock), RS2::WidthByBlock, RS2::SolidLine);
RS_Pen result(getDimensionLineColor(), RS2::WidthByBlock, RS2::SolidLine);
return result;
}
void LC_Tolerance::doUpdateDim(){
QList<QList<double>> divisionPoints;
createTextLabels(divisionPoints);
createFrameLines(divisionPoints);
divisionPoints.clear();
}
void LC_Tolerance::createTextLabels(QList<QList<double>> &divisions) {
double dimtxt = getDimtxt();
//qDebug() << "text:" << text;
QList<QStringList> fields = getFields();
joinFirstField = false;
// find out if we need to join the first fields of the first two lines:
if (fields.length()>1 && fields[0].length()>0 && fields[1].length()>0) {
QString field1 = fields[0][0];
QString field2 = fields[1][0];
QRegularExpression reg = QRegularExpression("\\\\F[gG][dD][tT];", QRegularExpression::CaseInsensitiveOption);
field1.replace(reg, "\\Fgdt;");
field2.replace(reg, "\\Fgdt;");
if (!field1.isEmpty()) {
joinFirstField = (field1==field2);
}
}
double cursorY = 0;
double angle = m_toleranceData.m_directionVector.angle();
double textAngle = 0.0;
RS_Pen textPen = getPenForText();
for (int k=0; k<fields.length(); k++) {
QStringList fieldsOfLine = fields[k];
double cursorX = dimtxt/2.0;
QList<double> row;
row << 0.0;
divisions.append(row);
// render text strings with distance of dimtxt:
for (int i=0; i<fieldsOfLine.length(); i++) {
QString field = fieldsOfLine[i];
LC_ERR << "field:" << field;
if (field.isEmpty()) {
continue;
}
RS_MTextData textData = RS_MTextData({cursorX, cursorY},
dimtxt, 30.0,
RS_MTextData::VAMiddle,
RS_MTextData::HALeft,
RS_MTextData::LeftToRight,
RS_MTextData::Exact,
1.0,
field,
getTextStyle(),
textAngle);
auto text = new RS_MText(this, textData);
addDimComponentEntity(text, textPen);
// move first symbol of first line down if fields are joined:
if (k==0 && i==0 && joinFirstField) {
text->move({0, -dimtxt});
}
cursorX += text->getUsedTextWidth();
cursorX += dimtxt;
divisions.last().push_back(cursorX - dimtxt/2);
if (k==1 && i==0 && joinFirstField) {
// skip first symbol of second line if fields are joined:
continue;
}
text->rotate({0,0},angle);
text->move(m_toleranceData.m_insertionPoint);
}
if (!divisions.isEmpty() && divisions.last().length() ==1) {
// remove single division line:
divisions.removeLast();
}
else {
cursorY -= dimtxt * 2;
}
}
}
void LC_Tolerance::createFrameLines(QList<QList<double>> &divisions) {
QList<RS_Line> ret;
double dimtxt = getDimtxt();
double offsetY = 0.0;
RS_Vector location = m_toleranceData.m_insertionPoint;
double angle = m_toleranceData.m_directionVector.angle();
RS_Pen linesPen = getPenForLines();
for (int i = 0; i < divisions.length(); i++) {
//qDebug() << "divisions:" << divisions[i];
auto current = divisions[i];
qsizetype currentLength = current.length();
// never show vertical lines for empty rows:
if (currentLength > 1) {
for (int k = 0; k < currentLength; k++) {
double division = current.at(k);
RS_Line* line = addDimComponentLine({division, -dimtxt + offsetY}, {division, dimtxt + offsetY}, linesPen);
line->rotate(angle);
line->move(location);
}
{
// top line of current line:
double startX = current.first();
if (joinFirstField && i == 1 && currentLength > 1) {
startX = current[1];
}
RS_Line* line = addDimComponentLine({startX, dimtxt + offsetY}, {current.last(), dimtxt + offsetY}, linesPen);
line->rotate(angle);
line->move(location);
}
{
// bottom line of current line:
double startX = current.first();
if (joinFirstField && i == 0 && currentLength > 1) {
startX = current[1];
}
RS_Line* line = addDimComponentLine({startX, -dimtxt + offsetY}, {current.last(), -dimtxt + offsetY}, linesPen);
line->rotate(angle);
line->move(location);
}
}
if (!current.isEmpty()) {
offsetY -= dimtxt * 2;
}
}
}
RS_Line* LC_Tolerance::addDimComponentLine(RS_Vector start, RS_Vector end, const RS_Pen &pen) {
auto line = new RS_Line(this, {start, end});
line->setPen(pen);
line->setLayer(nullptr);
addEntity(line);
return line;
}
void LC_Tolerance::addDimComponentEntity(RS_Entity* en, const RS_Pen &pen) {
en->setPen(pen);
en->setLayer(nullptr);
addEntity(en);
}
|