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