Cutting.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2007                                                    *
00003  *   Joachim Zettler <Joachim.Zettler@gmx.de>                              *
00004  *   Werner Mayer <wmayer@users.sourceforge.net>                           *
00005  *   This file is part of the FreeCAD CAx development system.              *
00006  *                                                                         *
00007  *   This library is free software; you can redistribute it and/or         *
00008  *   modify it under the terms of the GNU Library General Public           *
00009  *   License as published by the Free Software Foundation; either          *
00010  *   version 2 of the License, or (at your option) any later version.      *
00011  *                                                                         *
00012  *   This library  is distributed in the hope that it will be useful,      *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015  *   GNU Library General Public License for more details.                  *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU Library General Public     *
00018  *   License along with this library; see the file COPYING.LIB. If not,    *
00019  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00020  *   Suite 330, Boston, MA  02111-1307, USA                                *
00021  *                                                                         *
00022  ***************************************************************************/
00023 
00024 #include "PreCompiled.h"
00025 
00026 #include "Cutting.h"
00027 #include <Mod/Cam/App/ChangeDyna.h> //Only for Testing
00028 #include <Mod/Cam/App/best_fit.h>
00029 #include <Mod/Cam/App/path_simulate.h>
00030 #include <Mod/Cam/App/SpringbackCorrection.h>
00031 #include <Mod/Cam/App/UniGridApprox.h>
00032 #include <Mod/Cam/App/Approx.h>
00033 #include <Mod/Cam/App/deviation.h>
00034 #include <QTimer>
00035 #include <QByteArray>
00036 
00037 
00038 #include <Base/Vector3D.h>
00039 #include <Base/Console.h>
00040 #include <Base/Exception.h>
00041 #include <Base/Parameter.h>
00042 
00043 #include <Gui/ViewProvider.h>
00044 #include <Gui/Selection.h>
00045 #include <Gui/Application.h>
00046 #include <Gui/MainWindow.h>
00047 #include <Gui/View3DInventor.h>
00048 #include <Gui/View3DInventorViewer.h>
00049 
00050 #include <Gui/Selection.h>
00051 #include <Gui/ViewProvider.h>
00052 #include <App/Document.h>
00053 #include <Gui/Document.h>
00054 
00055 #include <TopExp_Explorer.hxx>
00056 #include <BRepAdaptor_Surface.hxx>
00057 #include <BRep_Builder.hxx>
00058 #include <TopoDS_Compound.hxx>
00059 #include <Mod/Cam/App/cutting_tools.h>
00060 #include <Mod/Part/App/PartFeature.h>
00061 #include <Mod/Mesh/App/MeshFeature.h>
00062 #include <Mod/Part/Gui/ViewProvider.h>
00063 
00064 #include <GCPnts_QuasiUniformDeflection.hxx>
00065 #include <GCPnts_QuasiUniformAbscissa.hxx>
00066 
00067 #include <BRepAdaptor_Curve.hxx>
00068 #include <BRepAdaptor_Surface.hxx>
00069 #include <Geom2d_Curve.hxx>
00070 #include <Geom2dAdaptor_Curve.hxx>
00071 #include <Geom_Surface.hxx>
00072 #include <Mod/Mesh/App/Core/Grid.h>
00073 #include <TopoDS_Face.hxx>
00074 #include <TopoDS.hxx>
00075 #include <BRepBuilderAPI_MakeFace.hxx>
00076 
00077 #include <Inventor/events/SoMouseButtonEvent.h>
00078 #include <Inventor/SoPickedPoint.h>
00079 
00080 #include <QMessageBox>
00081 #include <QFileDialog>
00082 
00083 using namespace CamGui;
00084 
00085 Cutting::Cutting(QWidget* parent,Qt::WFlags fl)
00086         :QDialog(parent,fl),m_Process(NULL),
00087                 m_PathSimulate(NULL),m_CuttingAlgo(NULL),
00088                 m_BestFit(NULL),m_MergeData(NULL),m_Deviation(NULL)
00089 {
00090     this->setupUi(this);
00091     m_timer= false;
00092 }
00093 
00094 Cutting::~Cutting()
00095 {
00096     delete m_CuttingAlgo;
00097     delete m_PathSimulate;
00098     delete m_Process;
00099     delete m_BestFit;
00100         delete m_Deviation;
00101         delete m_MergeData;
00102 }
00103 
00104 bool Cutting::getProcessOutput()
00105 {
00106     QByteArray result = m_Process->readAll();
00107     if (result.contains("Error"))
00108     {
00109         m_Process->kill();
00110         QMessageBox::critical(this, tr("FreeCAD CamWorkbench"),
00111                               tr("Fehler bei der Erzeugung\n"),
00112                               QMessageBox::Ok, QMessageBox::NoButton);
00113     }
00114     else if (result.contains("N o r m a l    t e r m i n a t i o n"))
00115     {
00116         QMessageBox::information(this, tr("FreeCAD CamWorkbench"),
00117                                  tr("Dyna-Job finished well\n"),
00118                                  QMessageBox::Ok, QMessageBox::NoButton);
00119     }
00120 
00121     return true;
00122 }
00123 void Cutting::on_adaptdynainput_clicked()
00124 {
00125     //First we have to select the LS-Dyna Masterfile and the current working dir
00126     QString filename, path, program;
00127     QStringList arguments;
00128     QString strcheck("dyna.str");
00129     filename = QFileDialog::getOpenFileName( this, "Open Dyna.Str or Master-Key File",filename,"Ls-Dyna Keywords (*.k) (*.str)" );
00130     if (filename.isNull())
00131         return;
00132     QFileInfo aFileInfo(filename);
00133     path = aFileInfo.absolutePath();
00134     QDir::setCurrent(path);
00135     program = "c:/Program Files/lsdyna/ls971d";
00136     //As the new Versions already account for a proper curve file we no longer have to adapt the input
00137     //if we already have a str File we will not step into the next if-case
00138     //if(!aFileInfo.fileName().contains("dyna.str"))
00139     //{
00140     //    arguments << " i="<< aFileInfo.fileName();
00141     //    m_Process = new QProcess(this);
00142     //    m_Process->start(program, arguments);
00143     //    //Now we check if the output is written correctly
00144     //    m_Process->waitForFinished(50000);
00145     //    aFileInfo.setFile("dyna.str");
00146     //    if (aFileInfo.size() == 0) //the file does not exist
00147     //    {
00148     //        QMessageBox::critical(this, tr("FreeCAD CamWorkbench"),
00149     //                          tr("Fehler bei der Erzeugung vom Struct File\n"),
00150     //                          QMessageBox::Ok, QMessageBox::NoButton);
00151     //        return;
00152     //    }
00153     //    else
00154     //    {
00155     //        QMessageBox::information(this, tr("FreeCAD CamWorkbench"),
00156     //                             tr("Structured-Dyna gut erzeugt\n"),
00157     //                             QMessageBox::Ok, QMessageBox::NoButton);
00158     //    }
00159     //}
00160     //ChangeDyna aFileChanger;
00161     //if (aFileChanger.Read("dyna.str"))
00162     // start_simulation->show();
00163     //else{
00164     //        QMessageBox::critical(this, tr("FreeCAD CamWorkbench"),
00165     //                          tr("Error while parsing the str File\n"),
00166     //                          QMessageBox::Ok, QMessageBox::NoButton);
00167     //        return;
00168     //}
00169 
00170 }
00171 
00172 void Cutting::on_start_simulation_clicked()
00173 {
00174     //check if the initial process is already killed
00175     m_Process->kill();
00176     QString program;
00177     QStringList arguments;
00178     program = "c:/Program Files/lsdyna/ls971d";
00179     arguments << " i=dyna2.str";
00180     connect(m_Process,SIGNAL(readyReadStandardError()),this,SLOT(getProcessOutput()));
00181     connect(m_Process,SIGNAL(readyReadStandardOutput()),this,SLOT(getProcessOutput()));
00182     m_Process->start(program, arguments);
00183 }
00184 
00185 void Cutting::on_Deviation_button_clicked()
00186 {
00187         m_Deviation = new Deviation();
00188         deviation_geometry1_button->setEnabled(true);
00189         deviation_geometry2_button->setEnabled(true);
00190         deviation_go_button->setEnabled(true);
00191 }
00192 
00193 void Cutting::on_deviation_geometry1_button_clicked()
00194 {
00195         selectShape();
00196 }
00197 
00198 void Cutting::on_deviation_geometry2_button_clicked()
00199 {
00200         selectMesh();
00201 }
00202 
00203 void Cutting::on_deviation_go_button_clicked()
00204 {
00205         QString current_filename = QFileDialog::getSaveFileName(this,"Select Deviation Files","","*.txt");
00206         m_Deviation->ImportGeometry(m_Shape, m_Mesh);
00207         m_Deviation->Compute();
00208         
00209         
00210         m_Deviation->WriteOutput(current_filename);
00211         
00212 }
00213 
00214 void Cutting::on_error_accumulation_select_files_button_clicked()
00215 {
00216         m_MergeData = new MergeData();
00217 
00218         QStringList m_dateinamen = QFileDialog::getOpenFileNames(
00219                 this,
00220                 "Select one or more files to open",
00221                 "c:",
00222                 "Deviation Files (*.txt)");
00223         
00224         if (!m_dateinamen.isEmpty())
00225         {
00226                 if (!m_MergeData->Einlesen(m_dateinamen))
00227                 {
00228                         QMessageBox::information(this, tr("FreeCAD CamWorkbench"),
00229                                 tr("Alles i.O. Output kann erzeugt werden\n"),
00230                                 QMessageBox::Ok, QMessageBox::NoButton);
00231                 }
00232         
00233                 
00234         }
00235         error_accumulation_go_button->setEnabled(true);
00236         
00237 }
00238 
00239 void Cutting::on_error_accumulation_go_button_clicked()
00240 {
00241                 QString current_filename = QFileDialog::getSaveFileName(this,"Select Output File","","*.txt");
00242 
00243         m_MergeData->WriteOutput(current_filename);
00244 
00245 }
00246 
00247 void Cutting::selectShape()
00248 {
00249     if (!m_timer)
00250     {
00251 
00252         int check_box1=0,check_box2=0;
00253         if (!m_Shape.IsNull())
00254         {
00255             check_box1 = QMessageBox::question(this, tr("FreeCAD CamWorkbench"),
00256                                                tr("You have already selected a CAD-Shape.\n"
00257                                                   "Do you want to make a new Selection?"),
00258                                                QMessageBox::Yes, QMessageBox::No);
00259         }
00260         else
00261         {
00262             check_box2 = QMessageBox::information(this, tr("FreeCAD CamWorkbench"),
00263                                                   tr("You have to select a CAD-Shape.\n"),
00264                                                   QMessageBox::Ok, QMessageBox::Cancel);
00265         }
00266         if ((check_box1 == QMessageBox::Yes) || (check_box2 == QMessageBox::Ok))
00267         {
00268             //First, remove the old selection from the Gui, so that we do not directly have the same CAD once again.
00269             Gui::Selection().clearCompleteSelection();
00270             //to make a Selection more easy, hide the dialog
00271             this->hide();
00272             QTimer::singleShot(100,this,SLOT(selectShape()));
00273             m_timer = true;
00274         }
00275     }
00276     else
00277     {
00278         std::vector<App::DocumentObject*> fea = Gui::Selection().getObjectsOfType(Part::Feature::getClassTypeId());
00279         if ( fea.size() == 1)
00280         {
00281             m_Shape = static_cast<Part::Feature*>(fea.front())->Shape.getValue();
00282             //std::vector<Gui::SelectionSingleton::SelObj> aSelection = Gui::Selection().getSelection();
00283             this->show();
00284             CalculateZLevel->setEnabled(true);
00285             CalculateFeatureBased->setEnabled(true);
00286             CalculateSpiralBased->setEnabled(true);
00287             m_timer = false;
00288         }
00289         else
00290         {
00291             QTimer::singleShot(100,this,SLOT(selectShape()));
00292             m_timer = true;
00293         }
00294     }
00295 }
00296 
00297 void Cutting::selectMesh()
00298 {
00299     if (!m_timer)
00300     {
00301         int check_box1=0,check_box2=0;
00302         if (m_Mesh.CountPoints() > 0)
00303         {
00304             check_box1 = QMessageBox::question(this, tr("FreeCAD CamWorkbench"),
00305                                                tr("You have already selected a Mesh.\n"
00306                                                   "Do you want to make a new Selection?"),
00307                                                QMessageBox::Yes, QMessageBox::No);
00308         }
00309         else
00310         {
00311             check_box2 = QMessageBox::information(this, tr("FreeCAD CamWorkbench"),
00312                                                   tr("You have to select a Mesh.\n"),
00313                                                   QMessageBox::Ok, QMessageBox::Cancel);
00314         }
00315         if ((check_box1 == QMessageBox::Yes) || (check_box2 == QMessageBox::Ok))
00316         {
00317             //First, remove the old selection from the Gui, so that we do not directly have the same CAD once again.
00318             Gui::Selection().clearCompleteSelection();
00319             //to make a Selection more easy, hide the dialog
00320             this->hide();
00321             QTimer::singleShot(100,this,SLOT(selectMesh()));
00322             m_timer = true;
00323         }
00324     }
00325     else
00326     {
00327         std::vector<App::DocumentObject*> fea = Gui::Selection().getObjectsOfType(Mesh::Feature::getClassTypeId());
00328         if ( fea.size() == 1)
00329         {
00330             m_Mesh = static_cast<Mesh::Feature*>(fea.front())->Mesh.getValue().getKernel();
00331             //std::vector<Gui::SelectionSingleton::SelObj> aSelection = Gui::Selection().getSelection();
00332             this->show();
00333             m_timer = false;
00334         }
00335         else
00336         {
00337             QTimer::singleShot(100,this,SLOT(selectMesh()));
00338             m_timer = true;
00339         }
00340     }
00341 }
00342 
00343 
00344 void Cutting::setFace(const TopoDS_Shape& aShape, const float x, const float y, const float z)
00345 {
00346     //check if a Shape is selected
00347     std::vector<App::DocumentObject*> fea = Gui::Selection().getObjectsOfType(Part::Feature::getClassTypeId());
00348     if ( fea.size() == 1)
00349     {
00350         int test = aShape.ShapeType();
00351         //get Hash Code of Selected Face inside the selected Shape and also the Coordinates of the click
00352         if (aShape.ShapeType() != TopAbs_FACE)
00353         {
00354             QMessageBox::information(this, tr("FreeCAD CamWorkbench"), tr("You have to select a Face!!\n"));
00355             return;
00356         }
00357 
00358         TopoDS_Face tempFace = TopoDS::Face(aShape);
00359         //Now search for the Hash-Code in the m_Shape
00360         TopExp_Explorer anExplorer;
00361         TopoDS_Face aselectedFace;
00362 
00363         //pickPoint.Set(x,y,z);
00364         for (anExplorer.Init(m_Shape,TopAbs_FACE);anExplorer.More();anExplorer.Next())
00365         {
00366             if (tempFace.HashCode(IntegerLast()) == anExplorer.Current().HashCode(IntegerLast()))
00367             {
00368                 if (m_selection == Springback)
00369                     (m_Spring->m_FixFaces).push_back(TopoDS::Face(anExplorer.Current()));
00370                 else if (m_selection == BestFit)
00371                     (m_BestFit->m_LowFaces).push_back(TopoDS::Face(anExplorer.Current()));
00372                 else if (m_selection == ToolpathCalculation)
00373                     m_CuttingAlgo->SetMachiningOrder(TopoDS::Face(anExplorer.Current()),x,y,z);
00374                 break;
00375             }
00376         }
00377     }
00378 }
00379 
00380 void Cutting::on_CalculateZLevel_clicked()
00381 {
00382     //Cutting-Klasse instanzieren
00383     if (m_CuttingAlgo == NULL)
00384         m_CuttingAlgo = new cutting_tools(m_Shape);
00385     else
00386     {
00387         delete m_CuttingAlgo;
00388         m_CuttingAlgo = new cutting_tools(m_Shape);
00389     }
00390     m_Mode = 1;
00391     CalculateFeatureBased->setEnabled(false);
00392     CalculateSpiralBased->setEnabled(false);
00393     toolpath_calculation_highest_level_button->setEnabled(true);
00394     m_selection = ToolpathCalculation;
00395 }
00396 
00397 void Cutting::on_CalculateFeatureBased_clicked()
00398 {
00399     if (m_CuttingAlgo == NULL)
00400         m_CuttingAlgo = new cutting_tools(m_Shape);
00401     else
00402     {
00403         delete m_CuttingAlgo;
00404         m_CuttingAlgo = new cutting_tools(m_Shape);
00405     }
00406     m_Mode = 2;
00407     toolpath_calculation_highest_level_button->setEnabled(true);
00408     m_selection = ToolpathCalculation;
00409     CalculateZLevel->setEnabled(false);
00410     CalculateSpiralBased->setEnabled(false);
00411 }
00412 
00413 void Cutting::on_CalculateSpiralBased_clicked()
00414 {
00415     if (m_CuttingAlgo == NULL)
00416         m_CuttingAlgo = new cutting_tools(m_Shape);
00417     else
00418     {
00419         delete m_CuttingAlgo;
00420         m_CuttingAlgo = new cutting_tools(m_Shape);
00421     }
00422     m_Mode = 3;//
00423     toolpath_calculation_highest_level_button->setEnabled(true);
00424     m_selection = ToolpathCalculation;
00425     CalculateZLevel->setEnabled(false);
00426     CalculateFeatureBased->setEnabled(false);
00427 
00428 }
00429 
00430 void Cutting::on_select_shape_z_level_button_clicked()
00431 {
00432     selectShape();
00433 }
00434 
00435 void Cutting::on_select_shape_feature_based_button_clicked()
00436 {
00437     selectShape();
00438 }
00439 
00440 void Cutting::on_select_shape_spiral_based_button_clicked()
00441 {
00442     selectShape();
00443 }
00444 
00445 void Cutting::on_toolpath_calculation_highest_level_button_clicked()
00446 {
00447     Gui::Document* doc = Gui::Application::Instance->activeDocument();
00448     Gui::View3DInventor* view = static_cast<Gui::View3DInventor*>(doc->getActiveView());
00449     if (view)
00450     {
00451         Gui::View3DInventorViewer* viewer = view->getViewer();
00452         viewer->setEditing(true);
00453         viewer->addEventCallback(SoMouseButtonEvent::getClassTypeId(), zLevelCallback, this);
00454         QMessageBox::information(this, tr("FreeCAD CamWorkbench"), tr("You have to pick a point.\n"));
00455         this->hide();
00456     }
00457     toolpath_calculation_middle_level_button->setEnabled(true);
00458     toolpath_calculation_lowest_level_button->setEnabled(true);
00459 }
00460 
00461 void Cutting::on_toolpath_calculation_middle_level_button_clicked()
00462 {
00463     Gui::Document* doc = Gui::Application::Instance->activeDocument();
00464     Gui::View3DInventor* view = static_cast<Gui::View3DInventor*>(doc->getActiveView());
00465     if (view)
00466     {
00467         Gui::View3DInventorViewer* viewer = view->getViewer();
00468         viewer->setEditing(true);
00469         viewer->addEventCallback(SoMouseButtonEvent::getClassTypeId(), zLevelCallback, this);
00470         QMessageBox::information(this, tr("FreeCAD CamWorkbench"), tr("You have to pick a point.\n"));
00471         this->hide();
00472     }
00473 }
00474 
00475 void Cutting::on_toolpath_calculation_lowest_level_button_clicked()
00476 {
00477     Gui::Document* doc = Gui::Application::Instance->activeDocument();
00478     Gui::View3DInventor* view = static_cast<Gui::View3DInventor*>(doc->getActiveView());
00479     if (view)
00480     {
00481         Gui::View3DInventorViewer* viewer = view->getViewer();
00482         viewer->setEditing(true);
00483         viewer->addEventCallback(SoMouseButtonEvent::getClassTypeId(), zLevelCallback, this);
00484         QMessageBox::information(this, tr("FreeCAD CamWorkbench"), tr("You have to pick a point.\n"));
00485         this->hide();
00486     }
00487     toolpath_calculation_go_button->setEnabled(true);
00488 }
00489 
00490 void Cutting::on_toolpath_calculation_go_button_clicked()
00491 {
00492     //Do the actual Cut
00493     //First transfer the settings to the Cutting_tools class
00494     m_CuttingAlgo->m_UserSettings = getSettings();
00495 
00496     if (!m_CuttingAlgo->arrangecuts_ZLEVEL())
00497     {
00498         std::cout << "Konnte nicht sauber schneiden" << std::endl;
00499     }
00500 
00501     bool ok = true;
00502     try
00503     {
00504         switch (m_Mode)
00505         {
00506         case 1:
00507             ok = m_CuttingAlgo->OffsetWires_Standard();
00508             break;
00509         case 2:
00510             ok = m_CuttingAlgo->OffsetWires_FeatureBased();
00511             break;
00512         case 3:
00513             ok = m_CuttingAlgo->OffsetWires_Spiral();
00514             break;
00515         }
00516     }
00517     catch (...)
00518     {
00519         std::cout<<"Fehler"<<std::endl;
00520     }
00521     if (!ok)
00522     {
00523         QMessageBox::critical(this, tr("FreeCAD CamWorkbench"),
00524                               tr("Irgendwas stimmt nicht. Nochmal alles neu versuchen\n"),
00525                               QMessageBox::Ok, QMessageBox::NoButton);
00526         delete m_CuttingAlgo;
00527         m_CuttingAlgo = new cutting_tools(m_Shape);
00528         return;
00529     }
00530     DisplayCAMOutput();
00531     GenRobotOut->setEnabled(true);
00532     GenSimOut->setEnabled(true);
00533 }
00534 
00535 void Cutting::on_GenSimOut_clicked()
00536 {
00537     QString dir = QFileDialog::getExistingDirectory(this, tr("Select Simulation-Path Output-Directory"),"d:/",
00538                   QFileDialog::ShowDirsOnly
00539                   | QFileDialog::DontResolveSymlinks);
00540     if (dir.isNull())
00541         return;
00542     QDir::setCurrent(dir);
00543     if (m_PathSimulate != NULL) delete m_PathSimulate;//If it exists already
00544     m_PathSimulate = new path_simulate(*(m_CuttingAlgo->getOutputhigh()),*(m_CuttingAlgo->getOutputlow()),m_CuttingAlgo->m_UserSettings);
00545     switch (m_Mode)
00546     {
00547     case 1:
00548         if (m_PathSimulate->MakePathSimulate())
00549             adaptdynainput->setEnabled(true);
00550         break;
00551     case 2:
00552         if (m_PathSimulate->MakePathSimulate_Feat(m_CuttingAlgo->getFlatAreas(),0))
00553             adaptdynainput->setEnabled(true);
00554         break;
00555      case 3:
00556         if (m_PathSimulate->MakePathSimulate_Feat(m_CuttingAlgo->getFlatAreas(),1))
00557             adaptdynainput->setEnabled(true);
00558     break;
00559     }
00560 
00561 }
00562 
00563 void Cutting::on_GenRobotOut_clicked()
00564 {
00565     QString dir = QFileDialog::getExistingDirectory(this, tr("Select File Output-Directory"),"d:/",
00566                   QFileDialog::ShowDirsOnly
00567                   | QFileDialog::DontResolveSymlinks);
00568     QDir::setCurrent(dir);
00569     if (m_PathSimulate != NULL) delete m_PathSimulate;
00570     m_PathSimulate = new path_simulate(*(m_CuttingAlgo->getOutputhigh()),*(m_CuttingAlgo->getOutputlow()),m_CuttingAlgo->m_UserSettings);
00571 
00572     switch (m_Mode)
00573     {
00574     case 1:
00575         m_PathSimulate->MakePathRobot();
00576         break;
00577     case 2:
00578         m_PathSimulate->MakePathRobot_Feat(m_CuttingAlgo->getFlatAreas());
00579         break;
00580     }
00581 }
00582 
00583 
00584 const CuttingToolsSettings& Cutting::getSettings()
00585 {
00586     //First transfer the settings to the Cutting_tools class
00587     m_Settings.cad_radius = cad_radius_box->value();
00588     m_Settings.correction_factor = correction_factor_box->value();
00589     m_Settings.level_distance = level_distance_box->value();
00590     m_Settings.limit_angle = limit_angle_box->value();
00591     m_Settings.sheet_thickness = sheet_thickness_box->value();
00592     m_Settings.slave_radius = slave_radius_box->value();
00593     m_Settings.master_radius = master_radius_box->value();
00594     m_Settings.max_Vel = max_vel->value();
00595     m_Settings.max_Acc = max_acc->value();
00596     m_Settings.spring_pretension = spring_pretension->value();
00597     m_Settings.x_offset_robot = xoffset_box->value();
00598     m_Settings.y_offset_robot = yoffset_box->value();
00599     m_Settings.clockwise = clockwise_checkbox->isChecked();
00600     m_Settings.error_tolerance = error_tolerance->value();
00601 
00602     return m_Settings;
00603 }
00604 
00605 
00606 void Cutting::on_BestFitButton_clicked()
00607 {
00608 
00609 
00610 
00611 
00612 
00613 
00614     m_selection = BestFit;
00615     m_BestFit = new best_fit();
00616 
00617 
00618         // Best-Fit based on Point-Clouds
00619         m_BestFit->Initialize_Mesh_Geometrie_1();
00620         m_BestFit->Initialize_Mesh_Geometrie_2();
00621         m_BestFit->Perform_PointCloud();
00622         m_BestFit->output_best_fit_mesh();
00623 
00624 
00625     best_fit_cad_button->setEnabled(true);
00626 }
00627 
00628 void Cutting::on_SpringbackButton_clicked()
00629 {
00630     m_Spring = new SpringbackCorrection();
00631     m_selection = Springback;
00632     best_fit_cad_button->setEnabled(true);
00633 
00634 }
00635 
00636 void Cutting::on_Approximate_button_clicked()
00637 {
00638     m_selection = Approx;
00639     best_fit_mesh_button->setEnabled(true);
00640 }
00641 
00642 void Cutting::on_best_fit_cad_button_clicked()
00643 {
00644     selectShape();
00645     best_fit_mesh_button->setEnabled(true);
00646 }
00647 
00648 void Cutting::on_best_fit_mesh_button_clicked()
00649 {
00650 if(m_selection == Springback)
00651         {
00652                 best_fit_mesh2_button->setEnabled(true);
00653                 m_Spring->Load(m_Shape);
00654         }
00655     selectMesh();
00656     best_fit_go_button->setEnabled(true);
00657     SelectFace_button->setEnabled(true);
00658 if(m_selection == Springback)
00659                 best_fit_mesh2_button->setEnabled(true);
00660 }
00661 void Cutting::on_best_fit_mesh2_button_clicked()
00662 {
00663         m_Spring->Load(m_Mesh);
00664     selectMesh();
00665 }
00666 
00667 void Cutting::on_SelectFace_button_clicked()
00668 {
00669 
00670 
00671     Gui::Document* doc = Gui::Application::Instance->activeDocument();
00672     Gui::View3DInventor* view = static_cast<Gui::View3DInventor*>(doc->getActiveView());
00673     if (view)
00674     {
00675         Gui::View3DInventorViewer* viewer = view->getViewer();
00676         viewer->setEditing(true);
00677         viewer->addEventCallback(SoMouseButtonEvent::getClassTypeId(), zLevelCallback, this);
00678         QMessageBox::information(this, tr("FreeCAD CamWorkbench"), tr("You have to pick a face.\n"));
00679         this->hide();
00680     }
00681 }
00682 
00683 void Cutting::on_best_fit_go_button_clicked()
00684 {
00685         bool out = 1;  //gibt an ob beim springback NUR die Fehlervektoren ausgegeben werden solle
00686     getSettings(); //First transfer the settings to the Cutting_tools class (-> m_Settings)
00687         
00688 
00689     switch (m_selection)
00690     {
00691 
00692     case BestFit:
00693 
00694                 
00695         m_BestFit->Load(m_Mesh,m_Shape);
00696         m_BestFit->Perform();
00697 
00698         best_fit_cad_button ->setEnabled(false);
00699         best_fit_mesh_button->setEnabled(false);
00700         best_fit_go_button  ->setEnabled(false);
00701 
00702         m_MeshOut = m_BestFit->m_MeshWork;
00703         m_MeshCad = m_BestFit->m_CadMesh;
00704         DisplayMeshOutput(m_MeshOut);
00705         //DisplayMeshOutput(m_MeshCad);
00706 
00707                 
00708 
00709 
00710         break;
00711 
00712     case Springback:
00713 
00714         m_Spring->Load(m_Mesh);
00715         m_Spring->Init_Setting(m_Settings);
00716         m_Spring->Init();
00717         m_Spring->Perform(m_Settings.limit_angle,out);
00718         m_MeshCad = m_Spring->m_CadMesh;
00719 
00720 
00721 
00722         best_fit_cad_button ->setEnabled(false);
00723         best_fit_mesh_button->setEnabled(false);
00724         best_fit_go_button  ->setEnabled(false);
00725 
00726                 if(out==0)
00727                 {
00728                         DisplayMeshOutput(m_MeshCad);
00729                         DisplayMeshOutput(m_Spring->m_Mesh_vis);
00730                         DisplayMeshOutput(m_Spring->m_Mesh_vis2);
00731                 }
00732 best_fit_mesh2_button->setEnabled(true);
00733         break;
00734 
00735     case Approx:
00736         /*MeshCore::MeshPointArray pnts   = m_Mesh.GetPoints();  // file "kleines.stl" hat spitze über der ebene ... nicht kompatibel mit diesem Algo
00737         MeshCore::MeshFacetArray facets = m_Mesh.GetFacets();
00738 
00739         for(int i=0; i<pnts.size(); ++i)
00740         {
00741          if(pnts[i].z >0.0)
00742           pnts[i].z *= -1;
00743         }
00744 
00745         m_Mesh.Assign(pnts,facets);*/
00746 
00747                 std::vector<double> CtrlPnts, U_knot, V_knot;
00748                 int degU,degV;  
00749                 m_App = new Approximate(m_Mesh,CtrlPnts,U_knot,V_knot,degU,degV,m_Settings.error_tolerance);
00750 
00751         //m_Approx = new UniGridApprox(m_Mesh, 1);
00752         //m_Approx->Perform(0.1);
00753 
00754         BRepBuilderAPI_MakeFace  Face(m_App->aAdaptorSurface.Surface());
00755         m_Shape = Face.Face();
00756                 
00757                 ofstream anOutputFile;
00758             anOutputFile.open("c:/approx_log.txt");
00759                 anOutputFile << "face zugewiesen" << endl;
00760                 
00761                 DisplayMeshOutput(m_App->MeshParam);
00762         DisplayShapeOutput();
00763         break;
00764     }
00765 }
00766 
00767 void Cutting::DisplayMeshOutput(const MeshCore::MeshKernel &mesh)
00768 {
00769     App::Document* doc = App::GetApplication().getActiveDocument();
00770     App::DocumentObject* obj = doc->addObject("Mesh::Feature","Best_Fit-Mesh");
00771 
00772     Mesh::Feature* part1 = static_cast<Mesh::Feature*>(obj);
00773     part1->Mesh.setValue(mesh);
00774 
00775     //doc->recompute();
00776 }
00777 
00778 void Cutting::DisplayShapeOutput()
00779 {
00780     App::Document* doc = App::GetApplication().getActiveDocument();
00781     App::DocumentObject* obj = doc->addObject("Part::Feature","Output-Shape");
00782 
00783     Part::Feature* part1 = static_cast<Part::Feature*>(obj);
00784     part1->Shape.setValue(m_Shape);
00785 
00786     //doc->recompute();
00787 }
00788 
00789 void Cutting::DisplayCAMOutput()
00790 {
00791     BRep_Builder BB;
00792     TopoDS_Compound aCompound1,aCompound2;
00793     BB.MakeCompound(aCompound1);
00794     BB.MakeCompound(aCompound2);
00795     TopoDS_Edge anEdge;
00796     const std::vector<Handle_Geom_BSplineCurve>* topCurves;
00797     const std::vector<Handle_Geom_BSplineCurve>* botCurves;
00798     std::vector<Handle_Geom_BSplineCurve>::const_iterator an_it1;
00799     topCurves = m_CuttingAlgo->getOutputhigh();
00800     botCurves = m_CuttingAlgo->getOutputlow();
00801     for (an_it1 = topCurves->begin();an_it1!=topCurves->end();an_it1++)
00802     {
00803         BB.MakeEdge(anEdge,*an_it1,0.01);
00804         BB.Add(aCompound1,anEdge);
00805 
00806     }
00807     for (an_it1 = botCurves->begin();an_it1!=botCurves->end();an_it1++)
00808     {
00809         BB.MakeEdge(anEdge,*an_it1,0.01);
00810         BB.Add(aCompound2,anEdge);
00811     }
00812 
00813     App::Document* doc = App::GetApplication().getActiveDocument();
00814     App::DocumentObject* obj = doc->addObject("Part::Feature","Master-Tool");
00815     App::DocumentObject* obj1 = doc->addObject("Part::Feature","Slave-Tool");
00816 
00817     Part::Feature* part1 = static_cast<Part::Feature*>(obj);
00818     Part::Feature* part2 = static_cast<Part::Feature*>(obj1);
00819         part1->Shape.setValue(aCompound1);
00820         part2->Shape.setValue(aCompound2);
00821 
00822 
00823 
00824 
00825 
00826 
00827     //
00828     //      for (unsigned int i=0;i<aTestOutput.size();++i)
00829     //      {
00830     //          BB.Add(aCompound,anEdge);
00831     //      }
00832 
00833     //anewCuttingEnv.OffsetWires_Standard(10.0);
00834 
00835     //std::vector<Handle_Geom_BSplineCurve> topCurves;
00836     //std::vector<Handle_Geom_BSplineCurve> botCurves;
00837     //std::vector<Handle_Geom_BSplineCurve>::iterator an_it;
00838     //topCurves = *(anewCuttingEnv.getOutputhigh());
00839     //botCurves = *(anewCuttingEnv.getOutputlow());
00840   
00841         ofstream anoutput;
00842         ofstream anoutput2;
00843         anoutput.open("c:/topCurves.txt");
00844         anoutput2.open("c:/botCurves.txt");
00845     for (an_it1 = topCurves->begin();an_it1!=topCurves->end();an_it1++)
00846     {
00847                 GeomAdaptor_Curve aCurveAdaptor(*an_it1);
00848         GCPnts_QuasiUniformDeflection aPointGenerator(aCurveAdaptor,0.1);
00849         for (int t=1;t<=aPointGenerator.NbPoints();++t)
00850         {
00851             anoutput << (aPointGenerator.Value(t)).X() <<","<< (aPointGenerator.Value(t)).Y() <<","<<(aPointGenerator.Value(t)).Z()<<std::endl;
00852         }
00853     }
00854     for (an_it1 = botCurves->begin();an_it1!=botCurves->end();an_it1++)
00855     {
00856         GeomAdaptor_Curve aCurveAdaptor(*an_it1);
00857         GCPnts_QuasiUniformDeflection aPointGenerator(aCurveAdaptor,0.1);
00858         for (int t=1;t<=aPointGenerator.NbPoints();++t)
00859         {
00860             anoutput2 << (aPointGenerator.Value(t)).X() <<","<< (aPointGenerator.Value(t)).Y() <<","<<(aPointGenerator.Value(t)).Z()<<std::endl;
00861         }
00862     }
00863     anoutput.close();
00864     anoutput2.close();
00865 
00866 }
00867 
00868 
00869 
00870 void Cutting::zLevelCallback(void * ud, SoEventCallback * n)
00871 {
00872     const SoMouseButtonEvent * mbe = (SoMouseButtonEvent *)n->getEvent();
00873     Gui::View3DInventorViewer* view  = reinterpret_cast<Gui::View3DInventorViewer*>(n->getUserData());
00874     Cutting* that = reinterpret_cast<Cutting*>(ud);
00875 
00876     // Mark all incoming mouse button events as handled, especially, to deactivate the selection node
00877     n->getAction()->setHandled();
00878     if (mbe->getButton() == SoMouseButtonEvent::BUTTON2 && mbe->getState() == SoButtonEvent::UP)
00879     {
00880         n->setHandled();
00881         view->setEditing(false);
00882         view->removeEventCallback(SoMouseButtonEvent::getClassTypeId(), zLevelCallback, that);
00883         that->show();
00884     }
00885     else if (mbe->getButton() == SoMouseButtonEvent::BUTTON1 && mbe->getState() == SoButtonEvent::DOWN)
00886     {
00887         const SoPickedPoint * point = n->getPickedPoint();
00888         if (point == NULL)
00889         {
00890             QMessageBox::warning(Gui::getMainWindow(),"z level", "No shape picked!");
00891             return;
00892         }
00893 
00894         n->setHandled();
00895 
00896         // By specifying the indexed mesh node 'pcFaceSet' we make sure that the picked point is
00897         // really from the mesh we render and not from any other geometry
00898         Gui::ViewProvider* vp = static_cast<Gui::ViewProvider*>(view->getViewProviderByPath(point->getPath()));
00899         if (!vp || !vp->getTypeId().isDerivedFrom(PartGui::ViewProviderPart::getClassTypeId()))
00900             return;
00901         PartGui::ViewProviderPart* vpp = static_cast<PartGui::ViewProviderPart*>(vp);
00902         TopoDS_Shape sh = vpp->getShape(point);
00903         if (!sh.IsNull())
00904         {
00905             // ok a shape was picked
00906             n->setHandled();
00907             view->setEditing(false);
00908             view->removeEventCallback(SoMouseButtonEvent::getClassTypeId(), zLevelCallback, that);
00909             that->show();
00910             SbVec3f pt = point->getPoint();
00911             that->setFace(sh, pt[0],pt[1],pt[2]);
00912         }
00913     }
00914 }
00915 
00916 
00917 
00918 #include "moc_Cutting.cpp"

Generated on Wed Nov 23 19:00:04 2011 for FreeCAD by  doxygen 1.6.1