-
Notifications
You must be signed in to change notification settings - Fork 8
/
Model.cpp
503 lines (409 loc) · 13.5 KB
/
Model.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
//
// Model.cpp
//
// Sketchup C++ Wrapper for C API
// MIT License
//
// Copyright (c) 2017 Tom Kaneko
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
#include "SUAPI-CppWrapper/model/Model.hpp"
#include <cassert>
#include "SUAPI-CppWrapper/model/Layer.hpp"
#include "SUAPI-CppWrapper/model/Axes.hpp"
#include "SUAPI-CppWrapper/model/Entities.hpp"
//#include "SUAPI-CppWrapper/Behavior.hpp"
#include "SUAPI-CppWrapper/model/Classifications.hpp"
#include "SUAPI-CppWrapper/model/ComponentDefinition.hpp"
#include "SUAPI-CppWrapper/model/Material.hpp"
#include "SUAPI-CppWrapper/model/AttributeDictionary.hpp"
#include "SUAPI-CppWrapper/model/TypedValue.hpp"
namespace CW {
SUModelRef Model::create_model() {
SUModelRef model = SU_INVALID;
SUResult res = SUModelCreate(&model);
assert(res == SU_ERROR_NONE);
return model;
}
Model::Model():
m_model(create_model()),
m_release_on_destroy(true)
{}
Model::Model(SUModelRef model_ref, bool release_on_destroy):
m_model(model_ref),
m_release_on_destroy(release_on_destroy)
{}
Model::Model(std::string file_path):
m_model(SU_INVALID),
m_result(SUModelCreateFromFile(&m_model, file_path.c_str())),
m_release_on_destroy(true)
{}
Model::Model(const Model& other):
m_model(other.m_model),
m_release_on_destroy(other.m_release_on_destroy)
{}
Model::~Model() {
if (m_release_on_destroy && SUIsValid(m_model)) {
SUModelRelease(&m_model);
}
}
SUModelRef Model::ref() const {
return m_model;
}
Model::operator SUModelRef() const {
return ref();
}
Model::operator SUModelRef*() {
return &m_model;
}
Model::operator bool() const {
if (m_result == SU_ERROR_NONE) {
return true;
}
return false;
}
bool Model::operator!() const {
return !bool(*this);
}
Layer Model::active_layer() const {
if(!(*this)) {
throw std::logic_error("CW::Model::active_layer(): Model is null");
}
SULayerRef layer = SU_INVALID;
SUResult res = SUModelGetDefaultLayer(m_model, &layer);
assert(res == SU_ERROR_NONE);
return Layer(layer);
}
/*
* Sets the active layer of the model.
* @param default_layer the Layer object to be the active layer on first opening the model
* @return status true if succsessful
* TODO: default layer cannot be set through API
*/
//bool active_layer(Layer default_layer) {}
bool Model::add_definition(ComponentDefinition& definition) {
if(!(*this)) {
throw std::logic_error("CW::Model::add_definition(): Model is null");
}
std::vector<ComponentDefinition> defs = {definition};
return add_definitions(defs);
}
bool Model::add_definitions(std::vector<ComponentDefinition>& definitions) {
if(!(*this)) {
throw std::logic_error("CW::Model::add_definitions(): Model is null");
}
SUResult res = SUModelAddComponentDefinitions(m_model, definitions.size(), definitions[0]);
if (res == SU_ERROR_NONE) {
return true;
}
return false;
}
std::vector<AttributeDictionary> Model::attribute_dictionaries() const {
if(!(*this)) {
throw std::logic_error("CW::Model::attribute_dictionaries(): Model is null");
}
size_t count = 0;
SUResult res = SUModelGetNumAttributeDictionaries(m_model, &count);
assert(res == SU_ERROR_NONE);
SUAttributeDictionaryRef* dicts = new SUAttributeDictionaryRef[count];
res = SUModelGetAttributeDictionaries(m_model, count, &dicts[0], &count);
assert(res == SU_ERROR_NONE);
std::vector<AttributeDictionary> dictionaries;
dictionaries.reserve(count);
for (size_t i=0; i < count; ++i) {
dictionaries.push_back(AttributeDictionary(dicts[i]));
}
delete dicts;
return dictionaries;
}
AttributeDictionary Model::attribute_dictionary(const std::string& dict_name) const {
if(!(*this)) {
throw std::logic_error("CW::Model::attribute_dictionary(): Model is null");
}
SUAttributeDictionaryRef dict = SU_INVALID;
SUResult res = SUModelGetAttributeDictionary(m_model, dict_name.c_str(), &dict);
assert(res == SU_ERROR_NONE);
return AttributeDictionary(dict);
}
Axes Model::axes() const {
if(!(*this)) {
throw std::logic_error("CW::Model::axes(): Model is null");
}
SUAxesRef axes = SU_INVALID;
SUResult res = SUModelGetAxes(m_model, &axes);
assert(res == SU_ERROR_NONE);
return Axes(axes);
}
//Behavior behavior(); // TODO: this may not be possible to retrieve
Classifications Model::classifications() const {
if(!(*this)) {
throw std::logic_error("CW::Model::classifications(): Model is null");
}
SUClassificationsRef classifications = SU_INVALID;
SUResult res = SUModelGetClassifications(m_model, &classifications);
assert(res == SU_ERROR_NONE);
return Classifications(classifications);
}
/*
* Returns the description attached to this model.
* @return description string object.
*/
//std::string description();
//bool description(std::string description_string);
std::vector<ComponentDefinition> Model::definitions() const {
if(!(*this)) {
throw std::logic_error("CW::Model::definitions(): Model is null");
}
size_t count = 0;
SUResult res = SUModelGetNumComponentDefinitions(m_model, &count);
assert(res == SU_ERROR_NONE);
SUComponentDefinitionRef* defs = new SUComponentDefinitionRef[count];
res = SUModelGetComponentDefinitions(m_model, count, &defs[0], &count);
assert(res == SU_ERROR_NONE);
std::vector<ComponentDefinition> definitions;
definitions.reserve(count);
for (size_t i=0; i < count; ++i) {
definitions.push_back(ComponentDefinition(defs[i]));
}
delete defs;
return definitions;
}
std::vector<ComponentDefinition> Model::group_definitions() const {
if(!(*this)) {
throw std::logic_error("CW::Model::group_definitions(): Model is null");
}
size_t count = 0;
SUResult res = SUModelGetNumGroupDefinitions(m_model, &count);
assert(res == SU_ERROR_NONE);
SUComponentDefinitionRef* defs = new SUComponentDefinitionRef[count];
res = SUModelGetGroupDefinitions(m_model, count, &defs[0], &count);
assert(res == SU_ERROR_NONE);
std::vector<ComponentDefinition> definitions;
definitions.reserve(count);
for (size_t i=0; i < count; ++i) {
definitions.push_back(ComponentDefinition(defs[i]));
}
delete defs;
return definitions;
}
Entities Model::entities() const {
if(!(*this)) {
throw std::logic_error("CW::Model::entities(): Model is null");
}
SUEntitiesRef entities = SU_INVALID;
SUResult res = SUModelGetEntities(m_model, &entities);
assert(res == SU_ERROR_NONE);
return Entities(entities);
}
//find_entity_by_id(); // TODO can this be done?
// TODO - build Location class before this method
/*
bool Model::georeferenced() const {
SULocationRef loc = SU_INVALID;
SUResult res = SUModelGetLocation(m_model, &loc);
assert(res == SU_ERROR_NONE);
Location location(loc);
if (loc) {
return true;
}
return false;
}
*/
TypedValue Model::get_attribute(const AttributeDictionary& dict, const std::string& key, const TypedValue& default_value) const {
if (!(*this)) {
throw std::logic_error("CW::Model::get_attribute(): Model is null");
}
return dict.get_attribute(key, default_value);
}
TypedValue Model::get_attribute(const std::string& dict_name, const std::string& key, const TypedValue& default_value) const {
if (!(*this)) {
throw std::logic_error("CW::Model::get_attribute(): Model is null");
}
AttributeDictionary dictionary = attribute_dictionary(dict_name);
return get_attribute(dictionary, key, default_value);
}
/*
* Returns the GUID of the model.
*/
//guid();
/*
* Returns the list of layers in the model.
* @return layers a vector array of Layer objects in the model.
*/
std::vector<Layer> Model::layers() const {
if (!(*this)) {
throw std::logic_error("CW::Model::layers(): Model is null");
}
size_t count = 0;
SUResult res = SUModelGetNumLayers(m_model, &count);
assert(res == SU_ERROR_NONE);
SULayerRef* layer_refs = new SULayerRef[count];
res = SUModelGetLayers(m_model, count, &layer_refs[0], &count);
assert(res == SU_ERROR_NONE);
std::vector<Layer> layers;
layers.reserve(count);
for (size_t i=0; i < count; ++i) {
layers.push_back(Layer(layer_refs[i]));
}
delete layer_refs;
return layers;
}
/*
* Returns the Location object of the model
* @return location Location object. If no location has been assigned to the model, the Location object returned will be invalid.
*/
// Location location();
std::vector<Material> Model::materials() const {
if (!(*this)) {
throw std::logic_error("CW::Model::materials(): Model is null");
}
size_t count = 0;
SUResult res = SUModelGetNumMaterials(m_model, &count);
assert(res == SU_ERROR_NONE);
if (count == 0) {
return std::vector<Material> {};
}
SUMaterialRef* mats = new SUMaterialRef[count];
res = SUModelGetMaterials(m_model, count, &mats[0], &count);
assert(res == SU_ERROR_NONE);
std::vector<Material> materials;
materials.reserve(count);
for (size_t i=0; i < count; ++i) {
materials.push_back(Material(mats[i]));
}
delete mats;
return materials;
}
void Model::add_materials(std::vector<Material>& materials) {
SUMaterialRef* mats = new SUMaterialRef[materials.size()];
for (size_t i=0; i < materials.size(); i++) {
mats[i] = materials[i].ref();
}
SU_RESULT res = SUModelAddMaterials(m_model, materials.size(), mats);
assert(res == SU_ERROR_NONE);
for (size_t i=0; i < materials.size(); i++) {
materials[i].attached(true);
}
delete mats;
}
String Model::name() const {
if (!(*this)) {
throw std::logic_error("CW::Model::name(): Model is null");
}
SUStringRef name = SU_INVALID;
SUModelGetName(m_model, &name);
return String(name);
}
bool Model::name(const String& name_string) {
if (!(*this)) {
throw std::logic_error("CW::Model::name(): Model is null");
}
std::string std_string = name_string;
SUResult res = SUModelSetName(m_model, std_string.c_str());
if (res == SU_ERROR_NONE) {
return true;
}
return false;
}
size_t Model::num_faces() const {
if (!(*this)) {
throw std::logic_error("CW::Model::num_faces(): Model is null");
}
ModelStatistics model_statistics((*this));
return model_statistics.num_faces();
}
/*
* Returns a key=>value list of options for the model.
* @see SUOptionsProviderRef
*/
//std::vector<std::pair<std::string, std::string>> Model::options() const;
/*
* Returns the path of the model.
*/
// TODO - probably delete this, as there is no way to get the path of the model through the API.
// std::string Model::path() const {}
/*
* Returns the first Entity object that a ray from a given point and direction vector will hit.
*/
//Entity raytest(Point3D point, Vector3D vector);
SUResult Model::save(const std::string& file_path) {
if (!(*this)) {
throw std::logic_error("CW::Model::save(): Model is null");
}
const char * c_string = file_path.c_str();
SUResult res = SUModelSaveToFile(m_model, c_string);
return res;
}
bool Model::save_with_version(const std::string& file_path, SUModelVersion version) {
if (!(*this)) {
throw std::logic_error("CW::Model::save_with_version(): Model is null");
}
SUResult res = SUModelSaveToFileWithVersion(m_model, file_path.c_str(), version);
if (res == SU_ERROR_NONE) {
return true;
}
return false;
}
/*
* Returns the array of Scene objects attached to the model.
* @return scenes array of Scene objects.
*/
// std::vector<Scene> scenes();
bool Model::set_attribute(AttributeDictionary& dict, const std::string& key, const TypedValue& value) {
if (!(*this)) {
throw std::logic_error("CW::Model::set_attribute(): Model is null");
}
return dict.set_attribute(key, value);
}
bool Model::set_attribute(const std::string& dict_name, const std::string& key, const TypedValue& value) {
if (!(*this)) {
throw std::logic_error("CW::Model::set_attribute(): Model is null");
}
AttributeDictionary dict = attribute_dictionary(dict_name);
return set_attribute(dict, key, value);
}
// set_datum()
/*
* Returns the ShadowInfo object of the model.
*/
// ShadowInfo shadow_info();
/*
* Returns the list of styles in the model.
* @return styles vector array of Style objects
*/
// std::vector<Style> styles();
// tags
// tags=
//std::string title() { return name();}
//std::string title(std::string name_value) { return name(name_value);}
/******************
* ModelStatistics *
*******************/
ModelStatistics::ModelStatistics(SUModelStatistics model_statistics):
m_model_statistics(model_statistics)
{}
ModelStatistics::ModelStatistics(const Model& model):
m_model_statistics(SUModelStatistics{})
{
SUResult res = SUModelGetStatistics(model, &m_model_statistics);
assert(res == SU_ERROR_NONE);
}
int ModelStatistics::num_faces() {
return m_model_statistics.entity_counts[SUModelStatistics::SUEntityType_Face];
}
} /* namespace CW */