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 | |
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(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 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(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 | Standard_Failure::Raise("TOcaf_Commands::CutObjects"); |
117 | if (!( ToolLab.FindAttribute(TNaming_NamedShape::GetID(),ToolNShape) )) |
118 | Standard_Failure::Raise("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 | MessageBox(0,"Cut not done.","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 | |
146 | |
147 | Standard_EXPORT Handle_Standard_Type& TOcafFunction_CutDriver_Type_() |
148 | { |
149 | |
150 | static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver); |
151 | if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver); |
152 | static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared); |
153 | if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared); |
154 | static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient); |
155 | if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient); |
156 | |
157 | |
158 | static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL}; |
159 | static Handle_Standard_Type _aType = new Standard_Type("TOcafFunction_CutDriver", |
160 | sizeof(TOcafFunction_CutDriver), |
161 | 1, |
162 | (Standard_Address)_Ancestors, |
163 | (Standard_Address)NULL); |
164 | |
165 | return _aType; |
166 | } |
167 | |
168 | |
169 | // DownCast method |
170 | // allow safe downcasting |
171 | // |
172 | const Handle(TOcafFunction_CutDriver) Handle(TOcafFunction_CutDriver)::DownCast(const Handle(Standard_Transient)& AnObject) |
173 | { |
174 | Handle(TOcafFunction_CutDriver) _anOtherObject; |
175 | |
176 | if (!AnObject.IsNull()) { |
177 | if (AnObject->IsKind(STANDARD_TYPE(TOcafFunction_CutDriver))) { |
178 | _anOtherObject = Handle(TOcafFunction_CutDriver)((Handle(TOcafFunction_CutDriver)&)AnObject); |
179 | } |
180 | } |
181 | |
182 | return _anOtherObject ; |
183 | } |
184 | const Handle(Standard_Type)& TOcafFunction_CutDriver::DynamicType() const |
185 | { |
186 | return STANDARD_TYPE(TOcafFunction_CutDriver) ; |
187 | } |
188 | Standard_Boolean TOcafFunction_CutDriver::IsKind(const Handle(Standard_Type)& AType) const |
189 | { |
190 | return (STANDARD_TYPE(TOcafFunction_CutDriver) == AType || TFunction_Driver::IsKind(AType)); |
191 | } |
192 | |
193 | Handle_TOcafFunction_CutDriver::~Handle_TOcafFunction_CutDriver() {} |
194 | |
195 | |