From 3aa6fa19c679d63bf9da9fd668252f72c7e9ecd7 Mon Sep 17 00:00:00 2001 From: wandererfan Date: Mon, 15 Jul 2019 19:55:55 -0400 Subject: [PATCH] [TD]Py functions for CenterLines and formatting --- src/Mod/TechDraw/App/CenterLinePy.xml | 11 + src/Mod/TechDraw/App/CenterLinePyImp.cpp | 60 +++++ src/Mod/TechDraw/App/Cosmetic.cpp | 115 +++++++++- src/Mod/TechDraw/App/Cosmetic.h | 30 ++- src/Mod/TechDraw/App/CosmeticEdgePy.xml | 11 + src/Mod/TechDraw/App/CosmeticEdgePyImp.cpp | 67 +++++- src/Mod/TechDraw/App/DrawUtil.cpp | 44 ++++ src/Mod/TechDraw/App/DrawUtil.h | 2 + src/Mod/TechDraw/App/DrawViewPart.cpp | 12 +- src/Mod/TechDraw/App/DrawViewPartPy.xml | 38 +++- src/Mod/TechDraw/App/DrawViewPartPyImp.cpp | 252 +++++++++++++++++---- src/Mod/TechDraw/Gui/TaskCenterLine.cpp | 40 ++-- 12 files changed, 604 insertions(+), 78 deletions(-) diff --git a/src/Mod/TechDraw/App/CenterLinePy.xml b/src/Mod/TechDraw/App/CenterLinePy.xml index 8d9c847898..8361d17f91 100644 --- a/src/Mod/TechDraw/App/CenterLinePy.xml +++ b/src/Mod/TechDraw/App/CenterLinePy.xml @@ -25,5 +25,16 @@ Create a copy of this centerline + + + Change the appearance of this CenterLine. cl.setFormat(style, color, weight, visible) + + + + + + returns the appearance attributes of this CenterLine. returns tuple(style, color, weight, visible). + + diff --git a/src/Mod/TechDraw/App/CenterLinePyImp.cpp b/src/Mod/TechDraw/App/CenterLinePyImp.cpp index 6ba67416c5..2558b23092 100644 --- a/src/Mod/TechDraw/App/CenterLinePyImp.cpp +++ b/src/Mod/TechDraw/App/CenterLinePyImp.cpp @@ -27,6 +27,7 @@ //# include #endif +#include "DrawUtil.h" #include "Cosmetic.h" #include "CenterLinePy.h" #include "CenterLinePy.cpp" @@ -107,6 +108,65 @@ PyObject* CenterLinePy::copy(PyObject *args) return cpy; } +PyObject* CenterLinePy::setFormat(PyObject* args) +{ +// Base::Console().Message("CLPI::setFormat()\n"); + PyObject* pTuple; + int style = 1; + double weight = 0.50; + double red = 0.0, green = 0.0, blue = 0.0, alpha = 0.0; + App::Color c(red, blue, green, alpha); + bool visible = 1; + if (!PyArg_ParseTuple(args, "O", &pTuple)) { + return NULL; + } + + TechDraw::CenterLine* cl = this->getCenterLinePtr(); + if (PyTuple_Check(pTuple)) { + int tSize = (int) PyTuple_Size(pTuple); + if (tSize > 3) { + PyObject* pStyle = PyTuple_GetItem(pTuple,0); + style = (int) PyLong_AsLong(pStyle); + PyObject* pWeight = PyTuple_GetItem(pTuple,1); + weight = PyFloat_AsDouble(pWeight); + PyObject* pColor = PyTuple_GetItem(pTuple,2); + c = DrawUtil::pyTupleToColor(pColor); + PyObject* pVisible = PyTuple_GetItem(pTuple,3); + visible = (bool) PyLong_AsLong(pVisible); + + cl->m_format.m_style = style; + cl->m_format.m_weight = weight; + cl->m_format.m_color = c; + cl->m_format.m_visible = visible; + } + } else { + Base::Console().Error("CLPI::setFormat - not a tuple!\n"); + } + + return Py_None; +} + +PyObject* CenterLinePy::getFormat(PyObject *args) +{ + (void) args; +// Base::Console().Message("CLPI::getFormat()\n"); + TechDraw::CenterLine* cl = this->getCenterLinePtr(); + + PyObject* pStyle = PyLong_FromLong((long) cl->m_format.m_style); + PyObject* pWeight = PyFloat_FromDouble(cl->m_format.m_weight); + PyObject* pColor = DrawUtil::colorToPyTuple(cl->m_format.m_color); + PyObject* pVisible = PyBool_FromLong((long) cl->m_format.m_visible); + + PyObject* result = PyTuple_New(4); + + PyTuple_SET_ITEM(result, 0, pStyle); + PyTuple_SET_ITEM(result, 1, pWeight); + PyTuple_SET_ITEM(result, 2, pColor); + PyTuple_SET_ITEM(result, 3, pVisible); + + return result; +} + PyObject *CenterLinePy::getCustomAttributes(const char* /*attr*/) const { return 0; diff --git a/src/Mod/TechDraw/App/Cosmetic.cpp b/src/Mod/TechDraw/App/Cosmetic.cpp index f854474ebb..60357b7e71 100644 --- a/src/Mod/TechDraw/App/Cosmetic.cpp +++ b/src/Mod/TechDraw/App/Cosmetic.cpp @@ -430,12 +430,12 @@ CenterLine::CenterLine(void) { m_start = Base::Vector3d(0.0, 0.0, 0.0); m_end = Base::Vector3d(0.0, 0.0, 0.0); - m_mode = 0; + m_mode = CLMODE::VERTICAL; m_hShift = 0.0; m_vShift = 0.0; m_rotate = 0.0; m_extendBy = 0.0; - m_type = 0; + m_type = CLTYPE::FACE; m_flip2Line = false; } @@ -461,12 +461,12 @@ CenterLine::CenterLine(Base::Vector3d p1, Base::Vector3d p2) { m_start = p1; m_end = p2; - m_mode = 0; + m_mode = CLMODE::VERTICAL; m_hShift = 0.0; m_vShift = 0.0; m_rotate = 0.0; m_extendBy = 0.0; - m_type = 0; + m_type = CLTYPE::FACE; m_flip2Line = false; } @@ -484,7 +484,7 @@ CenterLine::CenterLine(Base::Vector3d p1, Base::Vector3d p2, m_vShift = v; m_rotate = r; m_extendBy = x; - m_type = 0; + m_type = CLTYPE::FACE; m_flip2Line = false; } @@ -492,6 +492,55 @@ CenterLine::~CenterLine() { } +CenterLine* CenterLine::CenterLineBuilder(DrawViewPart* partFeat, std::vector subNames, int mode) +{ +// Base::Console().Message("CL::CLBuilder()\n"); + std::pair ends; + std::vector faces; + std::vector edges; + std::vector verts; + + std::string geomType = TechDraw::DrawUtil::getGeomTypeFromName(subNames.front()); + int type = CLTYPE::FACE; + if (geomType == "Face") { + type = CLTYPE::FACE; + ends = TechDraw::CenterLine::calcEndPoints(partFeat, + subNames, + mode, + 0.0, + 0.0, 0.0, 0.0); + faces = subNames; + } else if (geomType == "Edge") { + type = CLTYPE::EDGE; + ends = TechDraw::CenterLine::calcEndPoints2Lines(partFeat, + subNames, + mode, + 0.0, + 0.0, 0.0, 0.0, false); + edges = subNames; + } else if (geomType == "Vertex") { + type = CLTYPE::VERTEX; + ends = TechDraw::CenterLine::calcEndPoints2Points(partFeat, + subNames, + mode, + 0.0, + 0.0, 0.0, 0.0, false); + verts = subNames; + } + TechDraw::CenterLine* cl = new TechDraw::CenterLine(ends.first, ends.second); + if (cl != nullptr) { +// cl->m_start = ends.first; +// cl->m_end = ends.second; + cl->m_type = type; + cl->m_mode = mode; + cl->m_faces = faces; + cl->m_edges = edges; + cl->m_verts = verts; +// cl->m_flip2Line = false; + } + return cl; +} + TechDraw::BaseGeom* CenterLine::scaledGeometry(TechDraw::DrawViewPart* partFeat) { // Base::Console().Message("CL::scaledGeometry() - m_type: %d\n", m_type); @@ -504,18 +553,18 @@ TechDraw::BaseGeom* CenterLine::scaledGeometry(TechDraw::DrawViewPart* partFeat) } std::pair ends; - if (m_type == 0) { + if (m_type == CLTYPE::FACE) { ends = calcEndPoints(partFeat, m_faces, m_mode, m_extendBy, m_hShift,m_vShift, m_rotate); - } else if (m_type == 1) { + } else if (m_type == CLTYPE::EDGE) { ends = calcEndPoints2Lines(partFeat, m_edges, m_mode, m_extendBy, m_hShift, m_vShift, m_rotate, m_flip2Line); - } else if (m_type == 2) { + } else if (m_type == CLTYPE::VERTEX) { ends = calcEndPoints2Points(partFeat, m_verts, m_mode, @@ -577,7 +626,7 @@ void CenterLine::dump(char* title) //end points for face centerline std::pair CenterLine::calcEndPoints(DrawViewPart* partFeat, std::vector faceNames, - int vert, double ext, + int mode, double ext, double hShift, double vShift, double rotate) { @@ -623,10 +672,10 @@ std::pair CenterLine::calcEndPoints(DrawViewPart double Ymid = Ymin + Yspan; Base::Vector3d p1, p2; - if (vert == 0) { //vertical + if (mode == 0) { //vertical p1 = Base::Vector3d(Xmid, Ymax, 0.0); p2 = Base::Vector3d(Xmid, Ymin, 0.0); - } else if (vert == 1) { //horizontal + } else if (mode == 1) { //horizontal p1 = Base::Vector3d(Xmin, Ymid, 0.0); p2 = Base::Vector3d(Xmax,Ymid, 0.0); } else { //vert == 2 //aligned, but aligned doesn't make sense for face(s) bbox @@ -1054,7 +1103,51 @@ PyObject* CenterLine::getPyObject(void) return new CenterLinePy(new CenterLine(this->copy())); } +void CenterLine::setShifts(double h, double v) +{ + m_hShift = h; + m_vShift = v; +} +double CenterLine::getHShift(void) +{ + return m_hShift; +} + +double CenterLine::getVShift(void) +{ + return m_vShift; +} + +void CenterLine::setRotate(double r) +{ + m_rotate = r; +} + +double CenterLine::getRotate(void) +{ + return m_rotate; +} + +void CenterLine::setExtend(double e) +{ + m_extendBy = e; +} + +double CenterLine::getExtend(void) +{ + return m_extendBy; +} + +void CenterLine::setFlip(bool f) +{ + m_flip2Line = f; +} + +bool CenterLine::getFlip(void) +{ + return m_flip2Line; +} //------------------------------------------------------------------------------ TYPESYSTEM_SOURCE(TechDraw::GeomFormat,Base::Persistence) diff --git a/src/Mod/TechDraw/App/Cosmetic.h b/src/Mod/TechDraw/App/Cosmetic.h index 96047cc3b0..0f36518873 100644 --- a/src/Mod/TechDraw/App/Cosmetic.h +++ b/src/Mod/TechDraw/App/Cosmetic.h @@ -144,6 +144,19 @@ public: double x); virtual ~CenterLine(); + enum CLMODE { + VERTICAL, + HORIZONTAL, + ALIGNED + }; + + enum CLTYPE { + FACE, + EDGE, + VERTEX + }; + + // Persistence implementer --------------------- virtual unsigned int getMemSize(void) const; virtual void Save(Base::Writer &/*writer*/) const; @@ -154,13 +167,15 @@ public: CenterLine* clone(void) const; std::string toString(void) const; -/* bool fromCSV(std::string& lineSpec);*/ -/* CosmeticEdge* toCosmeticEdge(TechDraw::DrawViewPart* partFeat); //??*/ + + static CenterLine* CenterLineBuilder(TechDraw::DrawViewPart* partFeat, + std::vector subs, + int mode = 0); TechDraw::BaseGeom* scaledGeometry(TechDraw::DrawViewPart* partFeat); static std::pair calcEndPoints( TechDraw::DrawViewPart* partFeat, std::vector faceNames, - int vert, double ext, + int mode, double ext, double m_hShift, double m_vShift, double rotate); static std::pair calcEndPoints2Lines( @@ -176,6 +191,15 @@ public: double m_hShift, double m_vShift, double rotate, bool flip); void dump(char* title); + void setShifts(double h, double v); + double getHShift(void); + double getVShift(void); + void setRotate(double r); + double getRotate(void); + void setExtend(double e); + double getExtend(void); + void setFlip(bool f); + bool getFlip(void); Base::Vector3d m_start; Base::Vector3d m_end; diff --git a/src/Mod/TechDraw/App/CosmeticEdgePy.xml b/src/Mod/TechDraw/App/CosmeticEdgePy.xml index 19d6d38280..dad876be13 100644 --- a/src/Mod/TechDraw/App/CosmeticEdgePy.xml +++ b/src/Mod/TechDraw/App/CosmeticEdgePy.xml @@ -25,5 +25,16 @@ Create a copy of this CosmeticEdge + + + Change the appearance of this CometicEdge. edge.setFormat(style, color, weight, visible) + + + + + + returns the appearance attributes of this CometicEdge. returns tuple(style, color, weight, visible). + + diff --git a/src/Mod/TechDraw/App/CosmeticEdgePyImp.cpp b/src/Mod/TechDraw/App/CosmeticEdgePyImp.cpp index 8fa10178c4..c5e61c12c7 100644 --- a/src/Mod/TechDraw/App/CosmeticEdgePyImp.cpp +++ b/src/Mod/TechDraw/App/CosmeticEdgePyImp.cpp @@ -26,6 +26,9 @@ #endif +#include + +#include "DrawUtil.h" #include "Cosmetic.h" #include "CosmeticEdgePy.h" #include "CosmeticEdgePy.cpp" @@ -52,13 +55,13 @@ int CosmeticEdgePy::PyInit(PyObject* /*args*/, PyObject* /*kwd*/) return 0; } +//From Part::GeometryPy.cpp PyObject* CosmeticEdgePy::clone(PyObject *args) { if (!PyArg_ParseTuple(args, "")) return NULL; TechDraw::CosmeticEdge* geom = this->getCosmeticEdgePtr(); - geom->dump("CEPYI::clone"); PyTypeObject* type = this->GetType(); PyObject* cpy = 0; // let the type object decide @@ -86,7 +89,6 @@ PyObject* CosmeticEdgePy::copy(PyObject *args) return NULL; TechDraw::CosmeticEdge* geom = this->getCosmeticEdgePtr(); - geom->dump("CEPYI::copy"); PyTypeObject* type = this->GetType(); PyObject* cpy = 0; // let the type object decide @@ -108,6 +110,66 @@ PyObject* CosmeticEdgePy::copy(PyObject *args) return cpy; } +PyObject* CosmeticEdgePy::setFormat(PyObject* args) +{ +// Base::Console().Message("CEP::setFormat()\n"); + PyObject* pTuple; + int style = 1; + double weight = 0.50; + double red = 0.0, green = 0.0, blue = 0.0, alpha = 0.0; + App::Color c(red, blue, green, alpha); + bool visible = 1; + if (!PyArg_ParseTuple(args, "O", &pTuple)) { + return NULL; + } + + TechDraw::CosmeticEdge* ce = this->getCosmeticEdgePtr(); + if (PyTuple_Check(pTuple)) { + int tSize = (int) PyTuple_Size(pTuple); + if (tSize > 3) { + PyObject* pStyle = PyTuple_GetItem(pTuple,0); + style = (int) PyLong_AsLong(pStyle); + PyObject* pWeight = PyTuple_GetItem(pTuple,1); + weight = PyFloat_AsDouble(pWeight); + PyObject* pColor = PyTuple_GetItem(pTuple,2); + c = DrawUtil::pyTupleToColor(pColor); + PyObject* pVisible = PyTuple_GetItem(pTuple,3); + visible = (bool) PyLong_AsLong(pVisible); + + ce->m_format.m_style = style; + ce->m_format.m_weight = weight; + ce->m_format.m_color = c; + ce->m_format.m_visible = visible; + } + } else { + Base::Console().Error("CEPI::setFormat - not a tuple!\n"); + } + + return Py_None; +} + +PyObject* CosmeticEdgePy::getFormat(PyObject *args) +{ + (void) args; +// Base::Console().Message("CEP::getFormat()\n"); + TechDraw::CosmeticEdge* ce = this->getCosmeticEdgePtr(); + + PyObject* pStyle = PyLong_FromLong((long) ce->m_format.m_style); + PyObject* pWeight = PyFloat_FromDouble(ce->m_format.m_weight); + PyObject* pColor = DrawUtil::colorToPyTuple(ce->m_format.m_color); + PyObject* pVisible = PyBool_FromLong((long) ce->m_format.m_visible); + + PyObject* result = PyTuple_New(4); + + PyTuple_SET_ITEM(result, 0, pStyle); + PyTuple_SET_ITEM(result, 1, pWeight); + PyTuple_SET_ITEM(result, 2, pColor); + PyTuple_SET_ITEM(result, 3, pVisible); + + return result; +} + + PyObject *CosmeticEdgePy::getCustomAttributes(const char* /*attr*/) const { return 0; @@ -117,3 +179,4 @@ int CosmeticEdgePy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/) { return 0; } + diff --git a/src/Mod/TechDraw/App/DrawUtil.cpp b/src/Mod/TechDraw/App/DrawUtil.cpp index a477294651..c9c666d71d 100644 --- a/src/Mod/TechDraw/App/DrawUtil.cpp +++ b/src/Mod/TechDraw/App/DrawUtil.cpp @@ -62,6 +62,7 @@ #endif #include +#include #include #include #include @@ -542,6 +543,7 @@ Base::Vector3d DrawUtil::invertY(Base::Vector3d v) return result; } +//obs? was used in CSV prototype of Cosmetics std::vector DrawUtil::split(std::string csvLine) { // Base::Console().Message("DU::split - csvLine: %s\n",csvLine.c_str()); @@ -556,6 +558,7 @@ std::vector DrawUtil::split(std::string csvLine) return result; } +//obs? was used in CSV prototype of Cosmetics std::vector DrawUtil::tokenize(std::string csvLine, std::string delimiter) { // Base::Console().Message("DU::tokenize - csvLine: %s delimit: %s\n",csvLine.c_str(), delimiter.c_str()); @@ -572,6 +575,47 @@ std::vector DrawUtil::tokenize(std::string csvLine, std::string del return tokens; } +App::Color DrawUtil::pyTupleToColor(PyObject* pColor) +{ +// Base::Console().Message("DU::pyTupleToColor()\n"); + double red = 0.0, green = 0.0, blue = 0.0, alpha = 0.0; + App::Color c(red, blue, green, alpha); + if (PyTuple_Check(pColor)) { + int tSize = (int) PyTuple_Size(pColor); + if (tSize > 2) { + PyObject* pRed = PyTuple_GetItem(pColor,0); + red = PyFloat_AsDouble(pRed); + PyObject* pGreen = PyTuple_GetItem(pColor,1); + green = PyFloat_AsDouble(pGreen); + PyObject* pBlue = PyTuple_GetItem(pColor,2); + blue = PyFloat_AsDouble(pBlue); + } + if (tSize > 3) { + PyObject* pAlpha = PyTuple_GetItem(pColor,3); + alpha = PyFloat_AsDouble(pAlpha); + } + c = App::Color(red, blue, green, alpha); + } + return c; +} + +PyObject* DrawUtil::colorToPyTuple(App::Color color) +{ +// Base::Console().Message("DU::pyTupleToColor()\n"); + PyObject* pTuple = PyTuple_New(4); + PyObject* pRed = PyFloat_FromDouble(color.r); + PyObject* pGreen = PyFloat_FromDouble(color.g); + PyObject* pBlue = PyFloat_FromDouble(color.b); + PyObject* pAlpha = PyFloat_FromDouble(color.a); + + PyTuple_SET_ITEM(pTuple, 0,pRed); + PyTuple_SET_ITEM(pTuple, 1,pGreen); + PyTuple_SET_ITEM(pTuple, 2,pBlue); + PyTuple_SET_ITEM(pTuple, 3,pAlpha); + + return pTuple; +} + //============================ // various debugging routines. diff --git a/src/Mod/TechDraw/App/DrawUtil.h b/src/Mod/TechDraw/App/DrawUtil.h index 6d3ced8470..7ed6531b49 100644 --- a/src/Mod/TechDraw/App/DrawUtil.h +++ b/src/Mod/TechDraw/App/DrawUtil.h @@ -100,6 +100,8 @@ class TechDrawExport DrawUtil { static Base::Vector3d invertY(Base::Vector3d v); static std::vector split(std::string csvLine); static std::vector tokenize(std::string csvLine, std::string delimiter = ",$$$,"); + static App::Color pyTupleToColor(PyObject* pColor); + static PyObject* colorToPyTuple(App::Color color); //debugging routines static void dumpVertexes(const char* text, const TopoDS_Shape& s); diff --git a/src/Mod/TechDraw/App/DrawViewPart.cpp b/src/Mod/TechDraw/App/DrawViewPart.cpp index 8f922e45a5..9380952b33 100644 --- a/src/Mod/TechDraw/App/DrawViewPart.cpp +++ b/src/Mod/TechDraw/App/DrawViewPart.cpp @@ -1098,6 +1098,7 @@ int DrawViewPart::addCosmeticEdge(Base::Vector3d p1, Base::Vector3d p2) int newIdx = (int) (edges.size()); edges.push_back(ce); CosmeticEdges.setValues(edges); + recomputeFeature(); //execute needs to run to replace Geoms return newIdx; } @@ -1109,6 +1110,7 @@ int DrawViewPart::addCosmeticEdge(TopoDS_Edge e) int newIdx = (int) (edges.size()); edges.push_back(ce); CosmeticEdges.setValues(edges); + recomputeFeature(); //execute needs to run to replace Geoms return newIdx; } @@ -1118,6 +1120,7 @@ int DrawViewPart::addCosmeticEdge(CosmeticEdge* ce) int newIdx = (int) (edges.size()); edges.push_back(ce); CosmeticEdges.setValues(edges); + recomputeFeature(); //execute needs to run to replace Geoms return newIdx; } @@ -1152,6 +1155,7 @@ void DrawViewPart::removeCosmeticEdge(int idx) void DrawViewPart::replaceCosmeticEdge(int idx, TechDraw::CosmeticEdge* ce) { +// Base::Console().Message("DVP::replaceCosmeticEdge(%d, ce)\n", idx); std::vector edges = CosmeticEdges.getValues(); if (idx < (int) edges.size()) { edges.at(idx) = ce; @@ -1163,9 +1167,11 @@ void DrawViewPart::replaceCosmeticEdge(int idx, TechDraw::CosmeticEdge* ce) void DrawViewPart::replaceCosmeticEdgeByGeom(int geomIndex, TechDraw::CosmeticEdge* ce) { const std::vector &geoms = getEdgeGeometry(); - //TODO: check that geom has m_source == cosmetic - int sourceIndex = geoms.at(geomIndex)->sourceIndex(); - replaceCosmeticEdge(sourceIndex, ce); + int source = geoms.at(geomIndex)->source(); + if (source == 1) { //CosmeticEdge + int sourceIndex = geoms.at(geomIndex)->sourceIndex(); + replaceCosmeticEdge(sourceIndex, ce); + } } TechDraw::CosmeticEdge* DrawViewPart::getCosmeticEdgeByIndex(int idx) const diff --git a/src/Mod/TechDraw/App/DrawViewPartPy.xml b/src/Mod/TechDraw/App/DrawViewPartPy.xml index 0714198519..2eb898e9d3 100644 --- a/src/Mod/TechDraw/App/DrawViewPartPy.xml +++ b/src/Mod/TechDraw/App/DrawViewPartPy.xml @@ -30,7 +30,7 @@ - clearCosmeticVertices() - remove all CosmeticVertices from the View. Returns nothing. + clearCosmeticVertices() - remove all CosmeticVertices from the View. Returns None. @@ -53,24 +53,54 @@ getCosmeticEdgeByIndex(idx) - returns CosmeticEdge[idx]. + + + getCosmeticEdgeByGeom(idx) - returns CosmeticEdge using geometry index. + + + + + replaceCosmeticEdge(idx, ce) - replaces CosmeticEdge[idx] with ce. + + removeCosmeticEdge(idx) - remove CosmeticEdge[idx] from View. Returns None. + + + makeCenterLine(subNames, mode) - draw a center line on this viewPart. SubNames is a list of n Faces, 2 Edges or 2 Vertices (ex [Face1,Face2,Face3]. Returns index of added CenterLine. + + - clearCosmeticEdges() - remove all CosmeticLines from the View. Returns nothing. + clearCosmeticEdges() - remove all CosmeticLines from the View. Returns None. - clearCenterLines() - remove all CenterLines from the View. Returns nothing. + clearCenterLines() - remove all CenterLines from the View. Returns None. - clearGeomFormats() - remove all GeomFormats from the View. Returns nothing. + clearGeomFormats() - remove all GeomFormats from the View. Returns None. + + + + + adjustCenterLine(index, hShift, vShift, rotate, extend [,flip]). Returns None. + + + + + formatCenterLine(index, style, weight, color, visible). Returns None. + + + + + formatGeometricEdge(index, style, weight, color, visible). Returns None. diff --git a/src/Mod/TechDraw/App/DrawViewPartPyImp.cpp b/src/Mod/TechDraw/App/DrawViewPartPyImp.cpp index d487e321ee..1d543c4de3 100644 --- a/src/Mod/TechDraw/App/DrawViewPartPyImp.cpp +++ b/src/Mod/TechDraw/App/DrawViewPartPyImp.cpp @@ -49,12 +49,15 @@ #include "GeometryObject.h" // inclusion of the generated files (generated out of DrawViewPartPy.xml) +#include +#include +#include #include #include using namespace TechDraw; -App::Color pyTupleToColor(PyObject* pColor); +//TODO: errors to PyErrors // returns a string which represents the object e.g. when printed in python std::string DrawViewPartPy::representation(void) const @@ -141,11 +144,12 @@ PyObject* DrawViewPartPy::makeCosmeticLine(PyObject *args) if (pColor == nullptr) { ce->m_format.m_color = defCol; } else { - ce->m_format.m_color = pyTupleToColor(pColor); + ce->m_format.m_color = DrawUtil::pyTupleToColor(pColor); } } else { - //TODO: throw something - idx = -1; + std::string msg = "DVPPI:makeCosmeticLine - line creation failed"; + Base::Console().Message("%s\n",msg.c_str()); + throw Py::RuntimeError(msg); } return PyLong_FromLong(idx); } @@ -188,11 +192,12 @@ PyObject* DrawViewPartPy::makeCosmeticCircle(PyObject *args) if (pColor == nullptr) { ce->m_format.m_color = defCol; } else { - ce->m_format.m_color = pyTupleToColor(pColor); + ce->m_format.m_color = DrawUtil::pyTupleToColor(pColor); } } else { - //TODO: throw something - idx = -1; + std::string msg = "DVPPI:makeCosmeticCircle - circle creation failed"; + Base::Console().Message("%s\n",msg.c_str()); + throw Py::RuntimeError(msg); } return PyLong_FromLong(idx); } @@ -238,11 +243,12 @@ PyObject* DrawViewPartPy::makeCosmeticCircleArc(PyObject *args) if (pColor == nullptr) { ce->m_format.m_color = defCol; } else { - ce->m_format.m_color = pyTupleToColor(pColor); + ce->m_format.m_color = DrawUtil::pyTupleToColor(pColor); } } else { - //TODO: throw something - idx = -1; + std::string msg = "DVPPI:makeCosmeticCircleArc - arc creation failed"; + Base::Console().Message("%s\n",msg.c_str()); + throw Py::RuntimeError(msg); } return PyLong_FromLong(idx); } @@ -250,7 +256,7 @@ PyObject* DrawViewPartPy::makeCosmeticCircleArc(PyObject *args) PyObject* DrawViewPartPy::getCosmeticVertexByIndex(PyObject *args) { PyObject* result = nullptr; - int idx = 0; + int idx = -1; if (!PyArg_ParseTuple(args, "i", &idx)) { throw Py::TypeError("expected (index)"); } @@ -258,6 +264,8 @@ PyObject* DrawViewPartPy::getCosmeticVertexByIndex(PyObject *args) TechDraw::CosmeticVertex* cv = dvp->getCosmeticVertexByIndex(idx); if (cv != nullptr) { result = new CosmeticVertexPy(new CosmeticVertex(cv)); + } else { + result = Py_None; } return result; } @@ -277,20 +285,65 @@ PyObject* DrawViewPartPy::removeCosmeticVertex(PyObject *args) PyObject* DrawViewPartPy::getCosmeticEdgeByIndex(PyObject *args) { int idx = 0; - PyObject* result = nullptr; + PyObject* result = Py_None; if (!PyArg_ParseTuple(args, "i", &idx)) { throw Py::TypeError("expected (index)"); } DrawViewPart* dvp = getDrawViewPartPtr(); TechDraw::CosmeticEdge* ce = dvp->getCosmeticEdgeByIndex(idx); if (ce != nullptr) { - Base::Console().Message("DVPPI::getCosEdgebyIdx - CosmeticEdgePy not implemented yet\n"); - //make a py object? + result = new CosmeticEdgePy(new CosmeticEdge(ce)); + } else { + Base::Console().Error("DVPPI::getCosEdgebyIdx - edge %d not found\n", idx); } return result; } +PyObject* DrawViewPartPy::getCosmeticEdgeByGeom(PyObject *args) +{ +// Base::Console().Message("DVPPI::getCosmeticEdgeByGeom()\n"); + int idx = 0; + PyObject* result = Py_None; + if (!PyArg_ParseTuple(args, "i", &idx)) { + throw Py::TypeError("expected (index)"); + } + DrawViewPart* dvp = getDrawViewPartPtr(); + + TechDraw::BaseGeom* bg = dvp->getGeomByIndex(idx); + if (bg == nullptr) { + Base::Console().Error("DVPPI::getCEbyGeom - geom: %d not found\n",idx); + return result; + } + int source = bg->source(); + int sourceIndex = bg->sourceIndex(); + if (source == 1) { //cosmetic edge + TechDraw::CosmeticEdge* ce = dvp->getCosmeticEdgeByIndex(sourceIndex); + if (ce != nullptr) { + result = new CosmeticEdgePy(new CosmeticEdge(ce)); + } else { + Base::Console().Error("DVPPI::getCosEdgebyGeom - edge %d not found\n", idx); + } + } + return result; +} + +PyObject* DrawViewPartPy::replaceCosmeticEdge(PyObject *args) +{ +// Base::Console().Message("DVPPI::replaceCosmeticEdge()\n"); + int idx = 0; + PyObject* result = Py_None; + PyObject* pCE; + if (!PyArg_ParseTuple(args, "iO!", &idx, &(TechDraw::CosmeticEdgePy::Type), &pCE)) { + throw Py::TypeError("expected (index, CosmeticEdge)"); + } + TechDraw::CosmeticEdge* ce = static_cast(pCE)->getCosmeticEdgePtr(); + DrawViewPart* dvp = getDrawViewPartPtr(); + dvp->replaceCosmeticEdge(idx, ce); + + return result; +} + PyObject* DrawViewPartPy::removeCosmeticEdge(PyObject *args) { // Base::Console().Message("DVPPI::removeCosEdge()\n"); @@ -304,6 +357,153 @@ PyObject* DrawViewPartPy::removeCosmeticEdge(PyObject *args) return Py_None; } +PyObject* DrawViewPartPy::makeCenterLine(PyObject *args) +{ +// Base::Console().Message("DVPPI::makeCenterLine()\n"); + PyObject* pSubs; + int mode = 0; + std::vector subs; + + if (!PyArg_ParseTuple(args, "Oi",&pSubs, &mode)) { + throw Py::TypeError("expected (subNameList, mode)"); + } + + DrawViewPart* dvp = getDrawViewPartPtr(); + if (PyList_Check(pSubs)) { + int size = PyList_Size(pSubs); + int i = 0; + for ( ; i < size; i++) { + PyObject* po = PyList_GetItem(pSubs, i); +#if PY_MAJOR_VERSION >= 3 + if (PyUnicode_Check(po)) { + std::string s = PyUnicode_AsUTF8(po); //py3 only!!! + subs.push_back(s); + } +#else + if (PyString_Check(po)) { + std::string s = PyString_AsString(po); //py2 only!!! + subs.push_back(s); + } +#endif + } + } + + CenterLine* cl = nullptr; + int idx = -1; + if (!subs.empty()) { + cl = CenterLine::CenterLineBuilder(dvp, + subs, + mode); //vert,horiz,align + if (cl != nullptr) { + idx = dvp->addCenterLine(cl); + } else { + std::string msg = "DVPPI:makeCenterLine - line creation failed"; + Base::Console().Message("%s\n",msg.c_str()); + throw Py::RuntimeError(msg); + } + } + return PyLong_FromLong(idx); +} + +PyObject* DrawViewPartPy::adjustCenterLine(PyObject *args) +{ +// Base::Console().Message("DVPPI::adjustCenterLine()\n"); + int idx = -1; + double hShift = 0.0; + double vShift = 0.0; + double rotate = 0.0; + double extend = 0.0; + bool flip = false; + + if (!PyArg_ParseTuple(args, "idddd|p",&idx, &hShift, &vShift, &rotate, &extend, &flip)) { + throw Py::TypeError("expected (index, hShift, vShift, rotate, extend [,flip])"); + } + + DrawViewPart* dvp = getDrawViewPartPtr(); + CenterLine* cl = dvp->getCenterLineByIndex(idx); + if (cl != nullptr) { + cl->m_hShift = hShift; + cl->m_vShift = vShift; + cl->m_rotate = rotate; + cl->m_extendBy = extend; + cl->m_flip2Line = flip; + } else { + std::string msg = "DVPPI:adjustCenterLine - CenterLine not found"; + Base::Console().Message("%s\n",msg.c_str()); + throw Py::RuntimeError(msg); + } + return Py_None; +} + +PyObject* DrawViewPartPy::formatCenterLine(PyObject *args) +{ +// Base::Console().Message("DVPPI::formatCenterLine()\n"); + int idx = -1; + int style = Qt::SolidLine; + App::Color defColor = LineFormat::getDefEdgeColor(); + double weight = 0.5; + int visible = 1; + PyObject* pColor; + + if (!PyArg_ParseTuple(args, "iidOi",&idx, &style, &weight, &pColor, &visible)) { + throw Py::TypeError("expected (index, style, weight, color, visible)"); + } + + DrawViewPart* dvp = getDrawViewPartPtr(); + CenterLine* cl = dvp->getCenterLineByIndex(idx); + if (cl != nullptr) { + cl->m_format.m_style = style; + cl->m_format.m_weight = weight; + if (pColor == nullptr) { + cl->m_format.m_color = defColor; + } else { + cl->m_format.m_color = DrawUtil::pyTupleToColor(pColor); + } + cl->m_format.m_visible = visible; + } else { + std::string msg = "DVPPI:formatCenterLine - CenterLine not found"; + Base::Console().Message("%s\n",msg.c_str()); + throw Py::RuntimeError(msg); + } + return Py_None; +} + +PyObject* DrawViewPartPy::formatGeometricEdge(PyObject *args) +{ +// Base::Console().Message("DVPPI::formatGeometricEdge()\n"); + int idx = -1; + int style = Qt::SolidLine; + App::Color color = LineFormat::getDefEdgeColor(); + double weight = 0.5; + int visible = 1; + PyObject* pColor; + + if (!PyArg_ParseTuple(args, "iidOi",&idx, &style, &weight, &pColor, &visible)) { + throw Py::TypeError("expected (index, style, weight, color, visible)"); + } + + color = DrawUtil::pyTupleToColor(pColor); + DrawViewPart* dvp = getDrawViewPartPtr(); + TechDraw::GeomFormat* gf = dvp->getGeomFormatByGeom(idx); + if (gf != nullptr) { + gf->m_format.m_style = style; + gf->m_format.m_color = color; + gf->m_format.m_weight = weight; + gf->m_format.m_visible = visible; + } else { + TechDraw::LineFormat fmt(style, + weight, + color, + visible); + TechDraw::GeomFormat* newGF = new TechDraw::GeomFormat(idx, + fmt); +// int idx = + dvp->addGeomFormat(newGF); + } + return Py_None; +} + + PyObject *DrawViewPartPy::getCustomAttributes(const char* /*attr*/) const { return 0; @@ -314,27 +514,3 @@ int DrawViewPartPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/) return 0; } -App::Color pyTupleToColor(PyObject* pColor) -{ -// Base::Console().Message("DVPPI::pyTupleToColor()\n"); - double red = 0.0, green = 0.0, blue = 0.0, alpha = 0.0; - App::Color c(red, blue, green, alpha); - if (PyTuple_Check(pColor)) { - int tSize = (int) PyTuple_Size(pColor); - if (tSize > 2) { - PyObject* pRed = PyTuple_GetItem(pColor,0); - red = PyFloat_AsDouble(pRed); - PyObject* pGreen = PyTuple_GetItem(pColor,1); - green = PyFloat_AsDouble(pGreen); - PyObject* pBlue = PyTuple_GetItem(pColor,2); - blue = PyFloat_AsDouble(pBlue); - } - if (tSize > 3) { - PyObject* pAlpha = PyTuple_GetItem(pColor,3); - alpha = PyFloat_AsDouble(pAlpha); - } - c = App::Color(red, blue, green, alpha); - } - return c; -} - diff --git a/src/Mod/TechDraw/Gui/TaskCenterLine.cpp b/src/Mod/TechDraw/Gui/TaskCenterLine.cpp index 1c8fff23a0..8f2c313f58 100644 --- a/src/Mod/TechDraw/Gui/TaskCenterLine.cpp +++ b/src/Mod/TechDraw/Gui/TaskCenterLine.cpp @@ -228,34 +228,35 @@ void TaskCenterLine::createCenterLine(void) { // Base::Console().Message("TCL::createCenterLine() - m_type: %d\n", m_type); Gui::Command::openCommand("Create CenterLine"); - bool vertical = false; +// bool vertical = false; double hShift = ui->qsbHorizShift->rawValue(); double vShift = ui->qsbVertShift->rawValue(); double rotate = ui->dsbRotate->value(); double extendBy = ui->qsbExtend->rawValue(); std::pair ends; if (ui->rbVertical->isChecked()) { - m_mode = 0; - vertical = true; + m_mode = CenterLine::CLMODE::VERTICAL; +// vertical = true; } else if (ui->rbHorizontal->isChecked()) { - m_mode = 1; + m_mode = CenterLine::CLMODE::HORIZONTAL; } else if (ui->rbAligned->isChecked()) { - m_mode =2; + m_mode = CenterLine::CLMODE::ALIGNED; } - - if (m_type == 0) { + + //TODO: call CLBuilder here. + if (m_type == CenterLine::CLTYPE::FACE) { ends = TechDraw::CenterLine::calcEndPoints(m_partFeat, m_subNames, - vertical, + m_mode, extendBy, hShift, vShift, rotate); - } else if (m_type == 1) { + } else if (m_type == CenterLine::CLTYPE::EDGE) { ends = TechDraw::CenterLine::calcEndPoints2Lines(m_partFeat, m_subNames, m_mode, extendBy, hShift, vShift, rotate, m_flipped); - } else if (m_type == 2) { + } else if (m_type == CenterLine::CLTYPE::VERTEX) { ends = TechDraw::CenterLine::calcEndPoints2Points(m_partFeat, m_subNames, m_mode, @@ -267,6 +268,10 @@ void TaskCenterLine::createCenterLine(void) cl->m_start = ends.first; cl->m_end = ends.second; + //TODO: cl->setShifts(hShift, vShift); + // cl->setExtend(extendBy); + // cl->setRotate(rotate); + // cl->setFlip(m_flipped); App::Color ac; ac.setValue(ui->cpLineColor->color()); cl->m_format.m_color = ac; @@ -274,11 +279,12 @@ void TaskCenterLine::createCenterLine(void) cl->m_format.m_style = ui->cboxStyle->currentIndex() + 1; //Qt Styles start at 0:NoLine cl->m_format.m_visible = true; - if (m_type == 0) { + //TODO: CLBuilder replaces this + if (m_type == CenterLine::CLTYPE::FACE) { cl->m_faces = m_subNames; - } else if (m_type == 1) { + } else if (m_type == CenterLine::CLTYPE::EDGE) { cl->m_edges = m_subNames; - } else if (m_type == 2) { + } else if (m_type == CenterLine::CLTYPE::VERTEX) { cl->m_verts = m_subNames; } @@ -290,7 +296,7 @@ void TaskCenterLine::createCenterLine(void) cl->m_mode = m_mode; cl->m_type = m_type; cl->m_flip2Line = m_flipped; - + //end TODO m_partFeat->addCenterLine(cl); @@ -309,11 +315,11 @@ void TaskCenterLine::updateCenterLine(void) m_cl->m_format.m_visible = true; if (ui->rbVertical->isChecked()) { - m_mode = 0; + m_mode = CenterLine::CLMODE::VERTICAL; } else if (ui->rbHorizontal->isChecked()) { - m_mode = 1; + m_mode = CenterLine::CLMODE::HORIZONTAL; } else if (ui->rbAligned->isChecked()) { - m_mode =2; + m_mode = CenterLine::CLMODE::ALIGNED; } m_cl->m_mode = m_mode; m_cl->m_rotate = ui->dsbRotate->value();