Skip to content

Commit

Permalink
Issue #1767: fixed hatch pattern clone()
Browse files Browse the repository at this point in the history
Support RS_Pattern clone() with deep copying.

If this fix is safe, backport to 2.2.1
  • Loading branch information
dxli committed May 12, 2024
1 parent 47daf42 commit 72902cb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
10 changes: 8 additions & 2 deletions librecad/src/lib/engine/rs_entitycontainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,14 @@ RS_Entity* RS_EntityContainer::clone() const{
RS_DEBUG->print("RS_EntityContainer::clone: ori autoDel: %d",
autoDelete);

RS_EntityContainer* ec = new RS_EntityContainer(*this);
ec->setOwner(autoDelete);
RS_EntityContainer* ec = new RS_EntityContainer(getParent(), isOwner());
if (isOwner()) {
for (const auto* entity: entities)
if (entity != nullptr)
ec->entities.push_back(entity->clone());
} else {
ec->entities = entities;
}

RS_DEBUG->print("RS_EntityContainer::clone: clone autoDel: %d",
ec->isOwner());
Expand Down
17 changes: 12 additions & 5 deletions librecad/src/lib/engine/rs_pattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ RS_Pattern::RS_Pattern(const QString& fileName)
*/
RS_Entity* RS_Pattern::clone() const
{
auto* cloned = new RS_Pattern(*this);
auto* cloned = new RS_Pattern(fileName);
cloned->loaded = loaded;
if (loaded) {
for(auto* entity: *this)
cloned->addEntity(isOwner() ? entity->clone() : entity);
}
return cloned;
}

Expand All @@ -76,14 +81,16 @@ bool RS_Pattern::loadPattern() {
QString path;

if (!fileName.endsWith(".dxf", Qt::CaseInsensitive)) {
QStringList patterns = RS_SYSTEM->getPatternList();
foreach (const QString& path0, RS_SYSTEM->getPatternList()) {
if (QFileInfo(path0).baseName().toLower()==fileName.toLower()) {
path = path0;
RS_DEBUG->print("Pattern found: %s", path.toLatin1().data());
break;
}
}
if (path.isEmpty()) {
RS_DEBUG->print("Pattern not found: %s", fileName.toLatin1().data());
}
}

// We have the full path of the pattern:
Expand All @@ -99,11 +106,11 @@ bool RS_Pattern::loadPattern() {

RS_Graphic gr;
RS_FileIO::instance()->fileImport(gr, path);
for(auto e: gr){
if (e->rtti()==RS2::EntityLine ||
for(const auto* e: gr){
if (e && (e->rtti()==RS2::EntityLine ||
e->rtti()==RS2::EntityArc||
e->rtti()==RS2::EntityEllipse
) {
)) {
RS_Layer* l = e->getLayer();
RS_Entity* cl = e->clone();
cl->reparent(this);
Expand Down

0 comments on commit 72902cb

Please sign in to comment.