/src/contrib/geom-5.1.2.7/src/GEOMImpl/GEOMImpl_ShapeDriver.cpp

http://pythonocc.googlecode.com/ · C++ · 541 lines · 397 code · 73 blank · 71 comment · 118 complexity · d9490569230978092a31063e6c0a2a36 MD5 · raw file

  1. // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
  2. //
  3. // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
  4. // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
  5. //
  6. // This library is free software; you can redistribute it and/or
  7. // modify it under the terms of the GNU Lesser General Public
  8. // License as published by the Free Software Foundation; either
  9. // version 2.1 of the License.
  10. //
  11. // This library is distributed in the hope that it will be useful,
  12. // but 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 library; if not, write to the Free Software
  18. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. //
  20. // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
  21. #include <GEOMImpl_ShapeDriver.hxx>
  22. #include <GEOMImpl_IShapes.hxx>
  23. #include <GEOMImpl_Types.hxx>
  24. #include <GEOMImpl_Block6Explorer.hxx>
  25. #include <GEOM_Function.hxx>
  26. #include <GEOM_Object.hxx>
  27. // OCCT Includes
  28. #include <ShapeFix_Wire.hxx>
  29. #include <ShapeFix_Edge.hxx>
  30. #include <BRep_Tool.hxx>
  31. #include <BRep_Builder.hxx>
  32. #include <BRepAlgo_FaceRestrictor.hxx>
  33. #include <BRepBuilderAPI_Sewing.hxx>
  34. #include <BRepBuilderAPI_Copy.hxx>
  35. #include <BRepCheck.hxx>
  36. #include <BRepCheck_Analyzer.hxx>
  37. #include <BRepCheck_Shell.hxx>
  38. #include <BRepClass3d_SolidClassifier.hxx>
  39. #include <BRepBuilderAPI_MakeWire.hxx>
  40. #include <BRepBuilderAPI_Sewing.hxx>
  41. #include <ShapeAnalysis_FreeBounds.hxx>
  42. #include <TopAbs.hxx>
  43. #include <TopoDS.hxx>
  44. #include <TopoDS_Shape.hxx>
  45. #include <TopoDS_Edge.hxx>
  46. #include <TopoDS_Wire.hxx>
  47. #include <TopoDS_Shell.hxx>
  48. #include <TopoDS_Solid.hxx>
  49. #include <TopoDS_Compound.hxx>
  50. #include <TopoDS_Iterator.hxx>
  51. #include <TopExp_Explorer.hxx>
  52. #include <TopTools_MapOfShape.hxx>
  53. #include <TopTools_HSequenceOfShape.hxx>
  54. #include <TColStd_HSequenceOfTransient.hxx>
  55. #include <Precision.hxx>
  56. #include <Standard_NullObject.hxx>
  57. #include <Standard_TypeMismatch.hxx>
  58. #include <Standard_ConstructionError.hxx>
  59. #include "utilities.h"
  60. //=======================================================================
  61. //function : GetID
  62. //purpose :
  63. //=======================================================================
  64. const Standard_GUID& GEOMImpl_ShapeDriver::GetID()
  65. {
  66. static Standard_GUID aShapeDriver("FF1BBB54-5D14-4df2-980B-3A668264EA16");
  67. return aShapeDriver;
  68. }
  69. //=======================================================================
  70. //function : GEOMImpl_ShapeDriver
  71. //purpose :
  72. //=======================================================================
  73. GEOMImpl_ShapeDriver::GEOMImpl_ShapeDriver()
  74. {
  75. }
  76. //=======================================================================
  77. //function : Execute
  78. //purpose :
  79. //=======================================================================
  80. Standard_Integer GEOMImpl_ShapeDriver::Execute(TFunction_Logbook& log) const
  81. {
  82. if (Label().IsNull()) return 0;
  83. Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
  84. GEOMImpl_IShapes aCI (aFunction);
  85. Standard_Integer aType = aFunction->GetType();
  86. TopoDS_Shape aShape;
  87. BRep_Builder B;
  88. if (aType == WIRE_EDGES) {
  89. Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
  90. unsigned int ind, nbshapes = aShapes->Length();
  91. TopoDS_Wire aWire;
  92. B.MakeWire(aWire);
  93. BRepBuilderAPI_MakeWire MW;
  94. bool isMWDone = true;
  95. // add edges
  96. for (ind = 1; ind <= nbshapes; ind++) {
  97. Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
  98. TopoDS_Shape aShape_i = aRefShape->GetValue();
  99. if (aShape_i.IsNull()) {
  100. Standard_NullObject::Raise("Shape for wire construction is null");
  101. }
  102. if (aShape_i.ShapeType() == TopAbs_EDGE) {
  103. B.Add(aWire, TopoDS::Edge(aShape_i));
  104. MW.Add(TopoDS::Edge(aShape_i));
  105. if (!MW.IsDone()) {
  106. // check status after each edge/wire addition, because the final status
  107. // can be OK even in case, when some edges/wires was not accepted.
  108. isMWDone = false;
  109. }
  110. } else if (aShape_i.ShapeType() == TopAbs_WIRE) {
  111. TopExp_Explorer exp (aShape_i, TopAbs_EDGE);
  112. for (; exp.More(); exp.Next()) {
  113. B.Add(aWire, TopoDS::Edge(exp.Current()));
  114. MW.Add(TopoDS::Edge(exp.Current()));
  115. if (!MW.IsDone()) {
  116. // check status after each edge/wire addition, because the final status
  117. // can be OK even in case, when some edges/wires was not accepted.
  118. isMWDone = false;
  119. }
  120. }
  121. } else {
  122. Standard_TypeMismatch::Raise
  123. ("Shape for wire construction is neither an edge nor a wire");
  124. }
  125. }
  126. if (isMWDone) {
  127. aShape = MW;
  128. } else {
  129. // fix edges order
  130. Handle(ShapeFix_Wire) aFW = new ShapeFix_Wire;
  131. aFW->Load(aWire);
  132. aFW->FixReorder();
  133. if (aFW->StatusReorder(ShapeExtend_FAIL1)) {
  134. Standard_ConstructionError::Raise("Wire construction failed: several loops detected");
  135. } else if (aFW->StatusReorder(ShapeExtend_FAIL)) {
  136. Standard_ConstructionError::Raise("Wire construction failed");
  137. //} else if (aFW->StatusReorder(ShapeExtend_DONE2)) {
  138. // Standard_ConstructionError::Raise("Wire construction failed: some gaps detected");
  139. } else {
  140. }
  141. // IMP 0019766: Building a Wire from unconnected edges by introducing a tolerance
  142. Standard_Real aTolerance = aCI.GetTolerance();
  143. if (aTolerance < Precision::Confusion())
  144. aTolerance = Precision::Confusion();
  145. aFW->ClosedWireMode() = Standard_False;
  146. aFW->FixConnected(aTolerance);
  147. if (aFW->StatusConnected(ShapeExtend_FAIL)) {
  148. Standard_ConstructionError::Raise("Wire construction failed: cannot build connected wire");
  149. }
  150. // IMP 0019766
  151. if (aFW->StatusConnected(ShapeExtend_DONE3)) {
  152. // Confused with <prec> but not Analyzer.Precision(), set the same
  153. aFW->FixGapsByRangesMode() = Standard_True;
  154. if (aFW->FixGaps3d()) {
  155. Handle(ShapeExtend_WireData) sbwd = aFW->WireData();
  156. Handle(ShapeFix_Edge) aFe = new ShapeFix_Edge;
  157. for (Standard_Integer iedge = 1; iedge <= sbwd->NbEdges(); iedge++) {
  158. TopoDS_Edge aEdge = TopoDS::Edge(sbwd->Edge(iedge));
  159. aFe->FixVertexTolerance(aEdge);
  160. aFe->FixSameParameter(aEdge);
  161. }
  162. }
  163. else if (aFW->StatusGaps3d(ShapeExtend_FAIL)) {
  164. Standard_ConstructionError::Raise("Wire construction failed: cannot fix 3d gaps");
  165. }
  166. }
  167. aShape = aFW->WireAPIMake();
  168. }
  169. }
  170. else if (aType == FACE_WIRE) {
  171. Handle(GEOM_Function) aRefBase = aCI.GetBase();
  172. TopoDS_Shape aShapeBase = aRefBase->GetValue();
  173. if (aShapeBase.IsNull()) Standard_NullObject::Raise("Argument Shape is null");
  174. TopoDS_Wire W;
  175. if (aShapeBase.ShapeType() == TopAbs_WIRE) {
  176. W = TopoDS::Wire(aShapeBase);
  177. }
  178. else if (aShapeBase.ShapeType() == TopAbs_EDGE && aShapeBase.Closed()) {
  179. BRepBuilderAPI_MakeWire MW;
  180. MW.Add(TopoDS::Edge(aShapeBase));
  181. if (!MW.IsDone()) {
  182. Standard_ConstructionError::Raise("Wire construction failed");
  183. }
  184. W = MW;
  185. }
  186. else {
  187. Standard_NullObject::Raise
  188. ("Shape for face construction is neither a wire nor a closed edge");
  189. }
  190. GEOMImpl_Block6Explorer::MakeFace(W, aCI.GetIsPlanar(), aShape);
  191. if (aShape.IsNull()) {
  192. Standard_ConstructionError::Raise("Face construction failed");
  193. }
  194. }
  195. else if (aType == FACE_WIRES) {
  196. // Try to build a face from a set of wires and edges
  197. int ind;
  198. Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
  199. int nbshapes = aShapes->Length();
  200. if (nbshapes < 1) {
  201. Standard_ConstructionError::Raise("No wires or edges given");
  202. }
  203. // 1. Extract all edges from the given arguments
  204. TopTools_MapOfShape aMapEdges;
  205. Handle(TopTools_HSequenceOfShape) aSeqEdgesIn = new TopTools_HSequenceOfShape;
  206. BRep_Builder B;
  207. for (ind = 1; ind <= nbshapes; ind++) {
  208. Handle(GEOM_Function) aRefSh_i = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
  209. TopoDS_Shape aSh_i = aRefSh_i->GetValue();
  210. TopExp_Explorer anExpE_i (aSh_i, TopAbs_EDGE);
  211. for (; anExpE_i.More(); anExpE_i.Next()) {
  212. if (aMapEdges.Add(anExpE_i.Current())) {
  213. aSeqEdgesIn->Append(anExpE_i.Current());
  214. }
  215. }
  216. }
  217. // 2. Connect edges to wires of maximum length
  218. Handle(TopTools_HSequenceOfShape) aSeqWiresOut;
  219. ShapeAnalysis_FreeBounds::ConnectEdgesToWires(aSeqEdgesIn, Precision::Confusion(),
  220. /*shared*/Standard_False, aSeqWiresOut);
  221. // 3. Separate closed wires
  222. Handle(TopTools_HSequenceOfShape) aSeqClosedWires = new TopTools_HSequenceOfShape;
  223. Handle(TopTools_HSequenceOfShape) aSeqOpenWires = new TopTools_HSequenceOfShape;
  224. for (ind = 1; ind <= aSeqWiresOut->Length(); ind++) {
  225. if (aSeqWiresOut->Value(ind).Closed())
  226. aSeqClosedWires->Append(aSeqWiresOut->Value(ind));
  227. else
  228. aSeqOpenWires->Append(aSeqWiresOut->Value(ind));
  229. }
  230. if (aSeqClosedWires->Length() < 1) {
  231. Standard_ConstructionError::Raise
  232. ("There is no closed contour can be built from the given arguments");
  233. }
  234. // 4. Build a face / list of faces from all the obtained closed wires
  235. // 4.a. Basic face
  236. TopoDS_Shape aFFace;
  237. TopoDS_Wire aW1 = TopoDS::Wire(aSeqClosedWires->Value(1));
  238. GEOMImpl_Block6Explorer::MakeFace(aW1, aCI.GetIsPlanar(), aFFace);
  239. if (aFFace.IsNull()) {
  240. Standard_ConstructionError::Raise("Face construction failed");
  241. }
  242. // 4.b. Add other wires
  243. if (aSeqClosedWires->Length() == 1) {
  244. aShape = aFFace;
  245. }
  246. else {
  247. TopoDS_Compound C;
  248. BRep_Builder aBuilder;
  249. aBuilder.MakeCompound(C);
  250. BRepAlgo_FaceRestrictor FR;
  251. TopAbs_Orientation OriF = aFFace.Orientation();
  252. TopoDS_Shape aLocalS = aFFace.Oriented(TopAbs_FORWARD);
  253. FR.Init(TopoDS::Face(aLocalS), Standard_False, Standard_True);
  254. for (ind = 1; ind <= aSeqClosedWires->Length(); ind++) {
  255. TopoDS_Wire aW = TopoDS::Wire(aSeqClosedWires->Value(ind));
  256. FR.Add(aW);
  257. }
  258. FR.Perform();
  259. if (FR.IsDone()) {
  260. int k = 0;
  261. TopoDS_Shape aFace;
  262. for (; FR.More(); FR.Next()) {
  263. aFace = FR.Current().Oriented(OriF);
  264. aBuilder.Add(C, aFace);
  265. k++;
  266. }
  267. if (k == 1) {
  268. aShape = aFace;
  269. } else {
  270. aShape = C;
  271. }
  272. }
  273. }
  274. // 5. Add all open wires to the result
  275. if (aSeqOpenWires->Length() > 0) {
  276. //Standard_ConstructionError::Raise("There are some open wires");
  277. TopoDS_Compound C;
  278. BRep_Builder aBuilder;
  279. if (aSeqClosedWires->Length() == 1) {
  280. aBuilder.MakeCompound(C);
  281. aBuilder.Add(C, aShape);
  282. }
  283. else {
  284. C = TopoDS::Compound(aShape);
  285. }
  286. for (ind = 1; ind <= aSeqOpenWires->Length(); ind++) {
  287. aBuilder.Add(C, aSeqOpenWires->Value(ind));
  288. }
  289. aShape = C;
  290. }
  291. }
  292. else if (aType == SHELL_FACES) {
  293. Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
  294. unsigned int ind, nbshapes = aShapes->Length();
  295. // add faces
  296. BRepBuilderAPI_Sewing aSewing(Precision::Confusion()*10.0);
  297. for (ind = 1; ind <= nbshapes; ind++) {
  298. Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
  299. TopoDS_Shape aShape_i = aRefShape->GetValue();
  300. if (aShape_i.IsNull()) {
  301. Standard_NullObject::Raise("Face for shell construction is null");
  302. }
  303. aSewing.Add(aShape_i);
  304. }
  305. aSewing.Perform();
  306. TopoDS_Shape sh = aSewing.SewedShape();
  307. if( sh.ShapeType()==TopAbs_FACE && nbshapes==1 ) {
  308. // case for creation of shell from one face - PAL12722 (skl 26.06.2006)
  309. TopoDS_Shell ss;
  310. B.MakeShell(ss);
  311. B.Add(ss,sh);
  312. aShape = ss;
  313. }
  314. else {
  315. //TopExp_Explorer exp (aSewing.SewedShape(), TopAbs_SHELL);
  316. TopExp_Explorer exp (sh, TopAbs_SHELL);
  317. Standard_Integer ish = 0;
  318. for (; exp.More(); exp.Next()) {
  319. aShape = exp.Current();
  320. ish++;
  321. }
  322. if (ish != 1)
  323. aShape = aSewing.SewedShape();
  324. }
  325. }
  326. else if (aType == SOLID_SHELL) {
  327. Handle(GEOM_Function) aRefShell = aCI.GetBase();
  328. TopoDS_Shape aShapeShell = aRefShell->GetValue();
  329. if (aShapeShell.IsNull() || aShapeShell.ShapeType() != TopAbs_SHELL) {
  330. Standard_NullObject::Raise("Shape for solid construction is null or not a shell");
  331. }
  332. BRepCheck_Shell chkShell(TopoDS::Shell(aShapeShell));
  333. if(chkShell.Closed() == BRepCheck_NotClosed) return 0;
  334. TopoDS_Solid Sol;
  335. B.MakeSolid(Sol);
  336. B.Add(Sol, aShapeShell);
  337. BRepClass3d_SolidClassifier SC (Sol);
  338. SC.PerformInfinitePoint(Precision::Confusion());
  339. if (SC.State() == TopAbs_IN) {
  340. B.MakeSolid(Sol);
  341. B.Add(Sol, aShapeShell.Reversed());
  342. }
  343. aShape = Sol;
  344. }
  345. else if (aType == SOLID_SHELLS) {
  346. Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
  347. unsigned int ind, nbshapes = aShapes->Length();
  348. Standard_Integer ish = 0;
  349. TopoDS_Solid Sol;
  350. B.MakeSolid(Sol);
  351. // add shapes
  352. for (ind = 1; ind <= nbshapes; ind++) {
  353. Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
  354. TopoDS_Shape aShapeShell = aRefShape->GetValue();
  355. if (aShapeShell.IsNull()) {
  356. Standard_NullObject::Raise("Shell for solid construction is null");
  357. }
  358. if (aShapeShell.ShapeType() == TopAbs_SHELL) {
  359. B.Add(Sol, aShapeShell);
  360. ish++;
  361. }
  362. }
  363. if ( ish == 0 ) return 0;
  364. BRepClass3d_SolidClassifier SC (Sol);
  365. SC.PerformInfinitePoint(Precision::Confusion());
  366. switch (SC.State()) {
  367. case TopAbs_IN:
  368. aShape = Sol.Reversed(); break;
  369. case TopAbs_OUT:
  370. aShape = Sol; break;
  371. default: // not closed shell?
  372. return 0;
  373. }
  374. }
  375. else if (aType == COMPOUND_SHAPES) {
  376. Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
  377. unsigned int ind, nbshapes = aShapes->Length();
  378. // add shapes
  379. TopoDS_Compound C;
  380. B.MakeCompound(C);
  381. for (ind = 1; ind <= nbshapes; ind++) {
  382. Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
  383. TopoDS_Shape aShape_i = aRefShape->GetValue();
  384. if (aShape_i.IsNull()) {
  385. Standard_NullObject::Raise("Shape for compound construction is null");
  386. }
  387. B.Add(C, aShape_i);
  388. }
  389. aShape = C;
  390. }
  391. else if (aType == REVERSE_ORIENTATION) {
  392. Handle(GEOM_Function) aRefShape = aCI.GetBase();
  393. TopoDS_Shape aShape_i = aRefShape->GetValue();
  394. if (aShape_i.IsNull()) {
  395. Standard_NullObject::Raise("Shape for reverse is null");
  396. }
  397. BRepBuilderAPI_Copy Copy(aShape_i);
  398. if( Copy.IsDone() ) {
  399. TopoDS_Shape tds = Copy.Shape();
  400. if( tds.IsNull() ) {
  401. Standard_ConstructionError::Raise("Orientation aborted : Can not reverse the shape");
  402. }
  403. if( tds.Orientation() == TopAbs_FORWARD)
  404. tds.Orientation(TopAbs_REVERSED);
  405. else
  406. tds.Orientation(TopAbs_FORWARD);
  407. aShape = tds;
  408. }
  409. }
  410. if (aShape.IsNull()) return 0;
  411. // Check shape validity
  412. BRepCheck_Analyzer ana (aShape, false);
  413. if (!ana.IsValid()) {
  414. Standard_CString anErrStr("Algorithm has produced an invalid shape result");
  415. #ifdef THROW_ON_INVALID_SH
  416. Standard_ConstructionError::Raise(anErrStr);
  417. #else
  418. MESSAGE(anErrStr);
  419. //further processing can be performed here
  420. //...
  421. //in case of failure of automatic treatment
  422. //mark the corresponding GEOM_Object as problematic
  423. TDF_Label aLabel = aFunction->GetOwnerEntry();
  424. if (!aLabel.IsRoot()) {
  425. Handle(GEOM_Object) aMainObj = GEOM_Object::GetObject(aLabel);
  426. if (!aMainObj.IsNull())
  427. aMainObj->SetDirty(Standard_True);
  428. }
  429. #endif
  430. }
  431. aFunction->SetValue(aShape);
  432. log.SetTouched(Label());
  433. return 1;
  434. }
  435. //=======================================================================
  436. //function : GEOMImpl_ShapeDriver_Type_
  437. //purpose :
  438. //=======================================================================
  439. Standard_EXPORT Handle_Standard_Type& GEOMImpl_ShapeDriver_Type_()
  440. {
  441. static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
  442. if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
  443. static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
  444. if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
  445. static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
  446. if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
  447. static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
  448. static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_ShapeDriver",
  449. sizeof(GEOMImpl_ShapeDriver),
  450. 1,
  451. (Standard_Address)_Ancestors,
  452. (Standard_Address)NULL);
  453. return _aType;
  454. }
  455. //=======================================================================
  456. //function : DownCast
  457. //purpose :
  458. //=======================================================================
  459. const Handle(GEOMImpl_ShapeDriver) Handle(GEOMImpl_ShapeDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
  460. {
  461. Handle(GEOMImpl_ShapeDriver) _anOtherObject;
  462. if (!AnObject.IsNull()) {
  463. if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_ShapeDriver))) {
  464. _anOtherObject = Handle(GEOMImpl_ShapeDriver)((Handle(GEOMImpl_ShapeDriver)&)AnObject);
  465. }
  466. }
  467. return _anOtherObject;
  468. }