SUAPI-CppWrapper
C++WrapperforSketchUpCAPI
Model.hpp
1 //
2 // Model.hpp
3 //
4 // Sketchup C++ Wrapper for C API
5 // MIT License
6 //
7 // Copyright (c) 2017 Tom Kaneko
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
15 
16 // The above copyright notice and this permission notice shall be included in all
17 // copies or substantial portions of the Software.
18 
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 // SOFTWARE.
26 //
27 
28 #ifndef Model_hpp
29 #define Model_hpp
30 
31 #include <stdio.h>
32 #include <string>
33 #include <vector>
34 
35 #include <SketchUpAPI/model/model.h>
36 
37 namespace CW {
38 
39  // Forward Declarations:
40  class Entities;
41  class Behavior;
42  class Classifications;
43  class ComponentDefinition;
44  class Layer;
45  class Axes;
46  class AttributeDictionary;
47  class TypedValue;
48  class Material;
49  class String;
50  class RenderingOptions;
51  class ShadowInfo;
52  class OptionsManager;
53  class InstancePath;
54 
55 
56 class Model {
57  private:
58  SUModelRef m_model;
59  bool m_release_on_destroy;
60 
61  SUResult m_result = SU_ERROR_NONE; // the result on creation of model
62 
63  static SUModelRef create_model();
64 
65  public:
66  Model();
67  Model(SUModelRef model_ref, bool release_on_destroy = true);
68  Model(std::string file_path);
69 
70  /** Copy Constructor **/
71  Model(const Model& other);
72 
73  ~Model();
74 
75  /** Copy Assignment Operator **/
76  Model& operator=(const Model& other);
77 
78  /**
79  * Returns SUModelRef object for the Model.
80  */
81  SUModelRef ref() const;
82 
83  /*
84  * The class object can be converted to a SUModelRef without loss of data.
85  */
86  operator SUModelRef() const;
87  operator SUModelRef*();
88 
89  /*
90  * Returns true if object is valid.
91  */
92  operator bool() const;
93 
94  /**
95  * NOT operator. Checks if the SUModelRef is invalid.
96  * @return true if the model is invalid
97  */
98  bool operator!() const;
99 
100  /**
101  * Returns a string of the model version in the form "major.minor.build"
102  */
103  std::string version_string() const;
104 
105  /*
106  * Returns active, or 'default' Layer object
107  * @return layer Layer object that is the active layer
108  */
109  Layer active_layer() const;
110 
111  /*
112  * Sets the active layer of the model.
113  * @param default_layer the Layer object to be the active layer on first openign the model
114  * @return status true if succsessful
115  * TODO: default layer cannot be set through API
116  */
117  //bool active_layer(Layer default_layer);
118 
119 
120  /**
121  * Adds the Component Definition to the model
122  * @param definition the ComponentDefinition object to add to the model.
123  * @return true if definition was succeffully added, false, if error.
124  */
125  bool add_definition(ComponentDefinition& definition);
126  bool add_definitions(std::vector<ComponentDefinition>& definitions);
127 
128  /*
129  * The attribute_dictionaries method is used to retrieve the AttributeDictionaries collection attached to the model.
130  * @return vector of AttributeDictionary objects associated with the model. If no AttributeDictionary objects are associated with the entity, an empty vector will be returned.
131  */
132  std::vector<AttributeDictionary> attribute_dictionaries() const;
133 
134  /*
135  * Retrieves an attribute dictionary object with a given name that is attached to an Entity.
136  * @param dict_name string name of the AttributeDictionary object to get. If it does not exist, a new AttributeDictionary object will be created with the name.
137  * @return attribtue_dictionary AttributeDictionary of the smae name.
138  */
139  AttributeDictionary attribute_dictionary(const std::string& dict_name) const;
140 
141  /*
142  * Returns the Axes object of the model.
143  * @return axes Axes object of the model.
144  */
145  Axes axes() const;
146 
147 
148  //Behavior behavior(); // TODO: this may not be possible to retrieve
149 
150  /*
151  * Returns the Classifications object that is tied to the model.
152  */
153  Classifications classifications() const;
154 
155  /*
156  * Returns the description attached to this model.
157  * @return description string object.
158  */
159  //std::string description();
160  //bool description(std::string description_string);
161 
162  /*
163  * Returns the list of ComponentDefinitions in this model
164  * @return definitions vector array of definitions.
165  */
166  std::vector<ComponentDefinition> definitions() const;
167 
168  /*
169  * Returns the list of Group ComponentDefinitions in this model
170  * @return definitions vector array of definitions.
171  */
172  std::vector<ComponentDefinition> group_definitions() const;
173 
174  /**
175  * Returns the InstancePath of the given persistent ID in this model.
176  * @return InstancePath object of the given persistent ID.
177  */
178  InstancePath instance_path(const String& persistent_id) const;
179 
180  /*
181  * Returns the Entities object for this model.
182  * @return entities Entities object
183  */
184  Entities entities() const;
185 
186  //find_entity_by_id(); // TODO can this be done?
187 
188  /*
189  * Determine whether the model has been geolocated
190  * @return true if georeferenced (the model is assigned a location), false if not.
191  */
192  // TODO build Location class before enablign this method;
193  //bool georeferenced() const;
194 
195  /*
196  * Returns the value of the specified attribute.
197  * @param dict AttributeDictionary object in which to search for the attribute
198  * @param key string key to find.
199  * @param default_value if no attribute found, the default value to return
200  */
201  TypedValue get_attribute(const AttributeDictionary& dict, const std::string& key, const TypedValue& default_value) const;
202  TypedValue get_attribute(const std::string& dict_name, const std::string& key, const TypedValue& default_value) const;
203 
204  /*
205  * Returns the GUID of the model.
206  */
207  //guid();
208 
209  /*
210  * Returns the list of layers in the model.
211  * @return layers a vector array of Layer objects in the model.
212  */
213  std::vector<Layer> layers() const;
214 
215  /**
216  * Add layers to the model.
217  * @param layers - vector of layer objects to add. The layers must not be attached to any other model object.
218  */
219  void add_layers(std::vector<Layer>& layers);
220 
221  /**
222  * Checks if the given layer is in the list of layers in this model. This is an expensive method, and is recommended to be used only in debugging.
223  * @param layer - the layer object to check
224  * @return true if the layer is in the model.
225  */
226  bool layer_exists(const Layer& layer) const;
227 
228  /*
229  * Returns the Location object of the model
230  * @return location Location object. If no location has been assigned to the model, the Location object returned will be invalid.
231  */
232  // Location location();
233 
234  /*
235  * Returns the list of materials in the model.
236  * @return materials vector array of Material objects in the model.
237  */
238  std::vector<Material> materials() const;
239 
240  /**
241  * Add materials to the model.
242  * @param materials - vector of material objects which must not be attached to any other model object.
243  */
244  void add_materials(std::vector<Material>& materials);
245 
246  /**
247  * Checks if the given material is in the list of materials in this model. This is a very expensive method, and is recommended to be used only in debugging.
248  * @param material - the material object to check
249  * @return true if the material is in the model.
250  */
251  bool material_exists(const Material& material) const;
252 
253  /*
254  * Returns the name of the model
255  * @return name string of the model name.
256  */
257  String name() const;
258 
259  /*
260  * Sets the name of the model.
261  * @param name_string string to name the model with.
262  */
263  bool name(const String& name_string);
264 
265  /*
266  * The number of faces in the model. Useful for statistics.
267  */
268  size_t num_faces() const;
269 
270  /*
271  * Returns a key=>value list of options for the model.
272  * @see SUOptionsProviderRef
273  */
274  // TODO
275  //std::vector<std::pair<std::string, std::string>> options() const;
276  OptionsManager options();
277 
278  /*
279  * Returns the path of the model.
280  */
281  // TODO - probably delete this, as there is no way to get the path of the model through the API.
282  //std::string path() const;
283 
284  /*
285  * Returns the first Entity object that a ray from a given point and direction vector will hit.
286  */
287  //Entity raytest(Point3D point, Vector3D vector);
288 
289  /*
290  * Saves the model in the file path given.
291  * @param file_path string path to the file
292  * @return SUResult - SU_ERROR_NONE on success. @see SUModelSaveFileToFile for possible return values
293  */
294  SUResult save(const std::string& file_path);
295 
296  /*
297  * Saves the model in the file path given, in the given SU version.
298  * @param file_path string path to the file
299  * @param version SUModelVersion to save
300  * @return true on success, false on failure
301  */
302  bool save_with_version(const std::string& file_path, SUModelVersion version);
303 
304 
305  /*
306  * Returns the array of Scene objects attached to the model.
307  * @return scenes array of Scene objects.
308  */
309  // std::vector<Scene> scenes();
310 
311  /*
312  * Sets an attribute of the model.
313  * @param AttributeDictionary object or string name of AttributeDictionary object that the attribute is in.
314  * @param std::string attribute key.
315  * @param std::string value to set
316  * @return true on success, false on failure
317  */
318  bool set_attribute(AttributeDictionary& dict, const std::string& key, const TypedValue& value);
319  bool set_attribute(const std::string& dict_name, const std::string& key, const TypedValue& value);
320 
321  // set_datum()
322 
323  /*
324  * Returns the ShadowInfo object of the model.
325  */
326  ShadowInfo shadow_info();
327 
328  /*
329  * Returns the list of styles in the model.
330  * @return styles vector array of Style objects
331  */
332  // std::vector<Style> styles();
333 
334  // tags
335  // tags=
336 
337  //std::string title() { return name();}
338  //std::string title(std::string name_value) { return name(name_value);}
339 
340  /// RenderingOptions
342 
343 }; // class Model
344 
346  private:
347  SUModelStatistics m_model_statistics;
348 
349  public:
350  ModelStatistics(SUModelStatistics model_statistics);
351  ModelStatistics(const Model& model);
352 
353  /**
354  * Return the number of faces in the model.
355  */
356  int faces() const;
357  int edges() const;
358  int instances() const;
359  int groups() const;
360  int images() const;
361  int definitions() const;
362  int layers() const;
363  int materials() const;
364 };
365 
366 } /* namespace CW */
367 
368 #endif /* Model_hpp */
void add_layers(std::vector< Layer > &layers)
Definition: Model.cpp:362
std::string version_string() const
Definition: Model.cpp:118
bool layer_exists(const Layer &layer) const
Definition: Model.cpp:382
void add_materials(std::vector< Material > &materials)
Definition: Model.cpp:421
InstancePath instance_path(const String &persistent_id) const
Definition: Model.cpp:276
Model & operator=(const Model &other)
bool operator!() const
Definition: Model.cpp:114
RenderingOptions rendering_options()
RenderingOptions.
Definition: Model.cpp:553
Definition: Color.hpp:34
bool material_exists(const Material &material) const
Definition: Model.cpp:441
bool add_definition(ComponentDefinition &definition)
Definition: Model.cpp:147
SUModelRef ref() const
Definition: Model.cpp:91