1
/***************************************************************************
2
* Copyright (c) 2010 Jürgen Riegel <juergen.riegel@web.de> *
4
* This file is part of the FreeCAD CAx development system. *
6
* This library is free software; you can redistribute it and/or *
7
* modify it under the terms of the GNU Library General Public *
8
* License as published by the Free Software Foundation; either *
9
* version 2 of the License, or (at your option) any later version. *
11
* This library is distributed in the hope that it will be useful, *
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14
* GNU Library General Public License for more details. *
16
* You should have received a copy of the GNU Library General Public *
17
* License along with this library; see the file COPYING.LIB. If not, *
18
* write to the Free Software Foundation, Inc., 59 Temple Place, *
19
* Suite 330, Boston, MA 02111-1307, USA *
21
***************************************************************************/
23
#include "PreCompiled.h"
25
#include <Base/Console.h>
26
#include <Base/Reader.h>
27
#include <Base/Writer.h>
29
#include "PropertyGeometryList.h"
30
#include "GeometryMigrationExtension.h"
31
#include "GeometryPy.h"
32
#include "Part2DObject.h"
41
//**************************************************************************
42
// PropertyGeometryList
43
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
45
TYPESYSTEM_SOURCE(Part::PropertyGeometryList, App::PropertyLists)
47
//**************************************************************************
48
// Construction/Destruction
51
PropertyGeometryList::PropertyGeometryList() = default;
53
PropertyGeometryList::~PropertyGeometryList()
55
for (auto it : _lValueList) {
60
void PropertyGeometryList::setSize(int newSize)
62
for (unsigned int i = newSize; i < _lValueList.size(); i++)
63
delete _lValueList[i];
64
_lValueList.resize(newSize);
67
int PropertyGeometryList::getSize() const
69
return static_cast<int>(_lValueList.size());
72
void PropertyGeometryList::setValue(const Geometry* lValue)
76
Geometry* newVal = lValue->clone();
77
for (auto it : _lValueList)
79
_lValueList.resize(1);
80
_lValueList[0] = newVal;
85
void PropertyGeometryList::setValues(const std::vector<Geometry*>& lValue)
89
std::sort(_lValueList.begin(), _lValueList.end());
90
for (auto & geo : copy) {
91
auto range = std::equal_range(_lValueList.begin(), _lValueList.end(), geo);
92
// clone if the new entry does not exist in the original value list, or
93
// else, simply reuse it (i.e. erase it so that it won't get deleted below).
94
if (range.first == range.second)
97
_lValueList.erase(range.first, range.second);
99
for (auto v : _lValueList)
101
_lValueList = std::move(copy);
105
void PropertyGeometryList::setValues(std::vector<Geometry*> &&lValue)
107
// Unlike above, the moved version of setValues() indicates the caller want
108
// us to manager the memory of the passed in values. So no need clone.
110
std::sort(_lValueList.begin(), _lValueList.end());
111
for (auto geo : lValue) {
112
auto range = std::equal_range(_lValueList.begin(), _lValueList.end(), geo);
113
_lValueList.erase(range.first, range.second);
115
for (auto geo : _lValueList)
117
_lValueList = std::move(lValue);
121
void PropertyGeometryList::set1Value(int idx, std::unique_ptr<Geometry> &&lValue)
125
if(idx>=(int)_lValueList.size())
126
throw Base::IndexError("Index out of bound");
129
_lValueList.push_back(lValue.release());
131
delete _lValueList[idx];
132
_lValueList[idx] = lValue.release();
137
PyObject *PropertyGeometryList::getPyObject()
140
for (int i = 0; i < getSize(); i++) {
141
list.append(Py::asObject(_lValueList[i]->getPyObject()));
143
return Py::new_reference_to(list);
146
void PropertyGeometryList::setPyObject(PyObject *value)
148
// check container of this property to notify about changes
149
Part2DObject* part2d = dynamic_cast<Part2DObject*>(this->getContainer());
151
if (PySequence_Check(value)) {
152
Py::Sequence sequence(value);
153
Py_ssize_t nSize = sequence.size();
154
std::vector<Geometry*> values;
155
values.resize(nSize);
157
for (Py_ssize_t i=0; i < nSize; ++i) {
158
Py::Object item = sequence.getItem(i);
159
if (!PyObject_TypeCheck(item.ptr(), &(GeometryPy::Type))) {
160
std::string error = std::string("types in list must be 'Geometry', not ");
161
error += item.ptr()->ob_type->tp_name;
162
throw Base::TypeError(error);
165
values[i] = static_cast<GeometryPy*>(item.ptr())->getGeometryPtr();
170
part2d->acceptGeometry();
173
else if (PyObject_TypeCheck(value, &(GeometryPy::Type))) {
174
GeometryPy *pcObject = static_cast<GeometryPy*>(value);
175
setValue(pcObject->getGeometryPtr());
177
part2d->acceptGeometry();
181
std::string error = std::string("type must be 'Geometry' or list of 'Geometry', not ");
182
error += value->ob_type->tp_name;
183
throw Base::TypeError(error);
187
void PropertyGeometryList::trySaveGeometry(Geometry * geom, Base::Writer &writer) const
189
// Not all geometry classes implement Save() and throw an exception instead
192
for( auto & ext : geom->getExtensions() ) {
193
auto extension = ext.lock();
194
auto gpe = freecad_dynamic_cast<GeometryMigrationPersistenceExtension>(extension.get());
196
gpe->postSave(writer);
199
catch (const Base::NotImplementedError& e) {
200
Base::Console().Warning(std::string("PropertyGeometryList"), "Not yet implemented: %s\n", e.what());
204
void PropertyGeometryList::tryRestoreGeometry(Geometry * geom, Base::XMLReader &reader)
206
// Not all geometry classes implement Restore() and throw an exception instead
208
if (!reader.getAttributeAsInteger("migrated", "0") && reader.hasAttribute("id")) {
209
auto ext = std::make_unique<GeometryMigrationExtension>();
210
ext->setId(reader.getAttributeAsInteger("id"));
211
if(reader.hasAttribute("ref")) {
212
const char *ref = reader.getAttribute("ref");
213
int index = reader.getAttributeAsInteger("refIndex", "1");
214
unsigned long flags = (unsigned long)reader.getAttributeAsUnsigned("flags");
215
ext->setReference(ref, index, flags);
217
geom->setExtension(std::move(ext));
219
geom->Restore(reader);
221
catch (const Base::NotImplementedError& e) {
222
Base::Console().Warning(std::string("PropertyGeometryList"), "Not yet implemented: %s\n", e.what());
226
void PropertyGeometryList::Save(Writer &writer) const
228
writer.Stream() << writer.ind() << "<GeometryList count=\"" << getSize() <<"\">" << endl;
230
for (int i = 0; i < getSize(); i++) {
231
writer.Stream() << writer.ind() << "<Geometry type=\""
232
<< _lValueList[i]->getTypeId().getName() << "\"" << endl;
233
for( auto &e : _lValueList[i]->getExtensions() ) {
235
auto gpe = freecad_dynamic_cast<GeometryMigrationPersistenceExtension>(ext.get());
237
gpe->preSave(writer);
239
writer.Stream() << " migrated=\"1\">\n";
242
trySaveGeometry(_lValueList[i], writer);
244
writer.Stream() << writer.ind() << "</Geometry>" << endl;
247
writer.Stream() << writer.ind() << "</GeometryList>" << endl ;
250
void PropertyGeometryList::Restore(Base::XMLReader &reader)
253
reader.clearPartialRestoreObject();
254
reader.readElement("GeometryList");
255
// get the value of my attribute
256
int count = reader.getAttributeAsInteger("count");
257
std::vector<Geometry*> values;
258
values.reserve(count);
259
for (int i = 0; i < count; i++) {
260
reader.readElement("Geometry");
261
const char* TypeName = reader.getAttribute("type");
262
Geometry *newG = static_cast<Geometry *>(Base::Type::fromName(TypeName).createInstance());
263
tryRestoreGeometry(newG, reader);
265
if(reader.testStatus(Base::XMLReader::ReaderStatus::PartialRestoreInObject)) {
266
Base::Console().Error("Geometry \"%s\" within a PropertyGeometryList was subject to a partial restore.\n",reader.localName());
267
if(isOrderRelevant()) {
268
// Pushes the best try by the Geometry class
269
values.push_back(newG);
274
reader.clearPartialRestoreObject();
277
values.push_back(newG);
280
reader.readEndElement("Geometry");
283
reader.readEndElement("GeometryList");
286
setValues(std::move(values));
289
App::Property *PropertyGeometryList::Copy() const
291
PropertyGeometryList *p = new PropertyGeometryList();
292
p->setValues(_lValueList);
296
void PropertyGeometryList::Paste(const Property &from)
298
const PropertyGeometryList& FromList = dynamic_cast<const PropertyGeometryList&>(from);
299
setValues(FromList._lValueList);
302
unsigned int PropertyGeometryList::getMemSize() const
304
int size = sizeof(PropertyGeometryList);
305
for (int i = 0; i < getSize(); i++)
306
size += _lValueList[i]->getMemSize();
310
void PropertyGeometryList::moveValues(PropertyGeometryList &&other)
312
setValues(std::move(other._lValueList));