Reduce redundant code in View3DInventorViewer class
This commit is contained in:
parent
7e3751224a
commit
f05be462bc
|
@ -85,19 +85,16 @@ void SoFCOffscreenRenderer::writeToImage (QImage& img) const
|
||||||
BitmapFactory().convert(image, img);
|
BitmapFactory().convert(image, img);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SoFCOffscreenRenderer::writeToImageFile (const char *filename, const char* comment) const
|
void SoFCOffscreenRenderer::writeToImageFile(const char* filename, const char* comment, const SbMatrix& mat, const QImage& image)
|
||||||
{
|
{
|
||||||
Base::FileInfo file(filename);
|
Base::FileInfo file(filename);
|
||||||
if (file.hasExtension("JPG") || file.hasExtension("JPEG")) {
|
if (file.hasExtension("JPG") || file.hasExtension("JPEG")) {
|
||||||
QImage img;
|
|
||||||
writeToImage(img);
|
|
||||||
|
|
||||||
// writing comment in case of jpeg (Qt ignores setText() in case of jpeg)
|
// writing comment in case of jpeg (Qt ignores setText() in case of jpeg)
|
||||||
std::string com;
|
std::string com;
|
||||||
if (strcmp(comment,"")==0)
|
if (strcmp(comment,"")==0)
|
||||||
com = "Screenshot created by FreeCAD";
|
com = "Screenshot created by FreeCAD";
|
||||||
else if (strcmp(comment,"$MIBA")==0)
|
else if (strcmp(comment,"$MIBA")==0)
|
||||||
com = createMIBA();
|
com = createMIBA(mat);
|
||||||
else
|
else
|
||||||
com = comment;
|
com = comment;
|
||||||
|
|
||||||
|
@ -105,7 +102,7 @@ void SoFCOffscreenRenderer::writeToImageFile (const char *filename, const char*
|
||||||
QByteArray ba;
|
QByteArray ba;
|
||||||
QBuffer buffer(&ba);
|
QBuffer buffer(&ba);
|
||||||
buffer.open(QIODevice::WriteOnly);
|
buffer.open(QIODevice::WriteOnly);
|
||||||
img.save(&buffer, "JPG");
|
image.save(&buffer, "JPG");
|
||||||
writeJPEGComment(com, ba);
|
writeJPEGComment(com, ba);
|
||||||
|
|
||||||
QFile file(QString::fromUtf8(filename));
|
QFile file(QString::fromUtf8(filename));
|
||||||
|
@ -134,8 +131,7 @@ void SoFCOffscreenRenderer::writeToImageFile (const char *filename, const char*
|
||||||
|
|
||||||
// Supported by Qt
|
// Supported by Qt
|
||||||
if (supported) {
|
if (supported) {
|
||||||
QImage img;
|
QImage img = image;
|
||||||
writeToImage(img);
|
|
||||||
// set keywords for PNG format
|
// set keywords for PNG format
|
||||||
if (file.hasExtension("PNG")) {
|
if (file.hasExtension("PNG")) {
|
||||||
img.setText(QLatin1String("Title"), QString::fromUtf8(filename));
|
img.setText(QLatin1String("Title"), QString::fromUtf8(filename));
|
||||||
|
@ -143,7 +139,7 @@ void SoFCOffscreenRenderer::writeToImageFile (const char *filename, const char*
|
||||||
if (strcmp(comment,"")==0)
|
if (strcmp(comment,"")==0)
|
||||||
img.setText(QLatin1String("Description"), QLatin1String("Screenshot created by FreeCAD"));
|
img.setText(QLatin1String("Description"), QLatin1String("Screenshot created by FreeCAD"));
|
||||||
else if (strcmp(comment,"$MIBA")==0)
|
else if (strcmp(comment,"$MIBA")==0)
|
||||||
img.setText(QLatin1String("Description"), QLatin1String(createMIBA().c_str()));
|
img.setText(QLatin1String("Description"), QLatin1String(createMIBA(mat).c_str()));
|
||||||
else
|
else
|
||||||
img.setText(QLatin1String("Description"), QString::fromUtf8(comment));
|
img.setText(QLatin1String("Description"), QString::fromUtf8(comment));
|
||||||
img.setText(QLatin1String("Creation Time"), QDateTime::currentDateTime().toString());
|
img.setText(QLatin1String("Creation Time"), QDateTime::currentDateTime().toString());
|
||||||
|
@ -169,6 +165,9 @@ void SoFCOffscreenRenderer::writeToImageFile (const char *filename, const char*
|
||||||
throw Base::Exception(str.str());
|
throw Base::Exception(str.str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//
|
||||||
|
// Use internal buffer instead of QImage
|
||||||
|
//
|
||||||
else if (isWriteSupported(file.extension().c_str())) {
|
else if (isWriteSupported(file.extension().c_str())) {
|
||||||
// Any format which is supported by Coin only
|
// Any format which is supported by Coin only
|
||||||
if (!writeToFile(filename, file.extension().c_str()))
|
if (!writeToFile(filename, file.extension().c_str()))
|
||||||
|
@ -249,7 +248,7 @@ QStringList SoFCOffscreenRenderer::getWriteImageFiletypeInfo()
|
||||||
return formats;
|
return formats;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string SoFCOffscreenRenderer::createMIBA() const
|
std::string SoFCOffscreenRenderer::createMIBA(const SbMatrix& mat) const
|
||||||
{
|
{
|
||||||
std::stringstream com;
|
std::stringstream com;
|
||||||
const std::map<std::string, std::string>& cfg = App::Application::Config();
|
const std::map<std::string, std::string>& cfg = App::Application::Config();
|
||||||
|
@ -264,10 +263,10 @@ std::string SoFCOffscreenRenderer::createMIBA() const
|
||||||
com << "<MIBA xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://juergen-riegel.net/Miba/Miba2.xsd\" Version=\"2\"> \n" ;
|
com << "<MIBA xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://juergen-riegel.net/Miba/Miba2.xsd\" Version=\"2\"> \n" ;
|
||||||
com << " <View>\n";
|
com << " <View>\n";
|
||||||
com << " <Matrix \n";
|
com << " <Matrix \n";
|
||||||
com << " a11=\"" << _Matrix[0][0] <<"\" a12=\"" << _Matrix[1][0] <<"\" a13=\"" << _Matrix[2][0] <<"\" a14=\"" << _Matrix[3][0] << "\"\n";
|
com << " a11=\"" << mat[0][0] <<"\" a12=\"" << mat[1][0] <<"\" a13=\"" << mat[2][0] <<"\" a14=\"" << mat[3][0] << "\"\n";
|
||||||
com << " a21=\"" << _Matrix[0][1] <<"\" a22=\"" << _Matrix[1][1] <<"\" a23=\"" << _Matrix[2][1] <<"\" a24=\"" << _Matrix[3][1] << "\"\n";
|
com << " a21=\"" << mat[0][1] <<"\" a22=\"" << mat[1][1] <<"\" a23=\"" << mat[2][1] <<"\" a24=\"" << mat[3][1] << "\"\n";
|
||||||
com << " a31=\"" << _Matrix[0][2] <<"\" a32=\"" << _Matrix[1][2] <<"\" a33=\"" << _Matrix[2][2] <<"\" a34=\"" << _Matrix[3][2] << "\"\n";
|
com << " a31=\"" << mat[0][2] <<"\" a32=\"" << mat[1][2] <<"\" a33=\"" << mat[2][2] <<"\" a34=\"" << mat[3][2] << "\"\n";
|
||||||
com << " a41=\"" << _Matrix[0][3] <<"\" a42=\"" << _Matrix[1][3] <<"\" a43=\"" << _Matrix[2][3] <<"\" a44=\"" << _Matrix[3][3] << "\"\n";
|
com << " a41=\"" << mat[0][3] <<"\" a42=\"" << mat[1][3] <<"\" a43=\"" << mat[2][3] <<"\" a44=\"" << mat[3][3] << "\"\n";
|
||||||
com << " />\n" ;
|
com << " />\n" ;
|
||||||
com << " </View>\n" ;
|
com << " </View>\n" ;
|
||||||
com << " <Source>\n" ;
|
com << " <Source>\n" ;
|
||||||
|
|
|
@ -79,19 +79,22 @@ public:
|
||||||
* Note that you must still specify the full filename for the first argument, i.e. the second argument will
|
* Note that you must still specify the full filename for the first argument, i.e. the second argument will
|
||||||
* not automatically be attached to the filename -- it is only used to decide the filetype.
|
* not automatically be attached to the filename -- it is only used to decide the filetype.
|
||||||
*
|
*
|
||||||
|
* If \a comment is set to '$MIBA' information regarding the MIBA standard is
|
||||||
|
* embedded to the picture, otherwise the \a comment is embedded as is.
|
||||||
|
* The appropriate file format must support embedding meta information which
|
||||||
|
* is provided by JPEG or PNG.
|
||||||
|
*
|
||||||
* This does basically the same as writeToFile() unless that all QImage file formats are supported if not
|
* This does basically the same as writeToFile() unless that all QImage file formats are supported if not
|
||||||
* directly supported by Coin3D.
|
* directly supported by Coin3D.
|
||||||
*/
|
*/
|
||||||
void writeToImageFile (const char *filename, const char* comment) const;
|
void writeToImageFile(const char* filename, const char* comment, const SbMatrix& mat, const QImage& img);
|
||||||
/**
|
/**
|
||||||
* This method returns all image file formats supported by Coin3D (see getWriteFiletypeInfo()) with all QImage file formats that are
|
* This method returns all image file formats supported by Coin3D (see getWriteFiletypeInfo()) with all QImage file formats that are
|
||||||
* not directly supported by Coin3D, if so.
|
* not directly supported by Coin3D, if so.
|
||||||
*/
|
*/
|
||||||
QStringList getWriteImageFiletypeInfo();
|
QStringList getWriteImageFiletypeInfo();
|
||||||
|
|
||||||
std::string createMIBA() const;
|
std::string createMIBA(const SbMatrix& mat) const;
|
||||||
|
|
||||||
SbMatrix _Matrix;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Gui
|
} // namespace Gui
|
||||||
|
|
|
@ -516,81 +516,6 @@ void View3DInventorViewer::setSceneGraph (SoNode *root)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void View3DInventorViewer::savePicture(const char* filename, int w, int h,
|
|
||||||
int eBackgroundType, const char* comment) const
|
|
||||||
{
|
|
||||||
// if no valid color use the current background
|
|
||||||
bool useBackground = false;
|
|
||||||
SbViewportRegion vp(getViewportRegion());
|
|
||||||
if (w>0 && h>0)
|
|
||||||
vp.setWindowSize( (short)w, (short)h );
|
|
||||||
|
|
||||||
//NOTE: To support pixels per inch we must use SbViewportRegion::setPixelsPerInch( ppi );
|
|
||||||
//The default value is 72.0.
|
|
||||||
//If we need to support grayscale images with must either use SoOffscreenRenderer::LUMINANCE or
|
|
||||||
//SoOffscreenRenderer::LUMINANCE_TRANSPARENCY.
|
|
||||||
SoFCOffscreenRenderer& renderer = SoFCOffscreenRenderer::instance();
|
|
||||||
renderer.setViewportRegion(vp);
|
|
||||||
SoCallback* cb = 0;
|
|
||||||
|
|
||||||
// if we use transparency then we must not set a background color
|
|
||||||
switch(eBackgroundType){
|
|
||||||
case Current:
|
|
||||||
if (backgroundroot->findChild(pcBackGround) == -1) {
|
|
||||||
renderer.setBackgroundColor(this->getBackgroundColor());
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
useBackground = true;
|
|
||||||
cb = new SoCallback;
|
|
||||||
cb->setCallback(clearBuffer);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case White:
|
|
||||||
renderer.setBackgroundColor( SbColor(1.0, 1.0, 1.0) );
|
|
||||||
break;
|
|
||||||
case Black:
|
|
||||||
renderer.setBackgroundColor( SbColor(0.0, 0.0, 0.0) );
|
|
||||||
break;
|
|
||||||
case Transparent:
|
|
||||||
renderer.setComponents(SoFCOffscreenRenderer::RGB_TRANSPARENCY );
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
SoSeparator* root = new SoSeparator;
|
|
||||||
root->ref();
|
|
||||||
|
|
||||||
SoCamera* camera = getCamera();
|
|
||||||
if (useBackground) {
|
|
||||||
root->addChild(backgroundroot);
|
|
||||||
root->addChild(cb);
|
|
||||||
}
|
|
||||||
root->addChild(getHeadlight());
|
|
||||||
root->addChild(camera);
|
|
||||||
SoCallback* gl = new SoCallback;
|
|
||||||
gl->setCallback(setGLWidget,this->getGLWidget());
|
|
||||||
root->addChild(gl);
|
|
||||||
root->addChild(pcViewProviderRoot);
|
|
||||||
if (useBackground)
|
|
||||||
root->addChild(cb);
|
|
||||||
root->addChild(foregroundroot);
|
|
||||||
|
|
||||||
try {
|
|
||||||
// render the scene
|
|
||||||
if (!renderer.render(root))
|
|
||||||
throw Base::Exception("Offscreen rendering failed");
|
|
||||||
// set matrix for miba
|
|
||||||
renderer._Matrix = camera->getViewVolume().getMatrix();
|
|
||||||
renderer.writeToImageFile(filename, comment);
|
|
||||||
root->unref();
|
|
||||||
}
|
|
||||||
catch (...) {
|
|
||||||
root->unref();
|
|
||||||
throw; // re-throw exception
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void View3DInventorViewer::savePicture(int w, int h, int eBackgroundType, QImage& img) const
|
void View3DInventorViewer::savePicture(int w, int h, int eBackgroundType, QImage& img) const
|
||||||
{
|
{
|
||||||
// if no valid color use the current background
|
// if no valid color use the current background
|
||||||
|
|
|
@ -162,14 +162,8 @@ public:
|
||||||
//@{
|
//@{
|
||||||
/**
|
/**
|
||||||
* Creates an image with width \a w and height \a h of the current scene graph
|
* Creates an image with width \a w and height \a h of the current scene graph
|
||||||
* and exports the rendered scenegraph directly to file \a filename.
|
* and exports the rendered scenegraph to an image.
|
||||||
* If \a comment is set to '$MIBA' information regarding the MIBA standard is
|
|
||||||
* embedded to the picture, otherwise the \a comment is embedded as is.
|
|
||||||
* The appropriate file format must support embedding meta information which
|
|
||||||
* is provided by JPEG or PNG.
|
|
||||||
*/
|
*/
|
||||||
void savePicture(const char* filename, int w, int h, int eBackgroundType,
|
|
||||||
const char* comment) const;
|
|
||||||
void savePicture(int w, int h, int eBackgroundType, QImage&) const;
|
void savePicture(int w, int h, int eBackgroundType, QImage&) const;
|
||||||
void saveGraphic(int pagesize, int eBackgroundType, SoVectorizeAction* va) const;
|
void saveGraphic(int pagesize, int eBackgroundType, SoVectorizeAction* va) const;
|
||||||
//@}
|
//@}
|
||||||
|
|
|
@ -28,6 +28,8 @@
|
||||||
# include <sstream>
|
# include <sstream>
|
||||||
# include <QImage>
|
# include <QImage>
|
||||||
# include <QGLFramebufferObject>
|
# include <QGLFramebufferObject>
|
||||||
|
# include <Inventor/SbViewVolume.h>
|
||||||
|
# include <Inventor/nodes/SoCamera.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
@ -39,6 +41,7 @@
|
||||||
#include "NavigationStyle.h"
|
#include "NavigationStyle.h"
|
||||||
#include "SoFCSelection.h"
|
#include "SoFCSelection.h"
|
||||||
#include "SoFCSelectionAction.h"
|
#include "SoFCSelectionAction.h"
|
||||||
|
#include "SoFCOffscreenRenderer.h"
|
||||||
#include "SoFCVectorizeSVGAction.h"
|
#include "SoFCVectorizeSVGAction.h"
|
||||||
#include "SoFCVectorizeU3DAction.h"
|
#include "SoFCVectorizeU3DAction.h"
|
||||||
#include "SoFCDB.h"
|
#include "SoFCDB.h"
|
||||||
|
@ -620,24 +623,25 @@ Py::Object View3DInventorPy::saveImage(const Py::Tuple& args)
|
||||||
else
|
else
|
||||||
throw Py::Exception("Parameter 4 have to be (Current|Black|White|Transparent)");
|
throw Py::Exception("Parameter 4 have to be (Current|Black|White|Transparent)");
|
||||||
#endif
|
#endif
|
||||||
|
QImage img;
|
||||||
if (App::GetApplication().GetParameterGroupByPath
|
if (App::GetApplication().GetParameterGroupByPath
|
||||||
("User parameter:BaseApp/Preferences/Document")->GetBool("DisablePBuffers",false)) {
|
("User parameter:BaseApp/Preferences/Document")->GetBool("DisablePBuffers",false)) {
|
||||||
QImage img;
|
|
||||||
createImageFromFramebuffer(t, w, h, img);
|
createImageFromFramebuffer(t, w, h, img);
|
||||||
img.save(QString::fromUtf8(cFileName));
|
}
|
||||||
return Py::None();
|
else {
|
||||||
|
try {
|
||||||
|
_view->getViewer()->savePicture(w, h, t, img);
|
||||||
|
}
|
||||||
|
catch (const Base::Exception&) {
|
||||||
|
createImageFromFramebuffer(t, w, h, img);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
SoFCOffscreenRenderer& renderer = SoFCOffscreenRenderer::instance();
|
||||||
_view->getViewer()->savePicture(cFileName,w,h,t,cComment);
|
SoCamera* cam = _view->getViewer()->getCamera();
|
||||||
return Py::None();
|
renderer.writeToImageFile(cFileName, cComment, cam->getViewVolume().getMatrix(), img);
|
||||||
}
|
|
||||||
catch (const Base::Exception&) {
|
return Py::None();
|
||||||
QImage img;
|
|
||||||
createImageFromFramebuffer(t, w, h, img);
|
|
||||||
img.save(QString::fromUtf8(cFileName));
|
|
||||||
return Py::None();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Py::Object View3DInventorPy::saveVectorGraphic(const Py::Tuple& args)
|
Py::Object View3DInventorPy::saveVectorGraphic(const Py::Tuple& args)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user