Skip to content

Commit

Permalink
Drawing: Symbol command
Browse files Browse the repository at this point in the history
A new symbol command and Drawing::FeatureViewSymbol object allow to
load the contents of an external svg file and place it as a symbol
on a Drawing page. This symbol can then be moved and rescaled.
  • Loading branch information
yorikvanhavre committed Nov 1, 2013
1 parent df3d7ff commit 1f2c5b0
Show file tree
Hide file tree
Showing 9 changed files with 701 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Mod/Drawing/App/AppDrawing.cpp
Expand Up @@ -21,6 +21,7 @@
#include "FeatureView.h"
#include "FeatureViewPart.h"
#include "FeatureViewAnnotation.h"
#include "FeatureViewSymbol.h"
#include "FeatureProjection.h"
#include "FeatureClip.h"
#include "PageGroup.h"
Expand Down Expand Up @@ -60,6 +61,7 @@ void DrawingExport initDrawing()
Drawing::FeatureViewPartPython ::init();
Drawing::FeatureViewPython ::init();
Drawing::FeatureViewAnnotation ::init();
Drawing::FeatureViewSymbol ::init();
Drawing::FeatureClip ::init();
}

Expand Down
42 changes: 42 additions & 0 deletions src/Mod/Drawing/App/AppDrawingPy.cpp
Expand Up @@ -30,10 +30,12 @@
#include "ProjectionAlgos.h"
#include <Base/Console.h>
#include <Base/VectorPy.h>
#include <boost/regex.hpp>


using namespace Drawing;
using namespace Part;
using namespace std;

static PyObject *
project(PyObject *self, PyObject *args)
Expand Down Expand Up @@ -158,6 +160,44 @@ projectToDXF(PyObject *self, PyObject *args)
} PY_CATCH;
}

static PyObject *
removeSvgTags(PyObject *self, PyObject *args)
{
const char* svgcode;
if (!PyArg_ParseTuple(args, "s",&svgcode))
return NULL;

PY_TRY {
string svg(svgcode);
string empty = "";
string endline = "--endOfLine--";
string linebreak = "\\n";
// removing linebreaks for regex to work
boost::regex e1 ("\\n");
svg = boost::regex_replace(svg, e1, endline);
// removing starting xml definition
boost::regex e2 ("<\\?xml.*?\\?>");
svg = boost::regex_replace(svg, e2, empty);
// removing starting svg tag
boost::regex e3 ("<svg.*?>");
svg = boost::regex_replace(svg, e3, empty);
// removing sodipodi tags -- DANGEROUS, some sodipodi tags are single, better leave it
//boost::regex e4 ("<sodipodi.*?>");
//svg = boost::regex_replace(svg, e4, empty);
// removing metadata tags
boost::regex e5 ("<metadata.*?</metadata>");
svg = boost::regex_replace(svg, e5, empty);
// removing closing svg tags
boost::regex e6 ("</svg>");
svg = boost::regex_replace(svg, e6, empty);
// restoring linebreaks
boost::regex e7 ("--endOfLine--");
svg = boost::regex_replace(svg, e7, linebreak);
Py::String result(svg);
return Py::new_reference_to(result);
} PY_CATCH;
}



/* registration table */
Expand All @@ -170,5 +210,7 @@ struct PyMethodDef Drawing_methods[] = {
"string = projectToSVG(TopoShape[,App.Vector Direction, string type]) -- Project a shape and return the SVG representation as string."},
{"projectToDXF" ,projectToDXF ,METH_VARARGS,
"string = projectToDXF(TopoShape[,App.Vector Direction, string type]) -- Project a shape and return the DXF representation as string."},
{"removeSvgTags" ,removeSvgTags ,METH_VARARGS,
"string = removeSvgTags(string) -- Removes the opening and closing svg tags and other metatags from a svg code, making it embeddable"},
{NULL, NULL} /* end of table marker */
};
2 changes: 2 additions & 0 deletions src/Mod/Drawing/App/CMakeLists.txt
Expand Up @@ -29,6 +29,8 @@ SET(Features_SRCS
FeatureViewPart.h
FeatureViewAnnotation.cpp
FeatureViewAnnotation.h
FeatureViewSymbol.cpp
FeatureViewSymbol.h
FeatureClip.cpp
FeatureClip.h
PageGroup.cpp
Expand Down
87 changes: 87 additions & 0 deletions src/Mod/Drawing/App/FeatureViewSymbol.cpp
@@ -0,0 +1,87 @@
/***************************************************************************
* Copyright (c) Yorik van Havre (yorik@uncreated.net) 2013 *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/


#include "PreCompiled.h"

#ifndef _PreComp_
# include <sstream>
#endif

#include <iomanip>

#include <Base/Exception.h>
#include <Base/FileInfo.h>

#include "FeatureViewSymbol.h"

using namespace Drawing;
using namespace std;


//===========================================================================
// FeatureViewSymbol
//===========================================================================

PROPERTY_SOURCE(Drawing::FeatureViewSymbol, Drawing::FeatureView)


FeatureViewSymbol::FeatureViewSymbol(void)
{
static const char *vgroup = "Drawing view";

ADD_PROPERTY_TYPE(Symbol,(""),vgroup,App::Prop_Hidden,"The SVG code defining this symbol");

}

FeatureViewSymbol::~FeatureViewSymbol()
{
}

App::DocumentObjectExecReturn *FeatureViewSymbol::execute(void)
{
std::stringstream result;
result << "<g transform=\"translate(" << X.getValue() << "," << Y.getValue() << ")"
<< " rotate(" << Rotation.getValue() << ")"
<< " scale(" << Scale.getValue() << ")\">" << endl
<< Symbol.getValue() << endl
<< "</g>" << endl;

// Apply the resulting fragment
ViewResult.setValue(result.str().c_str());

return App::DocumentObject::StdReturn;
}

// Python Drawing feature ---------------------------------------------------------

namespace App {
/// @cond DOXERR
PROPERTY_SOURCE_TEMPLATE(Drawing::FeatureViewSymbolPython, Drawing::FeatureViewSymbol)
template<> const char* Drawing::FeatureViewSymbolPython::getViewProviderName(void) const {
return "DrawingGui::ViewProviderDrawingView";
}
/// @endcond

// explicit template instantiation
template class DrawingExport FeaturePythonT<Drawing::FeatureViewSymbol>;
}
71 changes: 71 additions & 0 deletions src/Mod/Drawing/App/FeatureViewSymbol.h
@@ -0,0 +1,71 @@
/***************************************************************************
* Copyright (c) Yorik van Havre (yorik@uncreated.net 2013) *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/




#ifndef _FeatureViewSymbol_h_
#define _FeatureViewSymbol_h_


#include <App/DocumentObject.h>
#include <App/PropertyLinks.h>
#include "FeatureView.h"
#include <App/FeaturePython.h>


namespace Drawing
{


/** Base class of all View Features in the drawing module
*/
class DrawingExport FeatureViewSymbol : public FeatureView
{
PROPERTY_HEADER(Drawing::FeatureView);

public:
/// Constructor
FeatureViewSymbol(void);
virtual ~FeatureViewSymbol();

App::PropertyString Symbol;

/** @name methods overide Feature */
//@{
/// recalculate the Feature
virtual App::DocumentObjectExecReturn *execute(void);
//@}

/// returns the type name of the ViewProvider
virtual const char* getViewProviderName(void) const {
return "DrawingGui::ViewProviderDrawingView";
}
};

typedef App::FeaturePythonT<FeatureViewSymbol> FeatureViewSymbolPython;


} //namespace Drawing


#endif
55 changes: 55 additions & 0 deletions src/Mod/Drawing/Gui/Command.cpp
Expand Up @@ -438,6 +438,60 @@ bool CmdDrawingClip::isActive(void)
return (getActiveGuiDocument() ? true : false);
}


//===========================================================================
// Drawing_Symbol
//===========================================================================

DEF_STD_CMD_A(CmdDrawingSymbol);

CmdDrawingSymbol::CmdDrawingSymbol()
: Command("Drawing_Symbol")
{
// seting the
sGroup = QT_TR_NOOP("Drawing");
sMenuText = QT_TR_NOOP("&Symbol");
sToolTipText = QT_TR_NOOP("Inserts a symbol from a svg file in the active drawing");
sWhatsThis = "Drawing_Symbol";
sStatusTip = QT_TR_NOOP("Inserts a symbol from a svg file in the active drawing");
sPixmap = "actions/drawing-symbol";
}

void CmdDrawingSymbol::activated(int iMsg)
{

std::vector<App::DocumentObject*> pages = this->getDocument()->getObjectsOfType(Drawing::FeaturePage::getClassTypeId());
if (pages.empty()){
QMessageBox::warning(Gui::getMainWindow(), QObject::tr("No page to insert"),
QObject::tr("Create a page to insert."));
return;
}
// Reading an image
QString filename = Gui::FileDialog::getOpenFileName(Gui::getMainWindow(), QObject::tr("Choose an SVG file to open"), QString::null,
QObject::tr("Scalable Vector Graphics (*.svg *.svgz)"));
if (!filename.isEmpty())
{
std::string PageName = pages.front()->getNameInDocument();
std::string FeatName = getUniqueObjectName("Symbol");
openCommand("Create Symbol");
doCommand(Doc,"import Drawing");
doCommand(Doc,"f = open(\"%s\",\"r\")",(const char*)filename.toUtf8());
doCommand(Doc,"svg = f.read()");
doCommand(Doc,"f.close()");
doCommand(Doc,"App.activeDocument().addObject('Drawing::FeatureViewSymbol','%s')",FeatName.c_str());
doCommand(Doc,"App.activeDocument().%s.Symbol = Drawing.removeSvgTags(svg)",FeatName.c_str());
doCommand(Doc,"App.activeDocument().%s.addObject(App.activeDocument().%s)",PageName.c_str(),FeatName.c_str());
updateActive();
commitCommand();
}
}

bool CmdDrawingSymbol::isActive(void)
{
return (getActiveGuiDocument() ? true : false);
}


//===========================================================================
// Drawing_ExportPage
//===========================================================================
Expand Down Expand Up @@ -536,6 +590,7 @@ void CreateDrawingCommands(void)
rcCmdMgr.addCommand(new CmdDrawingOpenBrowserView());
rcCmdMgr.addCommand(new CmdDrawingAnnotation());
rcCmdMgr.addCommand(new CmdDrawingClip());
rcCmdMgr.addCommand(new CmdDrawingSymbol());
rcCmdMgr.addCommand(new CmdDrawingExportPage());
rcCmdMgr.addCommand(new CmdDrawingProjectShape());
}
1 change: 1 addition & 0 deletions src/Mod/Drawing/Gui/Resources/Drawing.qrc
Expand Up @@ -18,6 +18,7 @@
<file>icons/actions/drawing-openbrowser.svg</file>
<file>icons/actions/drawing-annotation.svg</file>
<file>icons/actions/drawing-clip.svg</file>
<file>icons/actions/drawing-symbol.svg</file>
<file>translations/Drawing_af.qm</file>
<file>translations/Drawing_de.qm</file>
<file>translations/Drawing_fi.qm</file>
Expand Down

0 comments on commit 1f2c5b0

Please sign in to comment.