SUAPI-CppWrapper
C++WrapperforSketchUpCAPI
Group.cpp
1 //
2 // Group.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/Group.hpp"
32 
33 #include <cassert>
34 
35 #include "SUAPI-CppWrapper/String.hpp"
36 #include "SUAPI-CppWrapper/model/ComponentDefinition.hpp"
37 #include "SUAPI-CppWrapper/model/Model.hpp"
38 #include "SUAPI-CppWrapper/model/ComponentInstance.hpp"
39 #include "SUAPI-CppWrapper/model/Entities.hpp"
40 #include "SUAPI-CppWrapper/Transformation.hpp"
41 
42 
43 namespace CW {
44 
45 /**************************
46 * Private static methods **
47 ***************************/
48 SUGroupRef Group::create_group() {
49  SUGroupRef group_ref = SU_INVALID;
50  SUResult res = SUGroupCreate(&group_ref);
51  assert(res == SU_ERROR_NONE); _unused(res);
52  return group_ref;
53 }
54 
55 
56 SUGroupRef Group::copy_reference(const Group& other) {
57  if (other.m_attached) {
58  return other.ref();
59  }
60  // Groups cannot exist without a parent in the C API, so must always be attached
61  assert(false);
62  // The other group has not been attached to the model, so copy its properties to a new object
63  Group new_group;
64  new_group.transformation(other.transformation());
65  new_group.name(other.name());
66  new_group.entities().add(other.entities());
67  return new_group.ref();
68 }
69 
70 
72  Group(SU_INVALID, true)
73 {}
74 
75 
76 Group::Group(SUGroupRef group, bool attached):
77  ComponentInstance(SUGroupToComponentInstance(group), true)
78 {}
79 
81  Group(SUGroupFromEntity(instance.m_entity))
82 {
83  assert(instance.entity_type() == SURefType_Group);
84 }
85 
86 /** Copy constructor */
87 Group::Group(const Group& other):
88  ComponentInstance(other, SUGroupToComponentInstance(copy_reference(other)))
89 {}
90 
91 
93  if (!m_attached && SUIsValid(m_entity)) {
94  // There is no release function - we assume that the parent class ComponentInstance takes care of releasing.
95  }
96 }
97 
98 
99 Group& Group::operator=(const Group& other) {
100  // SUGroupRef does not have a release function, as groups must be added to a parent as soon as it is created. Therefore, we can assume that groups are always attached, and we are simply copying the reference to it.
101  assert(m_attached);
102  m_entity = other.m_entity;
103  return *this;
104 }
105 
106 
107 SUGroupRef Group::ref() const {
108  return SUGroupFromEntity(m_entity);
109 }
110 
111 
112 Group::operator SUGroupRef() const {
113  return ref();
114 }
115 
116 
118  if(!(*this)) {
119  throw std::logic_error("CW::Group::definition(): Group is null");
120  }
121  SUComponentDefinitionRef def_ref = SU_INVALID;
122  SUResult res = SUGroupGetDefinition(this->ref(), &def_ref);
123  assert(res == SU_ERROR_NONE); _unused(res);
124  return ComponentDefinition(def_ref);
125 }
126 
127 
129  if(!(*this)) {
130  throw std::logic_error("CW::Group::entities(): Group is null");
131  }
132  SUEntitiesRef entities = SU_INVALID;
133  SUGroupGetEntities(this->ref(), &entities);
134  return Entities(entities, this->model().ref());
135 }
136 
137 
139  if(!(*this)) {
140  throw std::logic_error("CW::Group::name(): Group is null");
141  }
142  String string;
143  SUStringRef * const string_ref = string;
144  SUResult res = SUGroupGetName(this->ref(), string_ref);
145  assert(res == SU_ERROR_NONE); _unused(res);
146  return string;
147 }
148 
149 
150 void Group::name(const String& string) {
151  if(!(*this)) {
152  throw std::logic_error("CW::Group::name(): Group is null");
153  }
154  std::string name_string = string.std_string();
155  SUResult res = SUGroupSetName(this->ref(), name_string.c_str());
156  assert(res == SU_ERROR_NONE); _unused(res);
157 }
158 
159 
161  if(!(*this)) {
162  throw std::logic_error("CW::Group::transformation(): Group is null");
163  }
164  SUTransformation transform{};
165  SUGroupGetTransform(this->ref(), &transform);
166  return Transformation(transform);
167 }
168 
169 
170 void Group::transformation(const Transformation& transform) {
171  if(!(*this)) {
172  throw std::logic_error("CW::Group::transformation(): Group is null");
173  }
174  SUTransformation transform_ref = transform.ref();
175  SUResult res = SUGroupSetTransform(this->ref(), &transform_ref);
176  assert(res == SU_ERROR_NONE); _unused(res);
177 }
178 
179 } /* namespace CW */
bool m_attached
Indicates whether the Entity has been attached to a model.
Definition: Entity.hpp:64
SUGroupRef ref() const
Definition: Group.cpp:107
bool attached() const
Returns true if the entity is attached to another object.
Definition: Entity.cpp:100
enum SURefType entity_type() const
Returns the type of the entity. See enum SURefType.
Definition: Entity.cpp:234
ComponentDefinition definition() const
Definition: Group.cpp:117
Group()
Definition: Group.cpp:71
Model model() const
Definition: Entity.cpp:241
SUEntityRef m_entity
The C SUEntityRef that this class wraps.
Definition: Entity.hpp:59
Group & operator=(const Group &other)
Definition: Group.cpp:99
Transformation transformation() const
Definition: Group.cpp:160
String name() const
Definition: Group.cpp:138
~Group()
Definition: Group.cpp:92
Definition: Color.hpp:34
Entities entities() const
Definition: Group.cpp:128