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 IMPLEMENT_STANDARD_RTTIEXT (TOcafFunction_CutDriver, TFunction_Driver)
22 //=======================================================================
25 //=======================================================================
27 const Standard_GUID& TOcafFunction_CutDriver::GetID() {
28 static Standard_GUID anID("22D22E52-D69A-11d4-8F1A-0060B0EE18E8");
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 //=======================================================================
39 TOcafFunction_CutDriver::TOcafFunction_CutDriver()
42 //=======================================================================
44 //purpose : Validation of the object label, its arguments and its results.
45 //=======================================================================
47 void TOcafFunction_CutDriver::Validate(Handle(TFunction_Logbook)& log) const
49 // We validate the object label ( Label() ), all the arguments and the results of the object:
50 log->SetValid(Label(), Standard_True);
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 //=======================================================================
60 Standard_Boolean TOcafFunction_CutDriver::MustExecute(const Handle(TFunction_Logbook)& log) const
62 // If the object's label is modified:
63 if (log->IsModified(Label())) return Standard_True;
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,
71 Handle(TDF_Reference) OriginalRef;
72 //TDF_Label aLabel = Label().FindChild(1);
74 BOOL f = Label().IsNull();
75 int a = Label().NbChildren();
77 TCollection_AsciiString aEntry;
78 TDF_Tool::Entry(Label(), aEntry);
79 std::cout << "Entry: "<<aEntry.ToCString()<<std::endl;
80 Label().FindChild(1).FindAttribute(TDF_Reference::GetID(),OriginalRef);
81 if (log->IsModified(OriginalRef->Get())) return Standard_True; // Original shape.
83 Handle(TDF_Reference) ToolRef;
84 Label().FindChild(2).FindAttribute(TDF_Reference::GetID(),ToolRef);
85 if (log->IsModified(ToolRef->Get())) return Standard_True; // Tool shape.
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;
92 //=======================================================================
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 //=======================================================================
104 Standard_Integer TOcafFunction_CutDriver::Execute(Handle(TFunction_Logbook)& /*log*/) const
106 // Let's get the arguments (OriginalNShape, ToolNShape of the object):
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();
115 // Get the TNaming_NamedShape attributes of these labels
116 Handle(TNaming_NamedShape) OriginalNShape, ToolNShape;
117 if (!( OriginalLab.FindAttribute(TNaming_NamedShape::GetID(),OriginalNShape) ))
118 throw Standard_Failure("TOcaf_Commands::CutObjects");
119 if (!( ToolLab.FindAttribute(TNaming_NamedShape::GetID(),ToolNShape) ))
120 throw Standard_Failure("TOcaf_Commands::CutObjects");
122 // Now, let's get the TopoDS_Shape of these TNaming_NamedShape:
123 TopoDS_Shape OriginalShape = OriginalNShape->Get();
124 TopoDS_Shape ToolShape = ToolNShape->Get();
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 successful:
132 MessageBoxW (NULL, L"Cut not done.", L"Cut Function Driver", MB_ICONERROR);
135 TopoDS_Shape ResultShape = mkCut.Shape();
137 // Build a TNaming_NamedShape using built cut
138 TNaming_Builder B(Label());
139 B.Modify( OriginalShape, ResultShape);
141 // If there are no any mistakes we return 0: