SUAPI-CppWrapper
C++WrapperforSketchUpCAPI
Axes.cpp
1 //
2 // Axes.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/Axes.hpp"
32 
33 #include <cassert>
34 
35 namespace CW {
36 
37 /**************************
38 * Private static methods **
39 ***************************/
40 SUAxesRef Axes::create_axes() {
41  SUAxesRef axes = SU_INVALID;
42  SUResult res = SUAxesCreate(&axes);
43  assert(res == SU_ERROR_NONE);
44  _unused(res);
45  return axes;
46 }
47 
48 SUAxesRef Axes::create_custom_axes(const SUPoint3D& origin, const SUVector3D& xaxis, const SUVector3D& yaxis, const SUVector3D& zaxis) {
49  SUAxesRef axes = SU_INVALID;
50  SUResult res = SUAxesCreateCustom(&axes, &origin, &xaxis, &yaxis, &zaxis);
51  if(res != SU_ERROR_NONE) {
52  // There is a problem with this axis, so return an invalid one
53  if (SUIsValid(axes)) {
54  SUAxesRelease(&axes);
55  }
56  return SU_INVALID;
57  }
58  return axes;
59 }
60 
61 SUAxesRef Axes::copy_reference(const Axes& other) {
62  if (other.m_attached) {
63  return SUAxesFromEntity(other.m_entity);
64  }
65  // The other axes has not been attached to the model, so copy its properties to a new object
66  SUAxesRef new_axes = create_custom_axes(other.origin(), other.x_axis(), other.y_axis(), other.z_axis());
67  return new_axes;
68 }
69 
70 
71 /*****************************
72 * Constructors / Destructor **
73 ******************************/
75  DrawingElement(SU_INVALID)
76 {}
77 
78 
79 Axes::Axes(SUAxesRef axes, bool attached):
80  DrawingElement(SUAxesToDrawingElement(axes), attached)
81 {}
82 
83 
85  DrawingElement(SUAxesToDrawingElement(create_custom_axes(origin, x_axis, y_axis, z_axis)), false)
86 {}
87 
88 /** Copy constructor */
89 Axes::Axes(const Axes& other):
90  DrawingElement(other, SUAxesToDrawingElement(copy_reference(other)))
91 {}
92 
93 
95  if (!m_attached && SUIsValid(m_entity)) {
96  SUAxesRef axes = this->ref();
97  SUResult res = SUAxesRelease(&axes);
98  assert(res == SU_ERROR_NONE);
99  _unused(res);
100  }
101 }
102 
103 
104 /******************
105 * Public Methods **
106 *******************/
107 
108 /** Copy assignment operator */
109 Axes& Axes::operator=(const Axes& other) {
110  if (m_attached && SUIsValid(m_entity)) {
111  SUAxesRef axes = this->ref();
112  SUResult res = SUAxesRelease(&axes);
113  assert(res == SU_ERROR_NONE);
114  _unused(res);
115  }
116  m_entity = SUAxesToEntity(copy_reference(other));
118  return *this;
119 }
120 
121 
122 bool Axes::operator!() const {
123  if (SUIsInvalid(m_entity)) {
124  return true;
125  }
126  return false;
127 }
128 
129 /**
130 * Returns the SU native reference
131 */
132 SUAxesRef Axes::ref() const {
133  return SUAxesFromEntity(m_entity);
134 }
135 
137  if (!(*this)) {
138  throw std::logic_error("CW::Axes::x_axis(): Axes is null");
139  }
140  SUVector3D axis;
141  SUResult res = SUAxesGetXAxis(this->ref(), &axis);
142  assert(res == SU_ERROR_NONE); _unused(res);
143  return Vector3D(axis);
144 }
145 
146 Vector3D Axes::y_axis() const {
147  if (!(*this)) {
148  throw std::logic_error("CW::Axes::x_axis(): Axes is null");
149  }
150  SUVector3D axis;
151  SUResult res = SUAxesGetYAxis(this->ref(), &axis);
152  assert(res == SU_ERROR_NONE); _unused(res);
153  return Vector3D(axis);
154 }
155 
156 Vector3D Axes::z_axis() const {
157  if (!(*this)) {
158  throw std::logic_error("CW::Axes::x_axis(): Axes is null");
159  }
160  SUVector3D axis;
161  SUResult res = SUAxesGetZAxis(this->ref(), &axis);
162  assert(res == SU_ERROR_NONE); _unused(res);
163  return Vector3D(axis);
164 }
165 
166 
168  if (!(*this)) {
169  throw std::logic_error("CW::Axes::x_axis(): Axes is null");
170  }
171  SUPoint3D origin;
172  SUResult res = SUAxesGetOrigin(this->ref(), &origin);
173  assert(res == SU_ERROR_NONE); _unused(res);
174  return Point3D(origin);
175 }
176 
177 
179  if (!(*this)) {
180  throw std::logic_error("CW::Axes::x_axis(): Axes is null");
181  }
182  SUTransformation transform;
183  SUResult res = SUAxesGetTransform(this->ref(), &transform);
184  assert(res == SU_ERROR_NONE); _unused(res);
185  return Transformation(transform);
186 }
187 
188 } /* namespace CW */
~Axes()
Definition: Axes.cpp:94
bool m_attached
Indicates whether the Entity has been attached to a model.
Definition: Entity.hpp:64
Axes & operator=(const Axes &other)
Definition: Axes.cpp:109
bool attached() const
Returns true if the entity is attached to another object.
Definition: Entity.cpp:100
Point3D origin() const
Definition: Axes.cpp:167
Transformation transformation() const
Definition: Axes.cpp:178
DrawingElement & operator=(const DrawingElement &other)
SUEntityRef m_entity
The C SUEntityRef that this class wraps.
Definition: Entity.hpp:59
Axes()
Definition: Axes.cpp:74
Vector3D x_axis() const
Definition: Axes.cpp:136
Definition: Color.hpp:34
SUAxesRef ref() const
Definition: Axes.cpp:132
bool operator!() const
Definition: Axes.cpp:122