bool RS_ActionPolylineEquidistant::makeContour() {
    if (container==NULL) {
        RS_DEBUG->print("RS_ActionPolylineEquidistant::makeContour: no valid container",
                        RS_Debug::D_WARNING);
        return false;
    }

    RS_Polyline* originalPolyline = (RS_Polyline*)originalEntity;
//create a list of entities to offset without length = 0
    QList<RS_Entity*> entities;
    for (RS_Entity* en=originalPolyline->firstEntity(); en!=NULL; en=originalPolyline->nextEntity()) {
        if (en->getLength() > 1.0e-12)
            entities.append(en);
    }
    if (entities.isEmpty()) {
        return false;
    }
    if (document!=NULL) {
        document->startUndoCycle();
    }
    double neg = 1.0;
    if(bRightSide)
        neg = -1.0;

    // Create new helper entities
    RS_Line line1(NULL, RS_LineData(RS_Vector(true), RS_Vector(true)));//current line
    RS_Line lineFirst(NULL, RS_LineData(RS_Vector(true), RS_Vector(true)));//previous line
    RS_Arc arc1(NULL, RS_ArcData(RS_Vector(true), 0,0,0,false));//current arc
    RS_Arc arcFirst(NULL, RS_ArcData(RS_Vector(true), 0,0,0,false));//previous arc

    for (int num=1; num<=number || (number==0 && num<=1); num++) {
        RS_Polyline* newPolyline = new RS_Polyline(container);
        newPolyline->setLayer(((RS_Polyline*)originalEntity)->getLayer());
        newPolyline->setPen(((RS_Polyline*)originalEntity)->getPen());

        bool first = true;
        bool closed = originalPolyline->isClosed();
        double bulge = 0.0;
        RS_Entity* en;
        RS_Entity* prevEntity = entities.last();
        RS_Entity* currEntity;
        for (int i = 0; i < entities.size(); ++i) {
            en = entities.at(i);
            RS_Vector v(false);
            if (en->rtti()==RS2::EntityArc) {
                currEntity = &arc1;
                calculateOffset(currEntity, en, dist*num*neg);
                bulge = arc1.getBulge();
            } else {
                currEntity = &line1;
                bulge = 0.0;
                calculateOffset(currEntity, en, dist*num*neg);
            }
            if (first) {
                if (closed) {
                    if (prevEntity->rtti()==RS2::EntityArc) {
                        prevEntity = calculateOffset(&arcFirst, prevEntity, dist*num*neg);
                    } else {
                        prevEntity = calculateOffset(&lineFirst, prevEntity, dist*num*neg);
                    }
                    v = calculateIntersection(prevEntity, currEntity);
                }
                if (!v.valid) {
                    v = currEntity->getStartpoint();
                    closed = false;
                } else if (currEntity->rtti()==RS2::EntityArc) {
                    //update bulge
                    arc1.setAngle1(arc1.getCenter().angleTo(v));
                    arc1.calculateEndpoints();
                    bulge = arc1.getBulge();
                }
                first = false;
                if (!prevEntity) break; //prevent crash if not exist offset for prevEntity
            } else {
                v = calculateIntersection(prevEntity, currEntity);
                if (!v.valid) {
                    v= prevEntity->getEndpoint();
                    double dess = currEntity->getStartpoint().distanceTo(prevEntity->getEndpoint());
                    if (dess > 1.0e-12) {
                        newPolyline->addVertex(v, bulge);
                        prevEntity = NULL;
                        break;
                    }
                }
                double startAngle = prevEntity->getStartpoint().angleTo(prevEntity->getEndpoint());
                if (prevEntity->rtti()==RS2::EntityArc) {
                    arcFirst.setAngle2(arcFirst.getCenter().angleTo(v));
                    arcFirst.calculateEndpoints();
                    newPolyline->setNextBulge(arcFirst.getBulge());
                }
                //check if the entity are reverted
                if (abs (prevEntity->getStartpoint().angleTo(prevEntity->getEndpoint())- startAngle) > 0.785) {
                    prevEntity = newPolyline->lastEntity();
                    RS_Vector v0 = calculateIntersection(prevEntity, currEntity);
                    if (prevEntity->rtti()==RS2::EntityArc) {
                        ((RS_Arc*)prevEntity)->setAngle2(arcFirst.getCenter().angleTo(v0));
                        ((RS_Arc*)prevEntity)->calculateEndpoints();
                        newPolyline->setNextBulge( ((RS_Arc*)prevEntity)->getBulge() );
                    } else {
                        ((RS_Line*)prevEntity)->setEndpoint(v0);
                        newPolyline->setNextBulge( 0.0 );
                    }
                    newPolyline->setEndpoint(v0);
                }
                if (currEntity->rtti()==RS2::EntityArc) {
                    arc1.setAngle1(arc1.getCenter().angleTo(v));
                    arc1.calculateEndpoints();
                    bulge = arc1.getBulge();
                } else
                    bulge = 0.0;
            }
            if (prevEntity) {
                newPolyline->addVertex(v, bulge, false);
                if (currEntity->rtti()==RS2::EntityArc) {
                    arcFirst.setData(arc1.getData());
                    arcFirst.calculateEndpoints();
                    prevEntity = &arcFirst;
                } else {
                    lineFirst.setStartpoint(line1.getStartpoint());
                    lineFirst.setEndpoint(line1.getEndpoint());
                    prevEntity = &lineFirst;
                }
            }
        }
        //properly terminated, check closed
        if (prevEntity) {
            if (closed) {
                if (currEntity->rtti()==RS2::EntityArc) {
                    arc1.setAngle2(arc1.getCenter().angleTo(newPolyline->getStartpoint()));
                    arc1.calculateEndpoints();
                    newPolyline->setNextBulge(arc1.getBulge());
                    bulge = arc1.getBulge();
                }
                newPolyline->setClosed(true, bulge);
            } else {
                newPolyline->addVertex(currEntity->getEndpoint(), bulge);
            }
        }
        if (!newPolyline->isEmpty()) {
            container->addEntity(newPolyline);
            document->addUndoable(newPolyline);
        }
    }
    if (document!=NULL) {
        document->endUndoCycle();
    }

    if (graphicView!=NULL) {
        graphicView->redraw();
    }

    return true;
}
Пример #2
0
/**
 * 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;
}