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>
8 #include <TOcafFunction_CutDriver.hxx>
10 #include <TNaming_NamedShape.hxx>
11 #include <TNaming_Builder.hxx>
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"
20 //=======================================================================
23 //=======================================================================
25 const Standard_GUID& TOcafFunction_CutDriver::GetID() {
26 static Standard_GUID anID("22D22E52-D69A-11d4-8F1A-0060B0EE18E8");
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 //=======================================================================
37 TOcafFunction_CutDriver::TOcafFunction_CutDriver()
40 //=======================================================================
42 //purpose : Validation of the object label, its arguments and its results.
43 //=======================================================================
45 void TOcafFunction_CutDriver::Validate(TFunction_Logbook& log) const
47 // We validate the object label ( Label() ), all the arguments and the results of the object:
48 log.SetValid(Label(), Standard_True);
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 //=======================================================================
58 Standard_Boolean TOcafFunction_CutDriver::MustExecute(const TFunction_Logbook& log) const
60 // If the object's label is modified:
61 if (log.IsModified(Label())) return Standard_True;
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,
69 Handle(TDF_Reference) OriginalRef;
70 //TDF_Label aLabel = Label().FindChild(1);
72 BOOL f = Label().IsNull();
73 int a = Label().NbChildren();
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.
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.
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;
90 //=======================================================================
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 //=======================================================================
102 Standard_Integer TOcafFunction_CutDriver::Execute(TFunction_Logbook& /*log*/) const
104 // Let's get the arguments (OriginalNShape, ToolNShape of the object):
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();
113 // Get the TNaming_NamedShape attributes of these labels
114 Handle(TNaming_NamedShape) OriginalNShape, ToolNShape;
115 if (!( OriginalLab.FindAttribute(TNaming_NamedShape::GetID(),OriginalNShape) ))
116 Standard_Failure::Raise("TOcaf_Commands::CutObjects");
117 if (!( ToolLab.FindAttribute(TNaming_NamedShape::GetID(),ToolNShape) ))
118 Standard_Failure::Raise("TOcaf_Commands::CutObjects");
120 // Now, let's get the TopoDS_Shape of these TNaming_NamedShape:
121 TopoDS_Shape OriginalShape = OriginalNShape->Get();
122 TopoDS_Shape ToolShape = ToolNShape->Get();
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:
130 MessageBoxW (NULL, L"Cut not done.", L"Cut Function Driver", MB_ICONERROR);
133 TopoDS_Shape ResultShape = mkCut.Shape();
135 // Build a TNaming_NamedShape using built cut
136 TNaming_Builder B(Label());
137 B.Modify( OriginalShape, ResultShape);
139 // If there are no any mistakes we return 0:
143 TOcafFunction_CutDriver::~TOcafFunction_CutDriver() {}
146 // allow safe downcasting
148 const Handle(TOcafFunction_CutDriver) TOcafFunction_CutDriver::DownCast(const Handle(Standard_Transient)& AnObject)
150 Handle(TOcafFunction_CutDriver) _anOtherObject;
152 if (!AnObject.IsNull()) {
153 if (AnObject->IsKind(STANDARD_TYPE(TOcafFunction_CutDriver))) {
154 _anOtherObject = Handle(TOcafFunction_CutDriver)((Handle(TOcafFunction_CutDriver)&)AnObject);
158 return _anOtherObject ;
160 const Handle(Standard_Type)& TOcafFunction_CutDriver::DynamicType() const
162 return STANDARD_TYPE(TOcafFunction_CutDriver) ;
164 Standard_Boolean TOcafFunction_CutDriver::IsKind(const Handle(Standard_Type)& AType) const
166 return (STANDARD_TYPE(TOcafFunction_CutDriver) == AType || TFunction_Driver::IsKind(AType));