Skip to content

Commit

Permalink
AutoScaling Fixes for non-DVP objects
Browse files Browse the repository at this point in the history
  • Loading branch information
WandererFan authored and yorikvanhavre committed Oct 31, 2016
1 parent a5a2070 commit 70b5c24
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 24 deletions.
25 changes: 7 additions & 18 deletions src/Mod/TechDraw/App/DrawProjGroup.cpp
Expand Up @@ -35,6 +35,7 @@
#include <Base/Console.h>
#include <Base/Exception.h>

#include "DrawUtil.h"
#include "DrawPage.h"
#include "DrawProjGroupItem.h"
#include "DrawProjGroup.h"
Expand Down Expand Up @@ -134,6 +135,9 @@ double DrawProjGroup::calculateAutomaticScale() const
arrangeViewPointers(viewPtrs);
double width, height;
minimumBbViews(viewPtrs, width, height); //get 1:1 bbxs
double bbFudge = 1.2;
width *= bbFudge;
height *= bbFudge;

// C++ Standard says casting bool to int gives 0 or 1
int numVertSpaces = (viewPtrs[0] || viewPtrs[3] || viewPtrs[7]) +
Expand All @@ -151,24 +155,9 @@ double DrawProjGroup::calculateAutomaticScale() const
double scale_x = availableX / width;
double scale_y = availableY / height;

double fudgeFactor = 0.90;
float working_scale = fudgeFactor * std::min(scale_x, scale_y);

//which gives the largest scale for which the min_space requirements can be met, but we want a 'sensible' scale, rather than 0.28457239...
//eg if working_scale = 0.115, then we want to use 0.1, similarly 7.65 -> 5, and 76.5 -> 50

float exponent = std::floor(std::log10(working_scale)); //if working_scale = a * 10^b, what is b?
working_scale *= std::pow(10, -exponent); //now find what 'a' is.

float valid_scales[2][8] = {{1.0, 1.25, 2.0, 2.5, 3.75, 5.0, 7.5, 10.0}, //equate to 1:10, 1:8, 1:5, 1:4, 3:8, 1:2, 3:4, 1:1
{1.0, 1.5 , 2.0, 3.0, 4.0 , 5.0, 8.0, 10.0}}; //equate to 1:1, 3:2, 2:1, 3:1, 4:1, 5:1, 8:1, 10:1

int i = 7;
while (valid_scales[(exponent >= 0)][i] > working_scale) //choose closest value smaller than 'a' from list.
i -= 1; //choosing top list if exponent -ve, bottom list for +ve exponent

//now have the appropriate scale, reapply the *10^b
double result = valid_scales[(exponent >= 0)][i] * pow(10, exponent);
double scaleFudge = 0.80;
float working_scale = scaleFudge * std::min(scale_x, scale_y);
double result = DrawUtil::sensibleScale(working_scale);
return result;
}

Expand Down
25 changes: 25 additions & 0 deletions src/Mod/TechDraw/App/DrawUtil.cpp
Expand Up @@ -137,6 +137,31 @@ bool DrawUtil::isZeroEdge(TopoDS_Edge e)
return result;
}

//based on Function provided by Joe Dowsett, 2014
double DrawUtil::sensibleScale(double working_scale)
{
double result = 1.0;
//which gives the largest scale for which the min_space requirements can be met, but we want a 'sensible' scale, rather than 0.28457239...
//eg if working_scale = 0.115, then we want to use 0.1, similarly 7.65 -> 5, and 76.5 -> 50

float exponent = std::floor(std::log10(working_scale)); //if working_scale = a * 10^b, what is b?
working_scale *= std::pow(10, -exponent); //now find what 'a' is.

int choices = 10;
float valid_scales[2][choices] =
{{1.0, 1.25, 2.0, 2.5, 3.75, 5.0, 7.5, 10.0, 50.0, 100.0}, //equate to 1:10, 1:8, 1:5, 1:4, 3:8, 1:2, 3:4, 1:1
// .1 .125 .375 .75
{1.0, 1.5 , 2.0, 3.0, 4.0 , 5.0, 8.0, 10.0, 50.0, 100.0}}; //equate to 1:1, 3:2, 2:1, 3:1, 4:1, 5:1, 8:1, 10:1
// 1.5:1
int i = choices - 1;
while (valid_scales[(exponent >= 0)][i] > working_scale) //choose closest value smaller than 'a' from list.
i -= 1; //choosing top list if exponent -ve, bottom list for +ve exponent

//now have the appropriate scale, reapply the *10^b
result = valid_scales[(exponent >= 0)][i] * pow(10, exponent);
return result;
}

//============================
// various debugging routines.
void DrawUtil::dumpVertexes(const char* text, const TopoDS_Shape& s)
Expand Down
1 change: 1 addition & 0 deletions src/Mod/TechDraw/App/DrawUtil.h
Expand Up @@ -42,6 +42,7 @@ class TechDrawExport DrawUtil {
static std::string makeGeomName(std::string geomType, int index);
static bool isSamePoint(TopoDS_Vertex v1, TopoDS_Vertex v2);
static bool isZeroEdge(TopoDS_Edge e);
static double sensibleScale(double working_scale);
//debugging routines
static void dumpVertexes(const char* text, const TopoDS_Shape& s);
static void dumpEdge(char* label, int i, TopoDS_Edge e);
Expand Down
11 changes: 8 additions & 3 deletions src/Mod/TechDraw/App/DrawView.cpp
Expand Up @@ -42,6 +42,7 @@
#include "DrawViewClip.h"
#include "DrawProjGroup.h"
#include "DrawProjGroupItem.h"
#include "DrawUtil.h"

#include <Mod/TechDraw/App/DrawViewPy.h> // generated from DrawViewPy.xml

Expand Down Expand Up @@ -202,10 +203,14 @@ double DrawView::autoScale(double w, double h) const
{
double fudgeFactor = 0.90;
QRectF viewBox = getRect();
double xScale = w/viewBox.width();
double yScale = h/viewBox.height();
//find a standard scale that's close? 1:2, 1:10, 1:100...?
//have to unscale rect to determine new scale
double vbw = viewBox.width()/Scale.getValue();
double vbh = viewBox.height()/Scale.getValue();
double xScale = w/vbw;
double yScale = h/vbh;
//TODO: find a standard scale that's close? 1:2, 1:10, 1:100...? Logic in TaskProjGroup
double newScale = fudgeFactor * std::min(xScale,yScale);
newScale = DrawUtil::sensibleScale(newScale);
return newScale;
}

Expand Down
10 changes: 9 additions & 1 deletion src/Mod/TechDraw/App/DrawViewPart.cpp
Expand Up @@ -736,7 +736,15 @@ double DrawViewPart::getBoxY(void) const

QRectF DrawViewPart::getRect() const
{
QRectF result(0.0,0.0,getBoxX(),getBoxY()); //this is from GO and is already scaled
double x = getBoxX();
double y = getBoxY();
QRectF result;
if (std::isinf(x) || std::isinf(y)) {
//geometry isn't created yet. return an arbitrary rect.
result = QRectF(0.0,0.0,100.0,100.0);
} else {
result = QRectF(0.0,0.0,getBoxX(),getBoxY()); //this is from GO and is already scaled
}
return result;
}

Expand Down
3 changes: 3 additions & 0 deletions src/Mod/TechDraw/Gui/TaskProjGroup.ui
Expand Up @@ -153,6 +153,9 @@
<property name="minimum">
<number>1</number>
</property>
<property name="maximum">
<number>999</number>
</property>
<property name="value">
<number>1</number>
</property>
Expand Down
2 changes: 0 additions & 2 deletions src/Mod/TechDraw/Gui/ViewProviderProjGroup.cpp
Expand Up @@ -29,7 +29,6 @@
# include <QMenu>
#endif

/// Here the FreeCAD includes sorted by Base,App,Gui......
#include <Base/Console.h>
#include <Base/Parameter.h>

Expand Down Expand Up @@ -110,7 +109,6 @@ void ViewProviderProjGroup::updateData(const App::Property* prop)

void ViewProviderProjGroup::onChanged(const App::Property *prop)
{
Base::Console().Message("TRACE - VPPG::onChanged(%s) \n",prop->getName());
if (prop == &(getViewObject()->Scale)) {
if (getViewObject()->ScaleType.isValue("Automatic")) {
getMDIViewPage()->redraw1View(getViewObject());
Expand Down

0 comments on commit 70b5c24

Please sign in to comment.