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