PageRenderTime 95ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/brlcad/branches/bullet/src/conv/step/Representation.cpp

https://bitbucket.org/vrrm/brl-cad-copy-for-fast-history-browsing-in-git
C++ | 280 lines | 181 code | 45 blank | 54 comment | 47 complexity | 9b9e58ee3c786428c3763bef56e6ec62 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, LGPL-2.1, Apache-2.0, AGPL-3.0, LGPL-3.0, GPL-3.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, 0BSD, BSD-3-Clause
  1. /* Representation.cpp
  2. * BRL-CAD
  3. *
  4. * Copyright (c) 1994-2012 United States Government as represented by
  5. * the U.S. Army Research Laboratory.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public License
  9. * version 2.1 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this file; see the file named COPYING for more
  18. * information.
  19. */
  20. /** @file step/Representation.cpp
  21. *
  22. * Routines to convert STEP "Representation" to BRL-CAD BREP
  23. * structures.
  24. *
  25. */
  26. /* interface header */
  27. #include "./Representation.h"
  28. /* implementation headers */
  29. #include "STEPWrapper.h"
  30. #include "Factory.h"
  31. #include "ManifoldSolidBrep.h"
  32. #include "GeometricRepresentationContext.h"
  33. #include "GlobalUncertaintyAssignedContext.h"
  34. #include "GlobalUnitAssignedContext.h"
  35. #include "ParametricRepresentationContext.h"
  36. #define BUFSIZE 255 // used for buffer size that stringifies step ID
  37. #define CLASSNAME "Representation"
  38. #define ENTITYNAME "Representation"
  39. string Representation::entityname = Factory::RegisterClass(ENTITYNAME, (FactoryMethod)Representation::Create);
  40. Representation::Representation() {
  41. step = NULL;
  42. id = 0;
  43. }
  44. Representation::Representation(STEPWrapper *sw, int step_id) {
  45. step = sw;
  46. id = step_id;
  47. }
  48. Representation::~Representation() {
  49. /*
  50. LIST_OF_REPRESENTATION_ITEMS::iterator i = items.begin();
  51. while (i != items.end()) {
  52. delete (*i);
  53. i = items.erase(i);
  54. }
  55. LIST_OF_REPRESENTATION_CONTEXT::iterator ic = context_of_items.begin();
  56. while (ic != context_of_items.end()) {
  57. delete (*ic);
  58. ic = context_of_items.erase(ic);
  59. }
  60. */
  61. items.clear();
  62. if (context_of_items.size() > 1) {
  63. LIST_OF_REPRESENTATION_CONTEXT::iterator ic = context_of_items.begin();
  64. while (ic != context_of_items.end()) {
  65. delete (*ic);
  66. ic = context_of_items.erase(ic);
  67. }
  68. } else {
  69. context_of_items.clear();
  70. }
  71. }
  72. double
  73. Representation::GetLengthConversionFactor() {
  74. LIST_OF_REPRESENTATION_CONTEXT::iterator i = context_of_items.begin();
  75. while (i != context_of_items.end()) {
  76. GlobalUnitAssignedContext *aGUAC = dynamic_cast<GlobalUnitAssignedContext *>(*i);
  77. if (aGUAC != NULL) {
  78. return aGUAC->GetLengthConversionFactor();
  79. }
  80. i++;
  81. }
  82. return 1000.0; // assume base of meters
  83. }
  84. double
  85. Representation::GetPlaneAngleConversionFactor() {
  86. LIST_OF_REPRESENTATION_CONTEXT::iterator i = context_of_items.begin();
  87. while (i != context_of_items.end()) {
  88. GlobalUnitAssignedContext *aGUAC = dynamic_cast<GlobalUnitAssignedContext *>(*i);
  89. if (aGUAC != NULL) {
  90. return aGUAC->GetPlaneAngleConversionFactor();
  91. }
  92. i++;
  93. }
  94. return 1.0; // assume base of radians
  95. }
  96. double
  97. Representation::GetSolidAngleConversionFactor() {
  98. LIST_OF_REPRESENTATION_CONTEXT::iterator i = context_of_items.begin();
  99. while (i != context_of_items.end()) {
  100. GlobalUnitAssignedContext *aGUAC = dynamic_cast<GlobalUnitAssignedContext *>(*i);
  101. if (aGUAC != NULL) {
  102. return aGUAC->GetSolidAngleConversionFactor();
  103. }
  104. i++;
  105. }
  106. return 1.0; // assume base of steradians
  107. }
  108. bool Representation::Load(STEPWrapper *sw, SDAI_Application_instance *sse)
  109. {
  110. step = sw;
  111. id = sse->STEPfile_id;
  112. // need to do this for local attributes to makes sure we have
  113. // the actual entity and not a complex/supertype parent
  114. sse = step->getEntity(sse, ENTITYNAME);
  115. name = step->getStringAttribute(sse, "name");
  116. if (items.empty()) {
  117. LIST_OF_ENTITIES *l = step->getListOfEntities(sse, "items");
  118. LIST_OF_ENTITIES::iterator i;
  119. for (i = l->begin(); i != l->end(); i++) {
  120. SDAI_Application_instance *entity = (*i);
  121. if (entity) {
  122. RepresentationItem *aRI = dynamic_cast<RepresentationItem *>(Factory::CreateObject(sw, entity));
  123. if (aRI != NULL)
  124. items.push_back(aRI);
  125. } else {
  126. std::cerr << CLASSNAME << ": Unhandled entity in attribute 'items'." << std::endl;
  127. l->clear();
  128. delete l;
  129. return false;
  130. }
  131. }
  132. l->clear();
  133. delete l;
  134. }
  135. if (context_of_items.empty()) {
  136. SDAI_Application_instance *entity = step->getEntityAttribute(sse, "context_of_items");
  137. if (entity) {
  138. if (entity->IsComplex()) {
  139. SDAI_Application_instance *sub_entity = step->getEntity(entity, "Geometric_Representation_Context");
  140. if (sub_entity) {
  141. GeometricRepresentationContext *aGRC = new GeometricRepresentationContext();
  142. context_of_items.push_back(aGRC);
  143. if (!aGRC->Load(step, sub_entity)) {
  144. std::cout << CLASSNAME << ":Error loading GeometricRepresentationContext" << std::endl;
  145. return false;
  146. }
  147. }
  148. sub_entity = step->getEntity(entity, "Global_Uncertainty_Assigned_Context");
  149. if (sub_entity) {
  150. GlobalUncertaintyAssignedContext *aGUAC = new GlobalUncertaintyAssignedContext();
  151. context_of_items.push_back(aGUAC);
  152. if (!aGUAC->Load(step, sub_entity)) {
  153. std::cout << CLASSNAME << ":Error loading GlobalUncertaintyAssignedContext" << std::endl;
  154. return false;
  155. }
  156. }
  157. sub_entity = step->getEntity(entity, "Global_Unit_Assigned_Context");
  158. if (sub_entity) {
  159. GlobalUnitAssignedContext *aGUAC = new GlobalUnitAssignedContext();
  160. context_of_items.push_back(aGUAC);
  161. if (!aGUAC->Load(step, sub_entity)) {
  162. std::cout << CLASSNAME << ":Error loading GlobalUnitAssignedContext" << std::endl;
  163. return false;
  164. }
  165. }
  166. sub_entity = step->getEntity(entity, "Parametric_Representation_Context");
  167. if (sub_entity) {
  168. ParametricRepresentationContext *aPRC = new ParametricRepresentationContext();
  169. context_of_items.push_back(aPRC);
  170. if (!aPRC->Load(step, sub_entity)) {
  171. std::cout << CLASSNAME << ":Error loading ParametricRepresentationContext" << std::endl;
  172. return false;
  173. }
  174. }
  175. } else {
  176. RepresentationContext *aRC = dynamic_cast<RepresentationContext *>(Factory::CreateObject(sw, entity));
  177. if (aRC != NULL) {
  178. context_of_items.push_back(aRC);
  179. } else {
  180. std::cout << CLASSNAME << ":Error loading RepresentationContext" << std::endl;
  181. return false;
  182. }
  183. }
  184. } else {
  185. std::cout << CLASSNAME << ":Error loading \"context_of_items\"" << std::endl;
  186. return false;
  187. }
  188. }
  189. return true;
  190. }
  191. void
  192. Representation::Print(int level) {
  193. TAB(level); std::cout << CLASSNAME << ":" << name << "(";
  194. std::cout << "ID:" << STEPid() << ")" << std::endl;
  195. TAB(level+1); std::cout << "items:" << std::endl;
  196. LIST_OF_REPRESENTATION_ITEMS::iterator i;
  197. for (i=items.begin(); i != items.end(); ++i) {
  198. //ManifoldSolidBrep *msb = (ManifoldSolidBrep *)(*i);
  199. (*i)->Print(level+1);
  200. //(*i)->LoadONBrep((ON_Brep *)NULL);
  201. (*i)->Print(level+1);
  202. }
  203. TAB(level+1); std::cout << "context_of_items:" << std::endl;
  204. LIST_OF_REPRESENTATION_CONTEXT::iterator ic;
  205. for (ic=context_of_items.begin(); ic != context_of_items.end(); ++ic) {
  206. (*ic)->Print(level+1);
  207. }
  208. }
  209. STEPEntity *
  210. Representation::Create(STEPWrapper *sw, SDAI_Application_instance *sse) {
  211. Factory::OBJECTS::iterator i;
  212. if ((i = Factory::FindObject(sse->STEPfile_id)) == Factory::objects.end()) {
  213. Representation *object = new Representation(sw, sse->STEPfile_id);
  214. Factory::AddObject(object);
  215. if (!object->Load(sw, sse)) {
  216. std::cerr << CLASSNAME << ":Error loading class in ::Create() method." << std::endl;
  217. delete object;
  218. return NULL;
  219. }
  220. return static_cast<STEPEntity *>(object);
  221. } else {
  222. return (*i).second;
  223. }
  224. }
  225. // Local Variables:
  226. // tab-width: 8
  227. // mode: C++
  228. // c-basic-offset: 4
  229. // indent-tabs-mode: t
  230. // c-file-style: "stroustrup"
  231. // End:
  232. // ex: shiftwidth=4 tabstop=8