SUAPI-CppWrapper
C++WrapperforSketchUpCAPI
Entity.hpp
1 //
2 // Entity.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 Entity_hpp
29 #define Entity_hpp
30 
31 #include <stdio.h>
32 #include <string>
33 #include <vector>
34 #include <math.h>
35 
36 #include <SketchUpAPI/model/entity.h>
37 
38 #include "SUAPI-CppWrapper/model/TypedValue.hpp"
39 
40 namespace CW {
41 
42 // Forward Declarations
43 class AttributeDictionary;
44 class Model;
45 class Entities;
46 
47 /*
48 * @brief Entity object wrapper.
49 *
50 * This is the base class for all SketchUp entities. Entities are basically anything that can be contained in a model, including Drawingelements such as Edges, SectionPlanes, Groups, etc. and entities that relate to those Drawingelements, such as Loops, Layers, etc.
51 *
52 * Keep in mind that the methods below are available on all subclasses. For example, an Edge's parent class is Drawingelement, and a Drawingelement's parent class is Entity. Therefore an Edge has all of the methods defined in Drawingelement and Entity.
53 */
54 class Entity {
55  protected:
56  /**
57  * @brief The C SUEntityRef that this class wraps.
58  */
59  SUEntityRef m_entity;
60 
61  /**
62  * @brief Indicates whether the Entity has been attached to a model.
63  */
64  bool m_attached;
65 
66  public:
67  /**
68  * @brief Constructor representing a null objject.
69  */
70  Entity();
71 
72  /**
73  * @brief Creates a new Entity object.
74  * @param entity - SUEntityRef from SU C API
75  * @param attached - (optional) flag indicating whether the entity is attached to a model. Defaults to true.
76  */
77  Entity(SUEntityRef entity, bool attached = true);
78 
79  /**
80  * @brief Copy constructor with an optional parameter for the entity reference.
81  *
82  * SUEntityRef objects cannot be created from this class, so the Ref object must be passed to this constructor from a derived class object.
83  * @param other - Entity object from which properties will be copied.
84  * @param entity_ref - (optional) SUEntityRef object to assign to the copied object.
85  */
86  Entity(const Entity& other, SUEntityRef entity_ref = SU_INVALID);
87 
88  /**
89  * @brief Destructor
90  *
91  * The C++ wrapper deals with releasing objects, so user does not have to keep track of memory allocations.
92  */
93  ~Entity();
94 
95  /** @brief Copy assignment operator */
96  Entity& operator=(const Entity& other);
97 
98  /*
99  * @brief Returns a copy of the wrapped SUEntityRef. USE WITH CAUTION.
100  *
101  * Note that the Entity object still manages the lifetime of the reference, so the SUEntityRef could become invalid.
102  */
103  operator SUEntityRef() const;
104 
105  /*
106  * @brief Returns a pointer to the wrapped SUEntityRef. USE WITH CAUTION.
107  *
108  * Note that the Entity object still manages the lifetime of the reference, so the SUEntityRef could become invalid.
109  */
110  operator SUEntityRef*();
111 
112  /*
113  * @brief Returns the wrapped SUEntityRef. USE WITH CAUTION.
114  *
115  * Note that the Entity object still manages the lifetime of the reference, so the SUEntityRef could become invalid.
116  */
117  SUEntityRef ref() const;
118 
119  /**
120  * @brief Returns true if the entity is attached to another object.
121  */
122  bool attached() const;
123 
124  /**
125  * @brief Method lets the object know that it has been attached to a model. This is important as it will let the object know that it does not need to "release" the object.
126  * @param attach - (optional) true to let the object know that it has been attached to a model. False to let the object know that it has not been attached.
127  */
128  void attached(bool attach);
129 
130  /**
131  * @brief Returns the AttributeDictionaries collection attached to the entity.
132  * @return vector of AttributeDictionary objects associated with the entity. If no AttributeDictionary objects are associated with the entity, an empty vector will be returned.
133  */
134  std::vector<AttributeDictionary> attribute_dictionaries() const;
135 
136  /**
137  * @brief Returns an attribute dictionary object with a given name that is attached to an Entity.
138  * @param name - string representing the name of the AttributeDictionary
139  * @return AttributeDictionary object with the given name.
140  */
141  AttributeDictionary attribute_dictionary(const std::string& name) const;
142 
143  /**
144  * @brief Adds the AttributeDictionary to the Entity.
145  * @param dict - AttributeDictionary to add
146  * @return true for success, false for failure.
147  */
149 
150  /**
151  * @brief Copies attributes from another Entity object to this one.
152  * @param entity - object to get the attributes from.
153  * @param bool true to delete existing attributes, false to retain and overwrite the values. // TODO: as the C API does not currentlty allow you to delete attributes, this flag can have no effect.
154  * @return true for success, false for failure.
155  */
156  bool copy_attributes_from(const Entity& entity /*, bool clear_existing = false*/);
157 
158  /**
159  * The delete_attribute method is used to delete an attribute from an entity.
160  * @param AttributeDictionary& object in which to find the key.
161  * @param std::string with the name of the key of the attribute.
162  */
163  /*
164  * TODO: deleting attributes is not possible with the current C API. It could be used, however, to store a list of deleted attributes, so if this object is copied, the deleted attributes are not copied over to the new object.
165  void delete_attribute(AttributeDictionary &dict, std::string key);
166  */
167 
168  /**
169  * @brief Checks if the entity is valid.
170  *
171  * Note that this function does not check if the entity has been deleted. @see operator!().
172  */
173  bool is_valid() const;
174 
175  /**
176  * @brief Returns true if this entity is not valid. Alias of !is_valid().
177  */
178  bool operator!() const;
179 
180  /**
181  * @brief Retrieve a unique ID assigned to an entity.
182  * @return int32_t key for the Entity object.
183  */
184  int32_t entityID() const;
185 
186  /**
187  * @brief Retrieves the value of an attribute in the entity's attribute dictionary.
188  * @param dict_name - string name of the attribute dictionary.
189  * @param key - the attribute key.
190  * @param default_value - (optional) default TypedValue to return if no attribute is found. Defaults to an empty TypedValue
191  * @return value TypedValue of the attribute. If no attribute found, the default value passed is returned.
192  */
193  TypedValue get_attribute(const std::string& dict_name, const std::string& key, const TypedValue& default_value = TypedValue()) const;
194 
195  /**
196  * @brief Retrieves the value of an attribute in the entity's attribute dictionary.
197  * @param dict - AttributeDictionary object to search.
198  * @param key - the attribute key.
199  * @param default_value - (optional) default TypedValue to return if no attribute is found. Defaults to an empty TypedValue
200  * @return value TypedValue of the attribute. If no attribute found, the default value passed is returned.
201  */
202  TypedValue get_attribute(const AttributeDictionary &dict, const std::string& key, const TypedValue& default_value = TypedValue()) const;
203 
204  /**
205  // TODO: C API does not currently allow traversing upwards
206  parent()
207  */
208 
209  /**
210  * @brief Sets the value of an attribute in the given AttributeDictionary object.
211  * @param dict_name - string name of AttributeDictionary object that the attribute is in.
212  * @param key - std::string attribute key.
213  * @param value - TypedValue to set.
214  * @return true on success, false on failure
215  */
216  bool set_attribute(const std::string& dict_name, const std::string& key, const TypedValue& value);
217 
218  /**
219  * @brief Sets the value of an attribute in the given AttributeDictionary object.
220  * @param dict - AttributeDictionary object that the attribute is in.
221  * @param key - std::string attribute key.
222  * @param value - TypedValue to set.
223  * @return true on success, false on failure
224  */
225  bool set_attribute(AttributeDictionary& dict, const std::string& key, const TypedValue& value);
226 
227  /**
228  * @brief Returns the type of the entity. See enum SURefType.
229  */
230  enum SURefType entity_type() const;
231 
232  /**
233  * Returns the model object that holds this entity.
234  */
235  Model model() const;
236 
237  /**
238  * Returns the parent Entities object.
239  */
240  Entities parent() const;
241 
242  /**
243  * Returns the persistent ID of the entity. If the entity is not an entity that can have a persistent ID, the method will fail.
244  */
245  int64_t persistent_id() const;
246 
247  /**
248  * Returns whether these objects are the same.
249  */
250  //bool operator==(const Entity& entity) const;
251 
252  /**
253  * @brief Equality operator for two entity objects
254  */
255  friend bool operator==(const Entity& lhs, const Entity& rhs);
256 
257  /**
258  * @brief Non-equality operator for two entity objects
259  */
260  friend bool operator!=(const Entity& lhs, const Entity& rhs);
261 
262  /**
263  * Hash function for use wht unordered_map
264  */
265  friend std::hash<CW::Entity>;
266 };
267 
268 } /* namespace CW */
269 
270 namespace std {
271  template <> struct hash<CW::Entity>
272  {
273  size_t operator()(const CW::Entity& k) const
274  {
275  static const size_t shift = (size_t)log2(1 + sizeof(CW::Entity));
276  return (size_t)(k.m_entity.ptr) >> shift;
277  }
278  };
279 
280 }
281 
282 #endif /* Entity_hpp */
bool m_attached
Indicates whether the Entity has been attached to a model.
Definition: Entity.hpp:64
TypedValue get_attribute(const std::string &dict_name, const std::string &key, const TypedValue &default_value=TypedValue()) const
Retrieves the value of an attribute in the entity&#39;s attribute dictionary.
Definition: Entity.cpp:195
Entity()
Constructor representing a null objject.
Definition: Entity.cpp:48
bool attached() const
Returns true if the entity is attached to another object.
Definition: Entity.cpp:100
friend bool operator==(const Entity &lhs, const Entity &rhs)
Equality operator for two entity objects.
Definition: Entity.cpp:280
STL namespace.
friend bool operator!=(const Entity &lhs, const Entity &rhs)
Non-equality operator for two entity objects.
Definition: Entity.cpp:291
bool copy_attributes_from(const Entity &entity)
Copies attributes from another Entity object to this one.
Definition: Entity.cpp:156
std::vector< AttributeDictionary > attribute_dictionaries() const
Returns the AttributeDictionaries collection attached to the entity.
Definition: Entity.cpp:110
enum SURefType entity_type() const
Returns the type of the entity. See enum SURefType.
Definition: Entity.cpp:234
~Entity()
Destructor.
Definition: Entity.cpp:66
Model model() const
Definition: Entity.cpp:241
SUEntityRef m_entity
The C SUEntityRef that this class wraps.
Definition: Entity.hpp:59
int32_t entityID() const
Retrieve a unique ID assigned to an entity.
Definition: Entity.cpp:185
bool is_valid() const
Checks if the entity is valid.
Definition: Entity.cpp:175
bool add_dictionary(AttributeDictionary &dict)
Adds the AttributeDictionary to the Entity.
Definition: Entity.cpp:146
Definition: Color.hpp:34
bool operator!() const
Returns true if this entity is not valid. Alias of !is_valid().
Definition: Entity.cpp:180
int64_t persistent_id() const
Definition: Entity.cpp:262
Entities parent() const
Definition: Entity.cpp:251
bool set_attribute(const std::string &dict_name, const std::string &key, const TypedValue &value)
Sets the value of an attribute in the given AttributeDictionary object.
Definition: Entity.cpp:217
AttributeDictionary attribute_dictionary(const std::string &name) const
Returns an attribute dictionary object with a given name that is attached to an Entity.
Definition: Entity.cpp:131
Entity & operator=(const Entity &other)
Copy assignment operator.
Definition: Entity.cpp:73