void ShapeBuilderWidget::createSolid()
{
    Gui::SelectionFilter partFilter  ("SELECT Part::Feature COUNT 1");
    bool matchPart = partFilter.match();
    if (!matchPart) {
        QMessageBox::critical(this, tr("Wrong selection"), tr("Select only one part object"));
        return;
    }

    QString line;
    QTextStream str(&line);

    std::vector<Gui::SelectionObject> sel = partFilter.Result[0];
    std::vector<Gui::SelectionObject>::iterator it;
    for (it=sel.begin();it!=sel.end();++it) {
        str << "App.ActiveDocument." << it->getFeatName() << ".Shape";
        break;
    }

    QString cmd;
    cmd = QString::fromAscii(
        "shell=%1\n"
        "if shell.ShapeType != 'Shell': raise Exception('Part object is not a shell')\n"
        "_=Part.Solid(shell)\n"
        "if _.isNull(): raise Exception('Failed to create solid')\n"
        "App.ActiveDocument.addObject('Part::Feature','Solid').Shape=_.removeSplitter()\n"
        "del _\n"
    ).arg(line);

    Gui::Application::Instance->activeDocument()->openCommand("Solid");
    Gui::Application::Instance->runPythonCode((const char*)cmd.toAscii(), false, false);
    Gui::Application::Instance->activeDocument()->commitCommand();
}
Example #2
0
void SweepWidget::on_buttonPath_clicked()
{
    if (!d->loop.isRunning()) {
        QList<QWidget*> c = this->findChildren<QWidget*>();
        for (QList<QWidget*>::iterator it = c.begin(); it != c.end(); ++it)
            (*it)->setEnabled(false);
        d->buttonText = d->ui.buttonPath->text();
        d->ui.buttonPath->setText(tr("Done"));
        d->ui.buttonPath->setEnabled(true);
        d->ui.labelPath->setText(tr("Select one or more connected edges in the 3d view and press 'Done'"));
        d->ui.labelPath->setEnabled(true);

        Gui::Selection().clearSelection();
        Gui::Selection().addSelectionGate(new Private::EdgeSelection());
        d->loop.exec();
    }
    else {
        QList<QWidget*> c = this->findChildren<QWidget*>();
        for (QList<QWidget*>::iterator it = c.begin(); it != c.end(); ++it)
            (*it)->setEnabled(true);
        d->ui.buttonPath->setText(d->buttonText);
        d->ui.labelPath->clear();
        Gui::Selection().rmvSelectionGate();
        d->loop.quit();

        Gui::SelectionFilter edgeFilter  ("SELECT Part::Feature SUBELEMENT Edge COUNT 1..");
        Gui::SelectionFilter partFilter  ("SELECT Part::Feature COUNT 1");
        bool matchEdge = edgeFilter.match();
        bool matchPart = partFilter.match();
        if (matchEdge) {
            // check if path is valid
            const std::vector<Gui::SelectionObject>& result = edgeFilter.Result[0];
            if (!isPathValid(result.front())) {
                QMessageBox::critical(this, tr("Sweep path"), tr("The selected sweep path is invalid."));
                Gui::Selection().clearSelection();
            }
        }
        else if (matchPart) {
            // check if path is valid
            const std::vector<Gui::SelectionObject>& result = partFilter.Result[0];
            if (!isPathValid(result.front())) {
                QMessageBox::critical(this, tr("Sweep path"), tr("The selected sweep path is invalid."));
                Gui::Selection().clearSelection();
            }
        }
    }
}
void ShapeBuilderWidget::createSolidFromShell()
{
    Gui::SelectionFilter partFilter  ("SELECT Part::Feature COUNT 1");
    bool matchPart = partFilter.match();
    if (!matchPart) {
        QMessageBox::critical(this, tr("Wrong selection"), tr("Select only one part object"));
        return;
    }

    QString line;
    QTextStream str(&line);

    std::vector<Gui::SelectionObject> sel = partFilter.Result[0];
    std::vector<Gui::SelectionObject>::iterator it;
    for (it=sel.begin();it!=sel.end();++it) {
        str << "App.ActiveDocument." << it->getFeatName() << ".Shape";
        break;
    }

    QString cmd;
    if (d->ui.checkRefine->isEnabled() && d->ui.checkRefine->isChecked()) {
        cmd = QString::fromLatin1(
            "shell=%1\n"
            "if shell.ShapeType != 'Shell': raise RuntimeError('Part object is not a shell')\n"
            "_=Part.Solid(shell)\n"
            "if _.isNull(): raise RuntimeError('Failed to create solid')\n"
            "App.ActiveDocument.addObject('Part::Feature','Solid').Shape=_.removeSplitter()\n"
            "del _\n"
        ).arg(line);
    }
    else {
        cmd = QString::fromLatin1(
            "shell=%1\n"
            "if shell.ShapeType != 'Shell': raise RuntimeError('Part object is not a shell')\n"
            "_=Part.Solid(shell)\n"
            "if _.isNull(): raise RuntimeError('Failed to create solid')\n"
            "App.ActiveDocument.addObject('Part::Feature','Solid').Shape=_\n"
            "del _\n"
        ).arg(line);
    }

    try {
        Gui::Application::Instance->activeDocument()->openCommand("Solid");
        Gui::Command::runCommand(Gui::Command::App, cmd.toLatin1());
        Gui::Application::Instance->activeDocument()->commitCommand();
    }
    catch (const Base::Exception&) {
        Gui::Application::Instance->activeDocument()->abortCommand();
        throw;
    }
}
void ShapeBuilderWidget::createFaceFromEdge()
{
    Gui::SelectionFilter edgeFilter  ("SELECT Part::Feature SUBELEMENT Edge COUNT 1..");
    bool matchEdge = edgeFilter.match();
    if (!matchEdge) {
        QMessageBox::critical(this, tr("Wrong selection"), tr("Select one or more edges"));
        return;
    }

    std::vector<Gui::SelectionObject> sel = edgeFilter.Result[0];
    std::vector<Gui::SelectionObject>::iterator it;
    std::vector<std::string>::const_iterator jt;

    QString list;
    QTextStream str(&list);
    str << "[";
    for (it=sel.begin();it!=sel.end();++it) {
        for (jt=it->getSubNames().begin();jt!=it->getSubNames().end();++jt) {
            str << "App.ActiveDocument." << it->getFeatName() << ".Shape." << jt->c_str() << ", ";
        }
    }
    str << "]";

    QString cmd;
    if (d->ui.checkPlanar->isChecked()) {
        cmd = QString::fromLatin1(
            "_=Part.Face(Part.Wire(Part.__sortEdges__(%1)))\n"
            "if _.isNull(): raise RuntimeError('Failed to create face')\n"
            "App.ActiveDocument.addObject('Part::Feature','Face').Shape=_\n"
            "del _\n"
        ).arg(list);
    }
    else {
        cmd = QString::fromLatin1(
            "_=Part.makeFilledFace(Part.__sortEdges__(%1))\n"
            "if _.isNull(): raise RuntimeError('Failed to create face')\n"
            "App.ActiveDocument.addObject('Part::Feature','Face').Shape=_\n"
            "del _\n"
        ).arg(list);
    }

    try {
        Gui::Application::Instance->activeDocument()->openCommand("Face");
        Gui::Command::runCommand(Gui::Command::App, cmd.toLatin1());
        Gui::Application::Instance->activeDocument()->commitCommand();
    }
    catch (const Base::Exception&) {
        Gui::Application::Instance->activeDocument()->abortCommand();
        throw;
    }
}
void ShapeBuilderWidget::createEdgeFromVertex()
{
    Gui::SelectionFilter vertexFilter  ("SELECT Part::Feature SUBELEMENT Vertex COUNT 2");
    bool matchVertex = vertexFilter.match();
    if (!matchVertex) {
        QMessageBox::critical(this, tr("Wrong selection"), tr("Select two vertices"));
        return;
    }

    std::vector<Gui::SelectionObject> sel = vertexFilter.Result[0];
    std::vector<QString> elements;
    std::vector<Gui::SelectionObject>::iterator it;
    std::vector<std::string>::const_iterator jt;
    for (it=sel.begin();it!=sel.end();++it) {
        for (jt=it->getSubNames().begin();jt!=it->getSubNames().end();++jt) {
            QString line;
            QTextStream str(&line);
            str << "App.ActiveDocument." << it->getFeatName() << ".Shape." << jt->c_str() << ".Point";
            elements.push_back(line);
        }
    }

    // should actually never happen
    if (elements.size() != 2) {
        QMessageBox::critical(this, tr("Wrong selection"), tr("Select two vertices"));
        return;
    }

    QString cmd;
    cmd = QString::fromLatin1(
        "_=Part.makeLine(%1, %2)\n"
        "if _.isNull(): raise RuntimeError('Failed to create edge')\n"
        "App.ActiveDocument.addObject('Part::Feature','Edge').Shape=_\n"
        "del _\n"
    ).arg(elements[0]).arg(elements[1]);

    try {
        Gui::Application::Instance->activeDocument()->openCommand("Edge");
        Gui::Command::runCommand(Gui::Command::App, cmd.toLatin1());
        Gui::Application::Instance->activeDocument()->commitCommand();
    }
    catch (const Base::Exception&) {
        Gui::Application::Instance->activeDocument()->abortCommand();
        throw;
    }
}
void ShapeBuilderWidget::createShell()
{
    Gui::SelectionFilter faceFilter  ("SELECT Part::Feature SUBELEMENT Face COUNT 2..");
    bool matchFace = faceFilter.match();
    if (!matchFace) {
        QMessageBox::critical(this, tr("Wrong selection"), tr("Select two or more faces"));
        return;
    }

    std::vector<Gui::SelectionObject> sel = faceFilter.Result[0];
    std::vector<Gui::SelectionObject>::iterator it;
    std::vector<std::string>::const_iterator jt;

    QString list;
    QTextStream str(&list);
    if (d->ui.checkFaces->isChecked()) {
        std::set<App::DocumentObject*> obj;
        for (it=sel.begin();it!=sel.end();++it)
            obj.insert(it->getObject());
        str << "[]";
        for (std::set<App::DocumentObject*>::iterator it = obj.begin(); it != obj.end(); ++it) {
            str << "+ App.ActiveDocument." << (*it)->getNameInDocument() << ".Shape.Faces";
        }
    }
    else {
        str << "[";
        for (it=sel.begin();it!=sel.end();++it) {
            for (jt=it->getSubNames().begin();jt!=it->getSubNames().end();++jt) {
                str << "App.ActiveDocument." << it->getFeatName() << ".Shape." << jt->c_str() << ", ";
            }
        }
        str << "]";
    }

    QString cmd;
    cmd = QString::fromAscii(
        "_=Part.Shell(%1)\n"
        "if _.isNull(): raise Exception('Failed to create shell')\n"
        "App.ActiveDocument.addObject('Part::Feature','Shell').Shape=_.removeSplitter()\n"
        "del _\n"
    ).arg(list);

    Gui::Application::Instance->activeDocument()->openCommand("Shell");
    Gui::Application::Instance->runPythonCode((const char*)cmd.toAscii(), false, false);
    Gui::Application::Instance->activeDocument()->commitCommand();
}
Example #7
0
void CmdRobotEdge2Trac::activated(int iMsg)
{
     
 /*   App::DocumentObject *obj = this->getDocument()->getObject(FeatName.c_str());
    App::Property *prop = &(dynamic_cast<Robot::Edge2TracObject *>(obj)->Source); 

    Gui::TaskView::TaskDialog* dlg = new TaskDlgEdge2Trac(dynamic_cast<Robot::Edge2TracObject *>(obj));
    Gui::Control().showDialog(dlg);*/


    Gui::SelectionFilter ObjectFilter("SELECT Robot::Edge2TracObject COUNT 1");
    Gui::SelectionFilter EdgeFilter  ("SELECT Part::Feature SUBELEMENT Edge COUNT 1..");

    if (ObjectFilter.match()) {
        Robot::Edge2TracObject *EdgeObj = static_cast<Robot::Edge2TracObject*>(ObjectFilter.Result[0][0].getObject());
        openCommand("Edit Edge2TracObject");
        doCommand(Gui,"Gui.activeDocument().setEdit('%s')",EdgeObj->getNameInDocument());
    }else if (EdgeFilter.match()) {
        // get the selected object
        //Part::Feature *part = static_cast<Part::Feature*>(EdgeFilter.Result[0][0].getObject());
        std::string obj_sub = EdgeFilter.Result[0][0].getAsPropertyLinkSubString();

        std::string FeatName = getUniqueObjectName("Edge2Trac");

        openCommand("Create a new Edge2TracObject");
        doCommand(Doc,"App.activeDocument().addObject('Robot::Edge2TracObject','%s')",FeatName.c_str());
        doCommand(Gui,"App.activeDocument().%s.Source = %s",FeatName.c_str(),obj_sub.c_str());
        doCommand(Gui,"Gui.activeDocument().setEdit('%s')",FeatName.c_str());

    }else {
        std::string FeatName = getUniqueObjectName("Edge2Trac");

        openCommand("Create a new Edge2TracObject");
        doCommand(Doc,"App.activeDocument().addObject('Robot::Edge2TracObject','%s')",FeatName.c_str());
        doCommand(Gui,"Gui.activeDocument().setEdit('%s')",FeatName.c_str());
    }
 


}
Example #8
0
void CmdFemCreateNodesSet::activated(int iMsg)
{
     
    Gui::SelectionFilter ObjectFilter("SELECT Fem::FemSetNodesObject COUNT 1");
    Gui::SelectionFilter FemMeshFilter  ("SELECT Fem::FemMeshObject COUNT 1");

    if (ObjectFilter.match()) {
        Fem::FemSetNodesObject *NodesObj = static_cast<Fem::FemSetNodesObject*>(ObjectFilter.Result[0][0].getObject());
        openCommand("Edit nodes-set");
        doCommand(Gui,"Gui.activeDocument().setEdit('%s')",NodesObj->getNameInDocument());
    }else if (FemMeshFilter.match()) {
        Fem::FemMeshObject *MeshObj = static_cast<Fem::FemMeshObject*>(FemMeshFilter.Result[0][0].getObject());

        std::string FeatName = getUniqueObjectName("NodesSet");

        openCommand("Create a new nodes-set");
        doCommand(Doc,"App.activeDocument().addObject('Fem::FemSetNodesObject','%s')",FeatName.c_str());
        doCommand(Gui,"App.activeDocument().%s.FemMesh = App.activeDocument().%s",FeatName.c_str(),MeshObj->getNameInDocument());
        doCommand(Gui,"Gui.activeDocument().setEdit('%s')",FeatName.c_str());

    }
}
Example #9
0
bool SweepWidget::accept()
{
    if (d->loop.isRunning())
        return false;
    Gui::SelectionFilter edgeFilter  ("SELECT Part::Feature SUBELEMENT Edge COUNT 1..");
    Gui::SelectionFilter partFilter  ("SELECT Part::Feature COUNT 1");
    bool matchEdge = edgeFilter.match();
    bool matchPart = partFilter.match();
    if (!matchEdge && !matchPart) {
        QMessageBox::critical(this, tr("Sweep path"), tr("Select one or more connected edges you want to sweep along."));
        return false;
    }

    // get the selected object
    std::string selection;
    std::string spineObject, spineLabel;
    const std::vector<Gui::SelectionObject>& result = matchEdge
        ? edgeFilter.Result[0] : partFilter.Result[0];
    selection = result.front().getAsPropertyLinkSubString();
    spineObject = result.front().getFeatName();
    spineLabel = result.front().getObject()->Label.getValue();

    QString list, solid, frenet;
    if (d->ui.checkSolid->isChecked())
        solid = QString::fromLatin1("True");
    else
        solid = QString::fromLatin1("False");

    if (d->ui.checkFrenet->isChecked())
        frenet = QString::fromLatin1("True");
    else
        frenet = QString::fromLatin1("False");

    QTextStream str(&list);

    int count = d->ui.selector->selectedTreeWidget()->topLevelItemCount();
    if (count < 1) {
        QMessageBox::critical(this, tr("Too few elements"), tr("At least one edge or wire is required."));
        return false;
    }
    for (int i=0; i<count; i++) {
        QTreeWidgetItem* child = d->ui.selector->selectedTreeWidget()->topLevelItem(i);
        QString name = child->data(0, Qt::UserRole).toString();
        if (name == QLatin1String(spineObject.c_str())) {
            QMessageBox::critical(this, tr("Wrong selection"), tr("'%1' cannot be used as profile and path.")
                .arg(QString::fromUtf8(spineLabel.c_str())));
            return false;
        }
        str << "App.getDocument('" << d->document.c_str() << "')." << name << ", ";
    }

    try {
        Gui::WaitCursor wc;
        QString cmd;
        cmd = QString::fromLatin1(
            "App.getDocument('%5').addObject('Part::Sweep','Sweep')\n"
            "App.getDocument('%5').ActiveObject.Sections=[%1]\n"
            "App.getDocument('%5').ActiveObject.Spine=%2\n"
            "App.getDocument('%5').ActiveObject.Solid=%3\n"
            "App.getDocument('%5').ActiveObject.Frenet=%4\n"
            )
            .arg(list)
            .arg(QLatin1String(selection.c_str()))
            .arg(solid)
            .arg(frenet)
            .arg(QString::fromLatin1(d->document.c_str()));

        Gui::Document* doc = Gui::Application::Instance->getDocument(d->document.c_str());
        if (!doc) throw Base::Exception("Document doesn't exist anymore");
        doc->openCommand("Sweep");
        Gui::Application::Instance->runPythonCode((const char*)cmd.toLatin1(), false, false);
        doc->getDocument()->recompute();
        App::DocumentObject* obj = doc->getDocument()->getActiveObject();
        if (obj && !obj->isValid()) {
            std::string msg = obj->getStatusString();
            doc->abortCommand();
            throw Base::Exception(msg);
        }
        doc->commitCommand();
    }
    catch (const Base::Exception& e) {
        QMessageBox::warning(this, tr("Input error"), QString::fromLatin1(e.what()));
        return false;
    }

    return true;
}
Example #10
0
void CmdSketcherNewSketch::activated(int iMsg)
{
    Gui::SelectionFilter SketchFilter("SELECT Sketcher::SketchObject COUNT 1");
    Gui::SelectionFilter FaceFilter  ("SELECT Part::Feature SUBELEMENT Face COUNT 1");

    if (SketchFilter.match()) {
        Sketcher::SketchObject *Sketch = static_cast<Sketcher::SketchObject*>(SketchFilter.Result[0][0].getObject());
        openCommand("Edit Sketch");
        doCommand(Gui,"Gui.activeDocument().setEdit('%s')",Sketch->getNameInDocument());
    }
    else if (FaceFilter.match()) {
        // get the selected object
        Part::Feature *part = static_cast<Part::Feature*>(FaceFilter.Result[0][0].getObject());
        Base::Placement ObjectPos = part->Placement.getValue();
        const std::vector<std::string> &sub = FaceFilter.Result[0][0].getSubNames();
        if (sub.size() > 1){
            // No assert for wrong user input!
            QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Several sub-elements selected"),
                QObject::tr("You have to select a single face as support for a sketch!"));
            return;
        }
        // get the selected sub shape (a Face)
        const Part::TopoShape &shape = part->Shape.getValue();
        TopoDS_Shape sh = shape.getSubShape(sub[0].c_str());
        const TopoDS_Face& face = TopoDS::Face(sh);
        if (face.IsNull()){
            // No assert for wrong user input!
            QMessageBox::warning(Gui::getMainWindow(), QObject::tr("No support face selected"),
                QObject::tr("You have to select a face as support for a sketch!"));
            return;
        }

        BRepAdaptor_Surface adapt(face);
        if (adapt.GetType() != GeomAbs_Plane){
            QMessageBox::warning(Gui::getMainWindow(), QObject::tr("No planar support"),
                QObject::tr("You need a planar face as support for a sketch!"));
            return;
        }

        std::string supportString = FaceFilter.Result[0][0].getAsPropertyLinkSubString();

        // create Sketch on Face
        std::string FeatName = getUniqueObjectName("Sketch");

        openCommand("Create a Sketch on Face");
        doCommand(Doc,"App.activeDocument().addObject('Sketcher::SketchObject','%s')",FeatName.c_str());
        doCommand(Gui,"App.activeDocument().%s.Support = %s",FeatName.c_str(),supportString.c_str());
        doCommand(Gui,"App.activeDocument().recompute()");  // recompute the sketch placement based on its support
        //doCommand(Gui,"Gui.activeDocument().activeView().setCamera('%s')",cam.c_str());
        doCommand(Gui,"Gui.activeDocument().setEdit('%s')",FeatName.c_str());
    }
    else {
        // ask user for orientation
        SketchOrientationDialog Dlg;

        if (Dlg.exec() != QDialog::Accepted)
            return; // canceled
        Base::Vector3d p = Dlg.Pos.getPosition();
        Base::Rotation r = Dlg.Pos.getRotation();

        // do the right view direction
        std::string camstring;
        switch(Dlg.DirType){
            case 0:
                camstring = "#Inventor V2.1 ascii \\n OrthographicCamera {\\n viewportMapping ADJUST_CAMERA \\n position 0 0 87 \\n orientation 0 0 1  0 \\n nearDistance -112.88701 \\n farDistance 287.28702 \\n aspectRatio 1 \\n focalDistance 87 \\n height 143.52005 }";
                break;
            case 1:
                camstring = "#Inventor V2.1 ascii \\n OrthographicCamera {\\n viewportMapping ADJUST_CAMERA \\n position 0 0 -87 \\n orientation -1 0 0  3.1415927 \\n nearDistance -112.88701 \\n farDistance 287.28702 \\n aspectRatio 1 \\n focalDistance 87 \\n height 143.52005 }";
                break;
            case 2:
                camstring = "#Inventor V2.1 ascii \\n OrthographicCamera {\\n viewportMapping ADJUST_CAMERA\\n  position 0 -87 0 \\n  orientation -1 0 0  4.712389\\n  nearDistance -112.88701\\n  farDistance 287.28702\\n  aspectRatio 1\\n  focalDistance 87\\n  height 143.52005\\n\\n}";
                break;
            case 3:
                camstring = "#Inventor V2.1 ascii \\n OrthographicCamera {\\n viewportMapping ADJUST_CAMERA\\n  position 0 87 0 \\n  orientation 0 0.70710683 0.70710683  3.1415927\\n  nearDistance -112.88701\\n  farDistance 287.28702\\n  aspectRatio 1\\n  focalDistance 87\\n  height 143.52005\\n\\n}";
                break;
            case 4:
                camstring = "#Inventor V2.1 ascii \\n OrthographicCamera {\\n viewportMapping ADJUST_CAMERA\\n  position 87 0 0 \\n  orientation 0.57735026 0.57735026 0.57735026  2.0943952 \\n  nearDistance -112.887\\n  farDistance 287.28699\\n  aspectRatio 1\\n  focalDistance 87\\n  height 143.52005\\n\\n}";
                break;
            case 5:
                camstring = "#Inventor V2.1 ascii \\n OrthographicCamera {\\n viewportMapping ADJUST_CAMERA\\n  position -87 0 0 \\n  orientation -0.57735026 0.57735026 0.57735026  4.1887903 \\n  nearDistance -112.887\\n  farDistance 287.28699\\n  aspectRatio 1\\n  focalDistance 87\\n  height 143.52005\\n\\n}";
                break;
        }
        std::string FeatName = getUniqueObjectName("Sketch");

        openCommand("Create a new Sketch");
        doCommand(Doc,"App.activeDocument().addObject('Sketcher::SketchObject','%s')",FeatName.c_str());
        doCommand(Doc,"App.activeDocument().%s.Placement = App.Placement(App.Vector(%f,%f,%f),App.Rotation(%f,%f,%f,%f))",FeatName.c_str(),p.x,p.y,p.z,r[0],r[1],r[2],r[3]);
        doCommand(Gui,"Gui.activeDocument().activeView().setCamera('%s')",camstring.c_str());
        doCommand(Gui,"Gui.activeDocument().setEdit('%s')",FeatName.c_str());
    }

}
Example #11
0
void CmdSketcherMapSketch::activated(int iMsg)
{
    App::Document* doc = App::GetApplication().getActiveDocument();
    std::vector<App::DocumentObject*> sel = doc->getObjectsOfType(Sketcher::SketchObject::getClassTypeId());
    if (sel.empty()) {
        QMessageBox::warning(Gui::getMainWindow(),
            qApp->translate(className(), "No sketch found"),
            qApp->translate(className(), "The document doesn't have a sketch"));
        return;
    }

    bool ok;
    QStringList items;
    for (std::vector<App::DocumentObject*>::iterator it = sel.begin(); it != sel.end(); ++it)
        items.push_back(QString::fromUtf8((*it)->Label.getValue()));
    QString text = QInputDialog::getItem(Gui::getMainWindow(),
        qApp->translate(className(), "Select sketch"),
        qApp->translate(className(), "Select a sketch from the list"),
        items, 0, false, &ok);
    if (!ok) return;
    int index = items.indexOf(text);

    std::string featName = sel[index]->getNameInDocument();
    Gui::SelectionFilter FaceFilter  ("SELECT Part::Feature SUBELEMENT Face COUNT 1");
    if (FaceFilter.match()) {
        // get the selected object
        Part::Feature *part = static_cast<Part::Feature*>(FaceFilter.Result[0][0].getObject());
        Base::Placement ObjectPos = part->Placement.getValue();
        const std::vector<std::string> &sub = FaceFilter.Result[0][0].getSubNames();
        if (sub.size() > 1){
            // No assert for wrong user input!
            QMessageBox::warning(Gui::getMainWindow(),
                qApp->translate(className(),"Several sub-elements selected"),
                qApp->translate(className(),"You have to select a single face as support for a sketch!"));
            return;
        }

        std::vector<App::DocumentObject*> input = part->getOutList();
        if (std::find(input.begin(), input.end(), sel[index]) != input.end()) {
            QMessageBox::warning(Gui::getMainWindow(),
                qApp->translate(className(),"Cyclic dependency"),
                qApp->translate(className(),"You cannot choose a support object depending on the selected sketch!"));
            return;
        }

        // get the selected sub shape (a Face)
        const Part::TopoShape &shape = part->Shape.getValue();
        TopoDS_Shape sh = shape.getSubShape(sub[0].c_str());
        const TopoDS_Face& face = TopoDS::Face(sh);
        if (face.IsNull()) {
            // No assert for wrong user input!
            QMessageBox::warning(Gui::getMainWindow(),
                qApp->translate(className(),"No support face selected"),
                qApp->translate(className(),"You have to select a face as support for a sketch!"));
            return;
        }

        BRepAdaptor_Surface adapt(face);
        if (adapt.GetType() != GeomAbs_Plane){
            QMessageBox::warning(Gui::getMainWindow(),
                qApp->translate(className(),"No planar support"),
                qApp->translate(className(),"You need a planar face as support for a sketch!"));
            return;
        }

        std::string supportString = FaceFilter.Result[0][0].getAsPropertyLinkSubString();

        openCommand("Map a Sketch on Face");
        doCommand(Gui,"App.activeDocument().%s.Support = %s",featName.c_str(),supportString.c_str());
        doCommand(Gui,"App.activeDocument().recompute()");
        doCommand(Gui,"Gui.activeDocument().setEdit('%s')",featName.c_str());
    }
    else {
        QMessageBox::warning(Gui::getMainWindow(),
            qApp->translate(className(), "No face selected"),
            qApp->translate(className(), "No face was selected to map the sketch to"));
    }
}
Example #12
0
bool SweepWidget::accept()
{
    Gui::SelectionFilter edgeFilter  ("SELECT Part::Feature SUBELEMENT Edge COUNT 1..");
    Gui::SelectionFilter partFilter  ("SELECT Part::Feature COUNT 1");
    bool matchEdge = edgeFilter.match();
    bool matchPart = partFilter.match();
    if (!matchEdge && !matchPart) {
        QMessageBox::critical(this, tr("Sweep path"), tr("Select an edge or wire you want to sweep along."));
        return false;
    }

    // get the selected object
    std::string selection;
    if (matchEdge) {
        const std::vector<Gui::SelectionObject>& result = edgeFilter.Result[0];
        selection = result.front().getAsPropertyLinkSubString();
    }
    else {
        const std::vector<Gui::SelectionObject>& result = partFilter.Result[0];
        selection = result.front().getAsPropertyLinkSubString();
    }

    QString list, solid, frenet;
    if (d->ui.checkSolid->isChecked())
        solid = QString::fromAscii("True");
    else
        solid = QString::fromAscii("False");

    if (d->ui.checkFrenet->isChecked())
        frenet = QString::fromAscii("True");
    else
        frenet = QString::fromAscii("False");

    QTextStream str(&list);

    int count = d->ui.selector->selectedTreeWidget()->topLevelItemCount();
    if (count < 1) {
        QMessageBox::critical(this, tr("Too few elements"), tr("At least one edge or wire is required."));
        return false;
    }
    for (int i=0; i<count; i++) {
        QTreeWidgetItem* child = d->ui.selector->selectedTreeWidget()->topLevelItem(i);
        QString name = child->data(0, Qt::UserRole).toString();
        str << "App.getDocument('" << d->document.c_str() << "')." << name << ", ";
    }

    try {
        QString cmd;
        cmd = QString::fromAscii(
            "App.getDocument('%5').addObject('Part::Sweep','Sweep')\n"
            "App.getDocument('%5').ActiveObject.Sections=[%1]\n"
            "App.getDocument('%5').ActiveObject.Spine=%2\n"
            "App.getDocument('%5').ActiveObject.Solid=%3\n"
            "App.getDocument('%5').ActiveObject.Frenet=%4\n"
            )
            .arg(list)
            .arg(QLatin1String(selection.c_str()))
            .arg(solid)
            .arg(frenet)
            .arg(QString::fromAscii(d->document.c_str()));

        Gui::Document* doc = Gui::Application::Instance->getDocument(d->document.c_str());
        if (!doc) throw Base::Exception("Document doesn't exist anymore");
        doc->openCommand("Sweep");
        Gui::Application::Instance->runPythonCode((const char*)cmd.toAscii(), false, false);
        doc->commitCommand();
        doc->getDocument()->recompute();
    }
    catch (const Base::Exception& e) {
        Base::Console().Error("%s\n", e.what());
        return false;
    }

    return true;
}
Example #13
0
void CmdPartRuledSurface::activated(int iMsg)
{
    bool ok = false;
    TopoDS_Shape curve1, curve2;
    std::string link1, link2, obj1, obj2;
    Gui::SelectionFilter edgeFilter  ("SELECT Part::Feature SUBELEMENT Edge COUNT 1..2");
    Gui::SelectionFilter wireFilter  ("SELECT Part::Feature SUBELEMENT Wire COUNT 1..2");
    Gui::SelectionFilter partFilter  ("SELECT Part::Feature COUNT 2");
    bool matchEdge = edgeFilter.match();
    bool matchWire = wireFilter.match();
    if (matchEdge || matchWire) {
        // get the selected object
        const std::vector<Gui::SelectionObject>& result = matchEdge
            ? edgeFilter.Result[0] : wireFilter.Result[0];
        // two edges from one object
        if (result.size() == 1) {
            const Part::Feature* part = static_cast<const Part::Feature*>(result[0].getObject());
            const std::vector<std::string>& edges = result[0].getSubNames();
            if (edges.size() != 2) {
                ok = false;
            }
            else {
                ok = true;
                // get the selected sub-shapes
                const Part::TopoShape& shape = part->Shape.getValue();
                curve1 = shape.getSubShape(edges[0].c_str());
                curve2 = shape.getSubShape(edges[1].c_str());
                obj1 = result[0].getObject()->getNameInDocument();
                link1 = edges[0];
                obj2 = result[0].getObject()->getNameInDocument();
                link2 = edges[1];
            }
        }
        // two objects and one edge per object
        else if (result.size() == 2) {
            const Part::Feature* part1 = static_cast<const Part::Feature*>(result[0].getObject());
            const std::vector<std::string>& edges1 = result[0].getSubNames();
            const Part::Feature* part2 = static_cast<const Part::Feature*>(result[1].getObject());
            const std::vector<std::string>& edges2 = result[1].getSubNames();
            if (edges1.size() != 1 || edges2.size() != 1) {
                ok = false;
            }
            else {
                ok = true;
                const Part::TopoShape& shape1 = part1->Shape.getValue();
                curve1 = shape1.getSubShape(edges1[0].c_str());
                const Part::TopoShape& shape2 = part2->Shape.getValue();
                curve2 = shape2.getSubShape(edges2[0].c_str());
                obj1 = result[0].getObject()->getNameInDocument();
                link1 = edges1[0];
                obj2 = result[1].getObject()->getNameInDocument();
                link2 = edges2[0];
            }
        }
    }
    else if (partFilter.match()) {
        const std::vector<Gui::SelectionObject>& result = partFilter.Result[0];
        const Part::Feature* part1 = static_cast<const Part::Feature*>(result[0].getObject());
        const Part::Feature* part2 = static_cast<const Part::Feature*>(result[1].getObject());
        const Part::TopoShape& shape1 = part1->Shape.getValue();
        curve1 = shape1._Shape;
        const Part::TopoShape& shape2 = part2->Shape.getValue();
        curve2 = shape2._Shape;
        obj1 = part1->getNameInDocument();
        obj2 = part2->getNameInDocument();

        if (!curve1.IsNull() && !curve2.IsNull()) {
            if (curve1.ShapeType() == TopAbs_EDGE &&
                curve2.ShapeType() == TopAbs_EDGE)
                ok = true;
            if (curve1.ShapeType() == TopAbs_WIRE &&
                curve2.ShapeType() == TopAbs_WIRE)
                ok = true;
        }
    }

    if (!ok) {
        QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"),
            QObject::tr("You have to select either two edges or two wires."));
        return;
    }

    openCommand("Create ruled surface");
    doCommand(Doc, "FreeCAD.ActiveDocument.addObject('Part::RuledSurface','Filled shape')");
    doCommand(Doc, "FreeCAD.ActiveDocument.ActiveObject.Curve1=(FreeCAD.ActiveDocument.%s,['%s'])"
                 ,obj1.c_str(), link1.c_str());
    doCommand(Doc, "FreeCAD.ActiveDocument.ActiveObject.Curve2=(FreeCAD.ActiveDocument.%s,['%s'])"
                 ,obj2.c_str(), link2.c_str());
    commitCommand();
    updateActive();
}