summaryrefslogtreecommitdiff
path: root/src/XSDRAWSTEP/XSDRAWSTEP.cxx
blob: 4136c3004ce24921061d70345f1ba0e96142313e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
//:k8 abv 6 Jan 98: using parameter "step.group" for writing assemblies/shapes
#include <XSDRAWSTEP.ixx>
#include <STEPControl_Controller.hxx>
#include <STEPControl_ActorWrite.hxx>
#include <STEPControl_Reader.hxx>
#include <STEPControl_Writer.hxx>
#include <STEPControl_StepModelType.hxx>
#include <XSDRAW.hxx>
#include <StepSelect_Activator.hxx>

#include <Draw_Appli.hxx>
#include <Draw_Interpretor.hxx>
#include <Draw_Printer.hxx>
#include <Message_Messenger.hxx>

#include <DBRep.hxx>

#include <TCollection_HAsciiString.hxx>
#include <Interface_Macros.hxx>
#include <Interface_InterfaceModel.hxx>

//  Pour le transfert (write)
//  Pour NewModel et Write : definition de produit (temporaire ...)
#include <Transfer_FinderProcess.hxx>
#include <TransferBRep_ShapeMapper.hxx>
#include <StepShape_ShapeRepresentation.hxx>
#include <TColStd_HSequenceOfTransient.hxx>

#include <XSControl_Controller.hxx>
#include <XSControl_WorkSession.hxx>
#include <StepData_StepModel.hxx>

#include <TopoDS.hxx>

// steptrans
#include <StepToTopoDS_MakeTransformed.hxx>
#include <StepGeom_Axis2Placement3d.hxx>

#include <stdio.h>
#include <Interface_Static.hxx>
#include <IFSelect_SessionPilot.hxx>
#include <STEPSelections_Counter.hxx>
#include <STEPSelections_AssemblyExplorer.hxx>

#include <Draw_ProgressIndicator.hxx>
#include <Transfer_TransientProcess.hxx>

#include <TopExp_Explorer.hxx>
#include <XSControl_TransferWriter.hxx>
#include <Message_ProgressSentry.hxx>


extern "C" {
static void cleanpilot ()
{
  XSDRAW::Session()->ClearData(1);
}
}


//=======================================================================
//function : Init
//purpose  : 
//=======================================================================

void XSDRAWSTEP::Init ()
{
  Handle(StepSelect_Activator)   stepact = new StepSelect_Activator;
  if (STEPControl_Controller::Init()) // XSDRAW::SetNorm("STEP AP-214"); trop tot
    XSDRAW::SetController(XSControl_Controller::Recorded("STEP"));
  
  atexit (cleanpilot);
}

//  ########  COMMANDE stepread  : teste le Reader  #########

//=======================================================================
//function : stepread
//purpose  : 
//=======================================================================

static Standard_Integer stepread (Draw_Interpretor& di/*theCommands*/, Standard_Integer argc, const char** argv) 
{
//  On admet le controller AP214 ou une variante
  DeclareAndCast(STEPControl_Controller,ctl,XSDRAW::Controller());
  if (ctl.IsNull()) XSDRAW::SetNorm("STEP");

  // Progress indicator
  Handle(Draw_ProgressIndicator) progress = new Draw_ProgressIndicator ( di, 1 );
  progress->SetScale ( 0, 100, 1 );
  progress->Show();
  
  STEPControl_Reader sr (XSDRAW::Session(),Standard_False);
  TCollection_AsciiString fnom,rnom;
  Standard_Boolean modfic = XSDRAW::FileAndVar
    (argv[1],argv[2],"STEP",fnom,rnom);
  if (modfic) di<<" File STEP to read : "<<fnom.ToCString()<<"\n";
  else        di<<" Model taken from the session : "<<fnom.ToCString()<<"\n";
  di<<" -- Names of variables BREP-DRAW prefixed by : "<<rnom.ToCString()<<"\n";
  IFSelect_ReturnStatus readstat = IFSelect_RetVoid;

  progress->NewScope ( 20, "Loading" ); // On average loading takes 20% 
  progress->Show();

  if (modfic) readstat = sr.ReadFile (fnom.ToCString());
  else  if (XSDRAW::Session()->NbStartingEntities() > 0) readstat = IFSelect_RetDone;

  progress->EndScope();
  progress->Show();

  if (readstat != IFSelect_RetDone) {
    if (modfic) di<<"Could not read file "<<fnom.ToCString()<<" , abandon"<<"\n";
    else di<<"No model loaded"<<"\n";
    return 1;
  }

  Standard_Boolean fromtcl = (argc > 3);
//   nom = "." -> fichier deja lu
  Standard_Integer i, num, nbs, modepri = 1;
  if (fromtcl) modepri = 4;
  Handle(Message_Messenger) aDIMessenger = 
    new Message_Messenger (new Draw_Printer(di));
  while (modepri) {
    num = sr.NbRootsForTransfer();
    if (!fromtcl) {
      di<<"NbRootsForTransfer="<<num<<" :\n";
      for (i = 1; i <= num; i ++) {
	di<<"Root."<<i<<", Ent. ";
	sr.Model()->Print(sr.RootForTransfer(i),aDIMessenger);
	di<<" Type:"<<sr.RootForTransfer(i)->DynamicType()->Name()<<"\n";
      }

      cout<<"Mode (0 End, 1 root n0 1, 2 one root/n0, 3 one entity/n0, 4 Selection) : "<<flush;
      cin>>modepri;
    }

    if (modepri == 0) { di<<"End Reading STEP"<<"\n"; return 0; }
    if (modepri <= 2) {
      num = 1;
      if (modepri == 2) {
	cout<<"Root N0 : "<<flush;  cin>>num;
      }

      progress->NewScope ( 80, "Translation" );
      progress->Show();
      sr.WS()->MapReader()->SetProgress ( progress );

      if (!sr.TransferRoot (num)) di<<"Transfer root n0 "<<num<<" : no result"<<"\n";
      else {
	nbs = sr.NbShapes();
	char shname[30];  sprintf (shname,"%s_%d",rnom.ToCString(),nbs);
	di<<"Transfer root n0 "<<num<<" OK  -> DRAW Shape: "<<shname<<"\n";
	di<<"Now, "<<nbs<<" Shapes produced"<<"\n";
	TopoDS_Shape sh = sr.Shape(nbs);
	DBRep::Set (shname,sh);
      }

      sr.WS()->MapReader()->SetProgress ( 0 );
      progress->EndScope();
      progress->Show();
    }
    else if (modepri == 3) {
      cout<<"Entity : "<<flush;  num = XSDRAW::GetEntityNumber();
      if (!sr.TransferOne (num)) di<<"Transfer entity n0 "<<num<<" : no result"<<"\n";
      else {
	nbs = sr.NbShapes();
	char shname[30];  sprintf (shname,"%s_%d",rnom.ToCString(),num);
	di<<"Transfer entity n0 "<<num<<" OK  -> DRAW Shape: "<<shname<<"\n";
	di<<"Now, "<<nbs<<" Shapes produced"<<"\n";
	TopoDS_Shape sh = sr.Shape(nbs);
	DBRep::Set (shname,sh);
      }
    }
    else if (modepri == 4) {
//      char snm[100];  Standard_Integer answer = 1;
      Handle(TColStd_HSequenceOfTransient)  list;

//  Selection, nommee ou via tcl. tcl : raccourcis admis
//   * donne xst-transferrable-roots
      if (fromtcl) {
	modepri = 0;    // d ioffice une seule passe
	if (argv[3][0] == '*' && argv[3][1] == '\0') {
	  di<<"Transferrable Roots : ";
	  list = XSDRAW::GetList("xst-transferrable-roots");
          //list = new TColStd_HSequenceOfTransient;
          //for(Standard_Integer j=1; j<=num; j++)
          //  list->Append(sr.RootForTransfer(j));
	}
        else {
	  di<<"List given by "<<argv[3];
	  if (argc > 4) di<<" "<<argv[4];
	  di<<" : ";
	  list = XSDRAW::GetList (argv[3], ( argc > 4 ? argv[4] : 0 ) );
	}
	if (list.IsNull()) { di<<"No list defined. Give a selection name or * for all transferrable roots"<<"\n"; continue; }
      } else {
	cout<<"Name of Selection :"<<flush;
	list = XSDRAW::GetList();
	if (list.IsNull()) { di<<"No list defined"<<"\n"; continue; }
      }

      Standard_Integer ill, nbl = list->Length();
      di<<"Nb entities selected : "<<nbl<<"\n";
      if (nbl == 0) continue;

      progress->NewScope ( 80, "Translation" );
      progress->Show();
      sr.WS()->MapReader()->SetProgress ( progress );

      Message_ProgressSentry PSentry ( progress, "Root", 0, nbl, 1 );
      for (ill = 1; ill <= nbl && PSentry.More(); ill ++, PSentry.Next()) {
	num = sr.Model()->Number(list->Value(ill));
	if (num == 0) continue;
	if (!sr.TransferOne(num)) di<<"Transfer entity n0 "<<num<<" : no result"<<"\n";
	else {
	  nbs = sr.NbShapes();
	  char shname[30];  sprintf (shname,"%s_%d",rnom.ToCString(),nbs);
	  di<<"Transfer entity n0 "<<num<<" OK  -> DRAW Shape: "<<shname<<"\n";
	  di<<"Now, "<<nbs<<" Shapes produced"<<"\n";
	  TopoDS_Shape sh = sr.Shape(nbs);
	  DBRep::Set (shname,sh);
	}
      }
      sr.WS()->MapReader()->SetProgress ( 0 );
      progress->EndScope();
      progress->Show();
    }
    else di<<"Unknown mode n0 "<<modepri<<"\n";
  }
  return 0;
}

//=======================================================================
//function : testreadstep
//purpose  : 
//=======================================================================
static Standard_Integer testread (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
{
  if (argc != 3)                                                                                      
    {                                                                                             
      di << "ERROR in " << argv[0] << "Wrong Number of Arguments."<<"\n";                     
      di << " Usage : " << argv[0] <<" file_name shape_name"<< "\n";                          
      return 1;                                                                                 
    }
  STEPControl_Reader Reader;
  Standard_CString filename = argv[1];
  IFSelect_ReturnStatus readstat = Reader.ReadFile(filename);
  di<<"Status from reading STEP file "<<filename<<" : ";  
  switch(readstat) {                                                              
    case IFSelect_RetVoid  : { di<<"empty file"<<"\n"; return 1; }            
    case IFSelect_RetDone  : { di<<"file read"<<"\n";    break; }             
    case IFSelect_RetError : { di<<"file not found"<<"\n";   return 1; }      
    case IFSelect_RetFail  : { di<<"error during read"<<"\n";  return 1; }    
    default  :  { di<<"failure"<<"\n";   return 1; }                          
  }  
  Reader.TransferRoots();
  TopoDS_Shape shape = Reader.OneShape();
  DBRep::Set(argv[2],shape); 
  di<<"Count of shapes produced : "<<Reader.NbShapes()<<"\n";
  return 0;
}
 
//=======================================================================
//function : readstep
//purpose  : 
//=======================================================================

static Standard_Integer readstep (Draw_Interpretor& di, Standard_Integer /*argc*/, const char** argv) 
{
//  On admet le controller AP214 ou une variante
  
  
  STEPControl_Reader sr;
  
  IFSelect_ReturnStatus readstat = IFSelect_RetVoid;

   readstat = sr.ReadFile (argv[1]);
 
  if (readstat != IFSelect_RetDone) {
    
    di<<"No model loaded"<<"\n";
    return 1;
  }

  
  Standard_Integer  num = sr.NbRootsForTransfer();
  Standard_Integer  modepri;
  di<<"NbRootsForTransfer="<<num<<" :\n";

  cout<<"Mode (0 End, 1 root n0 1, 2 one root/n0, 3 one entity/n0, 4 Selection) : "<<flush;
  cin>>modepri;
  if (modepri == 0) { di<<"End Reading STEP"<<"\n"; return 0; }
    

  if (!sr.TransferRoot (num)) di<<"Transfer root n0 "<<num<<" : no result"<<"\n";
  else {
    TopoDS_Shape sh = sr.OneShape();
    DBRep::Set (argv[2],sh);
  }
  cin>>modepri;
  return 0;
}

//  ########  COMMANDE steptrans  : teste les transformations  #########
//=======================================================================
//function : steptrans
//purpose  : 
//=======================================================================

static Standard_Integer steptrans (Draw_Interpretor& di, Standard_Integer argc, const char** argv) 
{
  if (argc < 5) { di<<"give shape-name new-shape + entity-n0 entity-n0: AXIS2"<<"\n";
		  return 1; }
  TopoDS_Shape shape = DBRep::Get(argv[1]);
  if (shape.IsNull()) { di<<"Not a shape : "<<argv[1]<<"\n"; return 1; }
  Handle(StepGeom_Axis2Placement3d) ax1,ax2;
  Standard_Integer n1 = 0, n2 = 0;
  n1 = XSDRAW::GetEntityNumber(argv[3]);
  if (argc > 4) n2 = XSDRAW::GetEntityNumber(argv[4]);
  if (n1 > 0) ax1 = Handle(StepGeom_Axis2Placement3d)::DownCast
    (XSDRAW::Entity(n1));
  if (n2 > 0) ax2 = Handle(StepGeom_Axis2Placement3d)::DownCast
    (XSDRAW::Entity(n2));
  StepToTopoDS_MakeTransformed mktrans;
  if (mktrans.Compute (ax1,ax2)) {
    TopLoc_Location loc (mktrans.Transformation());
    shape.Move (loc);
//    mktrans.Transform (shape);
    DBRep::Set (argv[2],shape);
    di<<"Transformed Shape as "<<argv[2]<<"\n";
  }
  else di<<"No transformation computed"<<"\n";
  return 0;
}

//  ########  COMMANDE stepwrite : teste le Writer  #########

//=======================================================================
//function : stepwrite
//purpose  : 
//=======================================================================

static Standard_Integer stepwrite (Draw_Interpretor& di, Standard_Integer argc, const char** argv) 
{
//  On admet le controller AP214 ou une variante
  DeclareAndCast(STEPControl_Controller,ctl,XSDRAW::Controller());
  if (ctl.IsNull()) {
    XSDRAW::SetNorm("STEP");
    //sln 14.01.2002 OCC51: assign new value to ctl in order to avoid exception during using one in the function
    ctl = Handle(STEPControl_Controller)::DownCast(XSDRAW::Controller());
  }

  if (argc < 3) {
    di<<"Give mode[1-4] and Shape name + optional file. Mode possible"<<"\n";
    di<<"f ou 1 : FacettedBRep        s ou 2 : ShellBasedSurfaceModel\n"
        <<"m ou 3 : ManifoldSolidBrep   w ou 4 : GeometricCurveSet/WireFrame"<<"\n";
    return 1; }
  char modeshape = argv[1][0];
  STEPControl_StepModelType mode;
  switch (modeshape) {
    case 'a' :
    case '0' : mode = STEPControl_AsIs;                    break;
    case 'f' :
    case '1' : mode = STEPControl_FacetedBrep;             break;
    case 's' :
    case '2' : mode = STEPControl_ShellBasedSurfaceModel;  break;
    case 'm' :
    case '3' : mode = STEPControl_ManifoldSolidBrep;       break;
    case 'w' :
    case '4' : mode = STEPControl_GeometricCurveSet;       break;
    default :  di<<"1st arg = mode, incorrect [give fsmw]"<<"\n"; return 1;
  }

  //:k8 abv 6 Jan 98: using parameter for writing mode (assemblies/shapes)
  Handle(STEPControl_ActorWrite) ActWrite = 
    Handle(STEPControl_ActorWrite)::DownCast ( ctl->ActorWrite() );
  if ( ! ActWrite.IsNull() ) 
    ActWrite->SetGroupMode (Interface_Static::IVal("write.step.assembly"));

  TopoDS_Shape shape = DBRep::Get(argv[2]);
  STEPControl_Writer sw (XSDRAW::Session(),Standard_False);
  Handle(Interface_InterfaceModel) stepmodel = sw.Model();
  Standard_Integer nbavant = (stepmodel.IsNull() ? 0 : stepmodel->NbEntities());

  Handle(Draw_ProgressIndicator) progress = new Draw_ProgressIndicator ( di, 1 );
  progress->NewScope(90,"Translating");
  progress->Show();
  sw.WS()->TransferWriter()->FinderProcess()->SetProgress(progress);

  Standard_Integer stat = sw.Transfer (shape,mode);
  di<<"execution status : "<<stat<<"\n";

  sw.WS()->TransferWriter()->FinderProcess()->SetProgress(0);
  progress->EndScope();
  progress->Show();
  progress->NewScope(10,"Writing");
  progress->Show();

//   Que s est-il passe
  stepmodel = sw.Model();
  Standard_Integer nbapres = (stepmodel.IsNull() ? 0 : stepmodel->NbEntities());
  if (nbavant > 0) di<<"Beware : Model not empty before transferring"<<"\n";
  if (nbapres <= nbavant) di<<"Beware : No data produced by this transfer"<<"\n";
  if (nbapres == 0) { di<<"No data to write"<<"\n"; return 0; }

  char nomfic[150] ;
  Standard_Integer modepri = 1;

  if (argc <= 3) {
    cout<<" Mode (0 end, 1 file) :"<<flush;
    cin >> modepri; di << "Output mode " << modepri << "\n";
  }
  else modepri = 2;

  if      (modepri == 0) return 0;
  else if (modepri == 1) {
    cout << " Output file name :" << flush;  cin  >> nomfic;
  }
  else if (modepri == 2) strcpy (nomfic,argv[3]);

  di << " Output on file : " << nomfic << "\n";
  stat = sw.Write(nomfic);
  switch (stat) {
    case IFSelect_RetVoid : di<<"No file written"<<"\n"; break;
    case IFSelect_RetDone : di<<"File "<<nomfic<<" written"<<"\n"; break;
    default : di<<"Error on writing file"<<"\n"; break;
  }

  progress->EndScope();
  progress->Show();

  return 0;
}
//=======================================================================
//function : testwritestep
//purpose  : 
//=======================================================================
static Standard_Integer testwrite (Draw_Interpretor& di, Standard_Integer argc, const char** argv) 
{
  if (argc != 3)                                                                                      
    {                                                                                             
      di << "ERROR in " << argv[0] << "Wrong Number of Arguments."<<"\n";                     
      di << " Usage : " << argv[0] <<" file_name shape_name "<< "\n"; 
      return 1;                                                                                 
    }
  STEPControl_Writer Writer;
  Standard_CString filename = argv[1];
  TopoDS_Shape shape = DBRep::Get(argv[2]); 
  IFSelect_ReturnStatus stat = Writer.Transfer(shape,STEPControl_AsIs);
  stat = Writer.Write(filename);
  if(stat != IFSelect_RetDone){
    di<<"Error on writing file"<<"\n";                                                               
    return 1; 
  }
  di<<"File Is Written"<<"\n";
  return 0;
}

//=======================================================================
//function : countexpected
//purpose  : 
//=======================================================================

static Standard_Integer countexpected
  (Draw_Interpretor& di, Standard_Integer /*argc*/, const char** /*argv*/)
{
  Handle(IFSelect_SessionPilot) pilot = XSDRAW::Pilot();
  Handle(IFSelect_WorkSession) WS = pilot->Session();
   const Interface_Graph& graph = WS->Graph();
  
  Handle(TColStd_HSequenceOfTransient) roots = WS->GiveList("xst-transferrable-roots", "");
  STEPSelections_Counter cnt;
  
  for (Standard_Integer i =1 ; i <= roots->Length(); i++) {
    cnt.Count(graph,roots->Value(i));
  }
  
  di<< "Instances of Faces \t: "<<cnt.NbInstancesOfFaces()<<"\n";
  di<< "Instances of Shells\t: "<<cnt.NbInstancesOfShells()<<"\n";
  di<< "Instances of Solids\t: "<<cnt.NbInstancesOfSolids()<<"\n";
  di<< "Instances of Wires in GS\t: "<<cnt.NbInstancesOfWires()<<"\n";
  di<< "Instances of Edges in GS\t: "<<cnt.NbInstancesOfEdges()<<"\n";
  
  di<< "Source Faces \t: "<<cnt.NbSourceFaces()<<"\n";
  di<< "Source Shells\t: "<<cnt.NbSourceShells()<<"\n";
  di<< "Source Solids\t: "<<cnt.NbSourceSolids()<<"\n";
  di<< "Source Wires in GS\t: "<<cnt.NbSourceWires()<<"\n";
  di<< "Source Edges in GS\t: "<<cnt.NbSourceEdges()<<"\n";
  
  return 1;
}

static Standard_Integer dumpassembly
  (Draw_Interpretor& /*di*/, Standard_Integer /*argc*/, const char** /*argv*/)
{
  Handle(IFSelect_SessionPilot) pilot = XSDRAW::Pilot();
  Handle(IFSelect_WorkSession) WS = pilot->Session();
  const Interface_Graph& graph = WS->Graph();
  
  STEPSelections_AssemblyExplorer exp(graph);
  exp.Dump(cout);
  return 0;
}

//  ########  COMMANDE stepwrite : teste le Writer  #########

void XSDRAWSTEP::InitCommands (Draw_Interpretor& theCommands)
{
  const char* g = "DE: STEP";  // Step transfer file commands
  XSDRAWSTEP::Init();
  XSDRAW::LoadDraw(theCommands);
  theCommands.Add("stepwrite" ,    "stepwrite mode[0-4 afsmw] shape",  __FILE__, stepwrite,     g);
  theCommands.Add("testwritestep", "testwritestep filename.stp shape", __FILE__, testwrite,     g);
  theCommands.Add("stepread",      "stepread  [file]",                 __FILE__, stepread,      g);
  theCommands.Add("testreadstep",  "testreadstep [file] [name DRAW]",  __FILE__, testread,      g);
  theCommands.Add("steptrans",     "steptrans shape stepax1 stepax2",  __FILE__, steptrans,     g);
  theCommands.Add("countexpected","TEST",                              __FILE__, countexpected, g);
  theCommands.Add("dumpassembly", "TEST",                              __FILE__, dumpassembly,  g);
  theCommands.Add("readstep",      "readstep  [file]",                 __FILE__, readstep,      g);
}