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)
102 di << "Use: stepread [file] [f or r (type of model full or reduced)]\n";
105 // On admet le controller AP214 ou une variante
106 DeclareAndCast(STEPControl_Controller,ctl,XSDRAW::Controller());
107 if (ctl.IsNull()) XSDRAW::SetNorm("STEP");
110 // Progress indicator
111 Handle(Draw_ProgressIndicator) progress = new Draw_ProgressIndicator ( di, 1 );
112 progress->SetScale ( 0, 100, 1 );
115 STEPControl_Reader sr (XSDRAW::Session(),Standard_False);
116 TCollection_AsciiString fnom,rnom;
117 Standard_Boolean modfic = XSDRAW::FileAndVar
118 (argv[1],argv[2],"STEP",fnom,rnom);
119 if (modfic) di<<" File STEP to read : "<<fnom.ToCString()<<"\n";
120 else di<<" Model taken from the session : "<<fnom.ToCString()<<"\n";
121 di<<" -- Names of variables BREP-DRAW prefixed by : "<<rnom.ToCString()<<"\n";
122 IFSelect_ReturnStatus readstat = IFSelect_RetVoid;
124 progress->NewScope ( 20, "Loading" ); // On average loading takes 20%
127 Standard_Boolean fromtcl = Standard_False;
128 Standard_Boolean aFullMode = Standard_False;
129 Standard_Integer k = 3;
132 if(argv[k][0] == 'f' || argv[3][0] == 'F')
134 aFullMode = Standard_True;
137 else if(argv[k][0] == 'r' || argv[3][0] == 'R')
139 aFullMode = Standard_False;
143 fromtcl = Standard_True;
149 cout<<"Full model for translation with additional info will be used \n"<<flush;
151 cout<<"Reduced model for translation without additional info will be used \n"<<flush;
153 sr.WS()->SetModeStat(aFullMode);
156 if (modfic) readstat = sr.ReadFile (fnom.ToCString());
157 else if (XSDRAW::Session()->NbStartingEntities() > 0) readstat = IFSelect_RetDone;
159 progress->EndScope();
162 if (readstat != IFSelect_RetDone) {
163 if (modfic) di<<"Could not read file "<<fnom.ToCString()<<" , abandon"<<"\n";
164 else di<<"No model loaded"<<"\n";
169 // nom = "." -> fichier deja lu
170 Standard_Integer i, num, nbs, modepri = 1;
171 if (fromtcl) modepri = 4;
172 Handle(Message_Messenger) aDIMessenger =
173 new Message_Messenger (new Draw_Printer(di));
175 num = sr.NbRootsForTransfer();
177 di<<"NbRootsForTransfer="<<num<<" :\n";
178 for (i = 1; i <= num; i ++) {
179 di<<"Root."<<i<<", Ent. ";
180 sr.Model()->Print(sr.RootForTransfer(i),aDIMessenger);
181 di<<" Type:"<<sr.RootForTransfer(i)->DynamicType()->Name()<<"\n";
184 cout<<"Mode (0 End, 1 root n0 1, 2 one root/n0, 3 one entity/n0, 4 Selection) : "<<flush;
188 if (modepri == 0) { di<<"End Reading STEP"<<"\n"; return 0; }
192 cout<<"Root N0 : "<<flush; cin>>num;
195 progress->NewScope ( 80, "Translation" );
197 sr.WS()->MapReader()->SetProgress ( progress );
199 if (!sr.TransferRoot (num)) di<<"Transfer root n0 "<<num<<" : no result"<<"\n";
202 char shname[30]; Sprintf (shname,"%s_%d",rnom.ToCString(),nbs);
203 di<<"Transfer root n0 "<<num<<" OK -> DRAW Shape: "<<shname<<"\n";
204 di<<"Now, "<<nbs<<" Shapes produced"<<"\n";
205 TopoDS_Shape sh = sr.Shape(nbs);
206 DBRep::Set (shname,sh);
209 sr.WS()->MapReader()->SetProgress ( 0 );
210 progress->EndScope();
213 else if (modepri == 3) {
214 cout<<"Entity : "<<flush; num = XSDRAW::GetEntityNumber();
215 if (!sr.TransferOne (num)) di<<"Transfer entity n0 "<<num<<" : no result"<<"\n";
218 char shname[30]; Sprintf (shname,"%s_%d",rnom.ToCString(),num);
219 di<<"Transfer entity n0 "<<num<<" OK -> DRAW Shape: "<<shname<<"\n";
220 di<<"Now, "<<nbs<<" Shapes produced"<<"\n";
221 TopoDS_Shape sh = sr.Shape(nbs);
222 DBRep::Set (shname,sh);
225 else if (modepri == 4) {
226 // char snm[100]; Standard_Integer answer = 1;
227 Handle(TColStd_HSequenceOfTransient) list;
229 // Selection, nommee ou via tcl. tcl : raccourcis admis
230 // * donne xst-transferrable-roots
232 modepri = 0; // d ioffice une seule passe
233 if (argv[k][0] == '*' && argv[k][1] == '\0') {
234 di<<"Transferrable Roots : ";
235 list = XSDRAW::GetList("xst-transferrable-roots");
236 //list = new TColStd_HSequenceOfTransient;
237 //for(Standard_Integer j=1; j<=num; j++)
238 // list->Append(sr.RootForTransfer(j));
241 di<<"List given by "<<argv[k];
242 if (argc > k+1) di<<" "<<argv[k+1];
244 list = XSDRAW::GetList (argv[k], ( argc > (k+1) ? argv[k+1] : 0 ) );
246 if (list.IsNull()) { di<<"No list defined. Give a selection name or * for all transferrable roots"<<"\n"; continue; }
248 cout<<"Name of Selection :"<<flush;
249 list = XSDRAW::GetList();
250 if (list.IsNull()) { di<<"No list defined"<<"\n"; continue; }
253 Standard_Integer ill, nbl = list->Length();
254 di<<"Nb entities selected : "<<nbl<<"\n";
255 if (nbl == 0) continue;
257 progress->NewScope ( 80, "Translation" );
259 sr.WS()->MapReader()->SetProgress ( progress );
261 Message_ProgressSentry PSentry ( progress, "Root", 0, nbl, 1 );
262 for (ill = 1; ill <= nbl && PSentry.More(); ill ++, PSentry.Next()) {
263 num = sr.Model()->Number(list->Value(ill));
264 if (num == 0) continue;
265 if (!sr.TransferOne(num)) di<<"Transfer entity n0 "<<num<<" : no result"<<"\n";
268 char shname[30]; Sprintf (shname,"%s_%d",rnom.ToCString(),nbs);
269 di<<"Transfer entity n0 "<<num<<" OK -> DRAW Shape: "<<shname<<"\n";
270 di<<"Now, "<<nbs<<" Shapes produced"<<"\n";
271 TopoDS_Shape sh = sr.Shape(nbs);
272 DBRep::Set (shname,sh);
275 sr.WS()->MapReader()->SetProgress ( 0 );
276 progress->EndScope();
279 else di<<"Unknown mode n0 "<<modepri<<"\n";
284 //=======================================================================
285 //function : testreadstep
287 //=======================================================================
288 static Standard_Integer testread (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
292 di << "ERROR in " << argv[0] << "Wrong Number of Arguments."<<"\n";
293 di << " Usage : " << argv[0] <<" file_name shape_name"<< "\n";
296 STEPControl_Reader Reader;
297 Standard_CString filename = argv[1];
298 IFSelect_ReturnStatus readstat = Reader.ReadFile(filename);
299 di<<"Status from reading STEP file "<<filename<<" : ";
301 case IFSelect_RetVoid : { di<<"empty file"<<"\n"; return 1; }
302 case IFSelect_RetDone : { di<<"file read"<<"\n"; break; }
303 case IFSelect_RetError : { di<<"file not found"<<"\n"; return 1; }
304 case IFSelect_RetFail : { di<<"error during read"<<"\n"; return 1; }
305 default : { di<<"failure"<<"\n"; return 1; }
307 Reader.TransferRoots();
308 TopoDS_Shape shape = Reader.OneShape();
309 DBRep::Set(argv[2],shape);
310 di<<"Count of shapes produced : "<<Reader.NbShapes()<<"\n";
314 //=======================================================================
315 //function : readstep
317 //=======================================================================
319 static Standard_Integer readstep (Draw_Interpretor& di, Standard_Integer /*argc*/, const char** argv)
321 // On admet le controller AP214 ou une variante
324 STEPControl_Reader sr;
326 IFSelect_ReturnStatus readstat = IFSelect_RetVoid;
328 readstat = sr.ReadFile (argv[1]);
330 if (readstat != IFSelect_RetDone) {
332 di<<"No model loaded"<<"\n";
337 Standard_Integer num = sr.NbRootsForTransfer();
338 Standard_Integer modepri;
339 di<<"NbRootsForTransfer="<<num<<" :\n";
341 cout<<"Mode (0 End, 1 root n0 1, 2 one root/n0, 3 one entity/n0, 4 Selection) : "<<flush;
343 if (modepri == 0) { di<<"End Reading STEP"<<"\n"; return 0; }
346 if (!sr.TransferRoot (num)) di<<"Transfer root n0 "<<num<<" : no result"<<"\n";
348 TopoDS_Shape sh = sr.OneShape();
349 DBRep::Set (argv[2],sh);
355 // ######## COMMANDE steptrans : teste les transformations #########
356 //=======================================================================
357 //function : steptrans
359 //=======================================================================
361 static Standard_Integer steptrans (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
363 if (argc < 5) { di<<"give shape-name new-shape + entity-n0 entity-n0: AXIS2"<<"\n";
365 TopoDS_Shape shape = DBRep::Get(argv[1]);
366 if (shape.IsNull()) { di<<"Not a shape : "<<argv[1]<<"\n"; return 1; }
367 Handle(StepGeom_Axis2Placement3d) ax1,ax2;
368 Standard_Integer n1 = 0, n2 = 0;
369 n1 = XSDRAW::GetEntityNumber(argv[3]);
370 if (argc > 4) n2 = XSDRAW::GetEntityNumber(argv[4]);
371 if (n1 > 0) ax1 = Handle(StepGeom_Axis2Placement3d)::DownCast
372 (XSDRAW::Entity(n1));
373 if (n2 > 0) ax2 = Handle(StepGeom_Axis2Placement3d)::DownCast
374 (XSDRAW::Entity(n2));
375 StepToTopoDS_MakeTransformed mktrans;
376 if (mktrans.Compute (ax1,ax2)) {
377 TopLoc_Location loc (mktrans.Transformation());
379 // mktrans.Transform (shape);
380 DBRep::Set (argv[2],shape);
381 di<<"Transformed Shape as "<<argv[2]<<"\n";
383 else di<<"No transformation computed"<<"\n";
387 // ######## COMMANDE stepwrite : teste le Writer #########
389 //=======================================================================
390 //function : stepwrite
392 //=======================================================================
394 static Standard_Integer stepwrite (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
396 // On admet le controller AP214 ou une variante
397 DeclareAndCast(STEPControl_Controller,ctl,XSDRAW::Controller());
399 XSDRAW::SetNorm("STEP");
400 //sln 14.01.2002 OCC51: assign new value to ctl in order to avoid exception during using one in the function
401 ctl = Handle(STEPControl_Controller)::DownCast(XSDRAW::Controller());
405 di<<"Give mode[1-4] and Shape name + optional file. Mode possible"<<"\n";
406 di<<"f ou 1 : FacettedBRep s ou 2 : ShellBasedSurfaceModel\n"
407 <<"m ou 3 : ManifoldSolidBrep w ou 4 : GeometricCurveSet/WireFrame"<<"\n";
409 char modeshape = argv[1][0];
410 STEPControl_StepModelType mode;
413 case '0' : mode = STEPControl_AsIs; break;
415 case '1' : mode = STEPControl_FacetedBrep; break;
417 case '2' : mode = STEPControl_ShellBasedSurfaceModel; break;
419 case '3' : mode = STEPControl_ManifoldSolidBrep; break;
421 case '4' : mode = STEPControl_GeometricCurveSet; break;
422 default : di<<"1st arg = mode, incorrect [give fsmw]"<<"\n"; return 1;
425 //:k8 abv 6 Jan 98: using parameter for writing mode (assemblies/shapes)
426 Handle(STEPControl_ActorWrite) ActWrite =
427 Handle(STEPControl_ActorWrite)::DownCast ( ctl->ActorWrite() );
428 if ( ! ActWrite.IsNull() )
429 ActWrite->SetGroupMode (Interface_Static::IVal("write.step.assembly"));
431 TopoDS_Shape shape = DBRep::Get(argv[2]);
432 STEPControl_Writer sw (XSDRAW::Session(),Standard_False);
433 Handle(Interface_InterfaceModel) stepmodel = sw.Model();
434 Standard_Integer nbavant = (stepmodel.IsNull() ? 0 : stepmodel->NbEntities());
436 Handle(Draw_ProgressIndicator) progress = new Draw_ProgressIndicator ( di, 1 );
437 progress->NewScope(90,"Translating");
439 sw.WS()->TransferWriter()->FinderProcess()->SetProgress(progress);
441 Standard_Integer stat = sw.Transfer (shape,mode);
442 if (stat == IFSelect_RetDone)
444 di << "Translation: OK\n";
448 di << "Error: translation failed, status = " << stat << "\n";
451 sw.WS()->TransferWriter()->FinderProcess()->SetProgress(0);
452 progress->EndScope();
454 progress->NewScope(10,"Writing");
457 // Que s est-il passe
458 stepmodel = sw.Model();
459 Standard_Integer nbapres = (stepmodel.IsNull() ? 0 : stepmodel->NbEntities());
460 if (nbavant > 0) di<<"Beware : Model not empty before transferring"<<"\n";
461 if (nbapres <= nbavant) di<<"Beware : No data produced by this transfer"<<"\n";
462 if (nbapres == 0) { di<<"No data to write"<<"\n"; return 0; }
465 di<<" Now, to write a file, command : writeall filename"<<"\n";
469 const char *nomfic = argv[3];
470 stat = sw.Write(nomfic);
472 case IFSelect_RetVoid : di<<"Error: No file written"<<"\n"; break;
473 case IFSelect_RetDone : di<<"File "<<nomfic<<" written"<<"\n"; break;
474 case IFSelect_RetStop : di<<"Error on writing file: no space on disk or destination is write protected"<<"\n"; break;
475 default : di<<"Error: File "<<nomfic<<" written with fail messages"<<"\n"; break;
478 progress->EndScope();
483 //=======================================================================
484 //function : testwritestep
486 //=======================================================================
487 static Standard_Integer testwrite (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
491 di << "ERROR in " << argv[0] << "Wrong Number of Arguments."<<"\n";
492 di << " Usage : " << argv[0] <<" file_name shape_name "<< "\n";
495 STEPControl_Writer Writer;
496 Standard_CString filename = argv[1];
497 TopoDS_Shape shape = DBRep::Get(argv[2]);
498 IFSelect_ReturnStatus stat = Writer.Transfer(shape,STEPControl_AsIs);
499 stat = Writer.Write(filename);
500 if(stat != IFSelect_RetDone){
501 di<<"Error on writing file"<<"\n";
504 di<<"File Is Written"<<"\n";
508 //=======================================================================
509 //function : countexpected
511 //=======================================================================
513 static Standard_Integer countexpected
514 (Draw_Interpretor& di, Standard_Integer /*argc*/, const char** /*argv*/)
516 Handle(IFSelect_SessionPilot) pilot = XSDRAW::Pilot();
517 Handle(IFSelect_WorkSession) WS = pilot->Session();
518 const Interface_Graph& graph = WS->Graph();
520 Handle(TColStd_HSequenceOfTransient) roots = WS->GiveList("xst-transferrable-roots", "");
521 STEPSelections_Counter cnt;
523 for (Standard_Integer i =1 ; i <= roots->Length(); i++) {
524 cnt.Count(graph,roots->Value(i));
527 di<< "Instances of Faces \t: "<<cnt.NbInstancesOfFaces()<<"\n";
528 di<< "Instances of Shells\t: "<<cnt.NbInstancesOfShells()<<"\n";
529 di<< "Instances of Solids\t: "<<cnt.NbInstancesOfSolids()<<"\n";
530 di<< "Instances of Wires in GS\t: "<<cnt.NbInstancesOfWires()<<"\n";
531 di<< "Instances of Edges in GS\t: "<<cnt.NbInstancesOfEdges()<<"\n";
533 di<< "Source Faces \t: "<<cnt.NbSourceFaces()<<"\n";
534 di<< "Source Shells\t: "<<cnt.NbSourceShells()<<"\n";
535 di<< "Source Solids\t: "<<cnt.NbSourceSolids()<<"\n";
536 di<< "Source Wires in GS\t: "<<cnt.NbSourceWires()<<"\n";
537 di<< "Source Edges in GS\t: "<<cnt.NbSourceEdges()<<"\n";
542 static Standard_Integer dumpassembly
543 (Draw_Interpretor& /*di*/, Standard_Integer /*argc*/, const char** /*argv*/)
545 Handle(IFSelect_SessionPilot) pilot = XSDRAW::Pilot();
546 Handle(IFSelect_WorkSession) WS = pilot->Session();
547 const Interface_Graph& graph = WS->Graph();
549 STEPSelections_AssemblyExplorer exp(graph);
554 static Standard_Integer stepfileunits (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
559 cout<<"Error: Invalid number of parameters. Should be: getfileunits name_file"<<endl;
562 STEPControl_Reader aStepReader;
564 IFSelect_ReturnStatus readstat = IFSelect_RetVoid;
565 readstat = aStepReader.ReadFile (argv[1]);
567 if (readstat != IFSelect_RetDone) {
569 di<<"No model loaded"<<"\n";
573 TColStd_SequenceOfAsciiString anUnitLengthNames;
574 TColStd_SequenceOfAsciiString anUnitAngleNames;
575 TColStd_SequenceOfAsciiString anUnitSolidAngleNames;
576 aStepReader.FileUnits( anUnitLengthNames,anUnitAngleNames,anUnitSolidAngleNames);
578 Standard_Integer i =1;
579 di<<"=====================================================\n";
580 di<<"LENTH Unit"<<"\n";
581 for( ; i <= anUnitLengthNames.Length() ; i++)
582 di<<anUnitLengthNames(i).ToCString()<<"\n";
584 di<<"=====================================================\n";
585 di<<"Angle Unit"<<"\n";
586 for( i =1 ; i <= anUnitAngleNames.Length() ; i++)
587 di<<anUnitAngleNames(i).ToCString()<<"\n";
589 di<<"=====================================================\n";
590 di<<"Solid Angle Unit"<<"\n";
591 for( i =1 ; i <= anUnitSolidAngleNames.Length() ; i++)
592 di<<anUnitSolidAngleNames(i).ToCString()<<"\n";
596 // ######## COMMANDE stepwrite : teste le Writer #########
598 void XSDRAWSTEP::InitCommands (Draw_Interpretor& theCommands)
600 const char* g = "DE: STEP"; // Step transfer file commands
602 XSDRAW::LoadDraw(theCommands);
603 theCommands.Add("stepwrite" , "stepwrite mode[0-4 afsmw] shape", __FILE__, stepwrite, g);
604 theCommands.Add("testwritestep", "testwritestep filename.stp shape", __FILE__, testwrite, g);
605 theCommands.Add("stepread", "stepread [file] [f or r (type of model full or reduced)]",__FILE__, stepread, g);
606 theCommands.Add("testreadstep", "testreadstep [file] [name DRAW]", __FILE__, testread, g);
607 theCommands.Add("steptrans", "steptrans shape stepax1 stepax2", __FILE__, steptrans, g);
608 theCommands.Add("countexpected","TEST", __FILE__, countexpected, g);
609 theCommands.Add("dumpassembly", "TEST", __FILE__, dumpassembly, g);
610 theCommands.Add("readstep", "readstep [file]", __FILE__, readstep, g);
611 theCommands.Add("stepfileunits" , "stepfileunits name_file", __FILE__, stepfileunits, g);