void RS_GraphicView::zoomAutoY(bool axis) { if (container) { double visibleHeight = 0.0; double minY = RS_MAXDOUBLE; double maxY = RS_MINDOUBLE; bool noChange = false; for(auto e: *container){ if (e->rtti()==RS2::EntityLine) { RS_Line* l = (RS_Line*)e; double x1, x2; x1 = toGuiX(l->getStartpoint().x); x2 = toGuiX(l->getEndpoint().x); if ( ((x1 > 0.0) && (x1 < (double) getWidth())) || ((x2 > 0.0) && (x2 < (double) getWidth()))) { minY = std::min(minY, l->getStartpoint().y); minY = std::min(minY, l->getEndpoint().y); maxY = std::max(maxY, l->getStartpoint().y); maxY = std::max(maxY, l->getEndpoint().y); } } } if (axis) { visibleHeight = std::max(maxY, 0.0) - std::min(minY, 0.0); } else { visibleHeight = maxY-minY; } if (visibleHeight<1.0) { noChange = true; } double fy = 1.0; if (visibleHeight>1.0e-6) { fy = (getHeight()-borderTop-borderBottom) / visibleHeight; if (factor.y<0.000001) { noChange = true; } } if (noChange==false) { setFactorY(fy); //centerOffsetY(); offsetY = (int)((getHeight()-borderTop-borderBottom - (visibleHeight*factor.y))/2.0 - (minY*factor.y)) + borderBottom; adjustOffsetControls(); adjustZoomControls(); // updateGrid(); } RS_DEBUG->print("Auto zoom y ok"); } }
/** * Creates an entity parallel to the given entity e through the given * 'coord'. * * @param coord Coordinate to define the distance / side (typically a * mouse coordinate). * @param number Number of parallels. * @param e Original entity. * * @return Pointer to the first created parallel or nullptr if no * parallel has been created. */ RS_Entity* RS_Creation::createParallelThrough(const RS_Vector& coord, int number, RS_Entity* e) { if (!e) { return nullptr; } double dist; if (e->rtti()==RS2::EntityLine) { RS_Line* l = (RS_Line*)e; RS_ConstructionLine cl(nullptr, RS_ConstructionLineData(l->getStartpoint(), l->getEndpoint())); dist = cl.getDistanceToPoint(coord); } else { dist = e->getDistanceToPoint(coord); } if (dist<RS_MAXDOUBLE) { return createParallel(coord, dist, number, e); } else { return nullptr; } }
RS_Vector RS_Ellipse::getNearestOrthTan(const RS_Vector& coord, const RS_Line& normal, bool onEntity ) { if ( !coord.valid ) { return RS_Vector(false); } RS_Vector direction=normal.getEndpoint() - normal.getStartpoint(); if (direction.squared()< RS_TOLERANCE*RS_TOLERANCE) { //undefined direction return RS_Vector(false); } //scale to ellipse angle RS_Vector aV(-getAngle()); direction.rotate(aV); double angle=direction.scale(RS_Vector(1.,getRatio())).angle(); double ra(getMajorRadius()); direction.set(ra*cos(angle),getRatio()*ra*sin(angle));//relative to center QList<RS_Vector> sol; for(int i=0;i<2;i++){ if(!onEntity || RS_Math::isAngleBetween(angle,getAngle1(),getAngle2(),isReversed())) { if(i){ sol.append(- direction); }else{ sol.append(direction); } } angle=RS_Math::correctAngle(angle+M_PI); } if(sol.size()<1) return RS_Vector(false); aV.y*=-1.; for(int i=0;i<sol.size();i++) sol[i].rotate(aV); RS_Vector vp; switch(sol.count()) { case 0: return RS_Vector(false); case 2: if( RS_Vector::dotP(sol[1],coord-getCenter())>0.) { vp=sol[1]; break; } default: vp=sol[0]; } return getCenter() + vp; }
/** * Checks if the given coordinate is inside the given contour. * * @param point Coordinate to check. * @param contour One or more entities which shape a contour. * If the given contour is not closed, the result is undefined. * The entities don't need to be in a specific order. * @param onContour Will be set to true if the given point it exactly * on the contour. */ bool RS_Information::isPointInsideContour(const RS_Vector& point, RS_EntityContainer* contour, bool* onContour) { if (contour==NULL) { RS_DEBUG->print(RS_Debug::D_WARNING, "RS_Information::isPointInsideContour: contour is NULL"); return false; } if (point.x < contour->getMin().x || point.x > contour->getMax().x || point.y < contour->getMin().y || point.y > contour->getMax().y) { return false; } double width = contour->getSize().x+1.0; bool sure; int counter; int tries = 0; double rayAngle = 0.0; do { sure = true; // create ray: RS_Vector v; v.setPolar(width*10.0, rayAngle); RS_Line ray(NULL, RS_LineData(point, point+v)); counter = 0; RS_VectorSolutions sol; if (onContour!=NULL) { *onContour = false; } for (RS_Entity* e = contour->firstEntity(RS2::ResolveAll); e!=NULL; e = contour->nextEntity(RS2::ResolveAll)) { // intersection(s) from ray with contour entity: sol = RS_Information::getIntersection(&ray, e, true); for (int i=0; i<=1; ++i) { RS_Vector p = sol.get(i); if (p.valid) { // point is on the contour itself if (p.distanceTo(point)<1.0e-5) { if (onContour!=NULL) { *onContour = true; } } else { if (e->rtti()==RS2::EntityLine) { RS_Line* line = (RS_Line*)e; // ray goes through startpoint of line: if (p.distanceTo(line->getStartpoint())<1.0e-4) { if (RS_Math::correctAngle(line->getAngle1())<M_PI) { counter++; sure = false; } } // ray goes through endpoint of line: else if (p.distanceTo(line->getEndpoint())<1.0e-4) { if (RS_Math::correctAngle(line->getAngle2())<M_PI) { counter++; sure = false; } } // ray goes through the line: else { counter++; } } else if (e->rtti()==RS2::EntityArc) { RS_Arc* arc = (RS_Arc*)e; if (p.distanceTo(arc->getStartpoint())<1.0e-4) { double dir = arc->getDirection1(); if ((dir<M_PI && dir>=1.0e-5) || ((dir>2*M_PI-1.0e-5 || dir<1.0e-5) && arc->getCenter().y>p.y)) { counter++; sure = false; } } else if (p.distanceTo(arc->getEndpoint())<1.0e-4) { double dir = arc->getDirection2(); if ((dir<M_PI && dir>=1.0e-5) || ((dir>2*M_PI-1.0e-5 || dir<1.0e-5) && arc->getCenter().y>p.y)) { counter++; sure = false; } } else { counter++; } } else if (e->rtti()==RS2::EntityCircle) { // tangent: if (i==0 && sol.get(1).valid==false) { if (!sol.isTangent()) { counter++; } else { sure = false; } } else if (i==1 || sol.get(1).valid==true) { counter++; } } } } } } rayAngle+=0.02; tries++; } while (!sure && rayAngle<2*M_PI && tries<6); // remove double intersections: /* QList<RS_Vector> is2; bool done; RS_Vector* av; do { done = true; double minDist = RS_MAXDOUBLE; double dist; av = NULL; for (RS_Vector* v = is.first(); v!=NULL; v = is.next()) { dist = point.distanceTo(*v); if (dist<minDist) { minDist = dist; done = false; av = v; } } if (!done && av!=NULL) { is2.append(*av); } } while (!done); */ return ((counter%2)==1); }
/** * Updates the Hatch. Called when the * hatch or it's data, position, alignment, .. changes. */ void RS_Hatch::update() { RS_DEBUG->print("RS_Hatch::update"); RS_DEBUG->print("RS_Hatch::update: contour has %d loops", count()); updateError = HATCH_OK; if (updateRunning) { return; } if (updateEnabled==false) { return; } if (data.solid==true) { calculateBorders(); return; } RS_DEBUG->print("RS_Hatch::update"); updateRunning = true; // delete old hatch: if (hatch) { removeEntity(hatch); hatch = nullptr; } if (isUndone()) { updateRunning = false; return; } if (!validate()) { RS_DEBUG->print(RS_Debug::D_WARNING, "RS_Hatch::update: invalid contour in hatch found"); updateRunning = false; updateError = HATCH_INVALID_CONTOUR; return; } // search pattern: RS_DEBUG->print("RS_Hatch::update: requesting pattern"); RS_Pattern* pat = RS_PATTERNLIST->requestPattern(data.pattern); if (!pat) { updateRunning = false; RS_DEBUG->print("RS_Hatch::update: requesting pattern: not found"); updateError = HATCH_PATTERN_NOT_FOUND; return; } RS_DEBUG->print("RS_Hatch::update: requesting pattern: OK"); RS_DEBUG->print("RS_Hatch::update: cloning pattern"); pat = (RS_Pattern*)pat->clone(); RS_DEBUG->print("RS_Hatch::update: cloning pattern: OK"); // scale pattern RS_DEBUG->print("RS_Hatch::update: scaling pattern"); pat->scale(RS_Vector(0.0,0.0), RS_Vector(data.scale, data.scale)); pat->calculateBorders(); forcedCalculateBorders(); RS_DEBUG->print("RS_Hatch::update: scaling pattern: OK"); // find out how many pattern-instances we need in x/y: int px1, py1, px2, py2; double f; RS_Hatch* copy = (RS_Hatch*)this->clone(); copy->rotate(RS_Vector(0.0,0.0), -data.angle); copy->forcedCalculateBorders(); // create a pattern over the whole contour. RS_Vector pSize = pat->getSize(); RS_Vector rot_center=pat->getMin(); // RS_Vector cPos = getMin(); RS_Vector cSize = getSize(); RS_DEBUG->print("RS_Hatch::update: pattern size: %f/%f", pSize.x, pSize.y); RS_DEBUG->print("RS_Hatch::update: contour size: %f/%f", cSize.x, cSize.y); if (cSize.x<1.0e-6 || cSize.y<1.0e-6 || pSize.x<1.0e-6 || pSize.y<1.0e-6 || cSize.x>RS_MAXDOUBLE-1 || cSize.y>RS_MAXDOUBLE-1 || pSize.x>RS_MAXDOUBLE-1 || pSize.y>RS_MAXDOUBLE-1) { delete pat; delete copy; updateRunning = false; RS_DEBUG->print("RS_Hatch::update: contour size or pattern size too small"); updateError = HATCH_TOO_SMALL; return; } // avoid huge memory consumption: else if ( cSize.x* cSize.y/(pSize.x*pSize.y)>1e4) { RS_DEBUG->print("RS_Hatch::update: contour size too large or pattern size too small"); delete pat; delete copy; updateError = HATCH_AREA_TOO_BIG; return; } f = copy->getMin().x/pSize.x; px1 = (int)floor(f); f = copy->getMin().y/pSize.y; py1 = (int)floor(f); f = copy->getMax().x/pSize.x; px2 = (int)ceil(f); f = copy->getMax().y/pSize.y; py2 = (int)ceil(f); RS_Vector dvx=RS_Vector(data.angle)*pSize.x; RS_Vector dvy=RS_Vector(data.angle+M_PI*0.5)*pSize.y; pat->rotate(rot_center, data.angle); pat->move(-rot_center); RS_EntityContainer tmp; // container for untrimmed lines // adding array of patterns to tmp: RS_DEBUG->print("RS_Hatch::update: creating pattern carpet"); for (int px=px1; px<px2; px++) { for (int py=py1; py<py2; py++) { for(auto e: *pat){ RS_Entity* te=e->clone(); te->move(dvx*px + dvy*py); tmp.addEntity(te); } } } delete pat; pat = nullptr; delete copy; copy = nullptr; RS_DEBUG->print("RS_Hatch::update: creating pattern carpet: OK"); RS_DEBUG->print("RS_Hatch::update: cutting pattern carpet"); // cut pattern to contour shape: RS_EntityContainer tmp2; // container for small cut lines RS_Line* line = nullptr; RS_Arc* arc = nullptr; RS_Circle* circle = nullptr; RS_Ellipse* ellipse = nullptr; for(auto e: tmp){ line = nullptr; arc = nullptr; circle = nullptr; ellipse = nullptr; RS_Vector startPoint; RS_Vector endPoint; RS_Vector center = RS_Vector(false); bool reversed=false; switch(e->rtti()){ case RS2::EntityLine: line=static_cast<RS_Line*>(e); startPoint = line->getStartpoint(); endPoint = line->getEndpoint(); break; case RS2::EntityArc: arc=static_cast<RS_Arc*>(e); startPoint = arc->getStartpoint(); endPoint = arc->getEndpoint(); center = arc->getCenter(); reversed = arc->isReversed(); break; case RS2::EntityCircle: circle=static_cast<RS_Circle*>(e); startPoint = circle->getCenter() + RS_Vector(circle->getRadius(), 0.0); endPoint = startPoint; center = circle->getCenter(); break; case RS2::EntityEllipse: ellipse = static_cast<RS_Ellipse*>(e); startPoint = ellipse->getStartpoint(); endPoint = ellipse->getEndpoint(); center = ellipse->getCenter(); reversed = ellipse->isReversed(); break; default: continue; } // getting all intersections of this pattern line with the contour: QList<std::shared_ptr<RS_Vector> > is; for(auto loop: entities){ if (loop->isContainer()) { for(auto p: * static_cast<RS_EntityContainer*>(loop)){ RS_VectorSolutions sol = RS_Information::getIntersection(e, p, true); for (const RS_Vector& vp: sol){ if (vp.valid) { is.append(std::shared_ptr<RS_Vector>( new RS_Vector(vp) )); RS_DEBUG->print(" pattern line intersection: %f/%f", vp.x, vp.y); } } } } } QList<std::shared_ptr<RS_Vector> > is2;//to be filled with sorted intersections is2.append(std::shared_ptr<RS_Vector>(new RS_Vector(startPoint))); // sort the intersection points into is2 (only if there are intersections): if(is.size() == 1) {//only one intersection is2.append(is.first()); } else if(is.size() > 1) { RS_Vector sp = startPoint; double sa = center.angleTo(sp); if(ellipse ) sa=ellipse->getEllipseAngle(sp); bool done; double minDist; double dist = 0.0; std::shared_ptr<RS_Vector> av; std::shared_ptr<RS_Vector> v; RS_Vector last = RS_Vector(false); do { done = true; minDist = RS_MAXDOUBLE; av.reset(); for (int i = 0; i < is.size(); ++i) { v = is.at(i); double a; switch(e->rtti()){ case RS2::EntityLine: dist = sp.distanceTo(*v); break; case RS2::EntityArc: case RS2::EntityCircle: a = center.angleTo(*v); dist = reversed? fmod(sa - a + 2.*M_PI,2.*M_PI): fmod(a - sa + 2.*M_PI,2.*M_PI); break; case RS2::EntityEllipse: a = ellipse->getEllipseAngle(*v); dist = reversed? fmod(sa - a + 2.*M_PI,2.*M_PI): fmod(a - sa + 2.*M_PI,2.*M_PI); break; default: break; } if (dist<minDist) { minDist = dist; done = false; av = v; } } // copy to sorted list, removing double points if (!done && av.get()) { if (last.valid==false || last.distanceTo(*av)>RS_TOLERANCE) { is2.append(std::shared_ptr<RS_Vector>(new RS_Vector(*av))); last = *av; } #if QT_VERSION < 0x040400 emu_qt44_removeOne(is, av); #else is.removeOne(av); #endif av.reset(); } } while(!done); } is2.append(std::shared_ptr<RS_Vector>(new RS_Vector(endPoint))); // add small cut lines / arcs to tmp2: for (int i = 1; i < is2.size(); ++i) { auto v1 = is2.at(i-1); auto v2 = is2.at(i); if (line) { tmp2.addEntity(new RS_Line{&tmp2, *v1, *v2}); } else if (arc || circle) { if(fabs(center.angleTo(*v2)-center.angleTo(*v1)) > RS_TOLERANCE_ANGLE) {//don't create an arc with a too small angle tmp2.addEntity(new RS_Arc(&tmp2, RS_ArcData(center, center.distanceTo(*v1), center.angleTo(*v1), center.angleTo(*v2), reversed))); } } } } // updating hatch / adding entities that are inside RS_DEBUG->print("RS_Hatch::update: cutting pattern carpet: OK"); //RS_EntityContainer* rubbish = new RS_EntityContainer(getGraphic()); // the hatch pattern entities: hatch = new RS_EntityContainer(this); hatch->setPen(RS_Pen(RS2::FlagInvalid)); hatch->setLayer(nullptr); hatch->setFlag(RS2::FlagTemp); //calculateBorders(); for(auto e: tmp2){ RS_Vector middlePoint; RS_Vector middlePoint2; if (e->rtti()==RS2::EntityLine) { RS_Line* line = static_cast<RS_Line*>(e); middlePoint = line->getMiddlePoint(); middlePoint2 = line->getNearestDist(line->getLength()/2.1, line->getStartpoint()); } else if (e->rtti()==RS2::EntityArc) { RS_Arc* arc = static_cast<RS_Arc*>(e); middlePoint = arc->getMiddlePoint(); middlePoint2 = arc->getNearestDist(arc->getLength()/2.1, arc->getStartpoint()); } else { middlePoint = RS_Vector{false}; middlePoint2 = RS_Vector{false}; } if (middlePoint.valid) { bool onContour=false; if (RS_Information::isPointInsideContour( middlePoint, this, &onContour) || RS_Information::isPointInsideContour(middlePoint2, this)) { RS_Entity* te = e->clone(); te->setPen(RS2::FlagInvalid); te->setLayer(nullptr); te->reparent(hatch); hatch->addEntity(te); } } } addEntity(hatch); //getGraphic()->addEntity(rubbish); forcedCalculateBorders(); // deactivate contour: activateContour(false); updateRunning = false; RS_DEBUG->print("RS_Hatch::update: OK"); }
/** * Testing function. */ void LC_SimpleTests::slotTestDumpEntities(RS_EntityContainer* d){ int level = 0; std::ofstream dumpFile; if (d) { dumpFile.open("debug_entities.html", std::ios::app); ++level; } else { d = QC_ApplicationWindow::getAppWindow()->getDocument(); dumpFile.open("debug_entities.html"); level = 0; } if (d) { if (level==0) { dumpFile << "<html>\n"; dumpFile << "<body>\n"; } for(auto e: *d){ dumpFile << "<table border=\"1\">\n"; dumpFile << "<tr><td>Entity: " << e->getId() << "</td></tr>\n"; dumpFile << "<tr><td><table><tr>" << "<td>VIS:" << e->isVisible() << "</td>" << "<td>UND:" << e->isUndone() << "</td>" << "<td>SEL:" << e->isSelected() << "</td>" << "<td>TMP:" << e->getFlag(RS2::FlagTemp) << "</td>"; QString lay = "NULL"; if (e->getLayer()) { lay = e->getLayer()->getName(); } dumpFile << "<td>Layer: " << lay.toLatin1().data() << "</td>" << "<td>Width: " << (int)e->getPen(false).getWidth() << "</td>" << "<td>Parent: " << e->getParent()->getId() << "</td>" << "</tr></table>"; dumpFile << "<tr><td>\n"; switch (e->rtti()) { case RS2::EntityPoint: { RS_Point* p = (RS_Point*)e; dumpFile << "<table><tr><td>" << "<b>Point:</b>" << "</td></tr>"; dumpFile << "<tr>" << "<td>" << p->getPos() << "</td>" << "</tr></table>"; } break; case RS2::EntityLine: { RS_Line* l = (RS_Line*)e; dumpFile << "<table><tr><td>" << "<b>Line:</b>" << "</td></tr>"; dumpFile << "<tr>" << "<td>" << l->getStartpoint() << "</td>" << "<td>" << l->getEndpoint() << "</td>" << "</tr></table>"; } break; case RS2::EntityArc: { RS_Arc* a = (RS_Arc*)e; dumpFile << "<table><tr><td>" << "<b>Arc:</b>" << "</td></tr>"; dumpFile << "<tr>" << "<td>Center: " << a->getCenter() << "</td>" << "<td>Radius: " << a->getRadius() << "</td>" << "<td>Angle 1: " << a->getAngle1() << "</td>" << "<td>Angle 2: " << a->getAngle2() << "</td>" << "<td>Startpoint: " << a->getStartpoint() << "</td>" << "<td>Endpoint: " << a->getEndpoint() << "</td>" << "<td>reversed: " << (int)a->isReversed() << "</td>" << "</tr></table>"; } break; case RS2::EntityCircle: { RS_Circle* c = (RS_Circle*)e; dumpFile << "<table><tr><td>" << "<b>Circle:</b>" << "</td></tr>"; dumpFile << "<tr>" << "<td>Center: " << c->getCenter() << "</td>" << "<td>Radius: " << c->getRadius() << "</td>" << "</tr></table>"; } break; case RS2::EntityDimAligned: { RS_DimAligned* d = (RS_DimAligned*)e; dumpFile << "<table><tr><td>" << "<b>Dimension / Aligned:</b>" << "</td></tr>"; dumpFile << "<tr>" << "<td>" << d->getDefinitionPoint() << "</td>" << "<td>" << d->getExtensionPoint1() << "</td>" << "<td>" << d->getExtensionPoint2() << "</td>" << "<td>Text: " << d->getText().toLatin1().data() << "</td>" << "<td>Label: " << d->getLabel().toLatin1().data() << "</td>" << "</tr></table>"; } break; case RS2::EntityDimLinear: { RS_DimLinear* d = (RS_DimLinear*)e; dumpFile << "<table><tr><td>" << "<b>Dimension / Linear:</b>" << "</td></tr>"; dumpFile << "<tr>" << "<td>" << d->getDefinitionPoint() << "</td>" << "<td>" << d->getExtensionPoint1() << "</td>" << "<td>" << d->getExtensionPoint2() << "</td>" << "<td>Text: " << d->getText().toLatin1().data() << "</td>" << "<td>Label: " << d->getLabel().toLatin1().data() << "</td>" << "</tr></table>"; } break; case RS2::EntityInsert: { RS_Insert* i = (RS_Insert*)e; dumpFile << "<table><tr><td>" << "<b>Insert:</b>" << "</td></tr>"; dumpFile << "<tr>" << "<td>Insertion point:" << i->getInsertionPoint() << "</td>" << "</tr></table>"; } break; case RS2::EntityMText: { RS_MText* t = (RS_MText*)e; dumpFile << "<table><tr><td>" << "<b>Text:</b>" << "</td></tr>"; dumpFile << "<tr>" << "<td>Text:" << t->getText().toLatin1().data() << "</td>" << "<td>Height:" << t->getHeight() << "</td>" << "</tr></table>"; } break; case RS2::EntityText: { RS_Text* t = (RS_Text*)e; dumpFile << "<table><tr><td>" << "<b>Text:</b>" << "</td></tr>"; dumpFile << "<tr>" << "<td>Text:" << t->getText().toLatin1().data() << "</td>" << "<td>Height:" << t->getHeight() << "</td>" << "</tr></table>"; } break; case RS2::EntityHatch: { RS_Hatch* h = (RS_Hatch*)e; dumpFile << "<table><tr><td>" << "<b>Hatch:</b>" << "</td></tr>"; dumpFile << "<tr>" << "<td>Pattern:" << h->getPattern().toLatin1().data() << "</td>" << "<td>Scale:" << h->getScale() << "</td>" << "<td>Solid:" << (int)h->isSolid() << "</td>" << "</tr></table>"; } break; default: dumpFile << "<tr><td>" << "<b>Unknown Entity: " << e->rtti() << "</b>" << "</td></tr>"; break; } if (e->isContainer() || e->rtti()==RS2::EntityHatch) { RS_EntityContainer* ec = (RS_EntityContainer*)e; dumpFile << "<table><tr><td valign=\"top\"> Contents:</td><td>\n"; dumpFile.close(); slotTestDumpEntities(ec); dumpFile.open("debug_entities.html", std::ios::app); dumpFile << "</td></tr></table>\n"; } dumpFile << "</td></tr>" << "</table>\n" << "<br><br>"; } if (level==0) { dumpFile << "</body>\n"; dumpFile << "</html>\n"; } else { level--; } } RS_DEBUG->print("%s\n: end\n", __func__); }
/** * Updates the Hatch. Called when the * hatch or it's data, position, alignment, .. changes. */ void RS_Hatch::update() { RS_DEBUG->print("RS_Hatch::update"); RS_DEBUG->print("RS_Hatch::update: contour has %d loops", count()); if (updateRunning) { return; } if (updateEnabled==false) { return; } if (data.solid==true) { calculateBorders(); return; } RS_DEBUG->print("RS_Hatch::update"); updateRunning = true; // delete old hatch: if (hatch!=NULL) { removeEntity(hatch); hatch = NULL; } if (isUndone()) { updateRunning = false; return; } if (!validate()) { RS_DEBUG->print(RS_Debug::D_WARNING, "RS_Hatch::update: invalid contour in hatch found"); updateRunning = false; return; } // search pattern: RS_DEBUG->print("RS_Hatch::update: requesting pattern"); RS_Pattern* pat = RS_PATTERNLIST->requestPattern(data.pattern); if (pat==NULL) { updateRunning = false; RS_DEBUG->print("RS_Hatch::update: requesting pattern: not found"); return; } RS_DEBUG->print("RS_Hatch::update: requesting pattern: OK"); RS_DEBUG->print("RS_Hatch::update: cloning pattern"); pat = (RS_Pattern*)pat->clone(); RS_DEBUG->print("RS_Hatch::update: cloning pattern: OK"); // scale pattern RS_DEBUG->print("RS_Hatch::update: scaling pattern"); pat->scale(RS_Vector(0.0,0.0), RS_Vector(data.scale, data.scale)); pat->calculateBorders(); forcedCalculateBorders(); RS_DEBUG->print("RS_Hatch::update: scaling pattern: OK"); // find out how many pattern-instances we need in x/y: int px1, py1, px2, py2; double f; RS_Hatch* copy = (RS_Hatch*)this->clone(); copy->rotate(RS_Vector(0.0,0.0), -data.angle); copy->forcedCalculateBorders(); // create a pattern over the whole contour. RS_Vector pSize = pat->getSize(); // RS_Vector cPos = getMin(); RS_Vector cSize = getSize(); RS_DEBUG->print("RS_Hatch::update: pattern size: %f/%f", pSize.x, pSize.y); RS_DEBUG->print("RS_Hatch::update: contour size: %f/%f", cSize.x, cSize.y); if (cSize.x<1.0e-6 || cSize.y<1.0e-6 || pSize.x<1.0e-6 || pSize.y<1.0e-6 || cSize.x>RS_MAXDOUBLE-1 || cSize.y>RS_MAXDOUBLE-1 || pSize.x>RS_MAXDOUBLE-1 || pSize.y>RS_MAXDOUBLE-1) { delete pat; delete copy; updateRunning = false; RS_DEBUG->print("RS_Hatch::update: contour size or pattern size too small"); return; } // avoid huge memory consumption: else if (cSize.x/pSize.x>100 || cSize.y/pSize.y>100) { RS_DEBUG->print("RS_Hatch::update: contour size too large or pattern size too small"); return; } f = copy->getMin().x/pat->getSize().x; px1 = (int)floor(f); f = copy->getMin().y/pat->getSize().y; py1 = (int)floor(f); f = copy->getMax().x/pat->getSize().x; px2 = (int)ceil(f) - 1; f = copy->getMax().y/pat->getSize().y; py2 = (int)ceil(f) - 1; RS_EntityContainer tmp; // container for untrimmed lines // adding array of patterns to tmp: RS_DEBUG->print("RS_Hatch::update: creating pattern carpet"); for (int px=px1; px<=px2; px++) { for (int py=py1; py<=py2; py++) { for (RS_Entity* e=pat->firstEntity(); e!=NULL; e=pat->nextEntity()) { RS_Entity* te = e->clone(); te->rotate(RS_Vector(0.0,0.0), data.angle); RS_Vector v1, v2; v1.setPolar(px*pSize.x, data.angle); v2.setPolar(py*pSize.y, data.angle+M_PI/2.0); te->move(v1+v2); tmp.addEntity(te); } } } delete pat; pat = NULL; RS_DEBUG->print("RS_Hatch::update: creating pattern carpet: OK"); RS_DEBUG->print("RS_Hatch::update: cutting pattern carpet"); // cut pattern to contour shape: RS_EntityContainer tmp2; // container for small cut lines RS_Line* line = NULL; RS_Arc* arc = NULL; RS_Circle* circle = NULL; RS_Ellipse* ellipse = NULL; for (RS_Entity* e=tmp.firstEntity(); e!=NULL; e=tmp.nextEntity()) { RS_Vector startPoint; RS_Vector endPoint; RS_Vector center = RS_Vector(false); bool reversed; switch(e->rtti()){ case RS2::EntityLine: line=static_cast<RS_Line*>(e); startPoint = line->getStartpoint(); endPoint = line->getEndpoint(); break; case RS2::EntityArc: arc=static_cast<RS_Arc*>(e); startPoint = arc->getStartpoint(); endPoint = arc->getEndpoint(); center = arc->getCenter(); reversed = arc->isReversed(); break; case RS2::EntityCircle: circle=static_cast<RS_Circle*>(e); startPoint = circle->getCenter() + RS_Vector(circle->getRadius(), 0.0); endPoint = startPoint; center = circle->getCenter(); break; case RS2::EntityEllipse: ellipse = static_cast<RS_Ellipse*>(e); startPoint = ellipse->getStartpoint(); endPoint = ellipse->getEndpoint(); center = ellipse->getCenter(); reversed = ellipse->isReversed(); break; default: continue; } // getting all intersections of this pattern line with the contour: QList<std::shared_ptr<RS_Vector> > is; is.append(std::shared_ptr<RS_Vector>(new RS_Vector(startPoint))); for (RS_Entity* loop=firstEntity(); loop!=NULL; loop=nextEntity()) { if (loop->isContainer()) { for (RS_Entity* p=((RS_EntityContainer*)loop)->firstEntity(); p!=NULL; p=((RS_EntityContainer*)loop)->nextEntity()) { RS_VectorSolutions sol = RS_Information::getIntersection(e, p, true); for (int i=0; i<=1; ++i) { if (sol.get(i).valid) { is.append(std::shared_ptr<RS_Vector>( new RS_Vector(sol.get(i)) )); RS_DEBUG->print(" pattern line intersection: %f/%f", sol.get(i).x, sol.get(i).y); } } } } } is.append(std::shared_ptr<RS_Vector>(new RS_Vector(endPoint))); // sort the intersection points into is2: RS_Vector sp = startPoint; double sa = center.angleTo(sp); if(ellipse != NULL) sa=ellipse->getEllipseAngle(sp); QList<std::shared_ptr<RS_Vector> > is2; bool done; double minDist; double dist = 0.0; std::shared_ptr<RS_Vector> av; std::shared_ptr<RS_Vector> v; RS_Vector last = RS_Vector(false); do { done = true; minDist = RS_MAXDOUBLE; av.reset(); for (int i = 0; i < is.size(); ++i) { v = is.at(i); double a; switch(e->rtti()){ case RS2::EntityLine: dist = sp.distanceTo(*v); break; case RS2::EntityArc: case RS2::EntityCircle: a = center.angleTo(*v); dist = reversed? fmod(sa - a + 2.*M_PI,2.*M_PI): fmod(a - sa + 2.*M_PI,2.*M_PI); break; case RS2::EntityEllipse: a = ellipse->getEllipseAngle(*v); dist = reversed? fmod(sa - a + 2.*M_PI,2.*M_PI): fmod(a - sa + 2.*M_PI,2.*M_PI); break; default: break; } if (dist<minDist) { minDist = dist; done = false; av = v; } } // copy to sorted list, removing double points if (!done && av.get()!=NULL) { if (last.valid==false || last.distanceTo(*av)>RS_TOLERANCE) { is2.append(std::shared_ptr<RS_Vector>(new RS_Vector(*av))); last = *av; } #if QT_VERSION < 0x040400 emu_qt44_removeOne(is, av); #else is.removeOne(av); #endif av.reset(); } } while(!done); // add small cut lines / arcs to tmp2: for (int i = 1; i < is2.size(); ++i) { auto v1 = is2.at(i-1); auto v2 = is2.at(i); if (line!=NULL) { tmp2.addEntity(new RS_Line(&tmp2, RS_LineData(*v1, *v2))); } else if (arc!=NULL || circle!=NULL) { tmp2.addEntity(new RS_Arc(&tmp2, RS_ArcData(center, center.distanceTo(*v1), center.angleTo(*v1), center.angleTo(*v2), reversed))); } } } // updating hatch / adding entities that are inside RS_DEBUG->print("RS_Hatch::update: cutting pattern carpet: OK"); //RS_EntityContainer* rubbish = new RS_EntityContainer(getGraphic()); // the hatch pattern entities: hatch = new RS_EntityContainer(this); hatch->setPen(RS_Pen(RS2::FlagInvalid)); hatch->setLayer(NULL); hatch->setFlag(RS2::FlagTemp); //calculateBorders(); for (RS_Entity* e=tmp2.firstEntity(); e!=NULL; e=tmp2.nextEntity()) { RS_Vector middlePoint; RS_Vector middlePoint2; if (e->rtti()==RS2::EntityLine) { RS_Line* line = (RS_Line*)e; middlePoint = line->getMiddlePoint(); middlePoint2 = line->getNearestDist(line->getLength()/2.1, line->getStartpoint()); } else if (e->rtti()==RS2::EntityArc) { RS_Arc* arc = (RS_Arc*)e; middlePoint = arc->getMiddlePoint(); middlePoint2 = arc->getNearestDist(arc->getLength()/2.1, arc->getStartpoint()); } else { middlePoint = RS_Vector(false); middlePoint2 = RS_Vector(false); } if (middlePoint.valid) { bool onContour=false; if (RS_Information::isPointInsideContour( middlePoint, this, &onContour) || RS_Information::isPointInsideContour(middlePoint2, this)) { RS_Entity* te = e->clone(); te->setPen(RS_Pen(RS2::FlagInvalid)); te->setLayer(NULL); te->reparent(hatch); hatch->addEntity(te); } } } addEntity(hatch); //getGraphic()->addEntity(rubbish); forcedCalculateBorders(); // deactivate contour: activateContour(false); updateRunning = false; RS_DEBUG->print("RS_Hatch::update: OK"); }
/** * Implementation of the method used for RS_Export to communicate * with this filter. * * @param file Full path to the LFF file that will be written. */ bool RS_FilterLFF::fileExport(RS_Graphic& g, const QString& file, RS2::FormatType /*type*/) { RS_DEBUG->print("LFF Filter: exporting file '%s'...", file.toLatin1().data()); RS_DEBUG->print("RS_FilterLFF::fileExport: open"); QFile f(file); QTextStream ts(&f); ts.setCodec("UTF-8"); if (f.open(QIODevice::WriteOnly | QIODevice::Truncate)) { RS_DEBUG->print("RS_FilterLFF::fileExport: open: OK"); RS_DEBUG->print("RS_FilterLFF::fileExport: header"); // header: ts << "# Format: LibreCAD Font 1\n"; ts << QString("# Creator: %1\n").arg(RS_SYSTEM->getAppName()); ts << QString("# Version: %1\n").arg(RS_SYSTEM->getAppVersion()); QString ns = g.getVariableString("Names", ""); if (!ns.isEmpty()) { QStringList names = ns.split(','); RS_DEBUG->print("002"); for (int i = 0; i < names.size(); ++i) { ts << QString("# Name: %1\n").arg(names.at(i)); } } QString es = g.getVariableString("Encoding", ""); ts << QString("# Encoding: UTF-8\n"); ts << QString("# LetterSpacing: %1\n").arg( g.getVariableDouble("LetterSpacing", 3.0)); ts << QString("# WordSpacing: %1\n").arg( g.getVariableDouble("WordSpacing", 6.75)); ts << QString("# LineSpacingFactor: %1\n").arg( g.getVariableDouble("LineSpacingFactor", 1.0)); QString dateline = QDate::currentDate().toString ("yyyy-MM-dd"); ts << QString("# Created: %1\n").arg( g.getVariableString("Created", dateline)); ts << QString("# Last modified: %1\n").arg(dateline); QString sa = g.getVariableString("Authors", ""); RS_DEBUG->print("authors: %s", sa.toLocal8Bit().data()); if (!sa.isEmpty()) { QStringList authors = sa.split(','); RS_DEBUG->print("count: %d", authors.count()); QString a; for (int i = 0; i < authors.size(); ++i) { ts << QString("# Author: %1\n").arg(authors.at(i)); } } es = g.getVariableString("License", ""); if (!es.isEmpty()) { ts << QString("# License: %1\n").arg(es); } else ts << "# License: unknown\n"; RS_DEBUG->print("RS_FilterLFF::fileExport: header: OK"); // iterate through blocks (=letters of font) for (uint i=0; i<g.countBlocks(); ++i) { RS_Block* blk = g.blockAt(i); RS_DEBUG->print("block: %d", i); if (blk!=NULL) { RS_DEBUG->print("002a: %s", (blk->getName().toLocal8Bit().data())); ts << QString("\n%1\n").arg(blk->getName()); // iterate through entities of this letter: for (RS_Entity* e=blk->firstEntity(RS2::ResolveNone); e!=NULL; e=blk->nextEntity(RS2::ResolveNone)) { if (!e->isUndone()) { // lines: if (e->rtti()==RS2::EntityLine) { RS_Line* l = (RS_Line*)e; ts << clearZeros(l->getStartpoint().x, 5) << ','; ts << clearZeros(l->getStartpoint().y, 5) << ';'; ts << clearZeros(l->getEndpoint().x, 5) << ','; ts << clearZeros(l->getEndpoint().y, 5) << '\n'; } // arcs: else if (e->rtti()==RS2::EntityArc) { RS_Arc* a = (RS_Arc*)e; ts << clearZeros(a->getStartpoint().x, 5) << ','; ts << clearZeros(a->getStartpoint().y, 5) << ';'; ts << clearZeros(a->getEndpoint().x, 5) << ','; ts << clearZeros(a->getEndpoint().y, 5) << ",A"; ts << clearZeros(a->getBulge(), 5) << '\n'; } else if (e->rtti()==RS2::EntityBlock) { RS_Block* b = (RS_Block*)e; QString uCode; uCode.setNum(b->getName().at(0).unicode(), 16); if (uCode.length()<4) { uCode = uCode.rightJustified(4, '0'); } ts << QString("C%1\n").arg(uCode); } else if (e->rtti()==RS2::EntityPolyline) { RS_Polyline* p = (RS_Polyline*)e; ts << clearZeros(p->getStartpoint().x, 5) << ','; ts << clearZeros(p->getStartpoint().y, 5); for (RS_Entity* e2=p->firstEntity(RS2::ResolveNone); e2!=NULL; e2=p->nextEntity(RS2::ResolveNone)) { if (e2->rtti()==RS2::EntityLine){ RS_Line* l = (RS_Line*)e2; ts << ';' << clearZeros(l->getEndpoint().x, 5) << ','; ts << clearZeros(l->getEndpoint().y, 5); } else if (e2->rtti()==RS2::EntityArc){ RS_Arc* a = (RS_Arc*)e2; ts << ';' << clearZeros(a->getEndpoint().x, 5) << ','; ts << clearZeros(a->getEndpoint().y, 5) <<",A"; ts << clearZeros(a->getBulge(), 5); } } ts<<'\n'; } // Ignore entities other than arcs / lines else {} } } } } f.close(); RS_DEBUG->print("LFF Filter: exporting file: OK"); return true; } else { RS_DEBUG->print("LFF Filter: exporting file failed"); } return false; }