summaryrefslogtreecommitdiff
path: root/src/DNaming/DNaming_Line3DDriver.cxx
blob: e9be78474f12d58b88be27aa9e6428aa62110cbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// File:	DNaming_Line3DDriver.cxx
// Created:	Fri Feb 26 16:54:50 2010
// Author:	Sergey ZARITCHNY <sergey.zaritchny@opencascade.com> 
// Copyright:	Open CasCade SA 2010	


#include <DNaming_Line3DDriver.ixx>
#include <gp_Pnt.hxx>
#include <gp_Vec.hxx>
#include <gp_Trsf.hxx>
#include <Precision.hxx>
#include <Standard_GUID.hxx>
#include <BRepBuilderAPI_MakeWire.hxx>
#include <BRepBuilderAPI_MakeEdge.hxx>
#include <BRepAlgo.hxx>
#include <Geom_BSplineCurve.hxx>
#include <GeomAPI_Interpolate.hxx>
#include <BRep_Tool.hxx>
#include <BRep_Builder.hxx>
#include <TopLoc_Location.hxx>
#include <TopTools_HSequenceOfShape.hxx>
#include <TColgp_HArray1OfPnt.hxx>
#include <TopTools_Array1OfShape.hxx>
#include <TopoDS.hxx>
#include <TopoDS_Shape.hxx>
#include <TopoDS_Vertex.hxx>
#include <TopoDS_Wire.hxx>
#include <TopAbs.hxx>
#include <TopExp.hxx>
#include <TopExp_Explorer.hxx>
#include <TDF_Label.hxx>
#include <TDF_AttributeMap.hxx>
#include <TDF_MapIteratorOfAttributeMap.hxx>
#include <TDataStd_Real.hxx>
#include <TDataStd_Integer.hxx>
#include <TNaming.hxx>
#include <TNaming_Builder.hxx>
#include <TNaming_NamedShape.hxx>
#include <TFunction_Logbook.hxx>
#include <TFunction_Function.hxx>
#include <DNaming.hxx>
#include <ModelDefinitions.hxx>

#ifdef WNT
#define EXCEPTION ...
#else
#define EXCEPTION Standard_Failure
#endif
//#define MDTV_DEB 1

#ifdef MDTV_DEB
#include <TDF_Tool.hxx>
#include <BRepTools.hxx>
static void Write(const TopoDS_Shape& shape,
                      const Standard_CString filename)
{
  ofstream save;
  save.open(filename);
  save << "DBRep_DrawableShape" << endl << endl;
  if(!shape.IsNull()) BRepTools::Write(shape, save);
  save.close();
}
#endif

inline static Standard_GUID getLine3DNamingVersionID() {
  static Standard_GUID anID("BB74BAD5-7AAD-44b4-850F-05416F09B28E");
  return anID;
}
//=======================================================================
//function : DNaming_Line3DDriver
//purpose  : Constructor
//=======================================================================
DNaming_Line3DDriver::DNaming_Line3DDriver()
{}

//=======================================================================
//function : Validate
//purpose  : Validates labels of a function in <theLog>.
//=======================================================================
void DNaming_Line3DDriver::Validate(TFunction_Logbook& theLog) const
{}

//=======================================================================
//function : MustExecute
//purpose  : Analyse in <theLog> if the loaded function must be executed
//=======================================================================
Standard_Boolean DNaming_Line3DDriver::MustExecute(const TFunction_Logbook& theLog) const
{
  return Standard_True;
}

//=======================================================================
//function : Execute
//purpose  : Execute the function
//=======================================================================
Standard_Integer DNaming_Line3DDriver::Execute(TFunction_Logbook& theLog) const
{
  Handle(TFunction_Function) aFunction;
  Label().FindAttribute(TFunction_Function::GetID(),aFunction);
  if(aFunction.IsNull()) return -1;

  aFunction->SetFailure(NOTDONE);  
  Handle(TNaming_NamedShape) aPrevILine3D = DNaming::GetFunctionResult(aFunction);
// Save location
  TopLoc_Location aLocation;
  if (!aPrevILine3D.IsNull() && !aPrevILine3D->IsEmpty()) {
    aLocation = aPrevILine3D->Get().Location();
  }

  const Standard_Integer aType = DNaming::GetInteger(aFunction, LINE3D_TYPE)->Get();
  Standard_Boolean isClosed(aType);
  Standard_Integer aCounter(0), aLength  = DNaming::GetInteger(aFunction, LINE3D_PNTNB)->Get();
  if(aLength < 2) {
    aFunction->SetFailure(WRONG_ARGUMENT);
    return -1;
  }

  Handle(TNaming_NamedShape) aNS1, aNS2;
  BRepBuilderAPI_MakeWire aMakeWire;
  TopoDS_Wire aWire;
  TopoDS_Shape aShape1, aShape2;
  
  TopTools_Array1OfShape anArV(1,aLength); //aLength - number of points
  for(aCounter = 1; aCounter <= aLength-1; aCounter++) {       
    Handle(TDataStd_UAttribute) aRefP1 = DNaming::GetObjectArg(aFunction, (LINE3D_TYPE + aCounter));
    aNS1 = DNaming::GetObjectValue(aRefP1);
    Handle(TDataStd_UAttribute) aRefP2 = DNaming::GetObjectArg(aFunction, (LINE3D_TYPE + aCounter +1));
    aNS2 =  DNaming::GetObjectValue(aRefP2);
#ifdef MDTV_DEB 
    if(!aNS1->IsEmpty()) {
      aShape1 = aNS1->Get();
      gp_Pnt aDebPoint = BRep_Tool::Pnt(TopoDS::Vertex(aShape1));
      //	cout << aCounter << " X = " <<  aDebPoint.X() << " Y = " <<  aDebPoint.Y() << " Z = " <<  aDebPoint.Z() << endl;
    } else
      cout << " Line3DDriver:: NS1 is empty" << endl;
    if(!aNS2->IsEmpty()) {
      aShape2 = aNS2->Get();
      gp_Pnt aDebPoint = BRep_Tool::Pnt(TopoDS::Vertex(aShape2));
      //      cout << aCounter+1 << " X = " <<  aDebPoint.X() << " Y = " <<  aDebPoint.Y() << " Z = " <<  aDebPoint.Z() << endl;
    } else
      cout << " Line3DDriver:: NS2 is empty" << endl;
#endif
    if(aNS1->IsEmpty() || aNS2->IsEmpty() || 
       aNS1->Get().IsNull() || aNS2->Get().IsNull()) {
      aFunction->SetFailure(WRONG_ARGUMENT);
      return -1;
    }
    aShape1 = aNS1->Get();
    aShape2 = aNS2->Get();
#ifdef MDTV_DEB 
    gp_Pnt aDebPoint = BRep_Tool::Pnt(TopoDS::Vertex(aShape1));
    //      cout << aCounter << " X = " <<  aDebPoint.X() << " Y = " <<  aDebPoint.Y() << " Z = " <<  aDebPoint.Z() << endl;
    aDebPoint = BRep_Tool::Pnt(TopoDS::Vertex(aShape2));
    //      cout << aCounter+1 << " X = " <<  aDebPoint.X() << " Y = " <<  aDebPoint.Y() << " Z = " <<  aDebPoint.Z() << endl;
#endif 
    if(aShape1.ShapeType() != TopAbs_VERTEX || aShape2.ShapeType() != TopAbs_VERTEX) {
      aFunction->SetFailure(WRONG_ARGUMENT);
      return -1;
    }
    BRepBuilderAPI_MakeEdge aMakeEdge(TopoDS::Vertex(aShape1), TopoDS::Vertex(aShape2));
    if(aMakeEdge.IsDone()) {
      anArV.SetValue(aCounter,   aShape1);
      anArV.SetValue(aCounter+1, aShape2);
      aMakeWire.Add(aMakeEdge.Edge());
    }
  }
//  } else // closed
  if(isClosed) { 
    Handle(TDataStd_UAttribute) aRefP1 = DNaming::GetObjectArg(aFunction, (LINE3D_TYPE + 1));
    aNS1 = DNaming::GetObjectValue(aRefP1);
    aShape1 = aNS1->Get();
    BRepBuilderAPI_MakeEdge aMakeEdge(TopoDS::Vertex(aShape2), TopoDS::Vertex(aShape1));
    if(aMakeEdge.IsDone()) aMakeWire.Add(aMakeEdge.Edge());
  }
  if(aMakeWire.IsDone()) aWire = aMakeWire.Wire(); 
  

  if(aWire.IsNull()) {
    aFunction->SetFailure(ALGO_FAILED);
    return -1;
  }

  if (!BRepAlgo::IsValid(aWire)) {
    aFunction->SetFailure(RESULT_NOT_VALID);
    return -1;
  }

  TDF_Label aResultLabel = RESPOSITION(aFunction); //aFunction->GetResult()->Label();
  try {  
    LoadNamingDS(aResultLabel, aWire, anArV, isClosed);

  } catch (EXCEPTION) {
    aFunction->SetFailure(NAMING_FAILED);
    return -1;
  }

  // restore location
  if(!aLocation.IsIdentity())
    TNaming::Displace(aResultLabel, aLocation, Standard_True);

  theLog.SetValid(aResultLabel, Standard_True);  

  aFunction->SetFailure(DONE);
  return 0;
}

//=======================================================================
//function : LoadNamingDS
//purpose  : Loads a Line3D in a data framework
//=======================================================================
void DNaming_Line3DDriver::LoadNamingDS (const TDF_Label& theResultLabel, 
					 const TopoDS_Wire& theWire, 
					 const TopTools_Array1OfShape& theArV,
					 const Standard_Boolean isClosed) const
{
  if(theWire.IsNull()) return;
//Wire
  TNaming_Builder aWBuilder(theResultLabel);
  aWBuilder.Generated(theWire);
#ifdef MDTV_DEB
  TDataStd_Name::Set(theResultLabel, "Line3DCurve");
#endif
  Standard_Integer aLength = theArV.Length();
  if(aLength < 2) return;
  TopoDS_Shape aShape;
  TopTools_Array1OfShape anArE(1,aLength);
  TopoDS_Vertex aFirst, aLast;
  for(Standard_Integer i = 1;i < aLength;i++) {
    gp_Pnt aP1  = BRep_Tool::Pnt(TopoDS::Vertex(theArV.Value(i)));
    gp_Pnt aP2  = BRep_Tool::Pnt(TopoDS::Vertex(theArV.Value(i+1)));
    Standard_Boolean aFound = Standard_False;
    TopExp_Explorer anExp(theWire,TopAbs_EDGE);
    for(;anExp.More();anExp.Next()) {
      const TopoDS_Edge& anE = TopoDS::Edge(anExp.Current());
      TopoDS_Vertex aV, aV1, aV2;
      TopExp::Vertices(anE, aV1, aV2);
      gp_Pnt aPE1 = BRep_Tool::Pnt(aV1);
      gp_Pnt aPE2 = BRep_Tool::Pnt(aV2);
      if(aP1.IsEqual(aPE1, Precision::Confusion()) && aP2.IsEqual(aPE2, Precision::Confusion())) {
        anArE.SetValue(i, anE);
        aFound = Standard_True;
        break;
      }
    }
    if(!aFound)
      anArE.SetValue(i, aShape);
    else aFound = Standard_False;
  }
  if(isClosed) {
    Standard_Boolean aFound = Standard_False;
    gp_Pnt aP1  = BRep_Tool::Pnt(TopoDS::Vertex(theArV.Value(aLength)));
    gp_Pnt aP2  = BRep_Tool::Pnt(TopoDS::Vertex(theArV.Value(1)));
    TopExp_Explorer anExp(theWire,TopAbs_EDGE);
    for(;anExp.More();anExp.Next()) {
      const TopoDS_Edge& anE = TopoDS::Edge(anExp.Current());
      TopoDS_Vertex aV, aV1, aV2;
      TopExp::Vertices(anE, aV1, aV2);
      gp_Pnt aPE1 = BRep_Tool::Pnt(aV1);
      gp_Pnt aPE2 = BRep_Tool::Pnt(aV2);
      if(aP1.IsEqual(aPE1, Precision::Confusion()) && aP2.IsEqual(aPE2, Precision::Confusion())) {
        anArE.SetValue(aLength, anE);
        aFound = Standard_True;
     // closed case
        aFirst = aV2;
        aLast  = aV1;
        break;
      }
    }
    if(!aFound)
      anArE.SetValue(aLength, aShape);
  } else {
    // open
    anArE.SetValue(aLength, aShape); // last edge
    TopExp::Vertices(theWire, aFirst, aLast);
  }

  // put edges
  // from 1:aLength - edges
  for(Standard_Integer i1 = 1;i1 <= aLength ;i1++) {
    TDF_Label aLab = theResultLabel.FindChild(i1);
    if(!anArE.Value(i1).IsNull()) {
      TNaming_Builder aBuilder(aLab);
      aBuilder.Generated(anArE.Value(i1));
    } else {
      Handle(TNaming_NamedShape) aNS;
      if(aLab.FindAttribute(TNaming_NamedShape::GetID(), aNS))
        TNaming_Builder aB(aLab);
    }
  }
  //put vertexes
  // aLength +1 - first vertex
  // aLength +2 - last vertex
  TDF_Label aLab1 = theResultLabel.FindChild(aLength+1);
  TDF_Label aLab2 = theResultLabel.FindChild(aLength+2);
  if(!aFirst.IsNull()) {
    TNaming_Builder aBuilder(aLab1);
    aBuilder.Generated(aFirst);
  } else {
    Handle(TNaming_NamedShape) aNS;
    if(aLab1.FindAttribute(TNaming_NamedShape::GetID(), aNS))
      TNaming_Builder aB(aLab1);
  }
  if(!aLast.IsNull()) {
    TNaming_Builder aBuilder(aLab2);
    aBuilder.Generated(aLast);
  } else {
    Handle(TNaming_NamedShape) aNS;
    if(aLab2.FindAttribute(TNaming_NamedShape::GetID(), aNS))
      TNaming_Builder aB(aLab2);
  }
}