Skip to content

Commit

Permalink
Gui: implement a static function schemaTranslatePoint to avoid code d…
Browse files Browse the repository at this point in the history
…uplication
  • Loading branch information
wwmayer committed May 30, 2020
1 parent b0084cd commit 0821649
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 107 deletions.
75 changes: 43 additions & 32 deletions src/Gui/Selection.cpp
Expand Up @@ -844,55 +844,66 @@ int SelectionSingleton::setPreselect(const char* pDocName, const char* pObjectNa
return DocName.empty()?0:1;
}

void SelectionSingleton::setPreselectCoord( float x, float y, float z)
namespace Gui {
std::array<std::pair<double, std::string>,3 > schemaTranslatePoint(double x, double y, double z, double precision)
{
static char buf[513];

// if nothing is in preselect ignore
if(CurrentPreselection.Object.getObjectName().empty()) return;

CurrentPreselection.x = x;
CurrentPreselection.y = y;
CurrentPreselection.z = z;

Base::Quantity mmx(Base::Quantity::MilliMetre);
mmx.setValue((double)x);
mmx.setValue(fabs(x) > precision ? x : 0.0);
Base::Quantity mmy(Base::Quantity::MilliMetre);
mmy.setValue((double)y);
mmy.setValue(fabs(y) > precision ? y : 0.0);
Base::Quantity mmz(Base::Quantity::MilliMetre);
mmz.setValue((double)z);
mmz.setValue(fabs(z) > precision ? z : 0.0);

double xfactor, yfactor, zfactor, factor;
QString xunit, yunit, zunit, unit;

QString xval = Base::UnitsApi::schemaTranslate(mmx, xfactor, xunit);
QString yval = Base::UnitsApi::schemaTranslate(mmy, yfactor, yunit);
QString zval = Base::UnitsApi::schemaTranslate(mmz, zfactor, zunit);

if (xfactor <= yfactor && xfactor <= zfactor)
{

Base::UnitsApi::schemaTranslate(mmx, xfactor, xunit);
Base::UnitsApi::schemaTranslate(mmy, yfactor, yunit);
Base::UnitsApi::schemaTranslate(mmz, zfactor, zunit);

if (xfactor <= yfactor && xfactor <= zfactor) {
factor = xfactor;
unit = xunit;
}
else if (yfactor <= xfactor && yfactor <= zfactor)
{
else if (yfactor <= xfactor && yfactor <= zfactor) {
factor = yfactor;
unit = yunit;
}
else
{
else {
factor = zfactor;
unit = zunit;
}

float xuser = x / factor;
float yuser = y / factor;
float zuser = z / factor;

double xuser = fabs(x) > precision ? x / factor : 0.0;
double yuser = fabs(y) > precision ? y / factor : 0.0;
double zuser = fabs(z) > precision ? z / factor : 0.0;

std::array<std::pair<double, std::string>, 3> ret = {std::make_pair(xuser, unit.toStdString()),
std::make_pair(yuser, unit.toStdString()),
std::make_pair(zuser, unit.toStdString())};
return ret;
}
}

void SelectionSingleton::setPreselectCoord( float x, float y, float z)
{
static char buf[513];

// if nothing is in preselect ignore
if(CurrentPreselection.Object.getObjectName().empty()) return;

CurrentPreselection.x = x;
CurrentPreselection.y = y;
CurrentPreselection.z = z;

auto pts = schemaTranslatePoint(x, y, z, 0.0);
snprintf(buf,512,"Preselected: %s.%s.%s (%f,%f,%f) %s",CurrentPreselection.pDocName
,CurrentPreselection.pObjectName
,CurrentPreselection.pSubName
,xuser,yuser,zuser,unit.toLatin1().data());
,CurrentPreselection.pObjectName
,CurrentPreselection.pSubName
,pts[0].first
,pts[1].first
,pts[2].first
,pts[0].second.c_str());

if (getMainWindow())
getMainWindow()->showMessage(QString::fromLatin1(buf));
Expand Down
52 changes: 13 additions & 39 deletions src/Gui/SoFCSelection.cpp
Expand Up @@ -52,7 +52,6 @@
#include "View3DInventorViewer.h"

#include <Base/Console.h>
#include <Base/UnitsApi.h>
#include "SoFCSelection.h"
#include "MainWindow.h"
#include "Selection.h"
Expand All @@ -75,6 +74,10 @@

using namespace Gui;

namespace Gui {
std::array<std::pair<double, std::string>,3 > schemaTranslatePoint(double x, double y, double z, double precision);
}

SoFullPath * Gui::SoFCSelection::currenthighlight = NULL;


Expand Down Expand Up @@ -403,44 +406,15 @@ SoFCSelection::handleEvent(SoHandleEventAction * action)

const auto &pt = pp->getPoint();

Base::Quantity mmx(Base::Quantity::MilliMetre);
mmx.setValue(fabs(pt[0])>1e-7?(double)pt[0]:0.0);
Base::Quantity mmy(Base::Quantity::MilliMetre);
mmy.setValue(fabs(pt[1])>1e-7?(double)pt[1]:0.0);
Base::Quantity mmz(Base::Quantity::MilliMetre);
mmz.setValue(fabs(pt[2])>1e-7?(double)pt[2]:0.0);

double xfactor, yfactor, zfactor, factor;
QString xunit, yunit, zunit, unit;

QString xval = Base::UnitsApi::schemaTranslate(mmx, xfactor, xunit);
QString yval = Base::UnitsApi::schemaTranslate(mmy, yfactor, yunit);
QString zval = Base::UnitsApi::schemaTranslate(mmz, zfactor, zunit);

if (xfactor <= yfactor && xfactor <= zfactor)
{
factor = xfactor;
unit = xunit;
}
else if (yfactor <= xfactor && yfactor <= zfactor)
{
factor = yfactor;
unit = yunit;
}
else
{
factor = zfactor;
unit = zunit;
}

float xuser = fabs(pt[0])>1e-7 ? pt[0] / factor : 0.0;
float yuser = fabs(pt[1])>1e-7 ? pt[1] / factor : 0.0;
float zuser = fabs(pt[2])>1e-7 ? pt[2] / factor : 0.0;

snprintf(buf,512,"Preselected: %s.%s.%s (%f, %f, %f) %s",documentName.getValue().getString()
,objectName.getValue().getString()
,subElementName.getValue().getString()
,xuser,yuser,zuser,unit.toLatin1().data());
auto pts = schemaTranslatePoint(pt[0], pt[1], pt[2], 1e-7);
snprintf(buf,512,"Preselected: %s.%s.%s (%f, %f, %f) %s"
,documentName.getValue().getString()
,objectName.getValue().getString()
,subElementName.getValue().getString()
,pts[0].first
,pts[1].first
,pts[2].first
,pts[0].second.c_str());

getMainWindow()->showMessage(QString::fromLatin1(buf));
}
Expand Down
43 changes: 7 additions & 36 deletions src/Gui/SoFCUnifiedSelection.cpp
Expand Up @@ -79,7 +79,6 @@

#include <Base/Console.h>
#include <Base/Tools.h>
#include <Base/UnitsApi.h>
#include <App/Application.h>
#include <App/Document.h>
#include <Gui/Document.h>
Expand All @@ -101,6 +100,10 @@ FC_LOG_LEVEL_INIT("SoFCUnifiedSelection",false,true,true)

using namespace Gui;

namespace Gui {
std::array<std::pair<double, std::string>,3 > schemaTranslatePoint(double x, double y, double z, double precision);
}

SoFullPath * Gui::SoFCUnifiedSelection::currenthighlight = NULL;

// *************************************************************************
Expand Down Expand Up @@ -480,46 +483,14 @@ bool SoFCUnifiedSelection::setHighlight(SoFullPath *path, const SoDetail *det,
{
const char *docname = vpd->getObject()->getDocument()->getName();
const char *objname = vpd->getObject()->getNameInDocument();

Base::Quantity mmx(Base::Quantity::MilliMetre);
mmx.setValue(fabs(x)>1e-7?(double)x:0.0);
Base::Quantity mmy(Base::Quantity::MilliMetre);
mmy.setValue(fabs(y)>1e-7?(double)y:0.0);
Base::Quantity mmz(Base::Quantity::MilliMetre);
mmz.setValue(fabs(z)>1e-7?(double)z:0.0);

double xfactor, yfactor, zfactor, factor;
QString xunit, yunit, zunit, unit;

QString xval = Base::UnitsApi::schemaTranslate(mmx, xfactor, xunit);
QString yval = Base::UnitsApi::schemaTranslate(mmy, yfactor, yunit);
QString zval = Base::UnitsApi::schemaTranslate(mmz, zfactor, zunit);

if (xfactor <= yfactor && xfactor <= zfactor)
{
factor = xfactor;
unit = xunit;
}
else if (yfactor <= xfactor && yfactor <= zfactor)
{
factor = yfactor;
unit = yunit;
}
else
{
factor = zfactor;
unit = zunit;
}

float xuser = fabs(x)>1e-7 ? x / factor : 0.0;
float yuser = fabs(y)>1e-7 ? y / factor : 0.0;
float zuser = fabs(z)>1e-7 ? z / factor : 0.0;

this->preSelection = 1;
static char buf[513];

auto pts = schemaTranslatePoint(x, y, z, 1e-7);
snprintf(buf,512,"Preselected: %s.%s.%s (%f, %f, %f) %s"
,docname,objname,element
,xuser,yuser,zuser,unit.toLatin1().data());
,pts[0].first,pts[1].first,pts[2].first,pts[0].second.c_str());

getMainWindow()->showMessage(QString::fromLatin1(buf));

Expand Down

0 comments on commit 0821649

Please sign in to comment.