1 // Copyright (c) 1999-2012 OPEN CASCADE SAS
3 // The content of this file is subject to the Open CASCADE Technology Public
4 // License Version 6.5 (the "License"). You may not use the content of this file
5 // except in compliance with the License. Please obtain a copy of the License
6 // at http://www.opencascade.org and read it completely before using this file.
8 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
9 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
11 // The Original Code and all software distributed under the License is
12 // distributed on an "AS IS" basis, without warranty of any kind, and the
13 // Initial Developer hereby disclaims all such warranties, including without
14 // limitation, any warranties of merchantability, fitness for a particular
15 // purpose or non-infringement. Please see the License for the specific terms
16 // and conditions governing the rights and limitations under the License.
18 //:k8 abv 6 Jan 98: using parameter "step.group" for writing assemblies/shapes
19 #include <XSDRAWSTEP.ixx>
20 #include <STEPControl_Controller.hxx>
21 #include <STEPControl_ActorWrite.hxx>
22 #include <STEPControl_Reader.hxx>
23 #include <STEPControl_Writer.hxx>
24 #include <STEPControl_StepModelType.hxx>
26 #include <StepSelect_Activator.hxx>
28 #include <Draw_Appli.hxx>
29 #include <Draw_Interpretor.hxx>
30 #include <Draw_Printer.hxx>
31 #include <Message_Messenger.hxx>
35 #include <TCollection_HAsciiString.hxx>
36 #include <Interface_Macros.hxx>
37 #include <Interface_InterfaceModel.hxx>
39 // Pour le transfert (write)
40 // Pour NewModel et Write : definition de produit (temporaire ...)
41 #include <Transfer_FinderProcess.hxx>
42 #include <TransferBRep_ShapeMapper.hxx>
43 #include <StepShape_ShapeRepresentation.hxx>
44 #include <TColStd_HSequenceOfTransient.hxx>
46 #include <XSControl_Controller.hxx>
47 #include <XSControl_WorkSession.hxx>
48 #include <StepData_StepModel.hxx>
53 #include <StepToTopoDS_MakeTransformed.hxx>
54 #include <StepGeom_Axis2Placement3d.hxx>
57 #include <Interface_Static.hxx>
58 #include <IFSelect_SessionPilot.hxx>
59 #include <STEPSelections_Counter.hxx>
60 #include <STEPSelections_AssemblyExplorer.hxx>
62 #include <Draw_ProgressIndicator.hxx>
63 #include <Transfer_TransientProcess.hxx>
65 #include <TopExp_Explorer.hxx>
66 #include <XSControl_TransferWriter.hxx>
67 #include <Message_ProgressSentry.hxx>
71 static void cleanpilot ()
73 XSDRAW::Session()->ClearData(1);
78 //=======================================================================
81 //=======================================================================
83 void XSDRAWSTEP::Init ()
85 Handle(StepSelect_Activator) stepact = new StepSelect_Activator;
86 if (STEPControl_Controller::Init()) // XSDRAW::SetNorm("STEP AP-214"); trop tot
87 XSDRAW::SetController(XSControl_Controller::Recorded("STEP"));
92 // ######## COMMANDE stepread : teste le Reader #########
94 //=======================================================================
97 //=======================================================================
99 static Standard_Integer stepread (Draw_Interpretor& di/*theCommands*/, Standard_Integer argc, const char** argv)
101 // On admet le controller AP214 ou une variante
102 DeclareAndCast(STEPControl_Controller,ctl,XSDRAW::Controller());
103 if (ctl.IsNull()) XSDRAW::SetNorm("STEP");
106 // Progress indicator
107 Handle(Draw_ProgressIndicator) progress = new Draw_ProgressIndicator ( di, 1 );
108 progress->SetScale ( 0, 100, 1 );
111 STEPControl_Reader sr (XSDRAW::Session(),Standard_False);
112 TCollection_AsciiString fnom,rnom;
113 Standard_Boolean modfic = XSDRAW::FileAndVar
114 (argv[1],argv[2],"STEP",fnom,rnom);
115 if (modfic) di<<" File STEP to read : "<<fnom.ToCString()<<"\n";
116 else di<<" Model taken from the session : "<<fnom.ToCString()<<"\n";
117 di<<" -- Names of variables BREP-DRAW prefixed by : "<<rnom.ToCString()<<"\n";
118 IFSelect_ReturnStatus readstat = IFSelect_RetVoid;
120 progress->NewScope ( 20, "Loading" ); // On average loading takes 20%
123 Standard_Boolean fromtcl = Standard_False;
124 Standard_Boolean aFullMode = 0;
125 Standard_Integer k = 3;
128 if(argv[k][0] == 'f' || argv[3][0] == 'F')
130 aFullMode = Standard_True;
133 else if(argv[k][0] == 'r' || argv[3][0] == 'R')
135 aFullMode = Standard_False;
139 fromtcl = Standard_True;
145 cout<<"Full model for translation with additional info will be used \n"<<flush;
147 cout<<"Reduced model for translation without additional info will be used \n"<<flush;
149 sr.WS()->SetModeStat(aFullMode);
152 if (modfic) readstat = sr.ReadFile (fnom.ToCString());
153 else if (XSDRAW::Session()->NbStartingEntities() > 0) readstat = IFSelect_RetDone;
155 progress->EndScope();
158 if (readstat != IFSelect_RetDone) {
159 if (modfic) di<<"Could not read file "<<fnom.ToCString()<<" , abandon"<<"\n";
160 else di<<"No model loaded"<<"\n";
165 // nom = "." -> fichier deja lu
166 Standard_Integer i, num, nbs, modepri = 1;
167 if (fromtcl) modepri = 4;
168 Handle(Message_Messenger) aDIMessenger =
169 new Message_Messenger (new Draw_Printer(di));
171 num = sr.NbRootsForTransfer();
173 di<<"NbRootsForTransfer="<<num<<" :\n";
174 for (i = 1; i <= num; i ++) {
175 di<<"Root."<<i<<", Ent. ";
176 sr.Model()->Print(sr.RootForTransfer(i),aDIMessenger);
177 di<<" Type:"<<sr.RootForTransfer(i)->DynamicType()->Name()<<"\n";
180 cout<<"Mode (0 End, 1 root n0 1, 2 one root/n0, 3 one entity/n0, 4 Selection) : "<<flush;
184 if (modepri == 0) { di<<"End Reading STEP"<<"\n"; return 0; }
188 cout<<"Root N0 : "<<flush; cin>>num;
191 progress->NewScope ( 80, "Translation" );
193 sr.WS()->MapReader()->SetProgress ( progress );
195 if (!sr.TransferRoot (num)) di<<"Transfer root n0 "<<num<<" : no result"<<"\n";
198 char shname[30]; sprintf (shname,"%s_%d",rnom.ToCString(),nbs);
199 di<<"Transfer root n0 "<<num<<" OK -> DRAW Shape: "<<shname<<"\n";
200 di<<"Now, "<<nbs<<" Shapes produced"<<"\n";
201 TopoDS_Shape sh = sr.Shape(nbs);
202 DBRep::Set (shname,sh);
205 sr.WS()->MapReader()->SetProgress ( 0 );
206 progress->EndScope();
209 else if (modepri == 3) {
210 cout<<"Entity : "<<flush; num = XSDRAW::GetEntityNumber();
211 if (!sr.TransferOne (num)) di<<"Transfer entity n0 "<<num<<" : no result"<<"\n";
214 char shname[30]; sprintf (shname,"%s_%d",rnom.ToCString(),num);
215 di<<"Transfer entity n0 "<<num<<" OK -> DRAW Shape: "<<shname<<"\n";
216 di<<"Now, "<<nbs<<" Shapes produced"<<"\n";
217 TopoDS_Shape sh = sr.Shape(nbs);
218 DBRep::Set (shname,sh);
221 else if (modepri == 4) {
222 // char snm[100]; Standard_Integer answer = 1;
223 Handle(TColStd_HSequenceOfTransient) list;
225 // Selection, nommee ou via tcl. tcl : raccourcis admis
226 // * donne xst-transferrable-roots
228 modepri = 0; // d ioffice une seule passe
229 if (argv[k][0] == '*' && argv[k][1] == '\0') {
230 di<<"Transferrable Roots : ";
231 list = XSDRAW::GetList("xst-transferrable-roots");
232 //list = new TColStd_HSequenceOfTransient;
233 //for(Standard_Integer j=1; j<=num; j++)
234 // list->Append(sr.RootForTransfer(j));
237 di<<"List given by "<<argv[k];
238 if (argc > k+1) di<<" "<<argv[k+1];
240 list = XSDRAW::GetList (argv[k], ( argc > (k+1) ? argv[k+1] : 0 ) );
242 if (list.IsNull()) { di<<"No list defined. Give a selection name or * for all transferrable roots"<<"\n"; continue; }
244 cout<<"Name of Selection :"<<flush;
245 list = XSDRAW::GetList();
246 if (list.IsNull()) { di<<"No list defined"<<"\n"; continue; }
249 Standard_Integer ill, nbl = list->Length();
250 di<<"Nb entities selected : "<<nbl<<"\n";
251 if (nbl == 0) continue;
253 progress->NewScope ( 80, "Translation" );
255 sr.WS()->MapReader()->SetProgress ( progress );
257 Message_ProgressSentry PSentry ( progress, "Root", 0, nbl, 1 );
258 for (ill = 1; ill <= nbl && PSentry.More(); ill ++, PSentry.Next()) {
259 num = sr.Model()->Number(list->Value(ill));
260 if (num == 0) continue;
261 if (!sr.TransferOne(num)) di<<"Transfer entity n0 "<<num<<" : no result"<<"\n";
264 char shname[30]; sprintf (shname,"%s_%d",rnom.ToCString(),nbs);
265 di<<"Transfer entity n0 "<<num<<" OK -> DRAW Shape: "<<shname<<"\n";
266 di<<"Now, "<<nbs<<" Shapes produced"<<"\n";
267 TopoDS_Shape sh = sr.Shape(nbs);
268 DBRep::Set (shname,sh);
271 sr.WS()->MapReader()->SetProgress ( 0 );
272 progress->EndScope();
275 else di<<"Unknown mode n0 "<<modepri<<"\n";
280 //=======================================================================
281 //function : testreadstep
283 //=======================================================================
284 static Standard_Integer testread (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
288 di << "ERROR in " << argv[0] << "Wrong Number of Arguments."<<"\n";
289 di << " Usage : " << argv[0] <<" file_name shape_name"<< "\n";
292 STEPControl_Reader Reader;
293 Standard_CString filename = argv[1];
294 IFSelect_ReturnStatus readstat = Reader.ReadFile(filename);
295 di<<"Status from reading STEP file "<<filename<<" : ";
297 case IFSelect_RetVoid : { di<<"empty file"<<"\n"; return 1; }
298 case IFSelect_RetDone : { di<<"file read"<<"\n"; break; }
299 case IFSelect_RetError : { di<<"file not found"<<"\n"; return 1; }
300 case IFSelect_RetFail : { di<<"error during read"<<"\n"; return 1; }
301 default : { di<<"failure"<<"\n"; return 1; }
303 Reader.TransferRoots();
304 TopoDS_Shape shape = Reader.OneShape();
305 DBRep::Set(argv[2],shape);
306 di<<"Count of shapes produced : "<<Reader.NbShapes()<<"\n";
310 //=======================================================================
311 //function : readstep
313 //=======================================================================
315 static Standard_Integer readstep (Draw_Interpretor& di, Standard_Integer /*argc*/, const char** argv)
317 // On admet le controller AP214 ou une variante
320 STEPControl_Reader sr;
322 IFSelect_ReturnStatus readstat = IFSelect_RetVoid;
324 readstat = sr.ReadFile (argv[1]);
326 if (readstat != IFSelect_RetDone) {
328 di<<"No model loaded"<<"\n";
333 Standard_Integer num = sr.NbRootsForTransfer();
334 Standard_Integer modepri;
335 di<<"NbRootsForTransfer="<<num<<" :\n";
337 cout<<"Mode (0 End, 1 root n0 1, 2 one root/n0, 3 one entity/n0, 4 Selection) : "<<flush;
339 if (modepri == 0) { di<<"End Reading STEP"<<"\n"; return 0; }
342 if (!sr.TransferRoot (num)) di<<"Transfer root n0 "<<num<<" : no result"<<"\n";
344 TopoDS_Shape sh = sr.OneShape();
345 DBRep::Set (argv[2],sh);
351 // ######## COMMANDE steptrans : teste les transformations #########
352 //=======================================================================
353 //function : steptrans
355 //=======================================================================
357 static Standard_Integer steptrans (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
359 if (argc < 5) { di<<"give shape-name new-shape + entity-n0 entity-n0: AXIS2"<<"\n";
361 TopoDS_Shape shape = DBRep::Get(argv[1]);
362 if (shape.IsNull()) { di<<"Not a shape : "<<argv[1]<<"\n"; return 1; }
363 Handle(StepGeom_Axis2Placement3d) ax1,ax2;
364 Standard_Integer n1 = 0, n2 = 0;
365 n1 = XSDRAW::GetEntityNumber(argv[3]);
366 if (argc > 4) n2 = XSDRAW::GetEntityNumber(argv[4]);
367 if (n1 > 0) ax1 = Handle(StepGeom_Axis2Placement3d)::DownCast
368 (XSDRAW::Entity(n1));
369 if (n2 > 0) ax2 = Handle(StepGeom_Axis2Placement3d)::DownCast
370 (XSDRAW::Entity(n2));
371 StepToTopoDS_MakeTransformed mktrans;
372 if (mktrans.Compute (ax1,ax2)) {
373 TopLoc_Location loc (mktrans.Transformation());
375 // mktrans.Transform (shape);
376 DBRep::Set (argv[2],shape);
377 di<<"Transformed Shape as "<<argv[2]<<"\n";
379 else di<<"No transformation computed"<<"\n";
383 // ######## COMMANDE stepwrite : teste le Writer #########
385 //=======================================================================
386 //function : stepwrite
388 //=======================================================================
390 static Standard_Integer stepwrite (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
392 // On admet le controller AP214 ou une variante
393 DeclareAndCast(STEPControl_Controller,ctl,XSDRAW::Controller());
395 XSDRAW::SetNorm("STEP");
396 //sln 14.01.2002 OCC51: assign new value to ctl in order to avoid exception during using one in the function
397 ctl = Handle(STEPControl_Controller)::DownCast(XSDRAW::Controller());
401 di<<"Give mode[1-4] and Shape name + optional file. Mode possible"<<"\n";
402 di<<"f ou 1 : FacettedBRep s ou 2 : ShellBasedSurfaceModel\n"
403 <<"m ou 3 : ManifoldSolidBrep w ou 4 : GeometricCurveSet/WireFrame"<<"\n";
405 char modeshape = argv[1][0];
406 STEPControl_StepModelType mode;
409 case '0' : mode = STEPControl_AsIs; break;
411 case '1' : mode = STEPControl_FacetedBrep; break;
413 case '2' : mode = STEPControl_ShellBasedSurfaceModel; break;
415 case '3' : mode = STEPControl_ManifoldSolidBrep; break;
417 case '4' : mode = STEPControl_GeometricCurveSet; break;
418 default : di<<"1st arg = mode, incorrect [give fsmw]"<<"\n"; return 1;
421 //:k8 abv 6 Jan 98: using parameter for writing mode (assemblies/shapes)
422 Handle(STEPControl_ActorWrite) ActWrite =
423 Handle(STEPControl_ActorWrite)::DownCast ( ctl->ActorWrite() );
424 if ( ! ActWrite.IsNull() )
425 ActWrite->SetGroupMode (Interface_Static::IVal("write.step.assembly"));
427 TopoDS_Shape shape = DBRep::Get(argv[2]);
428 STEPControl_Writer sw (XSDRAW::Session(),Standard_False);
429 Handle(Interface_InterfaceModel) stepmodel = sw.Model();
430 Standard_Integer nbavant = (stepmodel.IsNull() ? 0 : stepmodel->NbEntities());
432 Handle(Draw_ProgressIndicator) progress = new Draw_ProgressIndicator ( di, 1 );
433 progress->NewScope(90,"Translating");
435 sw.WS()->TransferWriter()->FinderProcess()->SetProgress(progress);
437 Standard_Integer stat = sw.Transfer (shape,mode);
438 di<<"execution status : "<<stat<<"\n";
440 sw.WS()->TransferWriter()->FinderProcess()->SetProgress(0);
441 progress->EndScope();
443 progress->NewScope(10,"Writing");
446 // Que s est-il passe
447 stepmodel = sw.Model();
448 Standard_Integer nbapres = (stepmodel.IsNull() ? 0 : stepmodel->NbEntities());
449 if (nbavant > 0) di<<"Beware : Model not empty before transferring"<<"\n";
450 if (nbapres <= nbavant) di<<"Beware : No data produced by this transfer"<<"\n";
451 if (nbapres == 0) { di<<"No data to write"<<"\n"; return 0; }
454 Standard_Integer modepri = 1;
457 cout<<" Mode (0 end, 1 file) :"<<flush;
458 cin >> modepri; di << "Output mode " << modepri << "\n";
462 if (modepri == 0) return 0;
463 else if (modepri == 1) {
464 cout << " Output file name :" << flush; cin >> nomfic;
466 else if (modepri == 2) strcpy (nomfic,argv[3]);
468 di << " Output on file : " << nomfic << "\n";
469 stat = sw.Write(nomfic);
471 case IFSelect_RetVoid : di<<"No file written"<<"\n"; break;
472 case IFSelect_RetDone : di<<"File "<<nomfic<<" written"<<"\n"; break;
473 case IFSelect_RetStop : di<<"Error on writing file: no space on disk or destination is write protected"<<"\n"; break;
474 default : di<<"File "<<nomfic<<" written with fail messages"<<"\n"; break;
477 progress->EndScope();
482 //=======================================================================
483 //function : testwritestep
485 //=======================================================================
486 static Standard_Integer testwrite (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
490 di << "ERROR in " << argv[0] << "Wrong Number of Arguments."<<"\n";
491 di << " Usage : " << argv[0] <<" file_name shape_name "<< "\n";
494 STEPControl_Writer Writer;
495 Standard_CString filename = argv[1];
496 TopoDS_Shape shape = DBRep::Get(argv[2]);
497 IFSelect_ReturnStatus stat = Writer.Transfer(shape,STEPControl_AsIs);
498 stat = Writer.Write(filename);
499 if(stat != IFSelect_RetDone){
500 di<<"Error on writing file"<<"\n";
503 di<<"File Is Written"<<"\n";
507 //=======================================================================
508 //function : countexpected
510 //=======================================================================
512 static Standard_Integer countexpected
513 (Draw_Interpretor& di, Standard_Integer /*argc*/, const char** /*argv*/)
515 Handle(IFSelect_SessionPilot) pilot = XSDRAW::Pilot();
516 Handle(IFSelect_WorkSession) WS = pilot->Session();
517 const Interface_Graph& graph = WS->Graph();
519 Handle(TColStd_HSequenceOfTransient) roots = WS->GiveList("xst-transferrable-roots", "");
520 STEPSelections_Counter cnt;
522 for (Standard_Integer i =1 ; i <= roots->Length(); i++) {
523 cnt.Count(graph,roots->Value(i));
526 di<< "Instances of Faces \t: "<<cnt.NbInstancesOfFaces()<<"\n";
527 di<< "Instances of Shells\t: "<<cnt.NbInstancesOfShells()<<"\n";
528 di<< "Instances of Solids\t: "<<cnt.NbInstancesOfSolids()<<"\n";
529 di<< "Instances of Wires in GS\t: "<<cnt.NbInstancesOfWires()<<"\n";
530 di<< "Instances of Edges in GS\t: "<<cnt.NbInstancesOfEdges()<<"\n";
532 di<< "Source Faces \t: "<<cnt.NbSourceFaces()<<"\n";
533 di<< "Source Shells\t: "<<cnt.NbSourceShells()<<"\n";
534 di<< "Source Solids\t: "<<cnt.NbSourceSolids()<<"\n";
535 di<< "Source Wires in GS\t: "<<cnt.NbSourceWires()<<"\n";
536 di<< "Source Edges in GS\t: "<<cnt.NbSourceEdges()<<"\n";
541 static Standard_Integer dumpassembly
542 (Draw_Interpretor& /*di*/, Standard_Integer /*argc*/, const char** /*argv*/)
544 Handle(IFSelect_SessionPilot) pilot = XSDRAW::Pilot();
545 Handle(IFSelect_WorkSession) WS = pilot->Session();
546 const Interface_Graph& graph = WS->Graph();
548 STEPSelections_AssemblyExplorer exp(graph);
553 // ######## COMMANDE stepwrite : teste le Writer #########
555 void XSDRAWSTEP::InitCommands (Draw_Interpretor& theCommands)
557 const char* g = "DE: STEP"; // Step transfer file commands
559 XSDRAW::LoadDraw(theCommands);
560 theCommands.Add("stepwrite" , "stepwrite mode[0-4 afsmw] shape", __FILE__, stepwrite, g);
561 theCommands.Add("testwritestep", "testwritestep filename.stp shape", __FILE__, testwrite, g);
562 theCommands.Add("stepread", "stepread [file] [f or r (type of model full or reduced)]",__FILE__, stepread, g);
563 theCommands.Add("testreadstep", "testreadstep [file] [name DRAW]", __FILE__, testread, g);
564 theCommands.Add("steptrans", "steptrans shape stepax1 stepax2", __FILE__, steptrans, g);
565 theCommands.Add("countexpected","TEST", __FILE__, countexpected, g);
566 theCommands.Add("dumpassembly", "TEST", __FILE__, dumpassembly, g);
567 theCommands.Add("readstep", "readstep [file]", __FILE__, readstep, g);