7
Model::Model(const Model &other)
10
for (ModelMaterialMap::const_iterator i = other.materials.begin(); i != other.materials.end(); ++i)
11
this->add_material(i->first, *i->second);
14
this->objects.reserve(other.objects.size());
15
for (ModelObjectPtrs::const_iterator i = other.objects.begin(); i != other.objects.end(); ++i)
16
this->add_object(**i);
19
Model& Model::operator= (Model other)
26
Model::swap(Model &other)
28
std::swap(this->materials, other.materials);
29
std::swap(this->objects, other.objects);
34
this->clear_objects();
35
this->clear_materials();
41
ModelObject* new_object = new ModelObject(this);
42
this->objects.push_back(new_object);
47
Model::add_object(const ModelObject &other)
49
ModelObject* new_object = new ModelObject(this, other);
50
this->objects.push_back(new_object);
55
Model::delete_object(size_t idx)
57
ModelObjectPtrs::iterator i = this->objects.begin() + idx;
59
this->objects.erase(i);
63
Model::clear_objects()
65
// int instead of size_t because it can be -1 when vector is empty
66
for (int i = this->objects.size()-1; i >= 0; --i)
67
this->delete_object(i);
71
Model::delete_material(t_model_material_id material_id)
73
ModelMaterialMap::iterator i = this->materials.find(material_id);
74
if (i != this->materials.end()) {
76
this->materials.erase(i);
81
Model::clear_materials()
83
while (!this->materials.empty())
84
this->delete_material( this->materials.begin()->first );
88
Model::add_material(t_model_material_id material_id)
90
ModelMaterial* material = this->get_material(material_id);
91
if (material == NULL) {
92
material = this->materials[material_id] = new ModelMaterial(this);
98
Model::add_material(t_model_material_id material_id, const ModelMaterial &other)
100
// delete existing material if any
101
ModelMaterial* material = this->get_material(material_id);
102
if (material != NULL) {
107
material = new ModelMaterial(this, other);
108
this->materials[material_id] = material;
113
Model::get_material(t_model_material_id material_id)
115
ModelMaterialMap::iterator i = this->materials.find(material_id);
116
if (i == this->materials.end()) {
125
Model::duplicate_objects_grid(unsigned int x, unsigned int y, coordf_t distance)
127
if (this->objects.size() > 1) throw "Grid duplication is not supported with multiple objects";
128
if (this->objects.empty()) throw "No objects!";
130
ModelObject* object = this->objects.front();
131
object->clear_instances();
134
object->bounding_box(&bb);
135
Sizef3 size = bb.size();
137
for (unsigned int x_copy = 1; x_copy <= x; ++x_copy) {
138
for (unsigned int y_copy = 1; y_copy <= y; ++y_copy) {
139
ModelInstance* instance = object->add_instance();
140
instance->offset.x = (size.x + distance) * (x_copy-1);
141
instance->offset.y = (size.y + distance) * (y_copy-1);
148
Model::has_objects_with_no_instances() const
150
for (ModelObjectPtrs::const_iterator i = this->objects.begin();
151
i != this->objects.end(); ++i)
153
if ((*i)->instances.empty()) {
162
REGISTER_CLASS(Model, "Model");
166
ModelMaterial::ModelMaterial(Model *model) : model(model) {}
167
ModelMaterial::ModelMaterial(Model *model, const ModelMaterial &other)
168
: model(model), config(other.config), attributes(other.attributes)
172
ModelMaterial::apply(const t_model_material_attributes &attributes)
174
this->attributes.insert(attributes.begin(), attributes.end());
179
REGISTER_CLASS(ModelMaterial, "Model::Material");
183
ModelObject::ModelObject(Model *model)
187
ModelObject::ModelObject(Model *model, const ModelObject &other)
189
input_file(other.input_file),
192
config(other.config),
193
layer_height_ranges(other.layer_height_ranges),
194
origin_translation(other.origin_translation),
195
_bounding_box(other._bounding_box),
196
_bounding_box_valid(other._bounding_box_valid)
199
this->volumes.reserve(other.volumes.size());
200
for (ModelVolumePtrs::const_iterator i = other.volumes.begin(); i != other.volumes.end(); ++i)
201
this->add_volume(**i);
203
this->instances.reserve(other.instances.size());
204
for (ModelInstancePtrs::const_iterator i = other.instances.begin(); i != other.instances.end(); ++i)
205
this->add_instance(**i);
208
ModelObject& ModelObject::operator= (ModelObject other)
215
ModelObject::swap(ModelObject &other)
217
std::swap(this->input_file, other.input_file);
218
std::swap(this->instances, other.instances);
219
std::swap(this->volumes, other.volumes);
220
std::swap(this->config, other.config);
221
std::swap(this->layer_height_ranges, other.layer_height_ranges);
222
std::swap(this->origin_translation, other.origin_translation);
223
std::swap(this->_bounding_box, other._bounding_box);
224
std::swap(this->_bounding_box_valid, other._bounding_box_valid);
227
ModelObject::~ModelObject()
229
this->clear_volumes();
230
this->clear_instances();
234
ModelObject::add_volume(const TriangleMesh &mesh)
236
ModelVolume* v = new ModelVolume(this, mesh);
237
this->volumes.push_back(v);
238
this->invalidate_bounding_box();
243
ModelObject::add_volume(const ModelVolume &other)
245
ModelVolume* v = new ModelVolume(this, other);
246
this->volumes.push_back(v);
247
this->invalidate_bounding_box();
252
ModelObject::delete_volume(size_t idx)
254
ModelVolumePtrs::iterator i = this->volumes.begin() + idx;
256
this->volumes.erase(i);
257
this->invalidate_bounding_box();
261
ModelObject::clear_volumes()
263
// int instead of size_t because it can be -1 when vector is empty
264
for (int i = this->volumes.size()-1; i >= 0; --i)
265
this->delete_volume(i);
269
ModelObject::add_instance()
271
ModelInstance* i = new ModelInstance(this);
272
this->instances.push_back(i);
273
this->invalidate_bounding_box();
278
ModelObject::add_instance(const ModelInstance &other)
280
ModelInstance* i = new ModelInstance(this, other);
281
this->instances.push_back(i);
282
this->invalidate_bounding_box();
287
ModelObject::delete_instance(size_t idx)
289
ModelInstancePtrs::iterator i = this->instances.begin() + idx;
291
this->instances.erase(i);
292
this->invalidate_bounding_box();
296
ModelObject::delete_last_instance()
298
this->delete_instance(this->instances.size() - 1);
302
ModelObject::clear_instances()
304
for (size_t i = 0; i < this->instances.size(); ++i)
305
this->delete_instance(i);
309
ModelObject::invalidate_bounding_box()
311
this->_bounding_box_valid = false;
315
REGISTER_CLASS(ModelObject, "Model::Object");
319
ModelVolume::ModelVolume(ModelObject* object, const TriangleMesh &mesh)
320
: object(object), mesh(mesh), modifier(false)
323
ModelVolume::ModelVolume(ModelObject* object, const ModelVolume &other)
324
: object(object), mesh(other.mesh), modifier(other.modifier)
326
this->material_id(other.material_id());
330
ModelVolume::material_id() const
332
return this->_material_id;
336
ModelVolume::material_id(t_model_material_id material_id)
338
this->_material_id = material_id;
340
// ensure this->_material_id references an existing material
341
(void)this->object->get_model()->add_material(material_id);
345
ModelVolume::material() const
347
return this->object->get_model()->get_material(this->_material_id);
351
REGISTER_CLASS(ModelVolume, "Model::Volume");
355
ModelInstance::ModelInstance(ModelObject *object)
356
: object(object), rotation(0), scaling_factor(1)
359
ModelInstance::ModelInstance(ModelObject *object, const ModelInstance &other)
360
: object(object), rotation(other.rotation), scaling_factor(other.scaling_factor), offset(other.offset)
364
REGISTER_CLASS(ModelInstance, "Model::Instance");