New Cam simulator based on low level OpenGL functions (faster and more precise) (#13884)
* Initial opengl test window * added core files * some fixes for code comparability with other platforms * more compatibility cleanup * add missing opengl libraries * Basic simulation window works! * try using different define * fix wrapper for better compatibility * Gui is now operational * Finally SIM works on actual freecad models * support drill commands * cleanup python script and add tool profile generation * Now using actual tools specified in the operation. * support mouse wheel and freecad-style 3d navigation * Support accuracy gauge * remove endsimulation reference * show simulation speed indicator * apply clang-format * apply changes suggested by code review * gui items are loaded via QT resource system instead of hard coded * use vector instead of pointer to pass tool profile points * Fix some more formatting errors.
This commit is contained in:
240
src/Mod/CAM/PathSimulator/AppGL/DlgCAMSimulator.cpp
Normal file
240
src/Mod/CAM/PathSimulator/AppGL/DlgCAMSimulator.cpp
Normal file
@@ -0,0 +1,240 @@
|
||||
/***************************************************************************
|
||||
* Copyright (c) 2024 Shai Seger <shaise at gmail> *
|
||||
* *
|
||||
* This file is part of the FreeCAD CAx development system. *
|
||||
* *
|
||||
* This library is free software; you can redistribute it and/or *
|
||||
* modify it under the terms of the GNU Library General Public *
|
||||
* License as published by the Free Software Foundation; either *
|
||||
* version 2 of the License, or (at your option) any later version. *
|
||||
* *
|
||||
* This library is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU Library General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU Library General Public *
|
||||
* License along with this library; see the file COPYING.LIB. If not, *
|
||||
* write to the Free Software Foundation, Inc., 59 Temple Place, *
|
||||
* Suite 330, Boston, MA 02111-1307, USA *
|
||||
* *
|
||||
***************************************************************************/
|
||||
|
||||
#include "PreCompiled.h"
|
||||
|
||||
#include "DlgCAMSimulator.h"
|
||||
#include "MillSimulation.h"
|
||||
#include <QtGui/QMatrix4x4>
|
||||
#include <QtGui/qscreen.h>
|
||||
#include <QDateTime>
|
||||
#include <QSurfaceFormat>
|
||||
#include <QMouseEvent>
|
||||
#include <QWheelEvent>
|
||||
#include <QPoint>
|
||||
|
||||
using namespace CAMSimulator;
|
||||
using namespace MillSim;
|
||||
|
||||
QOpenGLContext* gOpenGlContext;
|
||||
|
||||
using namespace MillSim;
|
||||
|
||||
namespace CAMSimulator
|
||||
{
|
||||
|
||||
DlgCAMSimulator::DlgCAMSimulator(QWindow* parent)
|
||||
: QWindow(parent)
|
||||
{
|
||||
setSurfaceType(QWindow::OpenGLSurface);
|
||||
mMillSimulator = new MillSimulation();
|
||||
}
|
||||
|
||||
void DlgCAMSimulator::render(QPainter* painter)
|
||||
{
|
||||
Q_UNUSED(painter);
|
||||
}
|
||||
|
||||
void DlgCAMSimulator::render()
|
||||
{
|
||||
mMillSimulator->ProcessSim((unsigned int)(QDateTime::currentMSecsSinceEpoch()));
|
||||
}
|
||||
|
||||
void DlgCAMSimulator::renderLater()
|
||||
{
|
||||
requestUpdate();
|
||||
}
|
||||
|
||||
bool DlgCAMSimulator::event(QEvent* event)
|
||||
{
|
||||
switch (event->type()) {
|
||||
case QEvent::UpdateRequest:
|
||||
renderNow();
|
||||
return true;
|
||||
default:
|
||||
return QWindow::event(event);
|
||||
}
|
||||
}
|
||||
|
||||
void DlgCAMSimulator::exposeEvent(QExposeEvent* event)
|
||||
{
|
||||
Q_UNUSED(event);
|
||||
|
||||
if (isExposed()) {
|
||||
renderNow();
|
||||
}
|
||||
}
|
||||
|
||||
void DlgCAMSimulator::mouseMoveEvent(QMouseEvent* ev)
|
||||
{
|
||||
mMillSimulator->MouseMove(ev->x(), ev->y());
|
||||
}
|
||||
|
||||
void DlgCAMSimulator::mousePressEvent(QMouseEvent* ev)
|
||||
{
|
||||
mMillSimulator->MousePress(ev->button(), true, ev->x(), ev->y());
|
||||
}
|
||||
|
||||
void DlgCAMSimulator::mouseReleaseEvent(QMouseEvent* ev)
|
||||
{
|
||||
mMillSimulator->MousePress(ev->button(), false, ev->x(), ev->y());
|
||||
}
|
||||
|
||||
void DlgCAMSimulator::wheelEvent(QWheelEvent* ev)
|
||||
{
|
||||
mMillSimulator->MouseScroll((float)ev->angleDelta().y() / 120.0f);
|
||||
}
|
||||
|
||||
void DlgCAMSimulator::resetSimulation()
|
||||
{
|
||||
mMillSimulator->Clear();
|
||||
}
|
||||
|
||||
void DlgCAMSimulator::addGcodeCommand(const char* cmd)
|
||||
{
|
||||
mMillSimulator->AddGcodeLine(cmd);
|
||||
}
|
||||
|
||||
void DlgCAMSimulator::addTool(const std::vector<float> toolProfilePoints,
|
||||
int toolNumber,
|
||||
float diameter,
|
||||
float resolution)
|
||||
{
|
||||
std::string toolCmd = "T" + std::to_string(toolNumber);
|
||||
mMillSimulator->AddGcodeLine(toolCmd.c_str());
|
||||
if (!mMillSimulator->ToolExists(toolNumber)) {
|
||||
mMillSimulator->AddTool(toolProfilePoints, toolNumber, diameter);
|
||||
}
|
||||
}
|
||||
|
||||
void DlgCAMSimulator::hideEvent(QHideEvent* ev)
|
||||
{
|
||||
mAnimating = false;
|
||||
}
|
||||
|
||||
void DlgCAMSimulator::startSimulation(const SimStock* stock, float quality)
|
||||
{
|
||||
mStock = *stock;
|
||||
mQuality = quality;
|
||||
mNeedsInitialize = true;
|
||||
show();
|
||||
setAnimating(true);
|
||||
}
|
||||
|
||||
void DlgCAMSimulator::initialize()
|
||||
{
|
||||
mMillSimulator
|
||||
->SetBoxStock(mStock.mPx, mStock.mPy, mStock.mPz, mStock.mLx, mStock.mLy, mStock.mLz);
|
||||
mMillSimulator->InitSimulation(mQuality);
|
||||
|
||||
const qreal retinaScale = devicePixelRatio();
|
||||
glViewport(0, 0, width() * retinaScale, height() * retinaScale);
|
||||
}
|
||||
|
||||
void DlgCAMSimulator::checkInitialization()
|
||||
{
|
||||
if (!mContext) {
|
||||
mContext = new QOpenGLContext(this);
|
||||
mContext->setFormat(requestedFormat());
|
||||
mContext->create();
|
||||
gOpenGlContext = mContext;
|
||||
mNeedsInitialize = true;
|
||||
}
|
||||
|
||||
mContext->makeCurrent(this);
|
||||
|
||||
if (mNeedsInitialize) {
|
||||
initializeOpenGLFunctions();
|
||||
initialize();
|
||||
mNeedsInitialize = false;
|
||||
}
|
||||
}
|
||||
|
||||
void DlgCAMSimulator::renderNow()
|
||||
{
|
||||
static unsigned int lastTime = 0;
|
||||
static int frameCount = 0;
|
||||
static int fps = 0;
|
||||
if (!isExposed()) {
|
||||
return;
|
||||
}
|
||||
|
||||
checkInitialization();
|
||||
|
||||
frameCount++;
|
||||
unsigned int curtime = QDateTime::currentMSecsSinceEpoch();
|
||||
unsigned int timediff = curtime - lastTime;
|
||||
if (timediff > 10000) {
|
||||
fps = frameCount * 1000 / timediff; // for debug only. not used otherwise.
|
||||
lastTime = curtime;
|
||||
frameCount = 0;
|
||||
}
|
||||
render();
|
||||
mContext->swapBuffers(this);
|
||||
|
||||
if (mAnimating) {
|
||||
renderLater();
|
||||
}
|
||||
}
|
||||
|
||||
void DlgCAMSimulator::setAnimating(bool animating)
|
||||
{
|
||||
mAnimating = animating;
|
||||
|
||||
if (animating) {
|
||||
renderLater();
|
||||
}
|
||||
}
|
||||
|
||||
DlgCAMSimulator* DlgCAMSimulator::GetInstance()
|
||||
{
|
||||
if (mInstance == nullptr) {
|
||||
QSurfaceFormat format;
|
||||
format.setSamples(16);
|
||||
format.setSwapInterval(2);
|
||||
mInstance = new DlgCAMSimulator();
|
||||
mInstance->setFormat(format);
|
||||
mInstance->resize(800, 600);
|
||||
mInstance->setModality(Qt::ApplicationModal);
|
||||
mInstance->show();
|
||||
}
|
||||
return mInstance;
|
||||
}
|
||||
|
||||
DlgCAMSimulator* DlgCAMSimulator::mInstance = nullptr;
|
||||
|
||||
//************************************************************************************************************
|
||||
// stock
|
||||
//************************************************************************************************************
|
||||
SimStock::SimStock(float px, float py, float pz, float lx, float ly, float lz, float res)
|
||||
: mPx(px)
|
||||
, mPy(py)
|
||||
, mPz(pz + 0.005 * lz)
|
||||
, mLx(lx)
|
||||
, mLy(ly)
|
||||
, mLz(1.01 * lz)
|
||||
{}
|
||||
|
||||
SimStock::~SimStock()
|
||||
{}
|
||||
|
||||
} // namespace CAMSimulator
|
||||
Reference in New Issue
Block a user