@@ -167,15 +167,33 @@ void FillBedJob::prepare()
167167 ap.bed_idx = PartPlateList::MAX_PLATES_COUNT ;
168168 ap.height = 1 ;
169169 ap.itemid = -1 ;
170- ap.setter = [this , mi_orig_trafo](const ArrangePolygon &p) {
170+ ap.setter = [this , sel_id, mi_orig_trafo](const ArrangePolygon &p) {
171171 ModelObject *mo = m_plater->model ().objects [m_object_idx];
172- ModelObject* newObj = m_plater->model ().add_object (*mo);
173- newObj->name = mo->name +" " + std::to_string (p.itemid );
174- for (ModelInstance *newInst : newObj->instances ) {
175- newInst->set_transformation (mi_orig_trafo);
176- newInst->apply_arrange_result (p.translation .cast <double >(), p.rotation );
172+
173+ if (m_instances) {
174+ ModelInstance *new_instance =
175+ mo->add_instance (*mo->instances [sel_id]);
176+
177+ new_instance->set_transformation (mi_orig_trafo);
178+ new_instance->apply_arrange_result (
179+ p.translation .cast <double >(),
180+ p.rotation );
181+ } else {
182+ ModelObject *new_object =
183+ m_plater->model ().add_object (*mo);
184+
185+ new_object->name =
186+ mo->name + " " + std::to_string (p.itemid );
187+
188+ for (ModelInstance *new_instance : new_object->instances ) {
189+ new_instance->set_transformation (mi_orig_trafo);
190+ new_instance->apply_arrange_result (
191+ p.translation .cast <double >(),
192+ p.rotation );
193+ }
194+
195+ m_plater->model ().set_assembly_pos (new_object);
177196 }
178- m_plater->model ().set_assembly_pos (newObj);
179197 };
180198 m_selected.emplace_back (ap);
181199 }
@@ -396,7 +414,7 @@ void FillBedJob::finalize()
396414 return s + int (ap.priority == 0 && ap.bed_idx == 0 );
397415 });
398416
399- int oldSize = m_plater->model ().objects .size ();
417+ const size_t old_size = m_plater->model ().objects .size ();
400418
401419 if (added_cnt > 0 ) {
402420 for (ArrangePolygon& ap : m_selected) {
@@ -418,14 +436,42 @@ void FillBedJob::finalize()
418436 BOOST_LOG_TRIVIAL (debug) << __FUNCTION__ << boost::format (" :selected: bed_id %1%, trans {%2%,%3%}" ) % ap.bed_idx % unscale<double >(ap.translation (X)) % unscale<double >(ap.translation (Y));
419437 }
420438
421- int newSize = m_plater->model ().objects .size ();
422- auto obj_list = m_plater->sidebar ().obj_list ();
423- for (size_t i = oldSize; i < newSize; i++) {
424- obj_list->add_object_to_list (i, true , true , false );
425- obj_list->update_printable_state (i, 0 );
426- }
439+ auto *obj_list = m_plater->sidebar ().obj_list ();
427440
428- BOOST_LOG_TRIVIAL (debug) << __FUNCTION__ << " : paste_objects_into_list" ;
441+ if (m_instances) {
442+ const size_t new_inst_cnt = model_object->instances .size ();
443+
444+ if (new_inst_cnt > inst_cnt) {
445+ // A single instance has no separate child node in the object
446+ // list. When transitioning to multiple instances, add nodes
447+ // for both the original instance and the newly created ones.
448+ const size_t list_items_to_add =
449+ inst_cnt == 1
450+ ? new_inst_cnt
451+ : new_inst_cnt - inst_cnt;
452+
453+ obj_list->increase_object_instances (
454+ m_object_idx,
455+ list_items_to_add);
456+ }
457+
458+ BOOST_LOG_TRIVIAL (debug)
459+ << __FUNCTION__
460+ << " : added "
461+ << (new_inst_cnt - inst_cnt)
462+ << " instances" ;
463+ } else {
464+ const size_t new_size = m_plater->model ().objects .size ();
465+
466+ for (size_t i = old_size; i < new_size; ++i) {
467+ obj_list->add_object_to_list (i, true , true , false );
468+ obj_list->update_printable_state (i, 0 );
469+ }
470+
471+ BOOST_LOG_TRIVIAL (debug)
472+ << __FUNCTION__
473+ << " : paste_objects_into_list" ;
474+ }
429475
430476 m_plater->update ();
431477 }
0 commit comments