Part: Prevent crash on cross-section of invalid object

This commit is contained in:
Kacper Donat
2025-08-13 16:15:16 +02:00
committed by Chris Hennes
parent cb3973e773
commit 0c23e15cc4
2 changed files with 45 additions and 32 deletions

View File

@@ -54,6 +54,10 @@
#include "CrossSections.h"
#include "ui_CrossSections.h"
#include <QMessageBox>
#include <Base/Interpreter.h>
#include <Gui/MainWindow.h>
using namespace PartGui;
namespace sp = std::placeholders;
@@ -205,11 +209,12 @@ void CrossSections::keyPressEvent(QKeyEvent* ke)
void CrossSections::accept()
{
apply();
QDialog::accept();
if (apply()) {
QDialog::accept();
}
}
void CrossSections::apply()
bool CrossSections::apply()
{
std::vector<App::DocumentObject*> docobjs = Gui::Selection().
getObjectsOfType(App::DocumentObject::getClassTypeId());
@@ -267,38 +272,46 @@ void CrossSections::apply()
}
#else
Base::SequencerLauncher seq("Cross-sections…", obj.size() * (d.size() + 1));
Gui::Command::runCommand(Gui::Command::App, "import Part\n");
Gui::Command::runCommand(Gui::Command::App, "from FreeCAD import Base\n");
for (auto it : obj) {
App::Document* doc = it->getDocument();
std::string s = it->getNameInDocument();
s += "_cs";
Gui::Command::runCommand(Gui::Command::App, QStringLiteral(
"wires=list()\n"
"shape=FreeCAD.getDocument(\"%1\").%2.Shape\n")
.arg(QLatin1String(doc->getName()),
QLatin1String(it->getNameInDocument())).toLatin1());
for (double jt : d) {
try {
Gui::Command::runCommand(Gui::Command::App, "import Part\n");
Gui::Command::runCommand(Gui::Command::App, "from FreeCAD import Base\n");
for (auto it : obj) {
App::Document* doc = it->getDocument();
std::string s = it->getNameInDocument();
s += "_cs";
Gui::Command::runCommand(Gui::Command::App, QStringLiteral(
"for i in shape.slice(Base.Vector(%1,%2,%3),%4):\n"
" wires.append(i)\n"
).arg(a).arg(b).arg(c).arg(jt).toLatin1());
seq.next();
"wires=list()\n"
"shape=FreeCAD.getDocument(\"%1\").%2.Shape\n")
.arg(QLatin1String(doc->getName()),
QLatin1String(it->getNameInDocument())).toLatin1());
for (double jt : d) {
Gui::Command::runCommand(Gui::Command::App, QStringLiteral(
"for i in shape.slice(Base.Vector(%1,%2,%3),%4):\n"
" wires.append(i)\n"
).arg(a).arg(b).arg(c).arg(jt).toLatin1());
seq.next();
}
Gui::Command::runCommand(Gui::Command::App, QStringLiteral(
"comp=Part.Compound(wires)\n"
"slice=FreeCAD.getDocument(\"%1\").addObject(\"Part::Feature\",\"%2\")\n"
"slice.Shape=comp\n"
"slice.purgeTouched()\n"
"del slice,comp,wires,shape")
.arg(QLatin1String(doc->getName()),
QLatin1String(s.c_str())).toLatin1());
}
Gui::Command::runCommand(Gui::Command::App, QStringLiteral(
"comp=Part.Compound(wires)\n"
"slice=FreeCAD.getDocument(\"%1\").addObject(\"Part::Feature\",\"%2\")\n"
"slice.Shape=comp\n"
"slice.purgeTouched()\n"
"del slice,comp,wires,shape")
.arg(QLatin1String(doc->getName()),
QLatin1String(s.c_str())).toLatin1());
seq.next();
} catch (Base::Exception& e) {
e.reportException();
QMessageBox::critical(Gui::getMainWindow(), tr("Cannot compute cross-sections"), QString::fromStdString(e.getMessage()));
return false;
}
seq.next();
#endif
return true;
}
void CrossSections::xyPlaneClicked()

View File

@@ -49,7 +49,7 @@ public:
explicit CrossSections(const Base::BoundBox3d& bb, QWidget* parent = nullptr, Qt::WindowFlags fl = Qt::WindowFlags());
~CrossSections() override;
void accept() override;
void apply();
bool apply();
protected:
void changeEvent(QEvent *e) override;