Salome HOME
Merge from V5_1_4_BR 07/05/2010
[modules/geom.git] / src / GEOMImpl / GEOMImpl_3DSketcherDriver.cxx
1 //  Copyright (C) 2007-2010  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
73   BRepBuilderAPI_MakePolygon aMakePoly;
74   int anArrayLength = aCoordsArray->Length();
75   double x, y, z;
76   gp_Pnt aPnt;
77   for (int i = 0; i <=(anArrayLength - 3); i+=3) {
78     x = aCoordsArray->Value(i+1);
79     y = aCoordsArray->Value(i+2);
80     z = aCoordsArray->Value(i+3);
81     aPnt = gp_Pnt(x, y, z);
82     aMakePoly.Add(aPnt);
83   }
84   if ( anArrayLength == 3) { // Only Start Point
85     BRepBuilderAPI_MakeVertex mkVertex (aPnt);
86     aShape = mkVertex.Shape();
87   }
88   else { // Make Wire
89     if (aCoordsArray->Value(1) == x && aCoordsArray->Value(2) == y && aCoordsArray->Value(3) == z)
90       aMakePoly.Close();
91     
92     if (aMakePoly.IsDone())
93       aShape = aMakePoly.Wire();
94   }
95
96   if (aShape.IsNull()) return 0;
97
98   aFunction->SetValue(aShape);
99   log.SetTouched(Label());
100   return 1;
101 }
102
103
104 //=======================================================================
105 //function :  GEOMImpl_3DSketcherDriver_Type_
106 //purpose  :
107 //=======================================================================
108 Standard_EXPORT Handle_Standard_Type& GEOMImpl_3DSketcherDriver_Type_()
109 {
110
111   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
112   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
113   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
114   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
115   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
116   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
117
118
119   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
120   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_3DSketcherDriver",
121                                                          sizeof(GEOMImpl_3DSketcherDriver),
122                                                          1,
123                                                          (Standard_Address)_Ancestors,
124                                                          (Standard_Address)NULL);
125
126   return _aType;
127 }
128
129 //=======================================================================
130 //function : DownCast
131 //purpose  :
132 //=======================================================================
133 const Handle(GEOMImpl_3DSketcherDriver) Handle(GEOMImpl_3DSketcherDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
134 {
135   Handle(GEOMImpl_3DSketcherDriver) _anOtherObject;
136
137   if (!AnObject.IsNull()) {
138      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_3DSketcherDriver))) {
139        _anOtherObject = Handle(GEOMImpl_3DSketcherDriver)((Handle(GEOMImpl_3DSketcherDriver)&)AnObject);
140      }
141   }
142
143   return _anOtherObject ;
144 }