Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Measure: Fix quick measure #13944

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/Mod/Measure/Gui/AppMeasureGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
**************************************************************************/

#include "PreCompiled.h"

#ifndef _PreComp_
#include <QApplication>
#endif
#include <Base/Console.h>
#include <Base/Interpreter.h>
#include <Base/PyObjectBase.h>
Expand All @@ -43,7 +45,7 @@

namespace MeasureGui
{
class Module: public Py::ExtensionModule<Module>

Check warning on line 48 in src/Mod/Measure/Gui/AppMeasureGui.cpp

View workflow job for this annotation

GitHub Actions / Lint / Lint

class 'Module' defines a default destructor but does not define a copy constructor, a copy assignment operator, a move constructor or a move assignment operator [cppcoreguidelines-special-member-functions]
{
public:
Module()
Expand All @@ -59,7 +61,7 @@

PyObject* initModule()
{
return Base::Interpreter().addModule(new Module);

Check warning on line 64 in src/Mod/Measure/Gui/AppMeasureGui.cpp

View workflow job for this annotation

GitHub Actions / Lint / Lint

initializing non-owner argument of type 'Py::ExtensionModuleBase *' with a newly created 'gsl::owner<>' [cppcoreguidelines-owning-memory]
}

}// namespace MeasureGui
Expand All @@ -71,7 +73,7 @@
PyErr_SetString(PyExc_ImportError, "Cannot load Gui module in console application.");
PyMOD_Return(nullptr);
}

// load dependent module
try {
Base::Interpreter().loadModule("Measure");
Expand All @@ -97,7 +99,11 @@

// Q_INIT_RESOURCE(Measure);

Base::Interpreter().addType(&MeasureGui::QuickMeasurePy::Type, mod, "QuickMeasure");
Base::Interpreter().addType(&MeasureGui::QuickMeasurePy::Type, mod, "QuickMeasure");

Check warning on line 102 in src/Mod/Measure/Gui/AppMeasureGui.cpp

View workflow job for this annotation

GitHub Actions / Lint / Lint

Potential memory leak [clang-analyzer-cplusplus.NewDeleteLeaks]

Check warning on line 102 in src/Mod/Measure/Gui/AppMeasureGui.cpp

View workflow job for this annotation

GitHub Actions / Lint / Lint

static member accessed through instance [readability-static-accessed-through-instance]

// Create a QuickMeasure instance
auto measure = new MeasureGui::QuickMeasure(QApplication::instance());

Check warning on line 105 in src/Mod/Measure/Gui/AppMeasureGui.cpp

View workflow job for this annotation

GitHub Actions / Lint / Lint

initializing non-owner 'MeasureGui::QuickMeasure *' with a newly created 'gsl::owner<>' [cppcoreguidelines-owning-memory]
Q_UNUSED(measure)

PyMOD_Return(mod);

Check warning on line 108 in src/Mod/Measure/Gui/AppMeasureGui.cpp

View workflow job for this annotation

GitHub Actions / Lint / Lint

Potential leak of memory pointed to by 'measure' [clang-analyzer-cplusplus.NewDeleteLeaks]
}
59 changes: 39 additions & 20 deletions src/Mod/Measure/Gui/QuickMeasure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@
#include "PreCompiled.h"

#ifndef _PreComp_
#include <BRepAdaptor_Curve.hxx>
#include <BRepAdaptor_Surface.hxx>
#include <TopoDS.hxx>
#include <TopoDS_Face.hxx>
#include <TopoDS_Shape.hxx>
#include <cmath>
#include <vector>
#endif
Expand All @@ -51,31 +46,53 @@
using namespace Measure;
using namespace MeasureGui;

QuickMeasure::QuickMeasure(QObject* parent) : QObject(parent)
QuickMeasure::QuickMeasure(QObject* parent)
: QObject(parent)
, measurement{new Measure::Measurement()}
{
measurement = new Measure::Measurement();
}

QuickMeasure::~QuickMeasure()
{
delete measurement;
}


void QuickMeasure::onSelectionChanged(const Gui::SelectionChanges& msg)
{
if (msg.Type == Gui::SelectionChanges::SetPreselect || msg.Type == Gui::SelectionChanges::RmvPreselect) {
return;
try {
tryMeasureSelection(msg);
}

Gui::Document* doc = Gui::Application::Instance->activeDocument();
if (!doc) { return; }
catch (const Base::IndexError&) {
// ignore this exception because it can be caused by trying to access a non-existing
// sub-element e.g. when selecting a construction geometry in sketcher
}
catch (const Base::Exception& e) {
e.ReportException();
}
}

measurement->clear();
void QuickMeasure::tryMeasureSelection(const Gui::SelectionChanges& msg)
{
if (canMeasureSelection(msg)) {
measurement->clear();
addSelectionToMeasurement();
printResult();
}
}

std::vector<Part::TopoShape> subShapes;
bool QuickMeasure::canMeasureSelection(const Gui::SelectionChanges& msg) const
{
if (msg.Type == Gui::SelectionChanges::SetPreselect ||
msg.Type == Gui::SelectionChanges::RmvPreselect) {
return false;
}

Gui::Document* doc = Gui::Application::Instance->activeDocument();
return doc != nullptr;
}

std::vector<App::DocumentObject*> docsToMove;
void QuickMeasure::addSelectionToMeasurement()
{
for (auto& selObj : Gui::Selection().getSelectionEx()) {
App::DocumentObject* obj = selObj.getObject();
const std::vector<std::string> subNames = selObj.getSubNames();
Expand All @@ -88,7 +105,10 @@
}
}
}
}

void QuickMeasure::printResult()
{
MeasureType mtype = measurement->getType();
if (mtype == MeasureType::Surfaces) {
Base::Quantity area(measurement->area(), Base::Unit::Area);
Expand All @@ -112,7 +132,7 @@
else if (mtype == MeasureType::Cylinder || mtype == MeasureType::Sphere || mtype == MeasureType::Torus) {
Base::Quantity area(measurement->area(), Base::Unit::Area);
Base::Quantity rad(measurement->radius(), Base::Unit::Length);
print(tr("Area: %1, Radius: %2").arg(area.getSafeUserString()).arg(rad.getSafeUserString()));
print(tr("Area: %1, Radius: %2").arg(area.getSafeUserString(), rad.getSafeUserString()));
}
else if (mtype == MeasureType::Edges) {
Base::Quantity dist(measurement->length(), Base::Unit::Length);
Expand All @@ -125,7 +145,7 @@
else if (mtype == MeasureType::TwoLines) {
Base::Quantity angle(measurement->angle(), Base::Unit::Length);
Base::Quantity dist(measurement->length(), Base::Unit::Length);
print(tr("Angle: %1, Total length: %2").arg(angle.getSafeUserString()).arg(dist.getSafeUserString()));
print(tr("Angle: %1, Total length: %2").arg(angle.getSafeUserString(), dist.getSafeUserString()));
}
else if (mtype == MeasureType::Line) {
Base::Quantity dist(measurement->length(), Base::Unit::Length);
Expand All @@ -146,7 +166,6 @@
else {
print(QString::fromLatin1(""));
}

}

void QuickMeasure::print(const QString& message)
Expand All @@ -155,4 +174,4 @@
}


#include "moc_QuickMeasure.cpp"
#include "moc_QuickMeasure.cpp"

Check failure on line 177 in src/Mod/Measure/Gui/QuickMeasure.cpp

View workflow job for this annotation

GitHub Actions / Lint / Lint

'moc_QuickMeasure.cpp' file not found [clang-diagnostic-error]
4 changes: 4 additions & 0 deletions src/Mod/Measure/Gui/QuickMeasure.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ class QuickMeasure : public QObject, Gui::SelectionObserver

private:
void onSelectionChanged(const Gui::SelectionChanges& msg) override;
void tryMeasureSelection(const Gui::SelectionChanges& msg);

bool canMeasureSelection(const Gui::SelectionChanges& msg) const;
void addSelectionToMeasurement();
void printResult();
void print(const QString& message);

Measure::Measurement* measurement;
Expand Down
4 changes: 1 addition & 3 deletions src/Mod/Measure/InitGui.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# /***************************************************************************

Check warning on line 1 in src/Mod/Measure/InitGui.py

View workflow job for this annotation

GitHub Actions / Lint / Lint

would reformat src/Mod/Measure/InitGui.py

Check warning on line 1 in src/Mod/Measure/InitGui.py

View workflow job for this annotation

GitHub Actions / Lint / Lint

Missing module docstring (missing-module-docstring)
# * Copyright (c) 2023 David Friedli <david[at]friedli-be.ch> *
# * *
# * This file is part of FreeCAD. *
Expand All @@ -20,12 +20,13 @@
# **************************************************************************/


# Import the measure module here in order to ensure that default measurement types are loaded during startup.

Check warning on line 23 in src/Mod/Measure/InitGui.py

View workflow job for this annotation

GitHub Actions / Lint / Lint

Line too long (109/100) (line-too-long)
# Note that they won't auto-load in gui-less mode. Ideally they would be loaded from "Init.py", similar

Check warning on line 24 in src/Mod/Measure/InitGui.py

View workflow job for this annotation

GitHub Actions / Lint / Lint

Line too long (103/100) (line-too-long)
# to how the import/export types are registered. This would require to register measurement types from

Check warning on line 25 in src/Mod/Measure/InitGui.py

View workflow job for this annotation

GitHub Actions / Lint / Lint

Line too long (102/100) (line-too-long)
# python which could complicate things further.

import Measure
import MeasureGui

Check warning on line 29 in src/Mod/Measure/InitGui.py

View workflow job for this annotation

GitHub Actions / Lint / Lint

Unused import MeasureGui (unused-import)
from MeasureCOM import makeMeasureCOM, MeasureCOM


Expand All @@ -34,12 +35,9 @@


# Register python measure types
import FreeCAD

Check warning on line 38 in src/Mod/Measure/InitGui.py

View workflow job for this annotation

GitHub Actions / Lint / Lint

Import "import FreeCAD" should be placed at the top of the module (wrong-import-position)
FreeCAD.MeasureManager.addMeasureType(
"CENTEROFMASS",
"Center of Mass",
MeasureCOM,
)

import MeasureGui
MeasureGui.QuickMeasure()
Loading