SUAPI-CppWrapper
C++WrapperforSketchUpCAPI
DrawingElement.cpp
1 //
2 // DrawingElement.cpp
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 // Macro for getting rid of unused variables commonly for assert checking
29 #define _unused(x) ((void)(x))
30 
31 #include "SUAPI-CppWrapper/model/DrawingElement.hpp"
32 
33 #include <cassert>
34 #include <stdexcept>
35 
36 #include "SUAPI-CppWrapper/model/Layer.hpp"
37 #include "SUAPI-CppWrapper/model/Material.hpp"
38 #include "SUAPI-CppWrapper/Geometry.hpp"
39 
40 
41 namespace CW {
42 
43 DrawingElement::DrawingElement(SUDrawingElementRef drawing_element, bool attached):
44  Entity(SUDrawingElementToEntity(drawing_element), attached)
45 {}
46 
47 
48 DrawingElement::DrawingElement(const DrawingElement& other, SUDrawingElementRef element_ref):
49  Entity(other, SUDrawingElementToEntity(element_ref))
50 {}
51 
53  Entity()
54 {}
55 
56 
57 /** Copy assignment operator */
59  if (!other.m_attached && SUIsValid(other.m_entity)) {
60  this->copy_properties_from(other);
61  }
62  Entity::operator=(other);
63  return (*this);
64 }
65 
66 
67 SUDrawingElementRef DrawingElement::ref() const {
68  return SUDrawingElementFromEntity(m_entity);
69 }
70 
71 
72 DrawingElement::operator SUDrawingElementRef() const {
73  return this->ref();
74 }
75 
76 
78  if (SUIsInvalid(m_entity)) {
79  throw std::logic_error("CW::DrawingElement::bounds(): DrawingElement is null");
80  }
81  SUBoundingBox3D box = SU_INVALID;
82  SUResult res = SUDrawingElementGetBoundingBox(this->ref(), &box);
83  assert(res == SU_ERROR_NONE); _unused(res);
84  return BoundingBox3D(box);
85 }
86 
87 
89  bool success = this->casts_shadows(element.casts_shadows());
90  if (!success)
91  return false;
92  success = this->hidden(element.hidden());
93  if (!success)
94  return false;
95  success = this->receive_shadows(element.receive_shadows());
96  if (!success)
97  return false;
98  Layer elem_layer = element.layer();
99  success = this->layer(elem_layer);
100  if (!success)
101  return false;
102  success = this->material(element.material());
103  if (!success)
104  return false;
105 
106  return true;
107 }
108 
109 
110 bool DrawingElement::casts_shadows() const {
111  if (SUIsInvalid(m_entity)) {
112  throw std::logic_error("CW::DrawingElement::casts_shadows(): DrawingElement is null");
113  }
114  bool cast_shadows_flag;
115  SUResult res = SUDrawingElementGetCastsShadows(this->ref(), &cast_shadows_flag);
116  assert(res == SU_ERROR_NONE); _unused(res);
117  return cast_shadows_flag;
118 }
119 
120 
121 bool DrawingElement::casts_shadows(bool casts_shadows) {
122  if (SUIsInvalid(m_entity)) {
123  throw std::logic_error("CW::DrawingElement::casts_shadows(): DrawingElement is null");
124  }
125  SUResult res = SUDrawingElementSetCastsShadows(this->ref(), casts_shadows);
126  if (res == SU_ERROR_NONE) {
127  return true;
128  }
129  return false;
130 }
131 
132 
133 bool DrawingElement::hidden() const {
134  if (SUIsInvalid(m_entity)) {
135  throw std::logic_error("CW::DrawingElement::hidden(): DrawingElement is null");
136  }
137  bool hide_flag;
138  SUDrawingElementGetHidden(this->ref(), &hide_flag);
139  return hide_flag;
140 }
141 
142 
143 bool DrawingElement::hidden(bool hidden) {
144  if (SUIsInvalid(m_entity)) {
145  throw std::logic_error("CW::DrawingElement::hidden(): DrawingElement is null");
146  }
147  SUResult res = SUDrawingElementSetHidden(this->ref(), hidden);
148  if (res == SU_ERROR_NONE) {
149  return true;
150  }
151  return false;
152 }
153 
154 
155 Layer DrawingElement::layer() const {
156  if (SUIsInvalid(m_entity)) {
157  throw std::logic_error("CW::DrawingElement::layer(): DrawingElement is null");
158  }
159  SULayerRef layer_ref = SU_INVALID;
160  SUResult res = SUDrawingElementGetLayer(this->ref(), &layer_ref);
161  if (res == SU_ERROR_NULL_POINTER_OUTPUT || res == SU_ERROR_NO_DATA) {
162  return Layer();
163  }
164  assert(res == SU_ERROR_NONE); _unused(res);
165  return Layer(layer_ref, true);
166 }
167 
168 
169 bool DrawingElement::layer(Layer& layer){
170  if (SUIsInvalid(m_entity)) {
171  throw std::logic_error("CW::DrawingElement::layer(): DrawingElement is null");
172  }
173  SUResult res = SUDrawingElementSetLayer(this->ref(), layer);
174  if (res == SU_ERROR_NONE) {
175  layer.attached(true);
176  return true;
177  }
178  return false;
179 }
180 
181 
182 Material DrawingElement::material() const {
183  if (SUIsInvalid(m_entity)) {
184  throw std::logic_error("CW::DrawingElement::material(): DrawingElement is null");
185  }
186  SUMaterialRef material_ref = SU_INVALID;
187  SUResult res = SUDrawingElementGetMaterial(this->ref(), &material_ref);
188  if (res == SU_ERROR_NO_DATA || res == SU_ERROR_NULL_POINTER_OUTPUT) {
189  return Material();
190  }
191  assert(res == SU_ERROR_NONE); _unused(res);
192  return Material(material_ref, true);
193 }
194 
195 
196 bool DrawingElement::material(const Material& material) {
197  if (SUIsInvalid(m_entity)) {
198  throw std::logic_error("CW::DrawingElement::material(): DrawingElement is null");
199  }
200  SUResult res = SUDrawingElementSetMaterial(this->ref(), material);
201  if (res == SU_ERROR_NONE) {
202  return true;
203  }
204  return false;
205 }
206 
207 
208 bool DrawingElement::receive_shadows() const {
209  if (SUIsInvalid(m_entity)) {
210  throw std::logic_error("CW::DrawingElement::receive_shadows(): DrawingElement is null");
211  }
212  bool receives_shadows_flag;
213  SUResult res = SUDrawingElementGetReceivesShadows(this->ref(), &receives_shadows_flag);
214  assert (res == SU_ERROR_NONE); _unused(res);
215  return receives_shadows_flag;
216 }
217 
218 
219 bool DrawingElement::receive_shadows(bool receives_shadows_flag) {
220  if (SUIsInvalid(m_entity)) {
221  throw std::logic_error("CW::DrawingElement::receive_shadows(): DrawingElement is null");
222  }
223  SUResult res = SUDrawingElementSetReceivesShadows(this->ref(), receives_shadows_flag);
224  if (res == SU_ERROR_NONE) {
225  return true;
226  }
227  return false;
228 }
229 
230 } /* namespace CW */
bool m_attached
Indicates whether the Entity has been attached to a model.
Definition: Entity.hpp:64
bool attached() const
Returns true if the entity is attached to another object.
Definition: Entity.cpp:100
BoundingBox3D bounds()
bool copy_properties_from(const DrawingElement &element)
DrawingElement & operator=(const DrawingElement &other)
SUEntityRef m_entity
The C SUEntityRef that this class wraps.
Definition: Entity.hpp:59
Definition: Color.hpp:34
SUDrawingElementRef ref() const
Entity & operator=(const Entity &other)
Copy assignment operator.
Definition: Entity.cpp:73