1
/***************************************************************************
2
* Copyright (c) 2002 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"
27
# include <Bnd_Box.hxx>
28
# include <BRepBndLib.hxx>
29
# include <BRepBuilderAPI_Copy.hxx>
30
# include <BRepTools.hxx>
31
# include <BRepTools_ShapeSet.hxx>
32
# include <OSD_OpenFile.hxx>
33
# include <Standard_Failure.hxx>
34
# include <Standard_Version.hxx>
38
#include <App/Application.h>
39
#include <App/Document.h>
40
#include <App/DocumentObject.h>
41
#include <App/ObjectIdentifier.h>
42
#include <Base/Console.h>
43
#include <Base/Exception.h>
44
#include <Base/FileInfo.h>
45
#include <Base/Reader.h>
46
#include <Base/Stream.h>
47
#include <Base/Writer.h>
49
#include "PartFeature.h"
51
#include "PropertyTopoShape.h"
52
#include "TopoShapePy.h"
53
#include "PartFeature.h"
55
FC_LOG_LEVEL_INIT("App", true, true)
57
namespace sp = std::placeholders;
60
TYPESYSTEM_SOURCE(Part::PropertyPartShape , App::PropertyComplexGeoData)
62
PropertyPartShape::PropertyPartShape() = default;
64
PropertyPartShape::~PropertyPartShape() = default;
66
void PropertyPartShape::setValue(const TopoShape& sh)
70
auto obj = Base::freecad_dynamic_cast<App::DocumentObject>(getContainer());
72
auto tag = obj->getID();
73
if(_Shape.Tag && tag!=_Shape.Tag) {
74
auto hasher = _Shape.Hasher?_Shape.Hasher:obj->getDocument()->getStringHasher();
75
_Shape.reTagElementMap(tag,hasher);
77
_Shape.Tag = obj->getID();
78
if (!_Shape.Hasher && _Shape.hasChildElementMap()) {
79
_Shape.Hasher = obj->getDocument()->getStringHasher();
80
_Shape.hashChildMaps();
87
void PropertyPartShape::setValue(const TopoDS_Shape& sh, bool resetElementMap)
90
auto obj = dynamic_cast<App::DocumentObject*>(getContainer());
92
_Shape.Tag = obj->getID();
93
_Shape.setShape(sh,resetElementMap);
98
const TopoDS_Shape& PropertyPartShape::getValue() const
100
return _Shape.getShape();
103
const TopoShape& PropertyPartShape::getShape() const
105
_Shape.initCache(-1);
106
// March, 2024 Toponaming project: There was originally an unused feature to disable
107
// elementMapping that has not been kept:
108
// if (Feature::isElementMappingDisabled(getContainer()))
110
// else if (!res.Tag) {
112
if (auto parent = Base::freecad_dynamic_cast<App::DocumentObject>(getContainer())) {
113
_Shape.Tag = parent->getID();
119
const Data::ComplexGeoData* PropertyPartShape::getComplexData() const
121
_Shape.initCache(-1);
122
return &(this->_Shape);
125
Base::BoundBox3d PropertyPartShape::getBoundingBox() const
127
Base::BoundBox3d box;
128
if (_Shape.getShape().IsNull())
131
// If the shape is empty an exception may be thrown
133
BRepBndLib::Add(_Shape.getShape(), bounds);
135
Standard_Real xMin, yMin, zMin, xMax, yMax, zMax;
136
bounds.Get(xMin, yMin, zMin, xMax, yMax, zMax);
145
catch (Standard_Failure&) {
151
void PropertyPartShape::setTransform(const Base::Matrix4D &rclTrf)
153
_Shape.setTransform(rclTrf);
156
Base::Matrix4D PropertyPartShape::getTransform() const
158
return _Shape.getTransform();
161
void PropertyPartShape::transformGeometry(const Base::Matrix4D &rclTrf)
164
_Shape.transformGeometry(rclTrf);
168
PyObject *PropertyPartShape::getPyObject()
170
Base::PyObjectBase* prop = static_cast<Base::PyObjectBase*>(_Shape.getPyObject());
176
void PropertyPartShape::setPyObject(PyObject *value)
178
if (PyObject_TypeCheck(value, &(TopoShapePy::Type))) {
179
auto shape = *static_cast<TopoShapePy*>(value)->getTopoShapePtr();
180
auto owner = dynamic_cast<App::DocumentObject*>(getContainer());
181
if(owner && owner->getDocument()) {
182
if(shape.Tag || shape.getElementMapSize()) {
183
// We can't trust the meaning of the input shape tag, so we
185
TopoShape res(owner->getID(),owner->getDocument()->getStringHasher(),shape.getShape());
186
res.mapSubElement(shape);
189
shape.Tag = owner->getID();
190
if ( shape.Hasher ) { // TODO: This null guard added during TNP transition
191
shape.Hasher->clear();
198
std::string error = std::string("type must be 'Shape', not ");
199
error += value->ob_type->tp_name;
200
throw Base::TypeError(error);
204
App::Property *PropertyPartShape::Copy() const
206
PropertyPartShape *prop = new PropertyPartShape();
208
// March, 2024 Toponaming project: There was originally a feature to enable making an element
209
// copy ( new geometry and map ) that has not been kept:
210
// if (PartParams::getShapePropertyCopy()) {
211
// // makeElementCopy() consume too much memory for complex geometry.
212
// prop->_Shape = this->_Shape.makeElementCopy();
214
// prop->_Shape = this->_Shape;
215
prop->_Shape = this->_Shape;
216
prop->_Ver = this->_Ver;
220
void PropertyPartShape::Paste(const App::Property &from)
222
auto prop = Base::freecad_dynamic_cast<const PropertyPartShape>(&from);
224
setValue(prop->_Shape);
229
unsigned int PropertyPartShape::getMemSize () const
231
return _Shape.getMemSize();
234
void PropertyPartShape::getPaths(std::vector<App::ObjectIdentifier> &paths) const
236
paths.push_back(App::ObjectIdentifier(getContainer()) << App::ObjectIdentifier::Component::SimpleComponent(getName())
237
<< App::ObjectIdentifier::Component::SimpleComponent(App::ObjectIdentifier::String("ShapeType")));
238
paths.push_back(App::ObjectIdentifier(getContainer()) << App::ObjectIdentifier::Component::SimpleComponent(getName())
239
<< App::ObjectIdentifier::Component::SimpleComponent(App::ObjectIdentifier::String("Orientation")));
240
paths.push_back(App::ObjectIdentifier(getContainer()) << App::ObjectIdentifier::Component::SimpleComponent(getName())
241
<< App::ObjectIdentifier::Component::SimpleComponent(App::ObjectIdentifier::String("Length")));
242
paths.push_back(App::ObjectIdentifier(getContainer()) << App::ObjectIdentifier::Component::SimpleComponent(getName())
243
<< App::ObjectIdentifier::Component::SimpleComponent(App::ObjectIdentifier::String("Area")));
244
paths.push_back(App::ObjectIdentifier(getContainer()) << App::ObjectIdentifier::Component::SimpleComponent(getName())
245
<< App::ObjectIdentifier::Component::SimpleComponent(App::ObjectIdentifier::String("Volume")));
248
void PropertyPartShape::beforeSave() const
252
auto owner = Base::freecad_dynamic_cast<App::DocumentObject>(getContainer());
253
if(owner && !_Shape.isNull() && _Shape.getElementMapSize()>0) {
254
auto ret = owner->getDocument()->addStringHasher(_Shape.Hasher);
255
_HasherIndex = ret.second;
256
_SaveHasher = ret.first;
260
void PropertyPartShape::Save (Base::Writer &writer) const
262
//See SaveDocFile(), RestoreDocFile()
263
writer.Stream() << writer.ind() << "<Part";
264
auto owner = dynamic_cast<App::DocumentObject*>(getContainer());
265
if(owner && !_Shape.isNull()
266
&& _Shape.getElementMapSize()>0
267
&& !_Shape.Hasher.isNull()) {
268
writer.Stream() << " HasherIndex=\"" << _HasherIndex << '"';
270
writer.Stream() << " SaveHasher=\"1\"";
273
// If exporting, do not export mapped element name, but still make a mark
275
if(!owner->isExporting())
276
version = _Ver.size()?_Ver:owner->getElementMapVersion(this);
278
version = _Ver.size()?_Ver:_Shape.getElementMapVersion();
279
writer.Stream() << " ElementMap=\"" << version << '"';
281
bool binary = writer.getMode("BinaryBrep");
282
bool toXML = writer.isForceXML();
284
writer.Stream() << " file=\""
285
<< writer.addFile(getFileName(binary?".bin":".brp").c_str(), this)
288
writer.Stream() << " binary=\"1\">\n";
289
_Shape.exportBinary(writer.beginCharStream(Base::CharStreamFormat::Base64Encoded));
290
writer.endCharStream() << writer.ind() << "</Part>\n";
292
writer.Stream() << " brep=\"1\">\n";
293
_Shape.exportBrep(writer.beginCharStream(Base::CharStreamFormat::Raw)<<'\n');
294
writer.endCharStream() << '\n' << writer.ind() << "</Part>\n";
299
_Shape.Hasher->setPersistenceFileName(getFileName(".Table").c_str());
301
_Shape.Hasher->setPersistenceFileName(0);
302
_Shape.Hasher->Save(writer);
306
_Shape.setPersistenceFileName(getFileName(".Map").c_str());
308
_Shape.setPersistenceFileName(0);
313
std::string PropertyPartShape::getElementMapVersion(bool restored) const {
316
return PropertyComplexGeoData::getElementMapVersion(false);
319
void PropertyPartShape::Restore(Base::XMLReader &reader)
321
reader.readElement("Part");
323
auto owner = Base::freecad_dynamic_cast<App::DocumentObject>(getContainer());
325
bool has_ver = reader.hasAttribute("ElementMap");
327
_Ver = reader.getAttribute("ElementMap");
329
int hasher_idx = static_cast<int>(reader.getAttributeAsInteger("HasherIndex", "-1"));
330
int save_hasher = static_cast<int>(reader.getAttributeAsInteger("SaveHasher", "0"));
334
if (reader.hasAttribute("file")) {
335
std::string file = reader.getAttribute("file");
337
// initiate a file read
338
reader.addFile(file.c_str(), this);
341
else if (reader.hasAttribute(("binary")) && reader.getAttributeAsInteger("binary")) {
343
shape.importBinary(reader.beginCharStream());
344
shape = shape.getShape();
346
else if (reader.hasAttribute("brep") && reader.getAttributeAsInteger("brep")) {
347
shape.importBrep(reader.beginCharStream(Base::CharStreamFormat::Raw));
350
reader.readEndElement("Part");
352
if(owner && hasher_idx>=0) {
353
_Shape.Hasher = owner->getDocument()->getStringHasher(hasher_idx);
355
_Shape.Hasher->Restore(reader);
359
// The file name here is not used for restore, but just a way to get
360
// more useful error message if something wrong when restoring
361
_Shape.setPersistenceFileName(getFileName().c_str());
362
if(owner && owner->getDocument()->testStatus(App::Document::PartialDoc))
363
_Shape.Restore(reader);
364
else if(_Ver == "?" || _Ver.empty()) {
365
// This indicate the shape is saved by legacy version without
368
// This will ask user for recompute after import
369
owner->getDocument()->addRecomputeObject(owner);
372
_Shape.Restore(reader);
373
if (owner ? owner->checkElementMapVersion(this, _Ver.c_str())
374
: _Shape.checkElementMapVersion(_Ver.c_str())) {
375
auto ver = owner?owner->getElementMapVersion(this):_Shape.getElementMapVersion();
376
if(!owner || !owner->getNameInDocument() || !_Shape.getElementMapSize()) {
379
// version mismatch, signal for regenerating.
380
static const char *warnedDoc=0;
381
if(warnedDoc != owner->getDocument()->getName()) {
382
warnedDoc = owner->getDocument()->getName();
383
FC_WARN("Recomputation required for document '" << warnedDoc
384
<< "' on geo element version change in " << getFullName()
385
<< ": " << _Ver << " -> " << ver);
387
owner->getDocument()->addRecomputeObject(owner);
391
} else if(owner && !owner->getDocument()->testStatus(App::Document::PartialDoc)) {
392
// Toponaming 09/2024: Original code has an infrastructure for document parameters we aren't bringing in:
393
// if(App::DocumentParams::getWarnRecomputeOnRestore()) {
394
// However, this warning appeared on all files without element maps, and is now superseded by a user dialog
395
// after loading that is triggered by any call to addRecomputeObject()
396
// FC_WARN("Pending recompute for generating element map: " << owner->getFullName());
397
owner->getDocument()->addRecomputeObject(owner);
400
if (!shape.isNull() || !_Shape.isNull()) {
402
_Shape.setShape(shape.getShape(),false);
407
void PropertyPartShape::afterRestore()
409
if (_Shape.isRestoreFailed()) {
410
// this cause GeoFeature::updateElementReference() to call
411
// PropertyLinkBase::updateElementReferences() with reverse = true, in
412
// order to try to regenerate the element map
415
else if (_Shape.getElementMapSize() == 0) {
417
_Shape.Hasher->clear();
419
PropertyComplexGeoData::afterRestore();
422
// The following function is copied from OCCT BRepTools.cxx and modified
423
// to disable saving of triangulation
426
static Standard_Boolean BRepTools_Write(const TopoDS_Shape& Sh, const Standard_CString File)
429
OSD_OpenStream(os, File, std::ios::out);
431
if (!os.rdbuf()->is_open())
432
return Standard_False;
434
Standard_Boolean isGood = (os.good() && !os.eof());
438
// See TopTools_FormatVersion of OCCT 7.6
445
BRepTools_ShapeSet SS(Standard_False);
446
SS.SetFormatNb(VERSION_1);
447
// SS.SetProgress(PR);
450
os << "DBRep_DrawableShape\n"; // for easy Draw read
460
isGood = os.good() && isGood && !errno;
465
void PropertyPartShape::saveToFile(Base::Writer &writer) const
467
// create a temporary file and copy the content to the zip stream
468
// once the tmp. filename is known use always the same because otherwise
469
// we may run into some problems on the Linux platform
470
static Base::FileInfo fi(App::Application::getTempFileName());
472
TopoDS_Shape myShape = _Shape.getShape();
473
if (!BRepTools_Write(myShape,static_cast<Standard_CString>(fi.filePath().c_str()))) {
474
// Note: Do NOT throw an exception here because if the tmp. file could
475
// not be created we should not abort.
476
// We only print an error message but continue writing the next files to the
478
App::PropertyContainer* father = this->getContainer();
479
if (father && father->isDerivedFrom(App::DocumentObject::getClassTypeId())) {
480
App::DocumentObject* obj = static_cast<App::DocumentObject*>(father);
481
Base::Console().Error("Shape of '%s' cannot be written to BRep file '%s'\n",
482
obj->Label.getValue(),fi.filePath().c_str());
485
Base::Console().Error("Cannot save BRep file '%s'\n", fi.filePath().c_str());
488
std::stringstream ss;
489
ss << "Cannot save BRep file '" << fi.filePath() << "'";
490
writer.addError(ss.str());
493
Base::ifstream file(fi, std::ios::in | std::ios::binary);
495
std::streambuf* buf = file.rdbuf();
496
writer.Stream() << buf;
504
void PropertyPartShape::loadFromFile(Base::Reader &reader)
506
BRep_Builder builder;
507
// create a temporary file and copy the content from the zip stream
508
Base::FileInfo fi(App::Application::getTempFileName());
510
// read in the ASCII file and write back to the file stream
511
Base::ofstream file(fi, std::ios::out | std::ios::binary);
512
unsigned long ulSize = 0;
514
std::streambuf* buf = file.rdbuf();
517
ulSize = buf->pubseekoff(0, std::ios::cur, std::ios::in);
521
// Read the shape from the temp file, if the file is empty the stored shape was already empty.
522
// If it's still empty after reading the (non-empty) file there must occurred an error.
525
if (!BRepTools::Read(shape, static_cast<Standard_CString>(fi.filePath().c_str()), builder)) {
526
// Note: Do NOT throw an exception here because if the tmp. created file could
527
// not be read it's NOT an indication for an invalid input stream 'reader'.
528
// We only print an error message but continue reading the next files from the
530
App::PropertyContainer* father = this->getContainer();
531
if (father && father->isDerivedFrom(App::DocumentObject::getClassTypeId())) {
532
App::DocumentObject* obj = static_cast<App::DocumentObject*>(father);
533
Base::Console().Error("BRep file '%s' with shape of '%s' seems to be empty\n",
534
fi.filePath().c_str(),obj->Label.getValue());
537
Base::Console().Warning("Loaded BRep file '%s' seems to be empty\n", fi.filePath().c_str());
542
// delete the temp file
547
void PropertyPartShape::loadFromStream(Base::Reader &reader)
550
reader.exceptions(std::istream::failbit | std::istream::badbit);
551
BRep_Builder builder;
553
BRepTools::Read(shape, reader, builder);
556
catch (const std::exception&) {
558
Base::Console().Warning("Failed to load BRep file %s\n", reader.getFileName().c_str());
562
void PropertyPartShape::SaveDocFile (Base::Writer &writer) const
564
// If the shape is empty we simply store nothing. The file size will be 0 which
565
// can be checked when reading in the data.
566
if (_Shape.getShape().IsNull())
568
TopoDS_Shape myShape = _Shape.getShape();
569
if (writer.getMode("BinaryBrep")) {
571
shape.setShape(myShape);
572
shape.exportBinary(writer.Stream());
575
bool direct = App::GetApplication().GetParameterGroupByPath
576
("User parameter:BaseApp/Preferences/Mod/Part/General")->GetBool("DirectAccess", true);
582
shape.setShape(myShape);
583
shape.exportBrep(writer.Stream());
588
void PropertyPartShape::RestoreDocFile(Base::Reader &reader)
590
Base::FileInfo brep(reader.getFileName());
591
if (brep.hasExtension("bin")) {
593
shape.importBinary(reader);
597
bool direct = App::GetApplication().GetParameterGroupByPath
598
("User parameter:BaseApp/Preferences/Mod/Part/General")->GetBool("DirectAccess", true);
600
loadFromFile(reader);
603
auto iostate = reader.exceptions();
604
loadFromStream(reader);
605
reader.exceptions(iostate);
610
// -------------------------------------------------------------------------
612
ShapeHistory::ShapeHistory(BRepBuilderAPI_MakeShape& mkShape, TopAbs_ShapeEnum type,
613
const TopoDS_Shape& newS, const TopoDS_Shape& oldS)
615
reset(mkShape,type,newS,oldS);
618
void ShapeHistory::reset(BRepBuilderAPI_MakeShape& mkShape, TopAbs_ShapeEnum type,
619
const TopoDS_Shape& newS, const TopoDS_Shape& oldS)
624
TopTools_IndexedMapOfShape newM, oldM;
625
TopExp::MapShapes(newS, type, newM); // map containing all old objects of type "type"
626
TopExp::MapShapes(oldS, type, oldM); // map containing all new objects of type "type"
628
// Look at all objects in the old shape and try to find the modified object in the new shape
629
for (int i=1; i<=oldM.Extent(); i++) {
631
TopTools_ListIteratorOfListOfShape it;
632
// Find all new objects that are a modification of the old object (e.g. a face was resized)
633
for (it.Initialize(mkShape.Modified(oldM(i))); it.More(); it.Next()) {
635
for (int j=1; j<=newM.Extent(); j++) { // one old object might create several new ones!
636
if (newM(j).IsPartner(it.Value())) {
637
shapeMap[i-1].push_back(j-1); // adjust indices to start at zero
643
// Find all new objects that were generated from an old object (e.g. a face generated from an edge)
644
for (it.Initialize(mkShape.Generated(oldM(i))); it.More(); it.Next()) {
646
for (int j=1; j<=newM.Extent(); j++) {
647
if (newM(j).IsPartner(it.Value())) {
648
shapeMap[i-1].push_back(j-1);
655
// Find all old objects that don't exist any more (e.g. a face was completely cut away)
656
if (mkShape.IsDeleted(oldM(i))) {
657
shapeMap[i-1] = std::vector<int>();
660
// Mop up the rest (will this ever be reached?)
661
for (int j=1; j<=newM.Extent(); j++) {
662
if (newM(j).IsPartner(oldM(i))) {
663
shapeMap[i-1].push_back(j-1);
672
void ShapeHistory::join(const ShapeHistory& newH)
676
for (ShapeHistory::MapList::const_iterator it = shapeMap.begin(); it != shapeMap.end(); ++it) {
677
int old_shape_index = it->first;
678
if (it->second.empty())
679
join.shapeMap[old_shape_index] = ShapeHistory::List();
680
for (ShapeHistory::List::const_iterator jt = it->second.begin(); jt != it->second.end(); ++jt) {
681
ShapeHistory::MapList::const_iterator kt = newH.shapeMap.find(*jt);
682
if (kt != newH.shapeMap.end()) {
683
ShapeHistory::List& ary = join.shapeMap[old_shape_index];
684
ary.insert(ary.end(), kt->second.begin(), kt->second.end());
689
shapeMap.swap(join.shapeMap);
692
// -------------------------------------------------------------------------
694
TYPESYSTEM_SOURCE(Part::PropertyShapeHistory , App::PropertyLists)
696
PropertyShapeHistory::PropertyShapeHistory() = default;
698
PropertyShapeHistory::~PropertyShapeHistory() = default;
700
void PropertyShapeHistory::setValue(const ShapeHistory& sh)
703
_lValueList.resize(1);
708
void PropertyShapeHistory::setValues(const std::vector<ShapeHistory>& values)
711
_lValueList = values;
715
PyObject *PropertyShapeHistory::getPyObject()
717
return Py::new_reference_to(Py::None());
720
void PropertyShapeHistory::setPyObject(PyObject *)
724
void PropertyShapeHistory::Save (Base::Writer &) const
728
void PropertyShapeHistory::Restore(Base::XMLReader &)
732
void PropertyShapeHistory::SaveDocFile (Base::Writer &) const
736
void PropertyShapeHistory::RestoreDocFile(Base::Reader &)
740
App::Property *PropertyShapeHistory::Copy() const
742
PropertyShapeHistory *p= new PropertyShapeHistory();
743
p->_lValueList = _lValueList;
747
void PropertyShapeHistory::Paste(const Property &from)
750
_lValueList = dynamic_cast<const PropertyShapeHistory&>(from)._lValueList;
754
// -------------------------------------------------------------------------
756
TYPESYSTEM_SOURCE(Part::PropertyFilletEdges , App::PropertyLists)
758
PropertyFilletEdges::PropertyFilletEdges() = default;
760
PropertyFilletEdges::~PropertyFilletEdges() = default;
762
void PropertyFilletEdges::setValue(int id, double r1, double r2)
765
_lValueList.resize(1);
766
_lValueList[0].edgeid = id;
767
_lValueList[0].radius1 = r1;
768
_lValueList[0].radius2 = r2;
772
void PropertyFilletEdges::setValues(const std::vector<FilletElement>& values)
775
_lValueList = values;
779
PyObject *PropertyFilletEdges::getPyObject()
781
Py::List list(getSize());
782
std::vector<FilletElement>::const_iterator it;
784
for (it = _lValueList.begin(); it != _lValueList.end(); ++it) {
786
ent.setItem(0, Py::Long(it->edgeid));
787
ent.setItem(1, Py::Float(it->radius1));
788
ent.setItem(2, Py::Float(it->radius2));
792
return Py::new_reference_to(list);
795
void PropertyFilletEdges::setPyObject(PyObject *value)
797
Py::Sequence list(value);
798
std::vector<FilletElement> values;
799
values.reserve(list.size());
800
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
803
fe.edgeid = (int)Py::Long(ent.getItem(0));
804
fe.radius1 = (double)Py::Float(ent.getItem(1));
805
fe.radius2 = (double)Py::Float(ent.getItem(2));
806
values.push_back(fe);
812
void PropertyFilletEdges::Save (Base::Writer &writer) const
814
if (!writer.isForceXML()) {
815
writer.Stream() << writer.ind() << "<FilletEdges file=\"" << writer.addFile(getName(), this) << "\"/>" << std::endl;
819
void PropertyFilletEdges::Restore(Base::XMLReader &reader)
821
reader.readElement("FilletEdges");
822
std::string file (reader.getAttribute("file") );
825
// initiate a file read
826
reader.addFile(file.c_str(),this);
830
void PropertyFilletEdges::SaveDocFile (Base::Writer &writer) const
832
Base::OutputStream str(writer.Stream());
833
uint32_t uCt = (uint32_t)getSize();
835
for (const auto & it : _lValueList) {
836
str << it.edgeid << it.radius1 << it.radius2;
840
void PropertyFilletEdges::RestoreDocFile(Base::Reader &reader)
842
Base::InputStream str(reader);
845
std::vector<FilletElement> values(uCt);
846
for (auto & it : values) {
847
str >> it.edgeid >> it.radius1 >> it.radius2;
852
App::Property *PropertyFilletEdges::Copy() const
854
PropertyFilletEdges *p= new PropertyFilletEdges();
855
p->_lValueList = _lValueList;
859
void PropertyFilletEdges::Paste(const Property &from)
862
_lValueList = dynamic_cast<const PropertyFilletEdges&>(from)._lValueList;
866
// -------------------------------------------------------------------------
868
TYPESYSTEM_SOURCE(Part::PropertyShapeCache, App::Property);
870
App::Property *PropertyShapeCache::Copy(void) const {
871
return new PropertyShapeCache();
874
void PropertyShapeCache::Paste(const App::Property &) {
878
void PropertyShapeCache::Save (Base::Writer &) const
882
void PropertyShapeCache::Restore(Base::XMLReader &)
887
* Make a new python List with a tuple for each cache entry containing the key and the shape
888
* @return the python list
890
PyObject *PropertyShapeCache::getPyObject() {
893
res.append(Py::TupleN(Py::String(v.first),shape2pyshape(v.second)));
894
return Py::new_reference_to(res);
898
* Remove the cache entries for every element in the list
899
* @param value A python list of entry names
901
void PropertyShapeCache::setPyObject(PyObject *value) {
904
if(value == Py_None) {
908
App::PropertyStringList prop;
909
prop.setPyObject(value);
910
for(const auto &sub : prop.getValues())
914
#define SHAPE_CACHE_NAME "_Part_ShapeCache"
916
* Find or create the shape cache for a document object
917
* @param obj The document object
918
* @param create True if we should create the cache if it doesn't exist
919
* @return The shape cache, or null if we aren't creating and it doesn't exist
921
PropertyShapeCache *PropertyShapeCache::get(const App::DocumentObject *obj, bool create) {
922
auto prop = Base::freecad_dynamic_cast<PropertyShapeCache>(
923
obj->getDynamicPropertyByName(SHAPE_CACHE_NAME));
924
if(prop && prop->getContainer()==obj)
929
prop = static_cast<PropertyShapeCache*>(
930
const_cast<App::DocumentObject*>(obj)->addDynamicProperty("Part::PropertyShapeCache",
931
SHAPE_CACHE_NAME,"Part","Shape cache",
932
App::Prop_NoPersist|App::Prop_Output|App::Prop_Hidden));
934
FC_ERR("Failed to add shape cache for " << obj->getFullName());
936
prop->connChanged = const_cast<App::DocumentObject*>(obj)->signalEarlyChanged.connect(
937
std::bind(&PropertyShapeCache::slotChanged,prop,sp::_1,sp::_2));
942
* Look up and return a shape in the cache
943
* @param obj The document object to look in
944
* @param shape The found shape is returned here
945
* @param subname The key to look up
946
* @return True if the name was found
948
bool PropertyShapeCache::getShape(const App::DocumentObject *obj, TopoShape &shape, const char *subname) {
949
// March, 2024 Toponaming project: There was originally a feature to disable shape cache
950
// that has not been kept:
951
// if (PartParams::getDisableShapeCache())
953
auto prop = get(obj,false);
956
if(!subname) subname = "";
957
auto it = prop->cache.find(subname);
958
if(it!=prop->cache.end()) {
960
return !shape.isNull();
966
* Find or create the property shape cache in a document object and then add an entry
967
* @param obj The Object
968
* @param shape The shape to cache
969
* @param subname The key to point at that shape
971
void PropertyShapeCache::setShape(
972
const App::DocumentObject *obj, const TopoShape &shape, const char *subname)
974
// March, 2024 Toponaming project: There was originally a feature to disable shape cache
975
// that has not been kept:
976
// if (PartParams::getDisableShapeCache())
978
auto prop = get(obj,true);
981
if(!subname) subname = "";
982
prop->cache[subname] = shape;
985
void PropertyShapeCache::slotChanged(const App::DocumentObject &, const App::Property &prop) {
986
auto propName = prop.getName();
987
if(!propName) return;
988
if(strcmp(propName,"Group")==0 ||
989
strcmp(propName,"Shape")==0 ||
990
strstr(propName,"Touched")!=0)
992
FC_LOG("clear shape cache on changed " << prop.getFullName());