Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/geom.git] / src / GEOMImpl / GEOMImpl_3DSketcherDriver.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <Standard_Stream.hxx>
21
22 #include <GEOMImpl_3DSketcherDriver.hxx>
23 #include <GEOMImpl_I3DSketcher.hxx>
24 #include <GEOMImpl_Types.hxx>
25 #include <GEOM_Function.hxx>
26
27 #include <GEOMImpl_IMeasureOperations.hxx>
28
29 // OCCT Includes
30 #include <BRepBuilderAPI_MakePolygon.hxx>
31 #include <BRepBuilderAPI_MakeVertex.hxx>
32 #include <TopoDS.hxx>
33 #include <TopoDS_Shape.hxx>
34 #include <TopoDS_Wire.hxx>
35 #include <gp_Pnt.hxx>
36
37 #include <Standard_ConstructionError.hxx>
38
39 //=======================================================================
40 //function : GetID
41 //purpose  :
42 //=======================================================================
43 const Standard_GUID& GEOMImpl_3DSketcherDriver::GetID()
44 {
45   static Standard_GUID a3DSketcherDriver("FF2BBB54-5D24-4df3-210B-3A678263EA26");
46   return a3DSketcherDriver;
47 }
48
49
50 //=======================================================================
51 //function : GEOMImpl_3DSketcherDriver
52 //purpose  :
53 //=======================================================================
54 GEOMImpl_3DSketcherDriver::GEOMImpl_3DSketcherDriver()
55 {
56 }
57
58 //=======================================================================
59 //function : Execute
60 //purpose  :
61 //=======================================================================
62 Standard_Integer GEOMImpl_3DSketcherDriver::Execute(TFunction_Logbook& log) const
63 {
64   if (Label().IsNull()) return 0;
65   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
66   
67   GEOMImpl_I3DSketcher aCI (aFunction);
68
69   TopoDS_Shape aShape;
70
71   Handle(TColStd_HArray1OfReal) aCoordsArray = aCI.GetCoordinates();
72   int anArrayLength = aCoordsArray->Length();
73
74   std::list<gp_Pnt> points;
75   
76   for (int i = 0; i <= (anArrayLength-3); i += 3) {
77     gp_Pnt aPnt = gp_Pnt(aCoordsArray->Value(i+1), aCoordsArray->Value(i+2), aCoordsArray->Value(i+3));
78     if (points.empty() || aPnt.Distance(points.back()) > gp::Resolution())
79       points.push_back(aPnt);
80   }
81
82   if ( points.size() == 1) { // Only Start Point
83     BRepBuilderAPI_MakeVertex mkVertex (points.back());
84     aShape = mkVertex.Shape();
85   }
86   else if ( points.size() > 1) { // Make Wire
87     BRepBuilderAPI_MakePolygon aMakePoly;
88     std::list<gp_Pnt>::iterator it;
89     for (it = points.begin(); it != points.end(); ++it) {
90       aMakePoly.Add(*it);
91     }
92
93     if (points.size() > 2 && 
94         points.back().X() == points.front().X() && 
95         points.back().Y() == points.front().Y() && 
96         points.back().Z() == points.front().Z())
97       aMakePoly.Close();
98     
99     if (aMakePoly.IsDone())
100       aShape = aMakePoly.Wire();
101   }
102
103   if (aShape.IsNull()) return 0;
104
105   aFunction->SetValue(aShape);
106   log.SetTouched(Label());
107   return 1;
108 }
109
110
111 //=======================================================================
112 //function :  GEOMImpl_3DSketcherDriver_Type_
113 //purpose  :
114 //=======================================================================
115 Standard_EXPORT Handle_Standard_Type& GEOMImpl_3DSketcherDriver_Type_()
116 {
117
118   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
119   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
120   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
121   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
122   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
123   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
124
125
126   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
127   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_3DSketcherDriver",
128                                                          sizeof(GEOMImpl_3DSketcherDriver),
129                                                          1,
130                                                          (Standard_Address)_Ancestors,
131                                                          (Standard_Address)NULL);
132
133   return _aType;
134 }
135
136 //=======================================================================
137 //function : DownCast
138 //purpose  :
139 //=======================================================================
140 const Handle(GEOMImpl_3DSketcherDriver) Handle(GEOMImpl_3DSketcherDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
141 {
142   Handle(GEOMImpl_3DSketcherDriver) _anOtherObject;
143
144   if (!AnObject.IsNull()) {
145      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_3DSketcherDriver))) {
146        _anOtherObject = Handle(GEOMImpl_3DSketcherDriver)((Handle(GEOMImpl_3DSketcherDriver)&)AnObject);
147      }
148   }
149
150   return _anOtherObject ;
151 }