1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13 
14 
15 #include <IFSelect_DispPerCount.hxx>
16 #include <IFSelect_DispPerFiles.hxx>
17 #include <IFSelect_DispPerOne.hxx>
18 #include <IFSelect_DispPerSignature.hxx>
19 #include <IFSelect_EditForm.hxx>
20 #include <IFSelect_GeneralModifier.hxx>
21 #include <IFSelect_GraphCounter.hxx>
22 #include <IFSelect_IntParam.hxx>
23 #include <IFSelect_ParamEditor.hxx>
24 #include <IFSelect_SelectModelEntities.hxx>
25 #include <IFSelect_SelectModelRoots.hxx>
26 #include <IFSelect_SelectPointed.hxx>
27 #include <IFSelect_SelectShared.hxx>
28 #include <IFSelect_SelectSharing.hxx>
29 #include <IFSelect_ShareOut.hxx>
30 #include <IFSelect_SignAncestor.hxx>
31 #include <IFSelect_Signature.hxx>
32 #include <IFSelect_SignCategory.hxx>
33 #include <IFSelect_SignCounter.hxx>
34 #include <IFSelect_SignType.hxx>
35 #include <IFSelect_SignValidity.hxx>
36 #include <IFSelect_WorkLibrary.hxx>
37 #include <Interface_CheckIterator.hxx>
38 #include <Interface_InterfaceModel.hxx>
39 #include <Interface_Macros.hxx>
40 #include <Interface_Protocol.hxx>
41 #include <Interface_Static.hxx>
42 #include <Message.hxx>
43 #include <Message_Messenger.hxx>
44 #include <Standard_DomainError.hxx>
45 #include <Standard_Transient.hxx>
46 #include <Standard_Type.hxx>
47 #include <TCollection_HAsciiString.hxx>
48 #include <TColStd_HSequenceOfHAsciiString.hxx>
49 #include <TColStd_IndexedMapOfTransient.hxx>
50 #include <TopoDS_Shape.hxx>
51 #include <Transfer_ActorOfFinderProcess.hxx>
52 #include <Transfer_ActorOfTransientProcess.hxx>
53 #include <Transfer_Binder.hxx>
54 #include <Transfer_FinderProcess.hxx>
55 #include <Transfer_SimpleBinderOfTransient.hxx>
56 #include <Transfer_TransientMapper.hxx>
57 #include <TransferBRep_ShapeMapper.hxx>
58 #include <XSControl_ConnectedShapes.hxx>
59 #include <XSControl_Controller.hxx>
60 #include <XSControl_SelectForTransfer.hxx>
61 #include <XSControl_SignTransferStatus.hxx>
62 #include <XSControl_TransferReader.hxx>
63 #include <XSControl_WorkSession.hxx>
64 
65 IMPLEMENT_STANDARD_RTTIEXT(XSControl_Controller,Standard_Transient)
66 
67 //  ParamEditor
68 //  Transferts
69 
70 static NCollection_DataMap<TCollection_AsciiString, Handle(Standard_Transient)> listad;
71 
72 //=======================================================================
73 //function : XSControl_Controller
74 //purpose  : Constructor
75 //=======================================================================
76 
XSControl_Controller(const Standard_CString theLongName,const Standard_CString theShortName)77 XSControl_Controller::XSControl_Controller (const Standard_CString theLongName, const Standard_CString theShortName)
78 : myShortName(theShortName), myLongName(theLongName)
79 {
80   // Standard parameters
81   Interface_Static::Standards();
82   TraceStatic ("read.precision.mode" , 5);
83   TraceStatic ("read.precision.val"  , 5);
84   TraceStatic ("write.precision.mode" , 6);
85   TraceStatic ("write.precision.val"  , 6);
86 }
87 
88 //=======================================================================
89 //function : TraceStatic
90 //purpose  :
91 //=======================================================================
92 
TraceStatic(const Standard_CString theName,const Standard_Integer theUse)93 void XSControl_Controller::TraceStatic (const Standard_CString theName, const Standard_Integer theUse)
94 {
95   Handle(Interface_Static) val = Interface_Static::Static(theName);
96   if (val.IsNull()) return;
97   myParams.Append (val);
98   myParamUses.Append(theUse);
99 }
100 
101 //=======================================================================
102 //function : SetNames
103 //purpose  :
104 //=======================================================================
105 
SetNames(const Standard_CString theLongName,const Standard_CString theShortName)106 void XSControl_Controller::SetNames (const Standard_CString theLongName, const Standard_CString theShortName)
107 {
108   if (theLongName && theLongName[0] != '\0') {
109     myLongName.Clear();  myLongName.AssignCat (theLongName);
110   }
111   if (theShortName && theShortName[0] != '\0') {
112     myShortName.Clear(); myShortName.AssignCat(theShortName);
113   }
114 }
115 
116 //=======================================================================
117 //function : Record
118 //purpose  :
119 //=======================================================================
120 
Record(const Standard_CString theName) const121 void XSControl_Controller::Record (const Standard_CString theName) const
122 {
123   if (listad.IsBound(theName)) {
124     Handle(Standard_Transient) thisadapt(this);
125     Handle(Standard_Transient) newadapt = listad.ChangeFind(theName);
126     if (newadapt->IsKind(thisadapt->DynamicType()))
127       return;
128     if (!(thisadapt->IsKind(newadapt->DynamicType())) && thisadapt != newadapt)
129       throw Standard_DomainError("XSControl_Controller : Record");
130   }
131   listad.Bind(theName, this);
132 }
133 
134 //=======================================================================
135 //function : Recorded
136 //purpose  :
137 //=======================================================================
138 
Handle(XSControl_Controller)139 Handle(XSControl_Controller) XSControl_Controller::Recorded(const Standard_CString theName)
140 {
141   Handle(Standard_Transient) recorded;
142   return (listad.Find(theName, recorded)?
143     Handle(XSControl_Controller)::DownCast(recorded) :
144     Handle(XSControl_Controller)());
145 }
146 
147 //    ####    DEFINITION    ####
148 
149 //=======================================================================
150 //function : ActorRead
151 //purpose  :
152 //=======================================================================
153 
Handle(Transfer_ActorOfTransientProcess)154 Handle(Transfer_ActorOfTransientProcess) XSControl_Controller::ActorRead (const Handle(Interface_InterfaceModel)&) const
155 {
156   return myAdaptorRead;
157 }
158 
159 //=======================================================================
160 //function : ActorWrite
161 //purpose  :
162 //=======================================================================
163 
Handle(Transfer_ActorOfFinderProcess)164 Handle(Transfer_ActorOfFinderProcess) XSControl_Controller::ActorWrite () const
165 {
166   return myAdaptorWrite;
167 }
168 
169 // ###########################
170 //  Help du Transfer : controle de valeur + help
171 
172 //=======================================================================
173 //function : SetModeWrite
174 //purpose  :
175 //=======================================================================
176 
SetModeWrite(const Standard_Integer modemin,const Standard_Integer modemax,const Standard_Boolean)177 void XSControl_Controller::SetModeWrite
178   (const Standard_Integer modemin, const Standard_Integer modemax, const Standard_Boolean )
179 {
180   if (modemin > modemax)  {  myModeWriteShapeN.Nullify(); return;  }
181   myModeWriteShapeN = new Interface_HArray1OfHAsciiString (modemin,modemax);
182 }
183 
184 //=======================================================================
185 //function : SetModeWriteHelp
186 //purpose  :
187 //=======================================================================
188 
SetModeWriteHelp(const Standard_Integer modetrans,const Standard_CString help,const Standard_Boolean)189 void XSControl_Controller::SetModeWriteHelp
190   (const Standard_Integer modetrans, const Standard_CString help, const Standard_Boolean )
191 {
192   if (myModeWriteShapeN.IsNull()) return;
193   if (modetrans < myModeWriteShapeN->Lower() ||
194       modetrans > myModeWriteShapeN->Upper()) return;
195   Handle(TCollection_HAsciiString) hl = new TCollection_HAsciiString (help);
196   myModeWriteShapeN->SetValue (modetrans,hl);
197 }
198 
199 //=======================================================================
200 //function : ModeWriteBounds
201 //purpose  :
202 //=======================================================================
203 
ModeWriteBounds(Standard_Integer & modemin,Standard_Integer & modemax,const Standard_Boolean) const204 Standard_Boolean  XSControl_Controller::ModeWriteBounds
205   (Standard_Integer& modemin, Standard_Integer& modemax, const Standard_Boolean ) const
206 {
207   modemin = modemax = 0;
208   if (myModeWriteShapeN.IsNull()) return Standard_False;
209   modemin = myModeWriteShapeN->Lower();
210   modemax = myModeWriteShapeN->Upper();
211   return Standard_True;
212 }
213 
214 //=======================================================================
215 //function : IsModeWrite
216 //purpose  :
217 //=======================================================================
218 
IsModeWrite(const Standard_Integer modetrans,const Standard_Boolean) const219 Standard_Boolean  XSControl_Controller::IsModeWrite
220   (const Standard_Integer modetrans, const Standard_Boolean ) const
221 {
222   if (myModeWriteShapeN.IsNull()) return Standard_True;
223   if (modetrans < myModeWriteShapeN->Lower()) return Standard_False;
224   if (modetrans > myModeWriteShapeN->Upper()) return Standard_False;
225   return Standard_True;
226 }
227 
228 //=======================================================================
229 //function : ModeWriteHelp
230 //purpose  :
231 //=======================================================================
232 
ModeWriteHelp(const Standard_Integer modetrans,const Standard_Boolean) const233 Standard_CString  XSControl_Controller::ModeWriteHelp
234   (const Standard_Integer modetrans, const Standard_Boolean ) const
235 {
236   if (myModeWriteShapeN.IsNull()) return "";
237   if (modetrans < myModeWriteShapeN->Lower()) return "";
238   if (modetrans > myModeWriteShapeN->Upper()) return "";
239   Handle(TCollection_HAsciiString) str = myModeWriteShapeN->Value(modetrans);
240   if (str.IsNull()) return "";
241   return str->ToCString();
242 }
243 
244 
245 // ###########################
246 //  Transfer : on fait ce qu il faut par defaut (avec ActorWrite)
247 //    peut etre redefini ...
248 
249 //=======================================================================
250 //function : RecognizeWriteTransient
251 //purpose  :
252 //=======================================================================
253 
RecognizeWriteTransient(const Handle (Standard_Transient)& obj,const Standard_Integer modetrans) const254 Standard_Boolean  XSControl_Controller::RecognizeWriteTransient
255   (const Handle(Standard_Transient)& obj,
256    const Standard_Integer modetrans) const
257 {
258   if (myAdaptorWrite.IsNull()) return Standard_False;
259   myAdaptorWrite->ModeTrans() = modetrans;
260   return myAdaptorWrite->Recognize (new Transfer_TransientMapper(obj));
261 }
262 
263 //=======================================================================
264 //function : TransferFinder
265 //purpose  : internal function
266 //=======================================================================
267 
TransferFinder(const Handle (Transfer_ActorOfFinderProcess)& theActor,const Handle (Transfer_Finder)& theMapper,const Handle (Transfer_FinderProcess)& theFP,const Handle (Interface_InterfaceModel)& theModel,const Standard_Integer theModeTrans,const Message_ProgressRange & theProgress)268 static IFSelect_ReturnStatus TransferFinder
269   (const Handle(Transfer_ActorOfFinderProcess)& theActor,
270    const Handle(Transfer_Finder)& theMapper,
271    const Handle(Transfer_FinderProcess)& theFP,
272    const Handle(Interface_InterfaceModel)& theModel,
273    const Standard_Integer theModeTrans,
274    const Message_ProgressRange& theProgress)
275 {
276   if (theActor.IsNull()) return IFSelect_RetError;
277   if (theModel.IsNull()) return IFSelect_RetError;
278   theActor->ModeTrans() = theModeTrans;
279   theFP->SetModel (theModel);
280   theFP->SetActor (theActor);
281   theFP->Transfer (theMapper, theProgress);
282 
283   IFSelect_ReturnStatus stat = IFSelect_RetFail;
284   Handle(Transfer_Binder) binder = theFP->Find (theMapper);
285   Handle(Transfer_SimpleBinderOfTransient) bindtr;
286   while (!binder.IsNull()) {
287     bindtr = Handle(Transfer_SimpleBinderOfTransient)::DownCast (binder);
288     if (!bindtr.IsNull()) {
289       Handle(Standard_Transient) ent = bindtr->Result();
290       if (!ent.IsNull()) {
291         stat = IFSelect_RetDone;
292         theModel->AddWithRefs (ent);
293       }
294     }
295     binder = binder->NextResult();
296   }
297   return stat;
298 }
299 
300 //=======================================================================
301 //function : TransferWriteTransient
302 //purpose  :
303 //=======================================================================
304 
TransferWriteTransient(const Handle (Standard_Transient)& theObj,const Handle (Transfer_FinderProcess)& theFP,const Handle (Interface_InterfaceModel)& theModel,const Standard_Integer theModeTrans,const Message_ProgressRange & theProgress) const305 IFSelect_ReturnStatus XSControl_Controller::TransferWriteTransient
306   (const Handle(Standard_Transient)& theObj,
307    const Handle(Transfer_FinderProcess)& theFP,
308    const Handle(Interface_InterfaceModel)& theModel,
309    const Standard_Integer theModeTrans,
310    const Message_ProgressRange& theProgress) const
311 {
312   if (theObj.IsNull()) return IFSelect_RetVoid;
313   return TransferFinder
314     (myAdaptorWrite,new Transfer_TransientMapper(theObj),theFP,theModel,theModeTrans, theProgress);
315 }
316 
317 //=======================================================================
318 //function : RecognizeWriteShape
319 //purpose  :
320 //=======================================================================
321 
RecognizeWriteShape(const TopoDS_Shape & shape,const Standard_Integer modetrans) const322 Standard_Boolean XSControl_Controller::RecognizeWriteShape
323   (const TopoDS_Shape& shape,
324    const Standard_Integer modetrans) const
325 {
326   if (myAdaptorWrite.IsNull()) return Standard_False;
327   myAdaptorWrite->ModeTrans() = modetrans;
328   return myAdaptorWrite->Recognize (new TransferBRep_ShapeMapper(shape));
329 }
330 
331 //=======================================================================
332 //function : TransferWriteShape
333 //purpose  :
334 //=======================================================================
335 
TransferWriteShape(const TopoDS_Shape & shape,const Handle (Transfer_FinderProcess)& FP,const Handle (Interface_InterfaceModel)& model,const Standard_Integer modetrans,const Message_ProgressRange & theProgress) const336 IFSelect_ReturnStatus XSControl_Controller::TransferWriteShape
337   (const TopoDS_Shape& shape,
338    const Handle(Transfer_FinderProcess)& FP,
339    const Handle(Interface_InterfaceModel)& model,
340    const Standard_Integer modetrans,
341    const Message_ProgressRange& theProgress) const
342 {
343   if (shape.IsNull()) return IFSelect_RetVoid;
344 
345   IFSelect_ReturnStatus theReturnStat = TransferFinder
346     (myAdaptorWrite,new TransferBRep_ShapeMapper(shape),FP,model,modetrans, theProgress);
347   return theReturnStat;
348 }
349 
350 // ###########################
351 //  Cutomisation ! On enregistre des Items pour une WorkSession
352 //     (annule et remplace)
353 //     Ensuite, on les remet en place a la demande
354 
355 //=======================================================================
356 //function : AddSessionItem
357 //purpose  :
358 //=======================================================================
359 
AddSessionItem(const Handle (Standard_Transient)& theItem,const Standard_CString theName,const Standard_Boolean toApply)360 void XSControl_Controller::AddSessionItem
361   (const Handle(Standard_Transient)& theItem, const Standard_CString theName, const Standard_Boolean toApply)
362 {
363   if (theItem.IsNull() || theName[0] == '\0') return;
364   myAdaptorSession.Bind(theName,theItem);
365   if (toApply && theItem->IsKind(STANDARD_TYPE(IFSelect_GeneralModifier)))
366     myAdaptorApplied.Append(theItem);
367 }
368 
369 //=======================================================================
370 //function : SessionItem
371 //purpose  :
372 //=======================================================================
373 
Handle(Standard_Transient)374 Handle(Standard_Transient)  XSControl_Controller::SessionItem (const Standard_CString theName) const
375 {
376   Handle(Standard_Transient) item;
377   if (!myAdaptorSession.IsEmpty())
378     item = myAdaptorSession.Find(theName);
379   return item;
380 }
381 
382 //=======================================================================
383 //function : Customise
384 //purpose  :
385 //=======================================================================
386 
Customise(Handle (XSControl_WorkSession)& WS)387 void XSControl_Controller::Customise (Handle(XSControl_WorkSession)& WS)
388 {
389   WS->SetParams (myParams,myParamUses);
390 
391   // General
392   if (!myAdaptorSession.IsEmpty()) {
393     NCollection_DataMap<TCollection_AsciiString, Handle(Standard_Transient)>::Iterator iter(myAdaptorSession);
394     for (; iter.More(); iter.Next())
395       WS->AddNamedItem (iter.Key().ToCString(), iter.ChangeValue());
396   }
397 
398   if (WS->NamedItem("xst-model-all").IsNull()) {
399 
400     Handle(IFSelect_SelectModelEntities) sle = new IFSelect_SelectModelEntities;
401     WS->AddNamedItem ("xst-model-all",sle);
402 
403     Handle(IFSelect_SelectModelRoots)    slr = new IFSelect_SelectModelRoots;
404     WS->AddNamedItem ("xst-model-roots",slr);
405 
406     if(strcasecmp(WS->SelectedNorm(),"STEP")) {
407       Handle(XSControl_SelectForTransfer) st1 = new XSControl_SelectForTransfer;
408       st1->SetInput (slr);
409       st1->SetReader (WS->TransferReader());
410       WS->AddNamedItem ("xst-transferrable-roots",st1);
411     }
412 
413     Handle(XSControl_SelectForTransfer) st2 = new XSControl_SelectForTransfer;
414     st2->SetInput (sle);
415     st2->SetReader (WS->TransferReader());
416     WS->AddNamedItem ("xst-transferrable-all",st2);
417 
418     Handle(XSControl_SignTransferStatus) strs = new XSControl_SignTransferStatus;
419     strs->SetReader (WS->TransferReader());
420     WS->AddNamedItem ("xst-transfer-status",strs);
421 
422     Handle(XSControl_ConnectedShapes) scs = new XSControl_ConnectedShapes;
423     scs->SetReader (WS->TransferReader());
424     WS->AddNamedItem ("xst-connected-faces",scs);
425 
426     Handle(IFSelect_SignType) stp = new IFSelect_SignType (Standard_False);
427     WS->AddNamedItem ("xst-long-type",stp);
428 
429     Handle(IFSelect_SignType) stc = new IFSelect_SignType (Standard_True);
430     WS->AddNamedItem ("xst-type",stc);
431 
432     WS->AddNamedItem ("xst-ancestor-type",new IFSelect_SignAncestor);
433     WS->AddNamedItem ("xst-types",new IFSelect_SignCounter(stp,Standard_False,Standard_True));
434     WS->AddNamedItem ("xst-category",new IFSelect_SignCategory);
435     WS->AddNamedItem ("xst-validity",new IFSelect_SignValidity);
436 
437     Handle(IFSelect_DispPerOne) dispone = new IFSelect_DispPerOne;
438     dispone->SetFinalSelection(slr);
439     WS->AddNamedItem ("xst-disp-one",dispone);
440 
441     Handle(IFSelect_DispPerCount) dispcount = new IFSelect_DispPerCount;
442     Handle(IFSelect_IntParam) intcount = new IFSelect_IntParam;
443     intcount->SetValue(5);
444     dispcount->SetCount(intcount);
445     dispcount->SetFinalSelection(slr);
446     WS->AddNamedItem ("xst-disp-count",dispcount);
447 
448     Handle(IFSelect_DispPerFiles) dispfiles = new IFSelect_DispPerFiles;
449     Handle(IFSelect_IntParam) intfiles = new IFSelect_IntParam;
450     intfiles->SetValue(10);
451     dispfiles->SetCount(intfiles);
452     dispfiles->SetFinalSelection(slr);
453     WS->AddNamedItem ("xst-disp-files",dispfiles);
454 
455     Handle(IFSelect_DispPerSignature) dispsign = new IFSelect_DispPerSignature;
456     dispsign->SetSignCounter(new IFSelect_SignCounter(Handle(IFSelect_Signature)(stc)));
457     dispsign->SetFinalSelection(slr);
458     WS->AddNamedItem ("xst-disp-sign",dispsign);
459 
460     // Not used directly but useful anyway
461     WS->AddNamedItem ("xst-pointed",new IFSelect_SelectPointed);
462     WS->AddNamedItem ("xst-sharing",new IFSelect_SelectSharing);
463     WS->AddNamedItem ("xst-shared",new IFSelect_SelectShared);
464     WS->AddNamedItem ("xst-nb-selected",new IFSelect_GraphCounter);
465 
466     //szv:mySignType = stp;
467     WS->SetSignType( stp );
468   }
469 
470   // Applied Modifiers
471   Standard_Integer i, nb = myAdaptorApplied.Length();
472   for (i = 1; i <= nb; i ++) {
473     const Handle(Standard_Transient) &anitem = myAdaptorApplied.Value(i);
474     Handle(TCollection_HAsciiString) name = WS->Name(anitem);
475     WS->SetAppliedModifier(GetCasted(IFSelect_GeneralModifier,anitem),WS->ShareOut());
476   }
477 
478   // Editors of Parameters
479   // Here for the specific manufacturers of controllers could create the
480   // Parameters: So wait here
481 
482   Handle(TColStd_HSequenceOfHAsciiString) listat = Interface_Static::Items();
483   Handle(IFSelect_ParamEditor) paramed = IFSelect_ParamEditor::StaticEditor (listat,"All Static Parameters");
484   WS->AddNamedItem ("xst-static-params-edit",paramed);
485   Handle(IFSelect_EditForm) paramform = paramed->Form(Standard_False);
486   WS->AddNamedItem ("xst-static-params",paramform);
487 }
488