11// Copyright (c) 2024 UltiMaker
22// CuraEngine is released under the terms of the AGPLv3 or higher
33
4- #ifndef PATHPLANNING_PRINTOPERATIONSEQUENCE_H
5- #define PATHPLANNING_PRINTOPERATIONSEQUENCE_H
4+ #pragma once
5+
6+ #include < range/v3/algorithm/contains.hpp>
67
78#include " geometry/Point3LL.h"
89#include " operation_transformation/PrintOperationTransformer.h"
910#include " print_operation/PrintOperation.h"
11+ #include " print_operation/PrintOperationPtr.h"
1012
1113namespace cura
1214{
@@ -56,41 +58,42 @@ class PrintOperationSequence : public PrintOperation, public std::enable_shared_
5658 * @return The first found operation, or a null ptr if none was found
5759 * @note This function can also be used to iterate over children by providing a search function that always returns false
5860 */
59- std::shared_ptr<PrintOperation> findOperation (
60- const std::function<bool (const std::shared_ptr<PrintOperation> &)>& search_function,
61+ PrintOperationPtr findOperation (
62+ const std::function<bool (const PrintOperationPtr &)>& search_function,
6163 const SearchOrder search_order = SearchOrder::Forward,
6264 const std::optional<size_t> max_depth = SearchDepth::DirectChildren) const ;
6365
6466 template <class OperationType >
6567 std::shared_ptr<OperationType>
6668 findOperationByType (const SearchOrder search_order = SearchOrder::Forward, const std::optional<size_t > max_depth = SearchDepth::DirectChildren) const ;
6769
68- const std::vector<std::shared_ptr<PrintOperation>>& getOperations () const noexcept ;
69-
70- std::vector<std::shared_ptr<PrintOperation>>& getOperations () noexcept ;
70+ const std::vector<PrintOperationPtr>& getOperations () const noexcept ;
7171
7272 template <class OperationType >
7373 std::vector<std::shared_ptr<OperationType>> getOperationsAs () noexcept ;
7474
75- void setOperations (std::vector<std::shared_ptr<PrintOperation>>& operations) noexcept ;
75+ // void setOperations(std::vector<PrintOperationPtr>& operations) noexcept;
76+
77+ template <class OperationType >
78+ void setOperations (std::vector<std::shared_ptr<OperationType>>& operations) noexcept ;
7679
7780protected:
78- void appendOperation (const std::shared_ptr<PrintOperation> & operation);
81+ void appendOperation (const PrintOperationPtr & operation);
7982
80- void removeOperation (const std::shared_ptr<PrintOperation> & operation);
83+ void removeOperation (const PrintOperationPtr & operation);
8184
8285 template <class ChildType >
8386 void applyProcessorToOperationsRecursively (PrintOperationTransformer<ChildType>& processor);
8487
8588private:
86- std::vector<std::shared_ptr<PrintOperation> > operations_;
89+ std::vector<PrintOperationPtr > operations_;
8790};
8891
8992template <class OperationType >
9093std::shared_ptr<OperationType> PrintOperationSequence::findOperationByType (const SearchOrder search_order, const std::optional<size_t > max_depth) const
9194{
92- std::shared_ptr<PrintOperation> found_operation = findOperation (
93- [](const std::shared_ptr<PrintOperation> & operation)
95+ PrintOperationPtr found_operation = findOperation (
96+ [](const PrintOperationPtr & operation)
9497 {
9598 return static_cast <bool >(std::dynamic_pointer_cast<OperationType>(operation));
9699 },
@@ -126,6 +129,32 @@ std::vector<std::shared_ptr<OperationType>> PrintOperationSequence::getOperation
126129 return result;
127130}
128131
132+ template <class OperationType >
133+ void PrintOperationSequence::setOperations (std::vector<std::shared_ptr<OperationType>>& operations) noexcept
134+ {
135+ for (const PrintOperationPtr& removed_operation : operations_)
136+ {
137+ if (! ranges::contains (operations, removed_operation))
138+ {
139+ removed_operation->setParent ({});
140+ }
141+ }
142+
143+ for (const PrintOperationPtr& added_operation : operations)
144+ {
145+ if (! ranges::contains (operations_, added_operation))
146+ {
147+ added_operation->setParent (weak_from_this ());
148+ }
149+ }
150+
151+ operations_.resize (operations.size ());
152+ for (size_t index = 0 ; index < operations.size (); ++index)
153+ {
154+ operations_[index] = operations[index];
155+ }
156+ }
157+
129158template <class ChildType >
130159void PrintOperationSequence::applyProcessorToOperationsRecursively (PrintOperationTransformer<ChildType>& processor)
131160{
@@ -144,5 +173,3 @@ void PrintOperationSequence::applyProcessorToOperationsRecursively(PrintOperatio
144173}
145174
146175} // namespace cura
147-
148- #endif // PATHPLANNING_PRINTOPERATIONSEQUENCE_H
0 commit comments