Implement automatic scaling

This commit is contained in:
WandererFan 2016-08-05 14:12:47 -04:00 committed by wmayer
parent fa57b7a5de
commit cf90d69319
16 changed files with 198 additions and 78 deletions

View File

@ -244,11 +244,17 @@ int DrawPage::addView(App::DocumentObject *docObj)
view->Y.setValue(getPageHeight()/2.0); view->Y.setValue(getPageHeight()/2.0);
} }
//add view to list
const std::vector<App::DocumentObject *> currViews = Views.getValues(); const std::vector<App::DocumentObject *> currViews = Views.getValues();
std::vector<App::DocumentObject *> newViews(currViews); std::vector<App::DocumentObject *> newViews(currViews);
newViews.push_back(docObj); newViews.push_back(docObj);
Views.setValues(newViews); Views.setValues(newViews);
Views.touch();
//check if View fits on Page
if ( !view->checkFit(this) ) {
Base::Console().Warning("%s is larger than page. Will be scaled.\n",view->getNameInDocument());
view->ScaleType.setValue("Automatic");
}
return Views.getSize(); return Views.getSize();
} }

View File

@ -149,7 +149,8 @@ double DrawProjGroup::calculateAutomaticScale() const
double scale_x = availableX / width; double scale_x = availableX / width;
double scale_y = availableY / height; double scale_y = availableY / height;
float working_scale = std::min(scale_x, scale_y); 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... //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 //eg if working_scale = 0.115, then we want to use 0.1, similarly 7.65 -> 5, and 76.5 -> 50
@ -168,6 +169,15 @@ double DrawProjGroup::calculateAutomaticScale() const
return valid_scales[(exponent >= 0)][i] * pow(10, exponent); return valid_scales[(exponent >= 0)][i] * pow(10, exponent);
} }
QRectF DrawProjGroup::getRect() const
{
DrawProjGroupItem *viewPtrs[10];
arrangeViewPointers(viewPtrs);
double width, height;
minimumBbViews(viewPtrs, width, height); //min space for 1:1 drawing
return QRectF(0,0,Scale.getValue() * width,Scale.getValue() * height);
}
void DrawProjGroup::minimumBbViews(DrawProjGroupItem *viewPtrs[10], void DrawProjGroup::minimumBbViews(DrawProjGroupItem *viewPtrs[10],
double &width, double &height) const double &width, double &height) const
{ {
@ -543,29 +553,52 @@ void DrawProjGroup::setFrontViewOrientation(const Base::Matrix4D &newMat)
App::DocumentObjectExecReturn *DrawProjGroup::execute(void) App::DocumentObjectExecReturn *DrawProjGroup::execute(void)
{ {
//if group hasn't been added to page yet, can't scale or distribute projItems
TechDraw::DrawPage *page = getPage();
if (!page) {
return DrawViewCollection::execute();
}
if (ScaleType.isValue("Automatic")) { if (ScaleType.isValue("Automatic")) {
//Recalculate scale if Group is too big
//Recalculate scale if (!checkFit(page)) {
double autoScale = calculateAutomaticScale(); double newScale = calculateAutomaticScale();
if(std::abs(Scale.getValue() - newScale) > FLT_EPSILON) {
if(std::abs(Scale.getValue() - autoScale) > FLT_EPSILON) { Scale.setValue(newScale);
// Set this Scale //Rebuild the DPGI's
Scale.setValue(autoScale); for( const auto it : Views.getValues() ) {
auto view( dynamic_cast<DrawProjGroupItem *>(it) );
if( view ) {
view->ScaleType.setValue("Custom");
view->Scale.setValue(newScale); //not sure we have to set scale here. DVP will do it based on ScaleType
view->Scale.setStatus(App::Property::ReadOnly,true);
//view->touch();
}
}
resetPositions();
}
}
} else if (ScaleType.isValue("Document")) {
double docScale = page->Scale.getValue();
if(std::abs(Scale.getValue() - docScale) > FLT_EPSILON) {
Scale.setValue(docScale);
//Rebuild the DPGI's //Rebuild the DPGI's
for( const auto it : Views.getValues() ) { const std::vector<App::DocumentObject *> &views = Views.getValues();
auto view( dynamic_cast<DrawProjGroupItem *>(it) ); for(std::vector<App::DocumentObject *>::const_iterator it = views.begin(); it != views.end(); ++it) {
if( view ) { App::DocumentObject *docObj = *it;
view->ScaleType.setValue("Custom"); if(docObj->getTypeId().isDerivedFrom(DrawProjGroupItem::getClassTypeId())) {
view->Scale.setValue(autoScale); DrawProjGroupItem *view = dynamic_cast<DrawProjGroupItem *>(*it);
view->ScaleType.setValue("Document");
view->Scale.setValue(docScale);
view->Scale.setStatus(App::Property::ReadOnly,true); view->Scale.setStatus(App::Property::ReadOnly,true);
view->touch(); //view->touch();
} }
} }
resetPositions(); resetPositions();
} }
} }
// recalculate positions for children // recalculate positions for children
if (Views.getSize()) { if (Views.getSize()) {
distributeProjections(); distributeProjections();

View File

@ -23,6 +23,7 @@
#ifndef _TECHDRAW_FEATUREVIEWGROUP_H_ #ifndef _TECHDRAW_FEATUREVIEWGROUP_H_
#define _TECHDRAW_FEATUREVIEWGROUP_H_ #define _TECHDRAW_FEATUREVIEWGROUP_H_
# include <QRectF>
#include <App/DocumentObject.h> #include <App/DocumentObject.h>
#include <App/PropertyStandard.h> #include <App/PropertyStandard.h>
@ -63,6 +64,7 @@ public:
Base::BoundBox3d getBoundingBox() const; Base::BoundBox3d getBoundingBox() const;
double calculateAutomaticScale() const; double calculateAutomaticScale() const;
virtual QRectF getRect(void) const;
/// Check if container has a view of a specific type /// Check if container has a view of a specific type
bool hasProjection(const char *viewProjType) const; bool hasProjection(const char *viewProjType) const;

View File

@ -56,17 +56,9 @@ DrawProjGroupItem::DrawProjGroupItem(void)
Type.setEnums(TypeEnums); Type.setEnums(TypeEnums);
ADD_PROPERTY(Type, ((long)0)); ADD_PROPERTY(Type, ((long)0));
// Set Hidden //projection group controls these
//Direction.StatusBits.set(3);
Direction.setStatus(App::Property::Hidden,true); Direction.setStatus(App::Property::Hidden,true);
// Set Hidden
//XAxisDirection.StatusBits.set(3);
XAxisDirection.setStatus(App::Property::Hidden,true); XAxisDirection.setStatus(App::Property::Hidden,true);
// Scale and ScaleType are Readonly
//Scale.StatusBits.set(2);
//ScaleType.StatusBits.set(2);
Scale.setStatus(App::Property::ReadOnly,true); Scale.setStatus(App::Property::ReadOnly,true);
ScaleType.setStatus(App::Property::ReadOnly,true); ScaleType.setStatus(App::Property::ReadOnly,true);
} }
@ -101,16 +93,6 @@ void DrawProjGroupItem::onDocumentRestored()
execute(); execute();
} }
/*
//TODO: Perhaps we don't need this anymore?
App::DocumentObjectExecReturn *DrawProjGroupItem::execute(void)
{
if(Type.isTouched()) {
Type.purgeTouched();
}
return TechDraw::DrawViewPart::execute();
}*/
PyObject *DrawProjGroupItem::getPyObject(void) PyObject *DrawProjGroupItem::getPyObject(void)
{ {

View File

@ -79,69 +79,63 @@ DrawView::~DrawView()
{ {
} }
App::DocumentObjectExecReturn *DrawView::recompute(void)
{
try {
return App::DocumentObject::recompute();
}
catch (Standard_Failure) {
Handle_Standard_Failure e = Standard_Failure::Caught();
App::DocumentObjectExecReturn* ret = new App::DocumentObjectExecReturn(e->GetMessageString());
if (ret->Why.empty()) ret->Why = "Unknown OCC exception";
return ret;
}
}
App::DocumentObjectExecReturn *DrawView::execute(void) App::DocumentObjectExecReturn *DrawView::execute(void)
{ {
//right way to handle this? can't do it at creation since don't have a parent. TechDraw::DrawPage *page = findParentPage();
if (ScaleType.isValue("Document")) { if(page) {
TechDraw::DrawPage *page = findParentPage(); if (ScaleType.isValue("Document")) {
if(page) {
if(std::abs(page->Scale.getValue() - Scale.getValue()) > FLT_EPSILON) { if(std::abs(page->Scale.getValue() - Scale.getValue()) > FLT_EPSILON) {
Scale.setValue(page->Scale.getValue()); Scale.setValue(page->Scale.getValue());
Scale.touch(); }
} else if (ScaleType.isValue("Automatic")) {
//check fit. if too big, rescale
if (!checkFit(page)) {
double newScale = autoScale(page->getPageWidth(),page->getPageHeight());
if(std::abs(newScale - Scale.getValue()) > FLT_EPSILON) { //stops onChanged/execute loop
Scale.setValue(newScale);
}
} }
} }
} }
return App::DocumentObject::execute(); return App::DocumentObject::execute();
} }
/// get called by the container when a Property was changed
void DrawView::onChanged(const App::Property* prop) void DrawView::onChanged(const App::Property* prop)
{ {
if (!isRestoring()) { if (!isRestoring()) {
if (prop == &ScaleType || if (prop == &Scale) {
prop == &Scale) { execute();
} else if (prop == &ScaleType) {
if (ScaleType.isValue("Document")) { if (ScaleType.isValue("Document")) {
TechDraw::DrawPage *page = findParentPage();
if(page) {
if(std::abs(page->Scale.getValue() - Scale.getValue()) > FLT_EPSILON) {
Scale.setValue(page->Scale.getValue()); // Reset scale from page
Scale.touch();
}
}
Scale.setStatus(App::Property::ReadOnly,true); Scale.setStatus(App::Property::ReadOnly,true);
App::GetApplication().signalChangePropertyEditor(Scale); App::GetApplication().signalChangePropertyEditor(Scale);
} else if ( ScaleType.isValue("Custom") ) { } else if ( ScaleType.isValue("Custom") ) {
Scale.setStatus(App::Property::ReadOnly,false); Scale.setStatus(App::Property::ReadOnly,false);
App::GetApplication().signalChangePropertyEditor(Scale); App::GetApplication().signalChangePropertyEditor(Scale);
} else if ( ScaleType.isValue("Automatic") ) {
Scale.setStatus(App::Property::ReadOnly,true);
App::GetApplication().signalChangePropertyEditor(Scale);
} }
//TODO else if (ScaleType.isValue("Automatic"))... execute();
DrawView::execute();
} else if (prop == &X || } else if (prop == &X ||
prop == &Y) { prop == &Y) {
setAutoPos(false); setAutoPos(false);
DrawView::execute(); execute();
} else if (prop == &Rotation) { } else if (prop == &Rotation) {
DrawView::execute(); execute();
} }
} }
App::DocumentObject::onChanged(prop); App::DocumentObject::onChanged(prop);
} }
////you must override this in derived class
QRectF DrawView::getRect() const
{
QRectF result(0,0,1,1);
return result;
}
void DrawView::onDocumentRestored() void DrawView::onDocumentRestored()
{ {
// Rebuild the view // Rebuild the view
@ -182,6 +176,29 @@ bool DrawView::isInClip()
return false; return false;
} }
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...?
double newScale = fudgeFactor * std::min(xScale,yScale);
return newScale;
}
//!check if View fits on Page
bool DrawView::checkFit(TechDraw::DrawPage* p) const
{
bool result = true;
QRectF viewBox = getRect();
if ( (viewBox.width() > p->getPageWidth()) ||
(viewBox.height() > p->getPageHeight()) ) {
result = false;
}
return result;
}
PyObject *DrawView::getPyObject(void) PyObject *DrawView::getPyObject(void)
{ {
if (PythonObject.is(Py::_None())) { if (PythonObject.is(Py::_None())) {

View File

@ -23,6 +23,8 @@
#ifndef _DrawView_h_ #ifndef _DrawView_h_
#define _DrawView_h_ #define _DrawView_h_
#include <QRectF>
#include <App/DocumentObject.h> #include <App/DocumentObject.h>
#include <App/PropertyStandard.h> #include <App/PropertyStandard.h>
#include <App/PropertyGeo.h> #include <App/PropertyGeo.h>
@ -54,7 +56,6 @@ public:
/** @name methods overide Feature */ /** @name methods overide Feature */
//@{ //@{
/// recalculate the Feature /// recalculate the Feature
virtual App::DocumentObjectExecReturn *recompute(void);
virtual App::DocumentObjectExecReturn *execute(void); virtual App::DocumentObjectExecReturn *execute(void);
virtual void onDocumentRestored(); virtual void onDocumentRestored();
//@} //@}
@ -71,6 +72,9 @@ public:
DrawPage* findParentPage() const; DrawPage* findParentPage() const;
bool allowAutoPos() {return autoPos;}; //sb in DPGI?? bool allowAutoPos() {return autoPos;}; //sb in DPGI??
void setAutoPos(bool state) {autoPos = state;}; void setAutoPos(bool state) {autoPos = state;};
virtual QRectF getRect() const; //must be overridden by derived class
virtual double autoScale(double w, double h) const;
virtual bool checkFit(DrawPage*) const;
protected: protected:
void onChanged(const App::Property* prop); void onChanged(const App::Property* prop);

View File

@ -73,8 +73,6 @@ DrawViewAnnotation::DrawViewAnnotation(void)
TextStyle.setEnums(TextStyleEnums); TextStyle.setEnums(TextStyleEnums);
ADD_PROPERTY(TextStyle, ((long)0)); ADD_PROPERTY(TextStyle, ((long)0));
//Scale.StatusBits.set(3); //hide scale. n/a for Annotation
//ScaleType.StatusBits.set(3);
Scale.setStatus(App::Property::Hidden,true); Scale.setStatus(App::Property::Hidden,true);
ScaleType.setStatus(App::Property::Hidden,true); ScaleType.setStatus(App::Property::Hidden,true);
} }
@ -104,6 +102,23 @@ void DrawViewAnnotation::onChanged(const App::Property* prop)
TechDraw::DrawView::onChanged(prop); TechDraw::DrawView::onChanged(prop);
} }
QRectF DrawViewAnnotation::getRect() const
{
QRectF result;
double tSize = TextSize.getValue();
int lines = Text.getValues().size();
int chars = 1;
for (auto& l:Text.getValues()) {
if ((int)l.size() > chars) {
chars = (int)l.size();
}
}
int w = chars * std::max(1,(int)tSize);
int h = lines * std::max(1,(int)tSize);
result = QRectF(0,0,Scale.getValue() * w,Scale.getValue() * h);
return result;
}
App::DocumentObjectExecReturn *DrawViewAnnotation::execute(void) App::DocumentObjectExecReturn *DrawViewAnnotation::execute(void)
{ {
return TechDraw::DrawView::execute(); return TechDraw::DrawView::execute();

View File

@ -27,7 +27,6 @@
#ifndef _DrawViewAnnotation_h_ #ifndef _DrawViewAnnotation_h_
#define _DrawViewAnnotation_h_ #define _DrawViewAnnotation_h_
#include <App/DocumentObject.h> #include <App/DocumentObject.h>
#include <App/PropertyLinks.h> #include <App/PropertyLinks.h>
#include <App/PropertyStandard.h> #include <App/PropertyStandard.h>
@ -39,8 +38,6 @@ namespace TechDraw
{ {
/** Base class of all View Features in the drawing module
*/
class TechDrawExport DrawViewAnnotation : public TechDraw::DrawView class TechDrawExport DrawViewAnnotation : public TechDraw::DrawView
{ {
PROPERTY_HEADER(TechDraw::DrawViewAnnotation); PROPERTY_HEADER(TechDraw::DrawViewAnnotation);
@ -58,6 +55,8 @@ public:
App::PropertyEnumeration TextStyle; // Plain,Bold,Italic,Bold-Italic App::PropertyEnumeration TextStyle; // Plain,Bold,Italic,Bold-Italic
App::PropertyFloat MaxWidth; App::PropertyFloat MaxWidth;
virtual QRectF getRect() const;
/** @name methods overide Feature */ /** @name methods overide Feature */
//@{ //@{
/// recalculate the Feature /// recalculate the Feature

View File

@ -25,7 +25,6 @@
#ifndef _DrawViewClip_h_ #ifndef _DrawViewClip_h_
#define _DrawViewClip_h_ #define _DrawViewClip_h_
#include <App/DocumentObject.h> #include <App/DocumentObject.h>
#include <App/DocumentObjectGroup.h> #include <App/DocumentObjectGroup.h>
#include <App/PropertyLinks.h> #include <App/PropertyLinks.h>
@ -71,6 +70,7 @@ public:
std::vector<std::string> getChildViewNames(); std::vector<std::string> getChildViewNames();
bool isViewInClip(App::DocumentObject* view); bool isViewInClip(App::DocumentObject* view);
virtual QRectF getRect(void) const { return QRectF(0,0,Width.getValue(),Height.getValue()); }
protected: protected:

View File

@ -155,3 +155,14 @@ App::DocumentObjectExecReturn *DrawViewCollection::execute(void)
return DrawView::execute(); return DrawView::execute();
} }
QRectF DrawViewCollection::getRect() const
{
QRectF result;
for (auto& v:Views.getValues()) {
TechDraw::DrawView *view = dynamic_cast<TechDraw::DrawView *>(v);
result = result.united(view->getRect().translated(view->X.getValue(),view->Y.getValue()));
}
return QRectF(0,0,Scale.getValue() * result.width(),Scale.getValue() * result.height());
}

View File

@ -61,6 +61,7 @@ public:
virtual const char* getViewProviderName(void) const { virtual const char* getViewProviderName(void) const {
return "TechDrawGui::ViewProviderViewCollection"; return "TechDrawGui::ViewProviderViewCollection";
} }
virtual QRectF getRect(void) const;
protected: protected:
void onChanged(const App::Property* prop); void onChanged(const App::Property* prop);

View File

@ -81,6 +81,7 @@ public:
virtual std::string getFormatedValue() const; virtual std::string getFormatedValue() const;
virtual double getDimValue() const; virtual double getDimValue() const;
DrawViewPart* getViewPart() const; DrawViewPart* getViewPart() const;
virtual QRectF getRect() const { return QRectF(0,0,1,1);} //pretend dimensions always fit!
protected: protected:
void onChanged(const App::Property* prop); void onChanged(const App::Property* prop);

View File

@ -581,6 +581,26 @@ Base::BoundBox3d DrawViewPart::getBoundingBox() const
return bbox; return bbox;
} }
double DrawViewPart::getBoxX(void) const
{
Base::BoundBox3d bbx = getBoundingBox(); //bbox is already scaled
return (bbx.MaxX - bbx.MinX);
}
double DrawViewPart::getBoxY(void) const
{
Base::BoundBox3d bbx = getBoundingBox();
return (bbx.MaxY - bbx.MinY);
}
QRectF DrawViewPart::getRect() const
{
QRectF result(0.0,0.0,getBoxX(),getBoxY()); //this is from GO and is already scaled
return result;
}
//! make a clean wire with sorted, oriented, connected, etc edges //! make a clean wire with sorted, oriented, connected, etc edges
TopoDS_Wire DrawViewPart::makeCleanWire(std::vector<TopoDS_Edge> edges, double tol) TopoDS_Wire DrawViewPart::makeCleanWire(std::vector<TopoDS_Edge> edges, double tol)
{ {

View File

@ -87,6 +87,9 @@ public:
TechDrawGeometry::Vertex* getProjVertexByIndex(int idx) const; //get existing geom for vertex idx in projection TechDrawGeometry::Vertex* getProjVertexByIndex(int idx) const; //get existing geom for vertex idx in projection
std::vector<TechDrawGeometry::BaseGeom*> getProjFaceByIndex(int idx) const; //get edges for face idx in projection std::vector<TechDrawGeometry::BaseGeom*> getProjFaceByIndex(int idx) const; //get edges for face idx in projection
virtual Base::BoundBox3d getBoundingBox() const; virtual Base::BoundBox3d getBoundingBox() const;
double getBoxX(void) const;
double getBoxY(void) const;
virtual QRectF getRect() const;
short mustExecute() const; short mustExecute() const;

View File

@ -61,7 +61,6 @@ DrawViewSymbol::~DrawViewSymbol()
{ {
} }
/// get called by the container when a Property was changed
void DrawViewSymbol::onChanged(const App::Property* prop) void DrawViewSymbol::onChanged(const App::Property* prop)
{ {
if (prop == &Symbol) { if (prop == &Symbol) {
@ -90,8 +89,8 @@ App::DocumentObjectExecReturn *DrawViewSymbol::execute(void)
std::string svg = Symbol.getValue(); std::string svg = Symbol.getValue();
const std::vector<std::string>& editText = EditableTexts.getValues(); const std::vector<std::string>& editText = EditableTexts.getValues();
if (!editText.empty()) { if (!editText.empty()) {
//TODO: has this ever been run? //TODO: has this ever been run?
boost::regex e1 ("<text.*?freecad:editable=\"(.*?)\".*?<tspan.*?>(.*?)</tspan>"); boost::regex e1 ("<text.*?freecad:editable=\"(.*?)\".*?<tspan.*?>(.*?)</tspan>");
string::const_iterator begin, end; string::const_iterator begin, end;
begin = svg.begin(); begin = svg.begin();
@ -121,6 +120,32 @@ App::DocumentObjectExecReturn *DrawViewSymbol::execute(void)
return DrawView::execute(); return DrawView::execute();
} }
QRectF DrawViewSymbol::getRect() const
{
std::string svg = Symbol.getValue();
double w = 64.0; //must default to something
double h = 64.0;
string::const_iterator begin, end;
begin = svg.begin();
end = svg.end();
boost::match_results<std::string::const_iterator> what;
boost::regex e1 ("width=\"([0-9.]*?)[a-zA-Z]*?\"");
if (boost::regex_search(begin, end, what, e1)) {
//std::string wText = what[0].str(); //this is the whole match 'width="100"'
std::string wNum = what[1].str(); //this is just the number 100
w = std::stod(wNum);
}
boost::regex e2 ("Height=\"([0-9.]*?)[a-zA-Z]*?\"");
if (boost::regex_search(begin, end, what, e1)) {
//std::string hText = what[0].str();
std::string hNum = what[1].str();
h = std::stod(hNum);
}
return (QRectF(0,0,Scale.getValue() * w,Scale.getValue() * h));
}
// Python Drawing feature --------------------------------------------------------- // Python Drawing feature ---------------------------------------------------------
namespace App { namespace App {

View File

@ -56,6 +56,7 @@ public:
virtual const char* getViewProviderName(void) const { virtual const char* getViewProviderName(void) const {
return "TechDrawGui::ViewProviderSymbol"; return "TechDrawGui::ViewProviderSymbol";
} }
virtual QRectF getRect() const;
protected: protected:
virtual void onChanged(const App::Property* prop); virtual void onChanged(const App::Property* prop);