Skip to content

Commit c3d1738

Browse files
gsohlergusohler
andauthored
Python: Sphere, Cylinder, Color, Rotation
- Node cloning for safe tree handling in python - Locking support for python interpreter --------- Co-authored-by: Guenther Sohler <guenther.sohler@photeon.com>
1 parent 5623229 commit c3d1738

File tree

10 files changed

+510
-4
lines changed

10 files changed

+510
-4
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,7 @@ set(CORE_SOURCES
791791
src/core/ImportNode.cc
792792
src/core/LinearExtrudeNode.cc
793793
src/core/LocalScope.cc
794+
src/core/node_clone.cc
794795
src/core/ModuleInstantiation.cc
795796
src/core/NodeDumper.cc
796797
src/core/NodeVisitor.cc

src/core/node.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class AbstractNode : public BaseVisitable, public std::enable_shared_from_this<A
6565
int idx; // Node index (unique per tree)
6666

6767
std::shared_ptr<const AbstractNode> getNodeByID(int idx, std::deque<std::shared_ptr<const AbstractNode>>& path) const;
68+
std::shared_ptr<AbstractNode> clone(void);
6869
};
6970

7071
class AbstractIntersectionNode : public AbstractNode

src/core/node_clone.cc

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* OpenSCAD (www.openscad.org)
3+
* Copyright (C) 2009-2011 Clifford Wolf <clifford@clifford.at> and
4+
* Marius Kintel <marius@kintel.net>
5+
*
6+
* This program is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 2 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* As a special exception, you have permission to link this program
12+
* with the CGAL library and distribute executables, as long as you
13+
* follow the requirements of the GNU GPL in regard to all of the
14+
* software in the executable aside from CGAL.
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU General Public License
22+
* along with this program; if not, write to the Free Software
23+
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24+
*
25+
*/
26+
27+
#include "src/geometry/linalg.h"
28+
#include "src/geometry/GeometryUtils.h"
29+
#include "src/core/primitives.h"
30+
#include "src/core/TransformNode.h"
31+
#include "src/core/RotateExtrudeNode.h"
32+
#include "src/core/LinearExtrudeNode.h"
33+
#include "src/core/CgalAdvNode.h"
34+
#include "src/core/CsgOpNode.h"
35+
#include "src/core/ColorNode.h"
36+
#include "src/core/RoofNode.h"
37+
#include "src/core/RenderNode.h"
38+
#include "src/core/SurfaceNode.h"
39+
#include "src/core/TextNode.h"
40+
#include "src/core/OffsetNode.h"
41+
#include "src/core/ProjectionNode.h"
42+
#include "src/core/ImportNode.h"
43+
44+
std::vector<ModuleInstantiation *> modinsts_list;
45+
46+
#define NodeCloneFunc(T) std::shared_ptr<T> clone_what(const T *node) {\
47+
ModuleInstantiation *inst = new ModuleInstantiation(node->modinst->name() ,\
48+
node->modinst->arguments, node->modinst->location());\
49+
modinsts_list.push_back(inst); \
50+
auto clone = std::make_shared<T>(*node);\
51+
clone->modinst = inst; \
52+
return clone;\
53+
}
54+
55+
#define NodeCloneUse(T) { const T *node = dynamic_cast<const T *>(this); if((node) != nullptr) clone=clone_what(node); }
56+
NodeCloneFunc(CubeNode)
57+
NodeCloneFunc(SphereNode)
58+
NodeCloneFunc(CylinderNode)
59+
NodeCloneFunc(PolyhedronNode)
60+
NodeCloneFunc(SquareNode)
61+
NodeCloneFunc(CircleNode)
62+
NodeCloneFunc(PolygonNode)
63+
NodeCloneFunc(TransformNode)
64+
NodeCloneFunc(ColorNode)
65+
NodeCloneFunc(RotateExtrudeNode)
66+
NodeCloneFunc(LinearExtrudeNode)
67+
NodeCloneFunc(CsgOpNode)
68+
NodeCloneFunc(CgalAdvNode)
69+
NodeCloneFunc(RoofNode)
70+
NodeCloneFunc(RenderNode)
71+
NodeCloneFunc(SurfaceNode)
72+
NodeCloneFunc(TextNode)
73+
NodeCloneFunc(OffsetNode)
74+
NodeCloneFunc(ProjectionNode)
75+
NodeCloneFunc(GroupNode)
76+
NodeCloneFunc(ImportNode)
77+
78+
std::shared_ptr<AbstractNode> AbstractNode::clone(void)
79+
{
80+
std::shared_ptr<AbstractNode> clone=nullptr;
81+
NodeCloneUse(CubeNode)
82+
NodeCloneUse(SphereNode)
83+
NodeCloneUse(CylinderNode)
84+
NodeCloneUse(PolyhedronNode)
85+
NodeCloneUse(SquareNode)
86+
NodeCloneUse(CircleNode)
87+
NodeCloneUse(PolygonNode)
88+
NodeCloneUse(TransformNode)
89+
NodeCloneUse(ColorNode)
90+
NodeCloneUse(RotateExtrudeNode)
91+
NodeCloneUse(LinearExtrudeNode)
92+
NodeCloneUse(CsgOpNode)
93+
NodeCloneUse(CgalAdvNode)
94+
NodeCloneUse(RoofNode)
95+
NodeCloneUse(RenderNode)
96+
NodeCloneUse(SurfaceNode)
97+
NodeCloneUse(TextNode)
98+
NodeCloneUse(OffsetNode)
99+
NodeCloneUse(ProjectionNode)
100+
NodeCloneUse(GroupNode)
101+
NodeCloneUse(ImportNode)
102+
if(clone != nullptr) {
103+
clone->idx = idx_counter++;
104+
clone->children.clear();
105+
for(const auto &child: this->children) {
106+
clone->children.push_back(child->clone());
107+
}
108+
return clone;
109+
}
110+
std::cout << "Type not defined for clone :" << typeid(this).name() << "\n\r";
111+
return std::shared_ptr<AbstractNode>(this);
112+
}

src/gui/CGALWorker.cc

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
#include "utils/printutils.h"
1414
#include "utils/exceptions.h"
1515

16+
#ifdef ENABLE_PYTHON
17+
#include "python/python_public.h"
18+
#endif
19+
1620
CGALWorker::CGALWorker()
1721
{
1822
this->tree = nullptr;
@@ -29,13 +33,19 @@ CGALWorker::~CGALWorker()
2933

3034
void CGALWorker::start(const Tree& tree)
3135
{
36+
#ifdef ENABLE_PYTHON
37+
python_unlock();
38+
#endif
3239
this->tree = &tree;
3340
this->thread->start();
3441
}
3542

3643
void CGALWorker::work()
3744
{
3845
// this is a worker thread: we don't want any exceptions escaping and crashing the app.
46+
#ifdef ENABLE_PYTHON
47+
python_lock();
48+
#endif
3949
std::shared_ptr<const Geometry> root_geom;
4050
try {
4151
GeometryEvaluator evaluator(*this->tree);
@@ -59,7 +69,9 @@ void CGALWorker::work()
5969
} catch (...) {
6070
LOG(message_group::Error, "Rendering cancelled by unknown exception.");
6171
}
62-
72+
#ifdef ENABLE_PYTHON
73+
python_unlock();
74+
#endif
6375
emit done(root_geom);
6476
thread->quit();
6577
}

src/gui/MainWindow.cc

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1505,7 +1505,11 @@ void MainWindow::saveBackup()
15051505
}
15061506

15071507
if (!this->tempFile) {
1508-
this->tempFile = new QTemporaryFile(backupPath.append(basename + "-backup-XXXXXXXX.scad"));
1508+
QString suffix="scad";
1509+
#ifdef ENABLE_PYTHON
1510+
if(this->python_active) suffix="py";
1511+
#endif
1512+
this->tempFile = new QTemporaryFile(backupPath.append(basename + "-backup-XXXXXXXX." + suffix));
15091513
}
15101514

15111515
if ((!this->tempFile->isOpen()) && (!this->tempFile->open())) {
@@ -1825,6 +1829,10 @@ bool MainWindow::trust_python_file(const std::string& file, const std::string&
18251829
this->trusted_edit_document_name = file;
18261830
return true;
18271831
}
1832+
if(content.rfind("from openscad import",0) == 0) { // 1st character already typed
1833+
this->trusted_edit_document_name=file;
1834+
return true;
1835+
}
18281836

18291837
if (settings.contains(setting_key)) {
18301838
QString str = settings.value(setting_key).toString();
@@ -2155,6 +2163,9 @@ void MainWindow::cgalRender()
21552163

21562164
void MainWindow::actionRenderDone(const std::shared_ptr<const Geometry>& root_geom)
21572165
{
2166+
#ifdef ENABLE_PYTHON
2167+
python_lock();
2168+
#endif
21582169
progress_report_fin();
21592170
if (root_geom) {
21602171
std::vector<std::string> options;

src/gui/TabManager.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,11 @@ void TabManager::setTabModified(EditorInterface *edt)
470470
void TabManager::openTabFile(const QString& filename)
471471
{
472472
par->setCurrentOutput();
473+
#ifdef ENABLE_PYTHON
474+
if(boost::algorithm::ends_with(filename, ".py")) {
475+
std::string templ="from openscad import *\n";
476+
} else
477+
#endif
473478
editor->setPlainText("");
474479

475480
QFileInfo fileinfo(filename);

0 commit comments

Comments
 (0)