0028225: MFC sample on OCAF uses old-style definition of a Handle-class
[occt.git] / samples / mfc / standard / 06_Ocaf / src / TOcafFunction_CutDriver.cxx
CommitLineData
7fd59977 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>
234e52be 16#include "Standard_GUID.hxx"
17#include "TFunction_Logbook.hxx"
18#include "TDF_Reference.hxx"
7fd59977 19
d7a28eda 20IMPLEMENT_STANDARD_RTTIEXT (TOcafFunction_CutDriver, TFunction_Driver)
21
7fd59977 22//=======================================================================
23//function : GetID
24//purpose :
25//=======================================================================
26
27const Standard_GUID& TOcafFunction_CutDriver::GetID() {
28 static Standard_GUID anID("22D22E52-D69A-11d4-8F1A-0060B0EE18E8");
29 return anID;
30}
31
32
33//=======================================================================
34//function : TPartStd_CutDriver
35//purpose : Creation of an instance of the driver. It's possible (and recommended)
36// : to have only one instance of a driver for the whole session.
37//=======================================================================
38
39TOcafFunction_CutDriver::TOcafFunction_CutDriver()
40{}
41
42//=======================================================================
43//function : Validate
44//purpose : Validation of the object label, its arguments and its results.
45//=======================================================================
46
f486f64d 47void TOcafFunction_CutDriver::Validate(Handle(TFunction_Logbook)& log) const
7fd59977 48{
49 // We validate the object label ( Label() ), all the arguments and the results of the object:
f486f64d 50 log->SetValid(Label(), Standard_True);
7fd59977 51}
52
53//=======================================================================
54//function : MustExecute
55//purpose : We call this method to check if the object was modified to
56// : be invoked. If the object label or an argument is modified,
57// : we must recompute the object - to call the method Execute().
58//=======================================================================
59
f486f64d 60Standard_Boolean TOcafFunction_CutDriver::MustExecute(const Handle(TFunction_Logbook)& log) const
7fd59977 61{
62 // If the object's label is modified:
f486f64d 63 if (log->IsModified(Label())) return Standard_True;
7fd59977 64
65 // Cut (in our simple case) has two arguments: The original shape, and the tool shape.
66 // They are on the child labels of the cut's label:
67 // So, OriginalNShape - is attached to the first child label
68 // ToolNShape - is attached to the second child label,
69 // .
70 // Let's check them:
71 Handle(TDF_Reference) OriginalRef;
72 //TDF_Label aLabel = Label().FindChild(1);
73/*
74 BOOL f = Label().IsNull();
75 int a = Label().NbChildren();
76*/
77 TCollection_AsciiString aEntry;
78 TDF_Tool::Entry(Label(), aEntry);
79 cout << "Entry: "<<aEntry.ToCString()<<endl;
80 Label().FindChild(1).FindAttribute(TDF_Reference::GetID(),OriginalRef);
f486f64d 81 if (log->IsModified(OriginalRef->Get())) return Standard_True; // Original shape.
7fd59977 82
83 Handle(TDF_Reference) ToolRef;
84 Label().FindChild(2).FindAttribute(TDF_Reference::GetID(),ToolRef);
f486f64d 85 if (log->IsModified(ToolRef->Get())) return Standard_True; // Tool shape.
7fd59977 86
87 // if there are no any modifications concerned the cut,
88 // it's not necessary to recompute (to call the method Execute()):
89 return Standard_False;
90}
91
92//=======================================================================
93//function : Execute
94//purpose :
95// : We compute the object and topologically name it.
96// : If during the execution we found something wrong,
97// : we return the number of the failure. For example:
98// : 1 - an attribute hasn't been found,
99// : 2 - algorithm failed,
100// : if there are no any mistakes occurred we return 0:
101// : 0 - no mistakes were found.
102//=======================================================================
103
f486f64d 104Standard_Integer TOcafFunction_CutDriver::Execute(Handle(TFunction_Logbook)& /*log*/) const
7fd59977 105{
106 // Let's get the arguments (OriginalNShape, ToolNShape of the object):
107
108 // First, we have to retrieve the TDF_Reference attributes to obtain the root labels of the OriginalNShape and the ToolNShape:
109 Handle(TDF_Reference) OriginalRef, ToolRef;
110 if (!Label().FindChild(1).FindAttribute(TDF_Reference::GetID(), OriginalRef )) return 1;
111 TDF_Label OriginalLab = OriginalRef->Get();
112 if (!Label().FindChild(2).FindAttribute(TDF_Reference::GetID(), ToolRef)) return 1;
113 TDF_Label ToolLab = ToolRef->Get();
114
115 // Get the TNaming_NamedShape attributes of these labels
116 Handle(TNaming_NamedShape) OriginalNShape, ToolNShape;
117 if (!( OriginalLab.FindAttribute(TNaming_NamedShape::GetID(),OriginalNShape) ))
9775fa61 118 throw Standard_Failure("TOcaf_Commands::CutObjects");
7fd59977 119 if (!( ToolLab.FindAttribute(TNaming_NamedShape::GetID(),ToolNShape) ))
9775fa61 120 throw Standard_Failure("TOcaf_Commands::CutObjects");
7fd59977 121
122 // Now, let's get the TopoDS_Shape of these TNaming_NamedShape:
123 TopoDS_Shape OriginalShape = OriginalNShape->Get();
124 TopoDS_Shape ToolShape = ToolNShape->Get();
125
126// STEP 2:
127 // Let's call for algorithm computing a cut operation:
128 BRepAlgoAPI_Cut mkCut(OriginalShape, ToolShape);
129 // Let's check if the Cut has been successfull:
130 if (!mkCut.IsDone())
131 {
576f8b11 132 MessageBoxW (NULL, L"Cut not done.", L"Cut Function Driver", MB_ICONERROR);
7fd59977 133 return 2;
134 }
135 TopoDS_Shape ResultShape = mkCut.Shape();
136
137 // Build a TNaming_NamedShape using built cut
138 TNaming_Builder B(Label());
139 B.Modify( OriginalShape, ResultShape);
140// That's all:
141 // If there are no any mistakes we return 0:
142 return 0;
143}