SUAPI-CppWrapper
C++WrapperforSketchUpCAPI
InstancePath.cpp
1 //
2 // InstancePath.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/InstancePath.hpp"
32 
33 #include "SUAPI-CppWrapper/model/ComponentInstance.hpp"
34 #include "SUAPI-CppWrapper/model/Entity.hpp"
35 #include "SUAPI-CppWrapper/Transformation.hpp"
36 #include "SUAPI-CppWrapper/String.hpp"
37 
38 #include <cassert>
39 
40 namespace CW {
41 
42 SUInstancePathRef InstancePath::create_instance_path() {
43  SUInstancePathRef instance_path_ref = SU_INVALID;
44  SUResult res = SUInstancePathCreate(&instance_path_ref);
45  assert(res == SU_ERROR_NONE); _unused(res);
46  return instance_path_ref;
47 }
48 
49 
50 SUInstancePathRef InstancePath::copy_reference(const InstancePath& other) {
51  SUInstancePathRef instance_path_ref = SU_INVALID;
52  SUResult res = SUInstancePathCreateCopy(&instance_path_ref, other.ref());
53  assert(res == SU_ERROR_NONE); _unused(res);
54  return instance_path_ref;
55 }
56 
57 
59  m_instance_path(create_instance_path())
60 {}
61 
62 
63 InstancePath::InstancePath(SUInstancePathRef instance_path):
64  m_instance_path(instance_path)
65 {}
66 
67 
69  InstancePath(copy_reference(other))
70 {}
71 
72 
74  SUResult res = SUInstancePathRelease(&m_instance_path);
75  assert(res == SU_ERROR_NONE); _unused(res);
76 }
77 
78 
80  SUResult res = SUInstancePathRelease(&m_instance_path);
81  assert(res == SU_ERROR_NONE); _unused(res);
82  m_instance_path = copy_reference(other);
83  return *this;
84 }
85 
86 
87 SUInstancePathRef InstancePath::ref() const {
88  return m_instance_path;
89 }
90 
91 
92 InstancePath::operator SUInstancePathRef() const {
93  return this->ref();
94 }
95 
96 
97 InstancePath::operator SUInstancePathRef*() {
98  return &m_instance_path;
99 }
100 
101 
103  SUResult res = SUInstancePathPushInstance(m_instance_path, instance.ref());
104  assert(res == SU_ERROR_NONE); _unused(res);
105  return *this;
106 }
107 
108 
110  SUResult res = SUInstancePathPopInstance(m_instance_path);
111  assert(res == SU_ERROR_NONE); _unused(res);
112  return *this;
113 }
114 
115 
117  SUResult res = SUInstancePathSetLeaf(m_instance_path, entity.ref());
118  assert(res == SU_ERROR_NONE); _unused(res);
119  return *this;
120 }
121 
122 
123 size_t InstancePath::depth() const {
124  size_t depth = 0;
125  SUResult res = SUInstancePathGetPathDepth(m_instance_path, &depth);
126  assert(res == SU_ERROR_NONE); _unused(res);
127  return depth;
128 }
129 
130 
131 size_t InstancePath::full_depth() const {
132  size_t depth = 0;
133  SUResult res = SUInstancePathGetFullDepth(m_instance_path, &depth);
134  assert(res == SU_ERROR_NONE); _unused(res);
135  return depth;
136 }
137 
138 
140  SUTransformation transform;
141  SUResult res = SUInstancePathGetTransform(m_instance_path, &transform);
142  assert(res == SU_ERROR_NONE); _unused(res);
143  return Transformation(transform);
144 }
145 
146 
148  SUTransformation transform;
149  SUResult res = SUInstancePathGetTransformAtDepth(m_instance_path, depth, &transform);
150  assert(res == SU_ERROR_NONE); _unused(res);
151  return Transformation(transform);
152 }
153 
154 
156  assert(this->valid());
157  SUComponentInstanceRef instance = SU_INVALID;
158  SUResult res = SUInstancePathGetInstanceAtDepth(m_instance_path, depth, &instance);
159  assert(res == SU_ERROR_NONE); _unused(res);
160  return ComponentInstance(instance);
161 }
162 
163 
165  SUEntityRef entity = SU_INVALID;
166  SUResult res = SUInstancePathGetLeafAsEntity(m_instance_path, &entity);
167  assert(res == SU_ERROR_NONE); _unused(res);
168  return Entity(entity);
169 }
170 
171 
173  SUDrawingElementRef element = SU_INVALID;
174  SUResult res = SUInstancePathGetLeaf(m_instance_path, &element);
175  assert(res == SU_ERROR_NONE); _unused(res);
176  return DrawingElement(element);
177 }
178 
179 
180 bool InstancePath::valid() const {
181  bool valid;
182  SUResult res = SUInstancePathIsValid(m_instance_path, &valid);
183  assert(res == SU_ERROR_NONE); _unused(res);
184  return valid;
185 }
186 
187 
188 bool InstancePath::empty() const {
189  bool empty;
190  SUResult res = SUInstancePathIsEmpty(m_instance_path, &empty);
191  assert(res == SU_ERROR_NONE); _unused(res);
192  return empty;
193 }
194 
195 
196 bool InstancePath::contains(const Entity& entity) const {
197  bool contains;
198  SUResult res = SUInstancePathContains(m_instance_path, entity.ref(), &contains);
199  assert(res == SU_ERROR_NONE); _unused(res);
200  return contains;
201 }
202 
203 
205  SUStringRef pid = SU_INVALID;
206  SUResult res = SUStringCreate(&pid);
207  assert(res == SU_ERROR_NONE);
208  res = SUInstancePathGetPersistentID(m_instance_path, &pid);
209  assert(res == SU_ERROR_NONE); _unused(res);
210  return String(pid);
211 }
212 
213 
215  SUStringRef pid = SU_INVALID;
216  SUResult res = SUStringCreate(&pid);
217  assert(res == SU_ERROR_NONE);
218  res = SUInstancePathGetPersistentIDAtDepth(m_instance_path, depth, &pid);
219  assert(res == SU_ERROR_NONE); _unused(res);
220  return String(pid);
221 }
222 
223 
224 } /* namespace CW */
225 
size_t depth() const
String persistent_id_at_depth(size_t depth) const
InstancePath & set_leaf(const Entity &entity)
Transformation total_transformation() const
bool empty() const
bool contains(const Entity &entity) const
InstancePath & operator=(const InstancePath &other)
size_t full_depth() const
Transformation transformation_at_depth(size_t depth) const
InstancePath & push(const ComponentInstance &instance)
ComponentInstance instance_at_depth(size_t depth) const
String persistent_id() const
Definition: Color.hpp:34
DrawingElement leaf() const
SUComponentInstanceRef ref() const
InstancePath & pop()
Entity leaf_entity() const
bool valid() const