1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
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.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 //:k8 abv 6 Jan 98: using parameter "step.group" for writing assemblies/shapes
17 #include <Draw_Appli.hxx>
18 #include <Draw_Interpretor.hxx>
19 #include <Draw_ProgressIndicator.hxx>
20 #include <IFSelect_SessionPilot.hxx>
21 #include <Interface_InterfaceModel.hxx>
22 #include <Interface_Macros.hxx>
23 #include <Interface_Static.hxx>
24 #include <Message.hxx>
25 #include <Message_Messenger.hxx>
26 #include <Message_ProgressSentry.hxx>
27 #include <STEPControl_ActorWrite.hxx>
28 #include <STEPControl_Controller.hxx>
29 #include <STEPControl_Reader.hxx>
30 #include <STEPControl_StepModelType.hxx>
31 #include <STEPControl_Writer.hxx>
32 #include <StepData_StepModel.hxx>
33 #include <StepGeom_Axis2Placement3d.hxx>
34 #include <StepSelect_Activator.hxx>
35 #include <STEPSelections_AssemblyExplorer.hxx>
36 #include <STEPSelections_Counter.hxx>
37 #include <StepShape_ShapeRepresentation.hxx>
38 #include <StepToTopoDS_MakeTransformed.hxx>
39 #include <TCollection_HAsciiString.hxx>
40 #include <TColStd_HSequenceOfTransient.hxx>
41 #include <TopExp_Explorer.hxx>
43 #include <Transfer_FinderProcess.hxx>
44 #include <Transfer_TransientProcess.hxx>
45 #include <TransferBRep_ShapeMapper.hxx>
46 #include <XSControl_Controller.hxx>
47 #include <XSControl_TransferReader.hxx>
48 #include <XSControl_TransferWriter.hxx>
49 #include <XSControl_WorkSession.hxx>
51 #include <XSDRAWSTEP.hxx>
54 // Pour le transfert (write)
55 // Pour NewModel et Write : definition de produit (temporaire ...)
58 static void cleanpilot ()
60 XSDRAW::Session()->ClearData(1);
65 //=======================================================================
68 //=======================================================================
70 void XSDRAWSTEP::Init ()
72 Handle(StepSelect_Activator) stepact = new StepSelect_Activator;
73 if (STEPControl_Controller::Init()) // XSDRAW::SetNorm("STEP AP-214"); trop tot
74 XSDRAW::SetController(XSControl_Controller::Recorded("STEP"));
79 // ######## COMMANDE stepread : teste le Reader #########
81 //=======================================================================
84 //=======================================================================
86 static Standard_Integer stepread (Draw_Interpretor& di/*theCommands*/, Standard_Integer argc, const char** argv)
89 di << "Use: stepread [file] [f or r (type of model full or reduced)]\n";
92 // On admet le controller AP214 ou une variante
93 DeclareAndCast(STEPControl_Controller,ctl,XSDRAW::Controller());
94 if (ctl.IsNull()) XSDRAW::SetNorm("STEP");
98 Handle(Draw_ProgressIndicator) progress = new Draw_ProgressIndicator ( di, 1 );
99 progress->SetScale ( 0, 100, 1 );
102 STEPControl_Reader sr (XSDRAW::Session(),Standard_False);
103 TCollection_AsciiString fnom,rnom;
104 Standard_Boolean modfic = XSDRAW::FileAndVar
105 (argv[1],argv[2],"STEP",fnom,rnom);
106 if (modfic) di<<" File STEP to read : "<<fnom.ToCString()<<"\n";
107 else di<<" Model taken from the session : "<<fnom.ToCString()<<"\n";
108 di<<" -- Names of variables BREP-DRAW prefixed by : "<<rnom.ToCString()<<"\n";
109 IFSelect_ReturnStatus readstat = IFSelect_RetVoid;
111 progress->NewScope ( 20, "Loading" ); // On average loading takes 20%
114 Standard_Boolean fromtcl = Standard_False;
115 Standard_Boolean aFullMode = Standard_False;
116 Standard_Integer k = 3;
119 if(argv[k][0] == 'f' || argv[3][0] == 'F')
121 aFullMode = Standard_True;
124 else if(argv[k][0] == 'r' || argv[3][0] == 'R')
126 aFullMode = Standard_False;
130 fromtcl = Standard_True;
136 std::cout<<"Full model for translation with additional info will be used \n"<<std::flush;
138 std::cout<<"Reduced model for translation without additional info will be used \n"<<std::flush;
140 sr.WS()->SetModeStat(aFullMode);
143 if (modfic) readstat = sr.ReadFile (fnom.ToCString());
144 else if (XSDRAW::Session()->NbStartingEntities() > 0) readstat = IFSelect_RetDone;
146 progress->EndScope();
149 if (readstat != IFSelect_RetDone) {
150 if (modfic) di<<"Could not read file "<<fnom.ToCString()<<" , abandon\n";
151 else di<<"No model loaded\n";
156 // nom = "." -> fichier deja lu
157 Standard_Integer i, num, nbs, modepri = 1;
158 if (fromtcl) modepri = 4;
160 num = sr.NbRootsForTransfer();
162 di<<"NbRootsForTransfer="<<num<<" :\n";
163 for (i = 1; i <= num; i ++) {
164 di<<"Root."<<i<<", Ent. ";
165 Standard_SStream aTmpStream;
166 sr.Model()->Print (sr.RootForTransfer(i), aTmpStream);
167 di << aTmpStream.str().c_str();
168 di<<" Type:"<<sr.RootForTransfer(i)->DynamicType()->Name()<<"\n";
171 std::cout<<"Mode (0 End, 1 root n0 1, 2 one root/n0, 3 one entity/n0, 4 Selection) : "<<std::flush;
175 if (modepri == 0) { di<<"End Reading STEP\n"; return 0; }
179 std::cout<<"Root N0 : "<<std::flush; std::cin>>num;
182 progress->NewScope ( 80, "Translation" );
184 sr.WS()->TransferReader()->TransientProcess()->SetProgress ( progress );
186 if (!sr.TransferRoot (num)) di<<"Transfer root n0 "<<num<<" : no result\n";
189 char shname[30]; Sprintf (shname,"%s_%d",rnom.ToCString(),nbs);
190 di<<"Transfer root n0 "<<num<<" OK -> DRAW Shape: "<<shname<<"\n";
191 di<<"Now, "<<nbs<<" Shapes produced\n";
192 TopoDS_Shape sh = sr.Shape(nbs);
193 DBRep::Set (shname,sh);
196 sr.WS()->TransferReader()->TransientProcess()->SetProgress ( 0 );
197 progress->EndScope();
200 else if (modepri == 3) {
201 std::cout<<"Entity : "<<std::flush; num = XSDRAW::GetEntityNumber();
202 if (!sr.TransferOne (num)) di<<"Transfer entity n0 "<<num<<" : no result\n";
205 char shname[30]; Sprintf (shname,"%s_%d",rnom.ToCString(),num);
206 di<<"Transfer entity n0 "<<num<<" OK -> DRAW Shape: "<<shname<<"\n";
207 di<<"Now, "<<nbs<<" Shapes produced\n";
208 TopoDS_Shape sh = sr.Shape(nbs);
209 DBRep::Set (shname,sh);
212 else if (modepri == 4) {
213 // char snm[100]; Standard_Integer answer = 1;
214 Handle(TColStd_HSequenceOfTransient) list;
216 // Selection, nommee ou via tcl. tcl : raccourcis admis
217 // * donne xst-transferrable-roots
219 modepri = 0; // d ioffice une seule passe
220 if (argv[k][0] == '*' && argv[k][1] == '\0') {
221 di<<"Transferrable Roots : ";
222 list = XSDRAW::GetList("xst-transferrable-roots");
223 //list = new TColStd_HSequenceOfTransient;
224 //for(Standard_Integer j=1; j<=num; j++)
225 // list->Append(sr.RootForTransfer(j));
228 di<<"List given by "<<argv[k];
229 if (argc > k+1) di<<" "<<argv[k+1];
231 list = XSDRAW::GetList (argv[k], ( argc > (k+1) ? argv[k+1] : 0 ) );
233 if (list.IsNull()) { di<<"No list defined. Give a selection name or * for all transferrable roots\n"; continue; }
235 std::cout<<"Name of Selection :"<<std::flush;
236 list = XSDRAW::GetList();
237 if (list.IsNull()) { di<<"No list defined\n"; continue; }
240 Standard_Integer ill, nbl = list->Length();
241 di<<"Nb entities selected : "<<nbl<<"\n";
242 if (nbl == 0) continue;
244 progress->NewScope ( 80, "Translation" );
246 sr.WS()->TransferReader()->TransientProcess()->SetProgress ( progress );
248 Message_ProgressSentry PSentry ( progress, "Root", 0, nbl, 1 );
249 for (ill = 1; ill <= nbl && PSentry.More(); ill ++, PSentry.Next()) {
250 num = sr.Model()->Number(list->Value(ill));
251 if (num == 0) continue;
252 if (!sr.TransferOne(num)) di<<"Transfer entity n0 "<<num<<" : no result\n";
255 char shname[30]; Sprintf (shname,"%s_%d",rnom.ToCString(),nbs);
256 di<<"Transfer entity n0 "<<num<<" OK -> DRAW Shape: "<<shname<<"\n";
257 di<<"Now, "<<nbs<<" Shapes produced\n";
258 TopoDS_Shape sh = sr.Shape(nbs);
259 DBRep::Set (shname,sh);
262 sr.WS()->TransferReader()->TransientProcess()->SetProgress ( 0 );
263 progress->EndScope();
266 else di<<"Unknown mode n0 "<<modepri<<"\n";
271 //=======================================================================
272 //function : testreadstep
274 //=======================================================================
275 static Standard_Integer testread (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
279 di << "ERROR in " << argv[0] << "Wrong Number of Arguments.\n";
280 di << " Usage : " << argv[0] <<" file_name shape_name\n";
283 STEPControl_Reader Reader;
284 Standard_CString filename = argv[1];
285 IFSelect_ReturnStatus readstat = Reader.ReadFile(filename);
286 di<<"Status from reading STEP file "<<filename<<" : ";
288 case IFSelect_RetVoid : { di<<"empty file\n"; return 1; }
289 case IFSelect_RetDone : { di<<"file read\n"; break; }
290 case IFSelect_RetError : { di<<"file not found\n"; return 1; }
291 case IFSelect_RetFail : { di<<"error during read\n"; return 1; }
292 default : { di<<"failure\n"; return 1; }
294 Reader.TransferRoots();
295 TopoDS_Shape shape = Reader.OneShape();
296 DBRep::Set(argv[2],shape);
297 di<<"Count of shapes produced : "<<Reader.NbShapes()<<"\n";
301 // ######## COMMANDE steptrans : teste les transformations #########
302 //=======================================================================
303 //function : steptrans
305 //=======================================================================
307 static Standard_Integer steptrans (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
309 if (argc < 5) { di<<"give shape-name new-shape + entity-n0 entity-n0: AXIS2\n";
311 TopoDS_Shape shape = DBRep::Get(argv[1]);
312 if (shape.IsNull()) { di<<"Not a shape : "<<argv[1]<<"\n"; return 1; }
313 Handle(StepGeom_Axis2Placement3d) ax1,ax2;
314 Standard_Integer n1 = 0, n2 = 0;
315 n1 = XSDRAW::GetEntityNumber(argv[3]);
316 if (argc > 4) n2 = XSDRAW::GetEntityNumber(argv[4]);
317 if (n1 > 0) ax1 = Handle(StepGeom_Axis2Placement3d)::DownCast
318 (XSDRAW::Entity(n1));
319 if (n2 > 0) ax2 = Handle(StepGeom_Axis2Placement3d)::DownCast
320 (XSDRAW::Entity(n2));
321 StepToTopoDS_MakeTransformed mktrans;
322 if (mktrans.Compute (ax1,ax2)) {
323 TopLoc_Location loc (mktrans.Transformation());
325 // mktrans.Transform (shape);
326 DBRep::Set (argv[2],shape);
327 di<<"Transformed Shape as "<<argv[2]<<"\n";
329 else di<<"No transformation computed\n";
333 // ######## COMMANDE stepwrite : teste le Writer #########
335 //=======================================================================
336 //function : stepwrite
338 //=======================================================================
340 static Standard_Integer stepwrite (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
342 // On admet le controller AP214 ou une variante
343 DeclareAndCast(STEPControl_Controller,ctl,XSDRAW::Controller());
345 XSDRAW::SetNorm("STEP");
346 //sln 14.01.2002 OCC51: assign new value to ctl in order to avoid exception during using one in the function
347 ctl = Handle(STEPControl_Controller)::DownCast(XSDRAW::Controller());
351 di<<"Give mode[1-4] and Shape name + optional file. Mode possible\n";
352 di<<"f ou 1 : FacettedBRep s ou 2 : ShellBasedSurfaceModel\n"
353 <<"m ou 3 : ManifoldSolidBrep w ou 4 : GeometricCurveSet/WireFrame\n";
355 char modeshape = argv[1][0];
356 STEPControl_StepModelType mode;
359 case '0' : mode = STEPControl_AsIs; break;
361 case '1' : mode = STEPControl_FacetedBrep; break;
363 case '2' : mode = STEPControl_ShellBasedSurfaceModel; break;
365 case '3' : mode = STEPControl_ManifoldSolidBrep; break;
367 case '4' : mode = STEPControl_GeometricCurveSet; break;
368 default : di<<"1st arg = mode, incorrect [give fsmw]\n"; return 1;
371 //:k8 abv 6 Jan 98: using parameter for writing mode (assemblies/shapes)
372 Handle(STEPControl_ActorWrite) ActWrite =
373 Handle(STEPControl_ActorWrite)::DownCast ( ctl->ActorWrite() );
374 if ( ! ActWrite.IsNull() )
375 ActWrite->SetGroupMode (Interface_Static::IVal("write.step.assembly"));
377 TopoDS_Shape shape = DBRep::Get(argv[2]);
378 STEPControl_Writer sw (XSDRAW::Session(),Standard_False);
379 Handle(Interface_InterfaceModel) stepmodel = sw.Model();
380 Standard_Integer nbavant = (stepmodel.IsNull() ? 0 : stepmodel->NbEntities());
382 Handle(Draw_ProgressIndicator) progress = new Draw_ProgressIndicator ( di, 1 );
383 progress->NewScope(90,"Translating");
385 sw.WS()->TransferWriter()->FinderProcess()->SetProgress(progress);
387 Standard_Integer stat = sw.Transfer (shape,mode);
388 if (stat == IFSelect_RetDone)
390 di << "Translation: OK\n";
394 di << "Error: translation failed, status = " << stat << "\n";
397 sw.WS()->TransferWriter()->FinderProcess()->SetProgress(0);
398 progress->EndScope();
400 progress->NewScope(10,"Writing");
403 // Que s est-il passe
404 stepmodel = sw.Model();
405 Standard_Integer nbapres = (stepmodel.IsNull() ? 0 : stepmodel->NbEntities());
406 if (nbavant > 0) di<<"Beware : Model not empty before transferring\n";
407 if (nbapres <= nbavant) di<<"Beware : No data produced by this transfer\n";
408 if (nbapres == 0) { di<<"No data to write\n"; return 0; }
411 di<<" Now, to write a file, command : writeall filename\n";
415 const char *nomfic = argv[3];
416 stat = sw.Write(nomfic);
418 case IFSelect_RetVoid : di<<"Error: No file written\n"; break;
419 case IFSelect_RetDone : di<<"File "<<nomfic<<" written\n"; break;
420 case IFSelect_RetStop : di<<"Error on writing file: no space on disk or destination is write protected\n"; break;
421 default : di<<"Error: File "<<nomfic<<" written with fail messages\n"; break;
424 progress->EndScope();
429 //=======================================================================
430 //function : testwritestep
432 //=======================================================================
433 static Standard_Integer testwrite (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
437 di << "ERROR in " << argv[0] << "Wrong Number of Arguments.\n";
438 di << " Usage : " << argv[0] <<" file_name shape_name \n";
441 STEPControl_Writer Writer;
442 Standard_CString filename = argv[1];
443 TopoDS_Shape shape = DBRep::Get(argv[2]);
444 IFSelect_ReturnStatus stat = Writer.Transfer(shape,STEPControl_AsIs);
445 stat = Writer.Write(filename);
446 if(stat != IFSelect_RetDone){
447 di<<"Error on writing file\n";
450 di<<"File Is Written\n";
454 //=======================================================================
455 //function : countexpected
457 //=======================================================================
459 static Standard_Integer countexpected
460 (Draw_Interpretor& di, Standard_Integer /*argc*/, const char** /*argv*/)
462 Handle(IFSelect_SessionPilot) pilot = XSDRAW::Pilot();
463 Handle(IFSelect_WorkSession) WS = pilot->Session();
464 const Interface_Graph& graph = WS->Graph();
466 Handle(TColStd_HSequenceOfTransient) roots = WS->GiveList("xst-transferrable-roots", "");
467 STEPSelections_Counter cnt;
469 for (Standard_Integer i =1 ; i <= roots->Length(); i++) {
470 cnt.Count(graph,roots->Value(i));
473 di<< "Instances of Faces \t: "<<cnt.NbInstancesOfFaces()<<"\n";
474 di<< "Instances of Shells\t: "<<cnt.NbInstancesOfShells()<<"\n";
475 di<< "Instances of Solids\t: "<<cnt.NbInstancesOfSolids()<<"\n";
476 di<< "Instances of Wires in GS\t: "<<cnt.NbInstancesOfWires()<<"\n";
477 di<< "Instances of Edges in GS\t: "<<cnt.NbInstancesOfEdges()<<"\n";
479 di<< "Source Faces \t: "<<cnt.NbSourceFaces()<<"\n";
480 di<< "Source Shells\t: "<<cnt.NbSourceShells()<<"\n";
481 di<< "Source Solids\t: "<<cnt.NbSourceSolids()<<"\n";
482 di<< "Source Wires in GS\t: "<<cnt.NbSourceWires()<<"\n";
483 di<< "Source Edges in GS\t: "<<cnt.NbSourceEdges()<<"\n";
488 static Standard_Integer dumpassembly
489 (Draw_Interpretor& /*di*/, Standard_Integer /*argc*/, const char** /*argv*/)
491 Handle(IFSelect_SessionPilot) pilot = XSDRAW::Pilot();
492 Handle(IFSelect_WorkSession) WS = pilot->Session();
493 const Interface_Graph& graph = WS->Graph();
495 STEPSelections_AssemblyExplorer exp(graph);
500 static Standard_Integer stepfileunits (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
505 std::cout<<"Error: Invalid number of parameters. Should be: getfileunits name_file"<<std::endl;
508 STEPControl_Reader aStepReader;
510 IFSelect_ReturnStatus readstat = IFSelect_RetVoid;
511 readstat = aStepReader.ReadFile (argv[1]);
513 if (readstat != IFSelect_RetDone) {
515 di<<"No model loaded\n";
519 TColStd_SequenceOfAsciiString anUnitLengthNames;
520 TColStd_SequenceOfAsciiString anUnitAngleNames;
521 TColStd_SequenceOfAsciiString anUnitSolidAngleNames;
522 aStepReader.FileUnits( anUnitLengthNames,anUnitAngleNames,anUnitSolidAngleNames);
524 Standard_Integer i =1;
525 di<<"=====================================================\n";
527 for( ; i <= anUnitLengthNames.Length() ; i++)
528 di<<anUnitLengthNames(i).ToCString()<<"\n";
530 di<<"=====================================================\n";
532 for( i =1 ; i <= anUnitAngleNames.Length() ; i++)
533 di<<anUnitAngleNames(i).ToCString()<<"\n";
535 di<<"=====================================================\n";
536 di<<"Solid Angle Unit\n";
537 for( i =1 ; i <= anUnitSolidAngleNames.Length() ; i++)
538 di<<anUnitSolidAngleNames(i).ToCString()<<"\n";
542 // ######## COMMANDE stepwrite : teste le Writer #########
544 void XSDRAWSTEP::InitCommands (Draw_Interpretor& theCommands)
546 const char* g = "DE: STEP"; // Step transfer file commands
548 XSDRAW::LoadDraw(theCommands);
549 theCommands.Add("stepwrite" , "stepwrite mode[0-4 afsmw] shape", __FILE__, stepwrite, g);
550 theCommands.Add("testwritestep", "testwritestep filename.stp shape", __FILE__, testwrite, g);
551 theCommands.Add("stepread", "stepread [file] [f or r (type of model full or reduced)]",__FILE__, stepread, g);
552 theCommands.Add("testreadstep", "testreadstep [file] [name DRAW]", __FILE__, testread, g);
553 theCommands.Add("steptrans", "steptrans shape stepax1 stepax2", __FILE__, steptrans, g);
554 theCommands.Add("countexpected","TEST", __FILE__, countexpected, g);
555 theCommands.Add("dumpassembly", "TEST", __FILE__, dumpassembly, g);
556 theCommands.Add("stepfileunits" , "stepfileunits name_file", __FILE__, stepfileunits, g);