Skip to content

Commit

Permalink
[TD]Python routines & extension for CosmeticVertex
Browse files Browse the repository at this point in the history
  • Loading branch information
WandererFan committed Dec 17, 2019
1 parent 5022026 commit d9e0c2e
Show file tree
Hide file tree
Showing 22 changed files with 800 additions and 249 deletions.
12 changes: 7 additions & 5 deletions src/Mod/TechDraw/App/AppTechDraw.cpp
Expand Up @@ -52,6 +52,8 @@
#include "PropertyCosmeticEdgeList.h"
#include "PropertyCosmeticVertexList.h"

#include "CosmeticExtension.h"

namespace TechDraw {
extern PyObject* initModule();
}
Expand All @@ -71,11 +73,6 @@ PyMOD_INIT_FUNC(TechDraw)
PyObject* mod = TechDraw::initModule();
Base::Console().Log("Loading TechDraw module... done\n");


// NOTE: To finish the initialization of our own type objects we must
// call PyType_Ready, otherwise we run into a segmentation fault, later on.
// This function is responsible for adding inherited slots from a type's base class.

TechDraw::DrawPage ::init();
TechDraw::DrawView ::init();
TechDraw::DrawViewCollection ::init();
Expand Down Expand Up @@ -118,6 +115,10 @@ PyMOD_INIT_FUNC(TechDraw)
TechDraw::PropertyCosmeticVertexList::init();
TechDraw::CosmeticVertex ::init();

TechDraw::CosmeticExtension ::init();
TechDraw::CosmeticExtensionPython::init();

// are these python init calls required? some modules don't have them
// Python Types
TechDraw::DrawPagePython ::init();
TechDraw::DrawViewPython ::init();
Expand All @@ -130,5 +131,6 @@ PyMOD_INIT_FUNC(TechDraw)
TechDraw::DrawTilePython ::init();
TechDraw::DrawTileWeldPython ::init();
TechDraw::DrawWeldSymbolPython::init();

PyMOD_Return(mod);
}
5 changes: 5 additions & 0 deletions src/Mod/TechDraw/App/CMakeLists.txt
Expand Up @@ -67,6 +67,7 @@ generate_from_xml(CosmeticVertexPy)
generate_from_xml(DrawTilePy)
generate_from_xml(DrawTileWeldPy)
generate_from_xml(DrawWeldSymbolPy)
generate_from_xml(CosmeticExtensionPy)

SET(Draw_SRCS
DrawPage.cpp
Expand Down Expand Up @@ -167,6 +168,8 @@ SET(Geometry_SRCS
PropertyCosmeticEdgeList.h
PropertyCosmeticVertexList.cpp
PropertyCosmeticVertexList.h
CosmeticExtension.cpp
CosmeticExtension.h
)

SET(Python_SRCS
Expand Down Expand Up @@ -220,6 +223,8 @@ SET(Python_SRCS
DrawTileWeldPyImp.cpp
DrawWeldSymbolPy.xml
DrawWeldSymbolPyImp.cpp
CosmeticExtensionPy.xml
CosmeticExtensionPyImp.cpp
)

SOURCE_GROUP("Mod" FILES ${TechDraw_SRCS})
Expand Down
4 changes: 3 additions & 1 deletion src/Mod/TechDraw/App/Cosmetic.cpp
Expand Up @@ -323,7 +323,9 @@ CosmeticVertex* CosmeticVertex::clone(void) const

PyObject* CosmeticVertex::getPyObject(void)
{
return new CosmeticVertexPy(new CosmeticVertex(this->copy()));
// return new CosmeticVertexPy(new CosmeticVertex(this->copy())); //shouldn't this be clone?
PyObject* result = new CosmeticVertexPy(this->clone()); //shouldn't this be clone?
return result;
}

void CosmeticVertex::dump(const char* title)
Expand Down
1 change: 1 addition & 0 deletions src/Mod/TechDraw/App/Cosmetic.h
Expand Up @@ -91,6 +91,7 @@ class TechDrawExport CosmeticVertex: public Base::Persistence, public TechDraw::

Base::Vector3d permaPoint; //permanent, unscaled value
int linkGeom; //connection to corresponding "geom" Vertex (fragile - index based!)
//better to do reverse search for CosmeticTag in vertex geometry
App::Color color;
double size;
int style;
Expand Down
192 changes: 192 additions & 0 deletions src/Mod/TechDraw/App/CosmeticExtension.cpp
@@ -0,0 +1,192 @@
/***************************************************************************
* Copyright (c) 2019 WandererFan <wandererfan@gmail.com> *
* *
* 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_
#endif // #ifndef _PreComp_

#include "CosmeticExtension.h"

#include <Base/Console.h>

#include <App/Application.h>
#include <App/FeaturePythonPyImp.h>

#include "CosmeticExtensionPy.h"

#include "Cosmetic.h"
#include "DrawUtil.h"
#include "DrawViewPart.h"

using namespace TechDraw;
using namespace std;


EXTENSION_PROPERTY_SOURCE(TechDraw::CosmeticExtension, App::DocumentObjectExtension)

CosmeticExtension::CosmeticExtension()
{
static const char *cgroup = "Cosmetics";

EXTENSION_ADD_PROPERTY_TYPE(CosmeticVertexes ,(0),cgroup,App::Prop_Output,"CosmeticVertex Save/Restore");

initExtensionType(CosmeticExtension::getExtensionClassTypeId());
}

CosmeticExtension::~CosmeticExtension()
{
}

//void CosmeticExtension::extHandleChangedPropertyName(Base::XMLReader &reader,
// const char* TypeName,
// const char* PropName)
//{
//}

//==============================================================================
//CosmeticVertex x,y are stored as unscaled, but mirrored (inverted Y) values.
//if you are creating a CV based on calculations of scaled geometry, you need to
//unscale x,y before creation.
//if you are creating a CV based on calculations of mirrored geometry, you need to
//mirror again before creation.

//returns unique CV id
//only adds cv to cvlist property. does not add to display geometry until dvp executes.
std::string CosmeticExtension::addCosmeticVertex(Base::Vector3d pos)
{
// Base::Console().Message("CEx::addCosmeticVertex(%s)\n",
// DrawUtil::formatVector(pos).c_str());
std::vector<CosmeticVertex*> verts = CosmeticVertexes.getValues();
Base::Vector3d tempPos = DrawUtil::invertY(pos);
TechDraw::CosmeticVertex* cv = new TechDraw::CosmeticVertex(tempPos);
verts.push_back(cv);
CosmeticVertexes.setValues(verts);
std::string result = cv->getTagAsString();
return result;
}

//get CV by unique id
TechDraw::CosmeticVertex* CosmeticExtension::getCosmeticVertex(std::string tagString) const
{
// Base::Console().Message("CEx::getCosmeticVertex(%s)\n", tagString.c_str());
CosmeticVertex* result = nullptr;
const std::vector<TechDraw::CosmeticVertex*> verts = CosmeticVertexes.getValues();
for (auto& cv: verts) {
std::string cvTag = cv->getTagAsString();
if (cvTag == tagString) {
result = cv;
break;
}
}
return result;
}

// find the cosmetic vertex corresponding to selection name (Vertex5)
// used when selecting
TechDraw::CosmeticVertex* CosmeticExtension::getCosmeticVertexBySelection(std::string name) const
{
// Base::Console().Message("CEx::getCVBySelection(%s)\n",name.c_str());
CosmeticVertex* result = nullptr;
App::DocumentObject* extObj = const_cast<App::DocumentObject*> (getExtendedObject());
TechDraw::DrawViewPart* dvp = dynamic_cast<TechDraw::DrawViewPart*>(extObj);
if (dvp == nullptr) {
return result;
}
int idx = DrawUtil::getIndexFromName(name);
TechDraw::Vertex* v = dvp->getProjVertexByIndex(idx);
if (v == nullptr) {
return result;
}
if (!v->cosmeticTag.empty()) {
result = getCosmeticVertex(v->cosmeticTag);
}
return result;
}

//overload for index only
TechDraw::CosmeticVertex* CosmeticExtension::getCosmeticVertexBySelection(int i) const
{
// Base::Console().Message("CEx::getCVBySelection(%d)\n", i);
std::stringstream ss;
ss << "Vertex" << i;
std::string vName = ss.str();
return getCosmeticVertexBySelection(vName);
}

void CosmeticExtension::removeCosmeticVertex(std::string delTag)
{
// Base::Console().Message("DVP::removeCV(%s)\n", delTag.c_str());
std::vector<CosmeticVertex*> cVerts = CosmeticVertexes.getValues();
std::vector<CosmeticVertex*> newVerts;
for (auto& cv: cVerts) {
if (cv->getTagAsString() != delTag) {
newVerts.push_back(cv);
}
}
CosmeticVertexes.setValues(newVerts);
}

void CosmeticExtension::removeCosmeticVertex(std::vector<std::string> delTags)
{
for (auto& t: delTags) {
removeCosmeticVertex(t);
}
}

bool CosmeticExtension::replaceCosmeticVertex(CosmeticVertex* newCV)
{
// Base::Console().Message("DVP::replaceCV(%s)\n", newCV->getTagAsString().c_str());
bool result = false;
std::vector<CosmeticVertex*> cVerts = CosmeticVertexes.getValues();
std::vector<CosmeticVertex*> newVerts;
std::string tag = newCV->getTagAsString();
for (auto& cv: cVerts) {
if (cv->getTagAsString() == tag) {
newVerts.push_back(newCV);
result = true;
} else {
newVerts.push_back(cv);
}
}
CosmeticVertexes.setValues(newVerts);
return result;
}

//================================================================================
PyObject* CosmeticExtension::getExtensionPyObject(void) {
if (ExtensionPythonObject.is(Py::_None())){
// ref counter is set to 1
ExtensionPythonObject = Py::Object(new CosmeticExtensionPy(this),true);
}
return Py::new_reference_to(ExtensionPythonObject);
}

namespace App {
/// @cond DOXERR
EXTENSION_PROPERTY_SOURCE_TEMPLATE(TechDraw::CosmeticExtensionPython, TechDraw::CosmeticExtension)
/// @endcond

// explicit template instantiation
template class TechDrawExport ExtensionPythonT<TechDraw::CosmeticExtension>;
}


73 changes: 73 additions & 0 deletions src/Mod/TechDraw/App/CosmeticExtension.h
@@ -0,0 +1,73 @@
/***************************************************************************
* Copyright (c) 2019 WandererFan <wandererfan@gmail.com> *
* *
* 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 TECHDRAW_COSMETICEXTENSION_H
#define TECHDRAW_COSMETICEXTENSION_H

#include <App/DocumentObject.h>
#include <App/DocumentObjectExtension.h>
#include <App/PropertyStandard.h>
#include <App/Material.h>

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

#include "PropertyCosmeticVertexList.h"


namespace TechDraw {
class DrawViewPart;
class GeometryObject;

class TechDrawExport CosmeticExtension : public App::DocumentObjectExtension {
EXTENSION_PROPERTY_HEADER(TechDraw::CosmeticObject);

public:
CosmeticExtension();
virtual ~CosmeticExtension();

TechDraw::PropertyCosmeticVertexList CosmeticVertexes;

virtual std::string addCosmeticVertex(Base::Vector3d pos);
virtual CosmeticVertex* getCosmeticVertexBySelection(std::string name) const;
virtual CosmeticVertex* getCosmeticVertexBySelection(int i) const;
virtual CosmeticVertex* getCosmeticVertex(std::string id) const;
virtual bool replaceCosmeticVertex(CosmeticVertex* newVertex);
virtual void removeCosmeticVertex(std::string tag);
virtual void removeCosmeticVertex(std::vector<std::string> delTags);

PyObject* getExtensionPyObject(void);

protected:
/* virtual void extHandleChangedPropertyName(Base::XMLReader &reader, */
/* const char* TypeName, */
/* const char* PropName);*/

private:

};

typedef App::ExtensionPythonT<CosmeticExtension> CosmeticExtensionPython;

} //end namespace TechDraw

#endif //TECHDRAW_COSMETICEXTENSION_H
17 changes: 17 additions & 0 deletions src/Mod/TechDraw/App/CosmeticExtensionPy.xml
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<GenerateModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="generateMetaModel_Module.xsd">
<PythonExport
Father="DocumentObjectExtensionPy"
Name="CosmeticExtensionPy"
Twin="CosmeticExtension"
TwinPointer="CosmeticExtension"
Include="Mod/TechDraw/App/CosmeticExtension.h"
Namespace="TechDraw"
FatherInclude="App/DocumentObjectExtensionPy.h"
FatherNamespace="App">
<Documentation>
<Author Licence="LGPL" Name="WandererFan" EMail="wandererfan@gmail.com" />
<UserDocu>This object represents cosmetic features for a DrawViewPart.</UserDocu>
</Documentation>
</PythonExport>
</GenerateModel>

0 comments on commit d9e0c2e

Please sign in to comment.