1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include <Standard_Stream.hxx>
21 #include <GEOMImpl_3DSketcherDriver.hxx>
22 #include <GEOMImpl_I3DSketcher.hxx>
23 #include <GEOMImpl_Types.hxx>
24 #include <GEOM_Function.hxx>
26 #include <GEOMImpl_IMeasureOperations.hxx>
28 #include <Basics_Utils.hxx>
31 #include <BRepBuilderAPI_MakePolygon.hxx>
32 #include <BRepBuilderAPI_MakeVertex.hxx>
33 #include <BRepBuilderAPI_MakeEdge.hxx>
34 #include <BRepBuilderAPI_MakeWire.hxx>
36 #include <TopoDS_Shape.hxx>
37 #include <TopoDS_Wire.hxx>
40 #include <Standard_ConstructionError.hxx>
42 //=======================================================================
45 //=======================================================================
46 const Standard_GUID& GEOMImpl_3DSketcherDriver::GetID()
48 static Standard_GUID a3DSketcherDriver("FF2BBB54-5D24-4df3-210B-3A678263EA26");
49 return a3DSketcherDriver;
53 //=======================================================================
54 //function : GEOMImpl_3DSketcherDriver
56 //=======================================================================
57 GEOMImpl_3DSketcherDriver::GEOMImpl_3DSketcherDriver()
61 //=======================================================================
64 //=======================================================================
65 Standard_Integer GEOMImpl_3DSketcherDriver::Execute(TFunction_Logbook& log) const
67 if (Label().IsNull()) return 0;
68 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
70 GEOMImpl_I3DSketcher aCI (aFunction);
74 if (aFunction->GetType() == SKETCHER3D_COORDS) {
75 Handle(TColStd_HArray1OfReal) aCoordsArray = aCI.GetCoordinates();
76 int anArrayLength = aCoordsArray->Length();
78 std::list<gp_Pnt> points;
80 for (int i = 0; i <= (anArrayLength-3); i += 3) {
81 gp_Pnt aPnt = gp_Pnt(aCoordsArray->Value(i+1), aCoordsArray->Value(i+2), aCoordsArray->Value(i+3));
82 if (points.empty() || aPnt.Distance(points.back()) > gp::Resolution())
83 points.push_back(aPnt);
86 if ( points.size() == 1) { // Only Start Point
87 BRepBuilderAPI_MakeVertex mkVertex (points.back());
88 aShape = mkVertex.Shape();
90 else if ( points.size() > 1) { // Make Wire
91 BRepBuilderAPI_MakePolygon aMakePoly;
92 std::list<gp_Pnt>::iterator it;
93 for (it = points.begin(); it != points.end(); ++it) {
97 if (points.size() > 2 &&
98 points.back().X() == points.front().X() &&
99 points.back().Y() == points.front().Y() &&
100 points.back().Z() == points.front().Z())
103 if (aMakePoly.IsDone())
104 aShape = aMakePoly.Wire();
107 else if (aFunction->GetType() == SKETCHER3D_COMMAND) {
108 Kernel_Utils::Localizer loc;
110 TCollection_AsciiString aCommand = aCI.GetCommand();
111 // "3DSketcher:CMD[:CMD[:CMD...]]"
113 // Split the command string to separate CMDs
115 TColStd_SequenceOfAsciiString aSequence;
116 if (aCommand.Length()) {
117 TCollection_AsciiString aToken = aCommand.Token(":", icmd);
118 while (aToken.Length() > 0) {
119 aSequence.Append(aToken);
120 aToken = aCommand.Token(":", ++icmd);
125 bool isFirstPointSet = false;
126 gp_XYZ p = gp::Origin().XYZ();
127 BRepBuilderAPI_MakeVertex MV0 (p);
128 TopoDS_Vertex V = TopoDS::Vertex(MV0.Shape());
131 TopoDS_Vertex V0 = V;
133 bool doClose = false;
134 BRepBuilderAPI_MakeWire MW;
136 int nbCMDs = aSequence.Length();
137 for (icmd = 1; icmd <= nbCMDs; icmd++) {
138 TCollection_AsciiString aCMD = aSequence.Value(icmd);
140 // Split the CMD into string values
141 TColStd_SequenceOfAsciiString aStrVals;
143 TCollection_AsciiString aToken = aCMD.Token(" ", ival);
144 while (aToken.Length() > 0) {
145 aStrVals.Append(aToken);
146 aToken = aCMD.Token(" ", ++ival);
149 // "TT x y z" : Create segment by point at X & Y or set the first point
150 // "T dx dy dz" : Create segment by point with DX & DY
152 // "OXY angleX angle2 length" : Create segment by two angles and length
153 // "OYZ angleY angle2 length" : Create segment by two angles and length
154 // "OXZ angleX angle2 length" : Create segment by two angles and length
156 // "WW" : Close Wire (to finish)
158 switch (aStrVals.Value(1).Value(1))
162 if (aStrVals.Length() != 4)
163 Standard_ConstructionError::Raise("3D Sketcher error: Bad format of command.");
166 vp.SetX(aStrVals.Value(2).RealValue());
167 vp.SetY(aStrVals.Value(3).RealValue());
168 vp.SetZ(aStrVals.Value(4).RealValue());
170 if (aStrVals.Value(1) == "TT") { // absolute coordinates
171 if (!isFirstPointSet) {
173 BRepBuilderAPI_MakeVertex MV (p);
174 V = TopoDS::Vertex(MV.Shape());
179 isFirstPointSet = true;
182 if ((vp - p).SquareModulus() > Precision::Confusion()) {
183 BRepBuilderAPI_MakeVertex MV (vp);
184 TopoDS_Vertex VV = TopoDS::Vertex(MV.Shape());
185 BRepBuilderAPI_MakeEdge ME (V, VV);
194 else if (aStrVals.Value(1) == "T") { // relative coordinates
195 if (vp.SquareModulus() > Precision::Confusion()) {
198 BRepBuilderAPI_MakeVertex MV (vp);
199 TopoDS_Vertex VV = TopoDS::Vertex(MV.Shape());
200 BRepBuilderAPI_MakeEdge ME (V, VV);
209 Standard_ConstructionError::Raise("3D Sketcher error: Bad format of command.");
214 if (aStrVals.Length() != 4)
215 Standard_ConstructionError::Raise("3D Sketcher error: Bad format of command.");
217 double anAngle = aStrVals.Value(2).RealValue() * M_PI/180.0;
218 double anAngle2 = aStrVals.Value(3).RealValue() * M_PI/180.0;
219 double aLength = aStrVals.Value(4).RealValue();
221 double aProjectedLength = aLength * cos(anAngle2);
224 vp.SetX(aStrVals.Value(2).RealValue());
225 vp.SetY(aStrVals.Value(3).RealValue());
226 vp.SetZ(aStrVals.Value(4).RealValue());
228 if (aStrVals.Value(1) == "OXY") {
229 vp.SetX(p.X() + aProjectedLength * cos(anAngle));
230 vp.SetY(p.Y() + aProjectedLength * sin(anAngle));
231 vp.SetZ(p.Z() + aLength * sin(anAngle2));
233 else if (aStrVals.Value(1) == "OYZ") {
234 vp.SetX(p.X() + aLength * sin(anAngle2));
235 vp.SetY(p.Y() + aProjectedLength * cos(anAngle));
236 vp.SetZ(p.Z() + aProjectedLength * sin(anAngle));
238 else if (aStrVals.Value(1) == "OXZ") {
239 vp.SetX(p.X() + aProjectedLength * cos(anAngle));
240 vp.SetY(p.Y() + aLength * sin(anAngle2));
241 vp.SetZ(p.Z() + aProjectedLength * sin(anAngle));
244 Standard_ConstructionError::Raise("3D Sketcher error: Bad format of command.");
246 BRepBuilderAPI_MakeVertex MV (vp);
247 TopoDS_Vertex VV = TopoDS::Vertex(MV.Shape());
248 BRepBuilderAPI_MakeEdge ME (V, VV);
258 if (aStrVals.Length() != 1)
259 Standard_ConstructionError::Raise("3D Sketcher error: Bad format of command.");
261 if (aStrVals.Value(1) == "WW")
264 Standard_ConstructionError::Raise("3D Sketcher error: Bad format of command.");
269 Standard_ConstructionError::Raise("3D Sketcher error: Bad format of command.");
275 nbEdges > 1 && // as 3D sketcher has only straight edges
276 (p - p0).SquareModulus() > Precision::Confusion()) {
277 BRepBuilderAPI_MakeEdge ME (V, V0);
284 Standard_ConstructionError::Raise("3D Sketcher error: Wire construction failed.");
289 if (isFirstPointSet) {
297 if (aShape.IsNull()) return 0;
299 aFunction->SetValue(aShape);
300 log.SetTouched(Label());
305 //=======================================================================
306 //function : GEOMImpl_3DSketcherDriver_Type_
308 //=======================================================================
309 Standard_EXPORT Handle_Standard_Type& GEOMImpl_3DSketcherDriver_Type_()
312 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
313 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
314 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
315 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
316 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
317 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
320 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
321 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_3DSketcherDriver",
322 sizeof(GEOMImpl_3DSketcherDriver),
324 (Standard_Address)_Ancestors,
325 (Standard_Address)NULL);
330 //=======================================================================
331 //function : DownCast
333 //=======================================================================
334 const Handle(GEOMImpl_3DSketcherDriver) Handle(GEOMImpl_3DSketcherDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
336 Handle(GEOMImpl_3DSketcherDriver) _anOtherObject;
338 if (!AnObject.IsNull()) {
339 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_3DSketcherDriver))) {
340 _anOtherObject = Handle(GEOMImpl_3DSketcherDriver)((Handle(GEOMImpl_3DSketcherDriver)&)AnObject);
344 return _anOtherObject ;