SUAPI-CppWrapper
C++WrapperforSketchUpCAPI
Curve.cpp
1 //
2 // Curve.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 <algorithm>
32 #include <cassert>
33 #include <stdexcept>
34 
35 #include "SUAPI-CppWrapper/model/Curve.hpp"
36 
37 namespace CW {
38 
39 
40 Curve::Curve(std::vector<Edge> edges, SUCurveType curve_type):
41  Curve(create_curve(edges, m_create_result), curve_type)
42 {
43 }
44 
45 Curve::Curve(SUCurveRef curve, SUCurveType curve_type):
46  Entity(SUCurveToEntity(curve)),
47  m_curve_type(curve_type)
48 {}
49 
50 Curve::~Curve(){
51 }
52 
53 SUCurveRef Curve::create_curve(std::vector<Edge>& edges, SUResult &result) {
54  SUCurveRef curve_ref = SU_INVALID;
55  std::vector<SUEdgeRef> refs(edges.size(), SU_INVALID);
56 
57  std::transform(edges.begin(), edges.end(), refs.begin(), [](const CW::Edge& edge) {return edge.ref(); });
58  result = SUCurveCreateWithEdges(&curve_ref, refs.data(), refs.size());
59  return curve_ref;
60 }
61 
62 
63 SUCurveRef Curve::ref() const {
64  return SUCurveFromEntity(m_entity);
65 }
66 
67 
68 std::vector<Edge> Curve::get_edges() const{
69  if (!(*this)) {
70  throw std::logic_error("CW::Curve::get_edges(): Curve is null");
71  }
72  size_t num_edges = 0;
73  SUResult res = SUCurveGetNumEdges(this->ref(), &num_edges);
74  assert(res == SU_ERROR_NONE);
75  std::vector<SUEdgeRef> ref_edges(num_edges, SU_INVALID);
76  res = SUCurveGetEdges(this->ref(), num_edges, ref_edges.data(), &num_edges);
77  assert(res == SU_ERROR_NONE); _unused(res);
78  std::vector<Edge> edges(num_edges);
79  std::transform(ref_edges.begin(), ref_edges.end(), edges.begin(),
80  [](const SUEdgeRef& value) {
81  return Edge(value);
82  });
83  return edges;
84 }
85 
86 
87 SUCurveType Curve::get_type() {
88  if (!(*this)) {
89  throw std::logic_error("CW::Curve::get_type(): Curve is null");
90  }
91  SUCurveGetType(this->ref(), &m_curve_type);
92  return m_curve_type;
93 }
94 
95 
96 SUResult Curve::get_result() const {
97  return m_create_result;
98 }
99 
100 
101 Curve::operator bool() const {
102  if (m_create_result == SU_ERROR_NONE) {
103  return true;
104  }
105  return false;
106 }
107 
108 
109 bool Curve::operator!() const {
110  return !bool(this);
111 }
112 
113 } /* namespace CW */
Entity()
Constructor representing a null objject.
Definition: Entity.cpp:48
SUCurveType get_type()
Definition: Curve.cpp:87
SUEntityRef m_entity
The C SUEntityRef that this class wraps.
Definition: Entity.hpp:59
bool operator!() const
Definition: Curve.cpp:109
Definition: Color.hpp:34
SUCurveRef ref() const
Definition: Curve.cpp:63
std::vector< Edge > get_edges() const
Definition: Curve.cpp:68
Curve(std::vector< Edge > edges, SUCurveType curve_type=SUCurveType_Simple)
Definition: Curve.cpp:40