SUAPI-CppWrapper
C++WrapperforSketchUpCAPI
LoopInput.cpp
1 //
2 // LoopInput.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/LoopInput.hpp"
32 
33 #include <cassert>
34 #include <iostream>
35 
36 #include "SUAPI-CppWrapper/Initialize.hpp"
37 #include "SUAPI-CppWrapper/model/Edge.hpp"
38 #include "SUAPI-CppWrapper/model/Curve.hpp"
39 #include "SUAPI-CppWrapper/model/GeometryInput.hpp"
40 #include "SUAPI-CppWrapper/model/Material.hpp"
41 #include "SUAPI-CppWrapper/model/Layer.hpp"
42 
43 namespace CW {
44 
45 SULoopInputRef LoopInput::create_loop_input_ref() {
46  SULoopInputRef loop_input = SU_INVALID;
47  SULoopInputCreate(&loop_input);
48  return loop_input;
49 }
50 
51 /******************************
52 ** Constructors / Destructor **
53 *******************************/
55  m_loop_input(create_loop_input_ref())
56 {}
57 
58 
59 LoopInput::LoopInput(SULoopInputRef loop_input, bool attached):
60  m_loop_input(loop_input),
61  m_attached(attached)
62 {}
63 
64 /*
65 LoopInput::LoopInput(std::vector<Edge> loop_edges, size_t vertex_index):
66  LoopInput()
67 {
68  for (size_t i=0; i < loop_edges.size(); ++i) {
69  add_vertex_index(vertex_index);
70  if (SU_API_VERSION_MAJOR >= 5) {
71  set_edge_hidden(i, loop_edges[i].hidden());
72  set_edge_soft(i, loop_edges[i].soft());
73  set_edge_smooth(i, loop_edges[i].smooth());
74  Material edge_material = loop_edges[i].material();
75  if (!!edge_material) {
76  set_edge_material(i, edge_material);
77  }
78  Layer edge_layer = loop_edges[i].layer();
79  if (!!edge_layer) {
80  set_edge_layer(i, edge_layer);
81  }
82  }
83  ++vertex_index;
84  }
85 }
86 */
87 
88 LoopInput::LoopInput(const std::vector<InputEdgeProperties>& loop_edge_properties, size_t vertex_index):
89  LoopInput()
90 {
91  for (size_t i=0; i < loop_edge_properties.size(); ++i) {
92  add_vertex_index(vertex_index);
93  if (SU_API_VERSION_MAJOR >= 5) {
94  set_edge_hidden(i, loop_edge_properties[i].hidden);
95  set_edge_soft(i, loop_edge_properties[i].soft);
96  set_edge_smooth(i, loop_edge_properties[i].smooth);
97  Material edge_material = loop_edge_properties[i].material;
98  if (!!edge_material) {
99  set_edge_material(i, edge_material);
100  }
101  Layer edge_layer = loop_edge_properties[i].layer;
102  if (!!edge_layer) {
103  set_edge_layer(i, edge_layer);
104  }
105  }
106  ++vertex_index;
107  }
108 }
109 
110 
112  m_loop_input(create_loop_input_ref())
113 {
114  // LoopInputRef cannot be copied across at this stage. If it is important, something can be done.
115  assert(false);
116 }
117 
118 LoopInput::~LoopInput() {
119  if (!m_attached && SUIsValid(m_loop_input)) {
120  SUResult res = SULoopInputRelease(&m_loop_input);
121  assert(res == SU_ERROR_NONE); _unused(res);
122  }
123 }
124 
125 /*******************
126 ** Public Methods **
127 ********************/
129  // There is no easy way to copy a LoopInput object.
130  assert(false);
131  // Note that this is a crude copying operation - we are simply creating a reference to the other object.
132  //m_loop_input = other.m_loop_input;
133  /**
134  size_t vertex_index = 0;
135  SULoopInputRef other_loop_input = other.ref();
136  while (SULoopInputEdgeSetHidden(other_loop_input, size_t edge_index, bool hidden))
137  */
138  return *this; // Temporary return value, to allow compiling on Windows
139 }
140 
141 
142 SULoopInputRef LoopInput::ref() const {
143  return m_loop_input;
144 }
145 
146 LoopInput::operator SULoopInputRef() const {
147  return ref();
148 }
149 
150 LoopInput::operator SULoopInputRef*() {
151  return &m_loop_input;
152 }
153 
154 LoopInput::operator bool() const {
155  if (SUIsInvalid(m_loop_input)) {
156  return false;
157  }
158  if (m_edge_num > 2) {
159  // TODO: this is a little suspect, as m_edge_num is not correct if the LoopInputRef was created before initialising this object.
160  return true;
161  }
162  return false;
163 }
164 
165 
167  if(!(*this)) {
168  throw std::logic_error("CW::LoopInput::add_vertex_index(): LoopInput is null");
169  }
170  SUResult res = SULoopInputAddVertexIndex(m_loop_input, index);
171  assert(res == SU_ERROR_NONE); _unused(res);
172  m_edge_num++;
173  return (*this);
174 }
175 
176 
177 LoopInput& LoopInput::set_edge_hidden(const size_t edge_index, const bool hidden) {
178  if(!(*this)) {
179  throw std::logic_error("CW::LoopInput::set_edge_hidden(): LoopInput is null");
180  }
181  SUResult res = SULoopInputEdgeSetHidden(m_loop_input, edge_index, hidden);
182  if(res == SU_ERROR_OUT_OF_RANGE) {
183  throw std::invalid_argument("CW::LoopInput::set_edge_hidden(): edge_index is larger than the number of vertices in the LoopInput");
184  }
185  assert(res == SU_ERROR_NONE); _unused(res);
186  return (*this);
187 }
188 
189 LoopInput& LoopInput::set_edge_soft(const size_t edge_index, const bool soft) {
190  if(!(*this)) {
191  throw std::logic_error("CW::LoopInput::set_edge_soft(): LoopInput is null");
192  }
193  SUResult res = SULoopInputEdgeSetSoft(m_loop_input, edge_index, soft);
194  if(res == SU_ERROR_OUT_OF_RANGE) {
195  throw std::invalid_argument("CW::LoopInput::set_edge_soft(): edge_index is larger than the number of vertices in the LoopInput");
196  }
197  assert(res == SU_ERROR_NONE); _unused(res);
198  return (*this);
199 }
200 
201 LoopInput& LoopInput::set_edge_smooth(const size_t edge_index, const bool smooth) {
202  if(!(*this)) {
203  throw std::logic_error("CW::LoopInput::set_edge_smooth(): LoopInput is null");
204  }
205  SUResult res = SULoopInputEdgeSetSoft(m_loop_input, edge_index, smooth);
206  if(res == SU_ERROR_OUT_OF_RANGE) {
207  throw std::invalid_argument("CW::LoopInput::set_edge_smooth(): edge_index is larger than the number of vertices in the LoopInput");
208  }
209  assert(res == SU_ERROR_NONE); _unused(res);
210  return (*this);
211 }
212 
213 LoopInput& LoopInput::set_edge_material(const size_t edge_index, const Material& material) {
214  if(!(*this)) {
215  throw std::logic_error("CW::LoopInput::set_edge_material(): LoopInput is null");
216  }
217  SUResult res = SULoopInputEdgeSetMaterial(m_loop_input, edge_index, material.ref());
218  if(res == SU_ERROR_OUT_OF_RANGE) {
219  throw std::invalid_argument("CW::LoopInput::set_edge_material(): edge_index is larger than the number of vertices in the LoopInput");
220  }
221  assert(res == SU_ERROR_NONE); _unused(res);
222  return (*this);}
223 
224 LoopInput& LoopInput::set_edge_layer(const size_t edge_index, const Layer& layer) {
225  if(!(*this)) {
226  throw std::logic_error("CW::LoopInput::set_edge_layer(): LoopInput is null");
227  }
228  SUResult res = SULoopInputEdgeSetLayer(m_loop_input, edge_index, layer.ref());
229  if(res == SU_ERROR_OUT_OF_RANGE) {
230  throw std::invalid_argument("CW::LoopInput::set_edge_layer(): edge_index is larger than the number of vertices in the LoopInput");
231  }
232  assert(res == SU_ERROR_NONE); _unused(res);
233  return (*this);
234 }
235 
236 
237 
238 /*
239 LoopInput& LoopInput::add_vertices(std::vector<Point3D> points)
240 {
241  for (size_t i=0; i< points.size(); ++i) {
242  if (i == (points.size()-1)) {
243  m_edges.push_back(Edge(points[i], points[0]));
244  }
245  else {
246  m_edges.push_back(Edge(points[i], points[i+1]));
247  }
248  }
249  return *this;
250 }
251 */
252 
253 
254 
255 /*
256 * Adds an edge on the end of the Loop
257 */
258 /*
259 LoopInput& add_edge(Edge edge);
260 LoopInput& add_edges(std::vector<Edge> edges);
261 
262 //LoopInput& add_curve(Curve curve);
263 //LoopInput& add_curves(std::vector<Curve> curves);
264 */
265 
266 } /* namespace CW */
LoopInput & operator=(const LoopInput &other)
Definition: LoopInput.cpp:128
SULayerRef ref() const
Definition: Layer.cpp:109
Definition: Color.hpp:34
LoopInput & add_vertex_index(const size_t index)
Definition: LoopInput.cpp:166