summaryrefslogtreecommitdiff
path: root/src/DNaming/DNaming_PointDriver.cxx
blob: 60ce48041e3367f53c004a8d03b9d64b5ff8317f (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
// File:	DNaming_PointDriver.cxx
// Created:	Thu Feb 25 18:49:04 2010
// Author:	Sergey ZARITCHNY <sergey.zaritchny@opencascade.com>
// Copyright:	Open CasCade SA 2010	


#include <DNaming_PointDriver.ixx>
//OCCT
#include <BRepBuilderAPI_MakeVertex.hxx>
#include <TopLoc_Location.hxx>
#include <TopoDS_Shape.hxx>
#include <TopoDS_Vertex.hxx>
#include <TopoDS.hxx>
#include <BRep_Tool.hxx>
#include <gp_Pnt.hxx>
#include <Standard_GUID.hxx>
// OCAF
#include <TNaming.hxx>
#include <TDF_Label.hxx>
#include <TDataStd_Real.hxx>
#include <TFunction_Logbook.hxx>
#include <TFunction_Function.hxx>
#include <TNaming_NamedShape.hxx>
#include <TNaming_Builder.hxx>

#include <DNaming.hxx>
#include <ModelDefinitions.hxx>

//=======================================================================
//function : DNaming_PointDriver
//purpose  : Constructor
//=======================================================================
DNaming_PointDriver::DNaming_PointDriver()
{}

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

//=======================================================================
//function : MustExecute
//purpose  : Analyse in <log> if the loaded function must be executed
//=======================================================================
Standard_Boolean DNaming_PointDriver::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_PointDriver::Execute(TFunction_Logbook& theLog) const
{
  Handle(TFunction_Function) aFunction;
  Label().FindAttribute(TFunction_Function::GetID(),aFunction);
  if(aFunction.IsNull()) return -1;
      
// perform calculations

  Standard_Real aDX = DNaming::GetReal(aFunction,PNT_DX)->Get();
  Standard_Real aDY = DNaming::GetReal(aFunction,PNT_DY)->Get();
  Standard_Real aDZ = DNaming::GetReal(aFunction,PNT_DZ)->Get();

  Handle(TNaming_NamedShape) aPrevPnt = DNaming::GetFunctionResult(aFunction);
// Save location
  TopLoc_Location aLocation;
  if (!aPrevPnt.IsNull() && !aPrevPnt->IsEmpty()) {
    aLocation = aPrevPnt->Get().Location();
  }
  gp_Pnt aPoint;
  if(aFunction->GetDriverGUID() == PNTRLT_GUID) {
    Handle(TDataStd_UAttribute) aRefPnt = DNaming::GetObjectArg(aFunction,PNTRLT_REF);
    Handle(TNaming_NamedShape) aRefPntNS = DNaming::GetObjectValue(aRefPnt);
    if (aRefPntNS.IsNull() || aRefPntNS->IsEmpty()) {
#ifdef DEB
      cout<<"PointDriver:: Ref Point is empty"<<endl;
#endif
      aFunction->SetFailure(WRONG_ARGUMENT);
      return -1;
    }
    TopoDS_Shape aRefPntShape = aRefPntNS->Get();
    TopoDS_Vertex aVertex = TopoDS::Vertex(aRefPntShape);
    aPoint = BRep_Tool::Pnt(aVertex);
    aPoint.SetX(aPoint.X()+aDX);
    aPoint.SetY(aPoint.Y()+aDY);
    aPoint.SetZ(aPoint.Z()+aDZ);
  } else 
    aPoint = gp_Pnt(aDX, aDY, aDZ);

  BRepBuilderAPI_MakeVertex aMakeVertex(aPoint);

  if(!aMakeVertex.IsDone()) {
    aFunction->SetFailure(ALGO_FAILED);
    return -1;
  }

  // Naming
  const TDF_Label& aResultLabel = RESPOSITION(aFunction);
  TNaming_Builder aBuilder(aResultLabel);
  aBuilder.Generated(aMakeVertex.Shape());
  
  // restore location
  if(!aLocation.IsIdentity())
    TNaming::Displace(aResultLabel, aLocation, Standard_True);

  theLog.SetValid(aResultLabel, Standard_True);  
  
  aFunction->SetFailure(DONE);
  return 0;
}