summaryrefslogtreecommitdiff
path: root/src/DNaming/DNaming_TransformationDriver.cxx
blob: e9848ac1cc2a4c85ddf33bbfc4d616c6017c59fe (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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
// File:	DNaming_TransformationDriver.cxx
// Created:	Thu May  7 11:55:58 2009
// Author:	Sergey ZARITCHNY <sergey.zaritchny@opencascade.com>
// Copyright:	Open CasCade SA 2009 

#include <DNaming_TransformationDriver.ixx>
#include <Standard_NullObject.hxx>
#include <TopAbs.hxx>
#include <TopoDS.hxx>
#include <TopoDS_Shape.hxx>
#include <TopoDS_Edge.hxx>
#include <TopoDS_Vertex.hxx>
#include <TopoDS_Compound.hxx>
#include <TopoDS_Iterator.hxx>
#include <TopExp.hxx>
#include <TopExp_Explorer.hxx>
#include <BRep_Builder.hxx>
#include <BRepBuilderAPI_Transform.hxx>
#include <BRep_Tool.hxx>
#include <BRepBuilderAPI_MakeShape.hxx>
#include <TopTools_MapOfShape.hxx>
#include <TopTools_DataMapOfShapeShape.hxx>
#include <TopTools_MapIteratorOfMapOfShape.hxx>
#include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
#include <TopTools_DataMapOfShapeInteger.hxx>
#include <TopTools_DataMapIteratorOfDataMapOfShapeInteger.hxx>
#include <TNaming_Builder.hxx>
#include <TNaming_NamedShape.hxx>
#include <TNaming_Tool.hxx>
#include <TFunction_Function.hxx>
#include <TFunction_Logbook.hxx>
#include <DNaming.hxx>
#include <TDataStd_Real.hxx>
#include <gp_Vec.hxx>
#include <gp_Ax1.hxx>
#include <gp_Ax2.hxx>
#include <gp_Pln.hxx>
#include <GeomLib_IsPlanarSurface.hxx>


#include <ModelDefinitions.hxx>

#ifdef WNT
#define EXCEPTION ...
#else
#define EXCEPTION Standard_Failure
#endif 

#define FACES_TAG  1
#define EDGES_TAG  2
#define VERTEX_TAG 3

//#define MDTV_DEB_TRSF
#ifdef  MDTV_DEB_TRSF
#include <TCollection_AsciiString.hxx>
#include <BRepTools.hxx>
#include <TDF_Tool.hxx>
void PrintE(const TDF_Label&       label)
{
  TCollection_AsciiString entry;
  TDF_Tool::Entry(label, entry);
  cout << "LabelEntry = "<< entry << endl;
}
#endif
//=======================================================================
//function : TransformationDriver
//purpose  : Constructor
//=======================================================================
DNaming_TransformationDriver::DNaming_TransformationDriver()
{}

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

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


//=======================================================================
//function : Execute
//purpose  : Execute the function and push in <log> the impacted labels
//=======================================================================
Standard_Integer DNaming_TransformationDriver::Execute(TFunction_Logbook& theLog) const
{
  Handle(TFunction_Function) aFunction;
  Label().FindAttribute(TFunction_Function::GetID(),aFunction);
  if(aFunction.IsNull()) return -1;

  Handle(TFunction_Function) aPrevFun = DNaming::GetPrevFunction(aFunction);
  if(aPrevFun.IsNull()) return -1;
  const TDF_Label& aLab = RESPOSITION(aPrevFun);
  Handle(TNaming_NamedShape) aContextNS;
  aLab.FindAttribute(TNaming_NamedShape::GetID(), aContextNS);
  if (aContextNS.IsNull() || aContextNS->IsEmpty()) {
#ifdef DEB
    cout<<"TransformationDriver:: Context is empty"<<endl;
#endif
    aFunction->SetFailure(WRONG_CONTEXT);
    return -1;
  }
//
  gp_Trsf aTransformation;
  const Standard_GUID& aGUID = aFunction->GetDriverGUID();
  
  try {
    if(aGUID == PTXYZ_GUID) {
      Standard_Real aDX = DNaming::GetReal(aFunction,PTRANSF_DX)->Get();
      Standard_Real aDY = DNaming::GetReal(aFunction,PTRANSF_DY)->Get();
      Standard_Real aDZ = DNaming::GetReal(aFunction,PTRANSF_DZ)->Get();  
      gp_Vec aVector(aDX, aDY, aDZ);
      aTransformation.SetTranslation(aVector);      
    }  
    else if(aGUID == PTALINE_GUID) {
      Handle(TDataStd_UAttribute) aLineObj  = DNaming::GetObjectArg(aFunction, PTRANSF_LINE);
      Handle(TNaming_NamedShape) aLineNS = DNaming::GetObjectValue(aLineObj);
      gp_Ax1 anAxis;
      if(!DNaming::ComputeAxis(aLineNS, anAxis)) Standard_Failure::Raise();
      gp_Vec aVector(anAxis.Direction());
      aVector.Normalize();
      Standard_Real anOffset = DNaming::GetReal(aFunction,PTRANSF_OFF)->Get();
      aVector *= anOffset;
      aTransformation.SetTranslation(aVector);   

    }  else if(aGUID == PRRLINE_GUID) {
      Handle(TDataStd_UAttribute) aLineObj  = DNaming::GetObjectArg(aFunction, PTRANSF_LINE);
      Handle(TNaming_NamedShape) aLineNS = DNaming::GetObjectValue(aLineObj);
      gp_Ax1 anAxis;
      if(!DNaming::ComputeAxis(aLineNS, anAxis)) Standard_Failure::Raise();

      Standard_Real anAngle = DNaming::GetReal(aFunction,PTRANSF_ANG)->Get();
      aTransformation.SetRotation(anAxis, anAngle);
    }  else if(aGUID == PMIRR_GUID) {
      Handle(TDataStd_UAttribute) aPlaneObj  = DNaming::GetObjectArg(aFunction, PTRANSF_PLANE);
      Handle(TNaming_NamedShape) aNS = DNaming::GetObjectValue(aPlaneObj);
      
      if(aNS.IsNull() ||  aNS->IsEmpty() || aNS->Get().IsNull() || 
	 aNS->Get().ShapeType() != TopAbs_FACE) Standard_Failure::Raise();
      TopoDS_Face aFace = TopoDS::Face(aNS->Get());
      Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
      GeomLib_IsPlanarSurface isPlanarSurface (aSurf);
      if(!isPlanarSurface.IsPlanar()) Standard_Failure::Raise();
      gp_Pln aPlane = isPlanarSurface.Plan();
      gp_Ax2 aMirrorAx2 = aPlane.Position().Ax2();
      aTransformation.SetMirror(aMirrorAx2);
    } else {
      aFunction->SetFailure(UNSUPPORTED_FUNCTION);
      return -1;
    }
  } catch (EXCEPTION) {
    aFunction->SetFailure(WRONG_ARGUMENT);
    return -1;
  }
//

// Naming
  LoadNamingDS(RESPOSITION(aFunction), aContextNS, aTransformation);

  theLog.SetValid(RESPOSITION(aFunction),Standard_True);  
  aFunction->SetFailure(DONE);
  return 0;
}
//=================================================================================
static void BuildMap(const TopTools_MapOfShape& SMap,
 		     BRepBuilderAPI_Transform& Transformer,
 		     TopTools_DataMapOfShapeShape& M)
{
  TopTools_MapIteratorOfMapOfShape anIt(SMap);
  for(;anIt.More();anIt.Next()) {
    if(!anIt.Key().IsNull()) {
      const TopoDS_Shape& aS = anIt.Key(); 
      M.Bind(aS,Transformer.ModifiedShape(aS));
    }
  } 
}
//=================================================================================
static void CollectShapes(const TopoDS_Shape& SSh, TopoDS_Compound& C, 
			  TopTools_MapOfShape& SMap, const TDF_Label& theLab,
			  TopTools_DataMapOfShapeInteger& TagMap, const Standard_Boolean isPrimitive)
{
  const TopAbs_ShapeEnum aType = SSh.ShapeType();
  BRep_Builder aB;
  switch(aType) {
  case TopAbs_COMPOUND:
    {
      TopoDS_Iterator it(SSh);
      for(;it.More();it.Next()) 
	CollectShapes(it.Value(), C, SMap, theLab, TagMap,isPrimitive);
    }
    break;
  case TopAbs_COMPSOLID:
  case TopAbs_SOLID:
  case TopAbs_SHELL:
    {
      TopExp_Explorer anEx(SSh, TopAbs_FACE);
      for(;anEx.More();anEx.Next()) {
	const Handle(TNaming_NamedShape) aNS =  TNaming_Tool::NamedShape(anEx.Current(), theLab);
	if(aNS.IsNull()) continue;
	if(SMap.Add(anEx.Current())) {
	  aB.Add(C,anEx.Current());
	  if(isPrimitive)
	    TagMap.Bind(anEx.Current(), aNS->Label().Tag());
	}
      }
      anEx.Init(SSh, TopAbs_EDGE); 
      for(;anEx.More();anEx.Next()) {
	const Handle(TNaming_NamedShape) aNS =  TNaming_Tool::NamedShape(anEx.Current(), theLab);
	if(aNS.IsNull()) continue;
	if(SMap.Add(anEx.Current())) {
	  aB.Add(C,anEx.Current());
	  if(isPrimitive)
	    TagMap.Bind(anEx.Current(), aNS->Label().Tag());
	}
      }
      anEx.Init(SSh, TopAbs_VERTEX); 
      for(;anEx.More();anEx.Next()) {
	const Handle(TNaming_NamedShape) aNS =  TNaming_Tool::NamedShape(anEx.Current(), theLab);
	if(aNS.IsNull()) continue;
	if(SMap.Add(anEx.Current())) {
	  aB.Add(C,anEx.Current());
	  if(isPrimitive)
	    TagMap.Bind(anEx.Current(), aNS->Label().Tag());
	}
      }
    }    
    break;
  case  TopAbs_FACE:
    {
      const Handle(TNaming_NamedShape) aNamedShape =  TNaming_Tool::NamedShape(SSh, theLab);
      if(!aNamedShape.IsNull())
	if(SMap.Add(SSh)) 
	  aB.Add(C,SSh); 
      TopExp_Explorer anEx(SSh, TopAbs_EDGE);
      for(;anEx.More();anEx.Next()) {
	const Handle(TNaming_NamedShape) aNS =  TNaming_Tool::NamedShape(anEx.Current(), theLab);
	if(aNS.IsNull()) continue;
	if(SMap.Add(anEx.Current())) {
	  aB.Add(C,anEx.Current());
	  if(isPrimitive)
	    TagMap.Bind(anEx.Current(), aNS->Label().Tag());
	}
      }
      anEx.Init(SSh, TopAbs_VERTEX); 
      for(;anEx.More();anEx.Next()) {
	const Handle(TNaming_NamedShape) aNS =  TNaming_Tool::NamedShape(anEx.Current(), theLab);
	if(aNS.IsNull()) continue;
	if(SMap.Add(anEx.Current())) {
	  aB.Add(C,anEx.Current());
	  if(isPrimitive)
	    TagMap.Bind(anEx.Current(), aNS->Label().Tag());
	}
      }
    }
    break;
  case TopAbs_WIRE:
    {      
      TopExp_Explorer anEx(SSh, TopAbs_EDGE);
      for(;anEx.More();anEx.Next()) {
	const Handle(TNaming_NamedShape) aNS =  TNaming_Tool::NamedShape(anEx.Current(), theLab);
	if(aNS.IsNull()) continue;
	if(SMap.Add(anEx.Current())) {
	  aB.Add(C,anEx.Current());
	  if(isPrimitive)
	    TagMap.Bind(anEx.Current(), aNS->Label().Tag());
	}
      }  
      anEx.Init(SSh, TopAbs_VERTEX); 
      for(;anEx.More();anEx.Next()) {
	const Handle(TNaming_NamedShape) aNS =  TNaming_Tool::NamedShape(anEx.Current(), theLab);
	if(aNS.IsNull()) continue;
	if(SMap.Add(anEx.Current())) {
	  aB.Add(C,anEx.Current()); 
	  if(isPrimitive)
	    TagMap.Bind(anEx.Current(), aNS->Label().Tag());
	}
      }     
    }
    break;
    
  case TopAbs_EDGE:
    {
      const Handle(TNaming_NamedShape) aNamedShape =  TNaming_Tool::NamedShape(SSh, theLab);
      if(!aNamedShape.IsNull())
	if(SMap.Add(SSh))
	  aB.Add(C,SSh); 
      TopExp_Explorer anEx(SSh, TopAbs_VERTEX);      
      anEx.Init(SSh, TopAbs_VERTEX); 
      for(;anEx.More();anEx.Next()) {
	const Handle(TNaming_NamedShape) aNS =  TNaming_Tool::NamedShape(anEx.Current(), theLab);
	if(aNS.IsNull()) continue;
	if(SMap.Add(anEx.Current())) {
	  aB.Add(C,anEx.Current()); 
	  if(isPrimitive)
	    TagMap.Bind(anEx.Current(), aNS->Label().Tag());
	}
      }   
    }
    break;
  case TopAbs_VERTEX:
  {
    const Handle(TNaming_NamedShape) aNS =  TNaming_Tool::NamedShape(SSh, theLab);
    if(!aNS.IsNull())
      if(SMap.Add(SSh)) {
	aB.Add(C,SSh); 
//	if(isPrimitive)
//	  TagMap.Bind(SSh, aNS->Label().Tag());
      }
  }
    break;
  default: /* Do nothing */ break;
  }   
}

//=======================================================================
//function : LoadAndName
//purpose  : 
//=======================================================================
void DNaming_TransformationDriver::LoadNamingDS (const TDF_Label& theResultLabel, 
						 const Handle(TNaming_NamedShape)& theSourceNS,
						 const gp_Trsf& theTrsf) const 
{
  TNaming_Builder aBuilder (theResultLabel);
  if(theSourceNS.IsNull() || theSourceNS->IsEmpty())
    return;
  const TopoDS_Shape& aSrcShape  = theSourceNS->Get();
  if (aSrcShape.IsNull()) {
#ifdef DEB
    cout<<"DNaming_TransformationDriver::LoadNamingDS: The result of the Transform operation is null"<<endl;
#endif
    return;
  }
  Standard_Boolean isPrimitive(Standard_False);
  if(theSourceNS->Evolution() == TNaming_PRIMITIVE) isPrimitive = Standard_True;
  const TDF_Label& aSrcLabel     = theSourceNS->Label();
#ifdef MDTV_DEB_TRSF
  cout <<"TransformationDriver: ";
  PrintE(aSrcLabel);
#endif

  TopoDS_Compound aCompShape;
  BRep_Builder    aB;
  aB.MakeCompound(aCompShape);
  TopTools_MapOfShape aSMap;
  TopTools_DataMapOfShapeInteger aTagMap;
  //Collect  shapes
  if(aSMap.Add(aSrcShape))
    aB.Add(aCompShape, aSrcShape); 
  CollectShapes(aSrcShape,aCompShape,aSMap, aSrcLabel, aTagMap, isPrimitive);

  //Transform
  BRepBuilderAPI_Transform aTransformer(aCompShape, theTrsf, Standard_False);
  TopTools_DataMapOfShapeShape aTMap;
  BuildMap (aSMap, aTransformer,aTMap);

//Load
  TopoDS_Shape aNewSh;
  if (aTMap.IsBound(aSrcShape)) aNewSh  = aTMap(aSrcShape);
  if(!aNewSh.IsNull()) {
    aBuilder.Modify(aSrcShape, aNewSh);
    aTMap.UnBind(aSrcShape);
  }

  TopTools_DataMapOfShapeShape SubShapes;
  TopExp_Explorer Exp(aNewSh, TopAbs_FACE);
  for (; Exp.More(); Exp.Next()) {
    SubShapes.Bind(Exp.Current(),Exp.Current());
  }
  for (Exp.Init(aNewSh, TopAbs_EDGE); Exp.More(); Exp.Next()) {
    SubShapes.Bind(Exp.Current(),Exp.Current());
  }
  for (Exp.Init(aNewSh, TopAbs_VERTEX); Exp.More(); Exp.Next()) {
    SubShapes.Bind(Exp.Current(),Exp.Current());
  }

  Standard_Integer aNextTag(0);
  TopTools_DataMapIteratorOfDataMapOfShapeInteger it(aTagMap);
  for(;it.More();it.Next()) {
    if(it.Value() > aNextTag)
      aNextTag = it.Value();
  }
  TopTools_DataMapIteratorOfDataMapOfShapeShape anIt(aTMap);
  Standard_Boolean aF, anE, aV;
  aF = anE = aV = Standard_True;
  TNaming_Builder* pB1 = NULL;
  TNaming_Builder* pB2 = NULL;
  TNaming_Builder* pB3 = NULL; 
  for(;anIt.More();anIt.Next()) {
    const TopoDS_Shape& aKey = anIt.Key();
    TopoDS_Shape newShape = anIt.Value();
    if(isPrimitive) {
      if (SubShapes.IsBound(newShape)) {
	newShape.Orientation((SubShapes(newShape)).Orientation());
      }
      if(aTagMap.IsBound(aKey)) {
	const TDF_Label& aLabel = theResultLabel.FindChild(aTagMap.Find(aKey),  Standard_True);
	TNaming_Builder aBuilder(aLabel);
	aBuilder.Modify(aKey, newShape);
      } else {
	aNextTag++;
	const TDF_Label& aLabel = theResultLabel.FindChild(aNextTag,  Standard_True);
	TNaming_Builder aBuilder(aLabel);
	aBuilder.Modify(aKey, newShape);
      }
    } else {
    if(aKey.ShapeType() == TopAbs_FACE) {
      if(aF) {
	const TDF_Label& aFLabel = theResultLabel.FindChild(FACES_TAG,  Standard_True);
	TNaming_Builder aFBuilder(aFLabel);
	pB1 = &aFBuilder;
	aF = Standard_False; 
      }
      if (SubShapes.IsBound(newShape)) {
	newShape.Orientation((SubShapes(newShape)).Orientation());
      }
      pB1->Modify(anIt.Key(), newShape);
    }
    else if(aKey.ShapeType() == TopAbs_EDGE) {
      if(anE) {
	const TDF_Label& aELabel = theResultLabel.FindChild(EDGES_TAG,  Standard_True);
	TNaming_Builder anEBuilder(aELabel);
	pB2 = &anEBuilder;
	anE = Standard_False; 
      }
      if (SubShapes.IsBound(newShape)) {
	newShape.Orientation((SubShapes(newShape)).Orientation());
      }
      pB2->Modify(anIt.Key(), newShape);
    }
    else if(aKey.ShapeType() == TopAbs_VERTEX) {
      if(aV) {
	const TDF_Label& aVLabel = theResultLabel.FindChild(VERTEX_TAG,  Standard_True);
	TNaming_Builder aVBuilder(aVLabel);
	pB3 = &aVBuilder;
	aV = Standard_False; 
      }
      if (SubShapes.IsBound(newShape)) {
	newShape.Orientation((SubShapes(newShape)).Orientation());
      }
      pB3->Modify(anIt.Key(), newShape);
    }
  }
  }
}