0026937: Eliminate NO_CXX_EXCEPTION macro support
[occt.git] / samples / mfc / standard / 06_Ocaf / src / TOcafFunction_CutDriver.cxx
1 // File:        TOcafFunction_CutDriver.cxx
2 // Created:     Mon Dec 27 10:37:13 1999
3 // Author:      Vladislav ROMASHKO
4 //              <vro@flox.nnov.matra-dtv.fr>
5
6
7 #include <stdafx.h>
8 #include <TOcafFunction_CutDriver.hxx>
9
10 #include <TNaming_NamedShape.hxx>
11 #include <TNaming_Builder.hxx>
12
13 #include <BRepAlgoAPI_Cut.hxx>
14 #include <TCollection_AsciiString.hxx>
15 #include <TDF_Tool.hxx>
16 #include "Standard_GUID.hxx"
17 #include "TFunction_Logbook.hxx"
18 #include "TDF_Reference.hxx"
19
20 //=======================================================================
21 //function : GetID
22 //purpose  :
23 //=======================================================================
24
25 const Standard_GUID& TOcafFunction_CutDriver::GetID() {
26   static Standard_GUID anID("22D22E52-D69A-11d4-8F1A-0060B0EE18E8");
27   return anID;
28 }
29
30
31 //=======================================================================
32 //function : TPartStd_CutDriver
33 //purpose  : Creation of an instance of the driver. It's possible (and recommended)
34 //         : to have only one instance of a driver for the whole session.
35 //=======================================================================
36
37 TOcafFunction_CutDriver::TOcafFunction_CutDriver()
38 {}
39
40 //=======================================================================
41 //function : Validate
42 //purpose  : Validation of the object label, its arguments and its results.
43 //=======================================================================
44
45 void TOcafFunction_CutDriver::Validate(Handle(TFunction_Logbook)& log) const
46 {
47   // We validate the object label ( Label() ), all the arguments and the results of the object:
48   log->SetValid(Label(), Standard_True);
49 }
50
51 //=======================================================================
52 //function : MustExecute
53 //purpose  : We call this method to check if the object was modified to
54 //         : be invoked. If the object label or an argument is modified,
55 //         : we must recompute the object - to call the method Execute().
56 //=======================================================================
57
58 Standard_Boolean TOcafFunction_CutDriver::MustExecute(const Handle(TFunction_Logbook)& log) const
59 {
60   // If the object's label is modified:
61   if (log->IsModified(Label())) return Standard_True; 
62
63   // Cut (in our simple case) has two arguments: The original shape, and the tool shape.
64   // They are on the child labels of the cut's label:
65   // So, OriginalNShape  - is attached to the first  child label
66   //     ToolNShape - is attached to the second child label,
67   //     .
68   // Let's check them:
69   Handle(TDF_Reference) OriginalRef;
70   //TDF_Label aLabel = Label().FindChild(1);
71 /*
72   BOOL f = Label().IsNull();
73   int a = Label().NbChildren();
74 */
75   TCollection_AsciiString aEntry;
76   TDF_Tool::Entry(Label(), aEntry);
77   cout << "Entry: "<<aEntry.ToCString()<<endl;
78   Label().FindChild(1).FindAttribute(TDF_Reference::GetID(),OriginalRef);
79   if (log->IsModified(OriginalRef->Get()))   return Standard_True; // Original shape.
80
81   Handle(TDF_Reference) ToolRef;
82   Label().FindChild(2).FindAttribute(TDF_Reference::GetID(),ToolRef);
83   if (log->IsModified(ToolRef->Get()))   return Standard_True; // Tool shape.
84   
85   // if there are no any modifications concerned the cut,
86   // it's not necessary to recompute (to call the method Execute()):
87   return Standard_False;
88 }
89
90 //=======================================================================
91 //function : Execute
92 //purpose  : 
93 //         : We compute the object and topologically name it.
94 //         : If during the execution we found something wrong,
95 //         : we return the number of the failure. For example:
96 //         : 1 - an attribute hasn't been found,
97 //         : 2 - algorithm failed,
98 //         : if there are no any mistakes occurred we return 0:
99 //         : 0 - no mistakes were found.
100 //=======================================================================
101
102 Standard_Integer TOcafFunction_CutDriver::Execute(Handle(TFunction_Logbook)& /*log*/) const
103 {
104   // Let's get the arguments (OriginalNShape, ToolNShape of the object):
105
106         // First, we have to retrieve the TDF_Reference attributes to obtain the root labels of the OriginalNShape and the ToolNShape:
107         Handle(TDF_Reference)  OriginalRef, ToolRef;
108         if (!Label().FindChild(1).FindAttribute(TDF_Reference::GetID(), OriginalRef )) return 1;
109         TDF_Label OriginalLab = OriginalRef->Get();
110         if (!Label().FindChild(2).FindAttribute(TDF_Reference::GetID(), ToolRef)) return 1;
111         TDF_Label ToolLab = ToolRef->Get();
112
113         // Get the TNaming_NamedShape attributes of these labels
114         Handle(TNaming_NamedShape) OriginalNShape, ToolNShape;
115         if (!( OriginalLab.FindAttribute(TNaming_NamedShape::GetID(),OriginalNShape) ))
116                 throw Standard_Failure("TOcaf_Commands::CutObjects");
117         if (!( ToolLab.FindAttribute(TNaming_NamedShape::GetID(),ToolNShape) ))
118                 throw Standard_Failure("TOcaf_Commands::CutObjects");
119
120         // Now, let's get the TopoDS_Shape of these TNaming_NamedShape:
121         TopoDS_Shape OriginalShape  = OriginalNShape->Get();
122         TopoDS_Shape ToolShape = ToolNShape->Get();
123
124 // STEP 2:
125         // Let's call for algorithm computing a cut operation:
126         BRepAlgoAPI_Cut mkCut(OriginalShape, ToolShape);
127         // Let's check if the Cut has been successfull:
128         if (!mkCut.IsDone()) 
129         {
130                 MessageBoxW (NULL, L"Cut not done.", L"Cut Function Driver", MB_ICONERROR);
131                 return 2;
132         }
133         TopoDS_Shape ResultShape = mkCut.Shape();
134
135         // Build a TNaming_NamedShape using built cut
136         TNaming_Builder B(Label());
137         B.Modify( OriginalShape, ResultShape);
138 // That's all:
139   // If there are no any mistakes we return 0:
140   return 0;
141 }
142
143 TOcafFunction_CutDriver::~TOcafFunction_CutDriver() {}
144  
145 // DownCast method
146 //   allow safe downcasting
147 //
148 const Handle(TOcafFunction_CutDriver) TOcafFunction_CutDriver::DownCast(const Handle(Standard_Transient)& AnObject) 
149 {
150   Handle(TOcafFunction_CutDriver) _anOtherObject;
151
152   if (!AnObject.IsNull()) {
153      if (AnObject->IsKind(STANDARD_TYPE(TOcafFunction_CutDriver))) {
154        _anOtherObject = Handle(TOcafFunction_CutDriver)::DownCast (AnObject);
155      }
156   }
157
158   return _anOtherObject ;
159 }
160 const Handle(Standard_Type)& TOcafFunction_CutDriver::DynamicType() const 
161
162   return STANDARD_TYPE(TOcafFunction_CutDriver) ; 
163 }
164 Standard_Boolean TOcafFunction_CutDriver::IsKind(const Handle(Standard_Type)& AType) const 
165
166   return (STANDARD_TYPE(TOcafFunction_CutDriver) == AType || TFunction_Driver::IsKind(AType)); 
167 }
168
169
170