3 #include "Application.h"
11 #include <QFileDialog>
12 #include <QApplication>
14 #include <QStyleFactory>
16 #include <AIS_Shape.hxx>
17 #include <AIS_InteractiveObject.hxx>
19 #include <FSD_File.hxx>
21 #include <IGESControl_Reader.hxx>
22 #include <IGESControl_Writer.hxx>
23 #include <IGESControl_Controller.hxx>
24 #include <STEPControl_Reader.hxx>
25 #include <STEPControl_Writer.hxx>
26 #include <STEPControl_StepModelType.hxx>
27 #include <Interface_Static.hxx>
28 //#include <Interface_TraceFile.hxx>
30 #include <StlAPI_Writer.hxx>
31 #include <VrmlAPI_Writer.hxx>
33 #include <BRepTools.hxx>
34 #include <BRep_Tool.hxx>
35 #include <BRep_Builder.hxx>
38 #include <TopoDS_Shape.hxx>
39 #include <TopoDS_Compound.hxx>
40 #include <TopExp_Explorer.hxx>
41 #include <TopTools_HSequenceOfShape.hxx>
43 #include <Geom_Line.hxx>
44 #include <Geom_Curve.hxx>
45 #include <Geom_Plane.hxx>
46 #include <Geom_Surface.hxx>
49 #include <Standard_ErrorHandler.hxx>
50 #include <Standard_CString.hxx>
52 // ---------------------------- TranslateDlg -----------------------------------------
54 class TranslateDlg : public QFileDialog
57 TranslateDlg( QWidget* = 0, Qt::WindowFlags flags = 0, bool = true );
60 void setMode( const int );
61 void addMode( const int, const QString& );
65 void showEvent ( QShowEvent* event );
68 QListView* findListView( const QObjectList& );
75 TranslateDlg::TranslateDlg( QWidget* parent, Qt::WindowFlags flags, bool modal )
76 : QFileDialog( parent, flags )
78 setOption( QFileDialog::DontUseNativeDialog );
81 QGridLayout* grid = ::qobject_cast<QGridLayout*>( layout() );
85 QVBoxLayout *vbox = new QVBoxLayout;
87 QWidget* paramGroup = new QWidget( this );
88 paramGroup->setLayout( vbox );
90 myBox = new QComboBox( paramGroup );
91 vbox->addWidget( myBox );
93 int row = grid->rowCount();
94 grid->addWidget( paramGroup, row, 1, 1, 3 ); // make combobox occupy 1 row and 3 columns starting from 1
98 TranslateDlg::~TranslateDlg()
102 int TranslateDlg::getMode() const
104 if ( myBox->currentIndex() < 0 || myBox->currentIndex() > (int)myList.count() - 1 )
107 return myList.at( myBox->currentIndex() );
110 void TranslateDlg::setMode( const int mode )
112 int idx = myList.indexOf( mode );
114 myBox->setCurrentIndex( idx );
117 void TranslateDlg::addMode( const int mode, const QString& name )
120 myBox->addItem( name );
121 myList.append( mode );
122 myBox->updateGeometry();
126 void TranslateDlg::clear()
131 myBox->updateGeometry();
135 QListView* TranslateDlg::findListView( const QObjectList & childList )
137 QListView* listView = 0;
138 for ( int i = 0, n = childList.count(); i < n && !listView; i++ )
140 listView = qobject_cast<QListView*>( childList.at( i ) );
141 if ( !listView && childList.at( i ) )
143 listView = findListView( childList.at( i )->children() );
149 void TranslateDlg::showEvent ( QShowEvent* event )
151 QFileDialog::showEvent ( event );
152 QListView* aListView = findListView( children() );
153 aListView->setViewMode( QListView::ListMode );
157 // ---------------------------- Translate -----------------------------------------
159 Translate::Translate( QObject* parent )
165 Translate::~Translate()
171 QString Translate::info() const
176 bool Translate::importModel( const int format, const Handle(AIS_InteractiveContext)& ic )
179 QString fileName = selectFileName( format, true );
180 if ( fileName.isEmpty() )
183 if ( !QFileInfo( fileName ).exists() )
185 myInfo = QObject::tr( "INF_TRANSLATE_FILENOTFOUND" ).arg( fileName );
189 QApplication::setOverrideCursor( Qt::WaitCursor );
190 Handle(TopTools_HSequenceOfShape) shapes = importModel( format, fileName );
191 QApplication::restoreOverrideCursor();
193 return displayShSequence(ic, shapes);
196 bool Translate::displayShSequence(const Handle(AIS_InteractiveContext)& ic,
197 const Handle(TopTools_HSequenceOfShape)& shapes )
199 if ( shapes.IsNull() || !shapes->Length() )
202 for ( int i = 1; i <= shapes->Length(); i++ )
203 ic->Display( new AIS_Shape( shapes->Value( i ) ), false );
204 ic->UpdateCurrentViewer();
208 Handle(TopTools_HSequenceOfShape) Translate::importModel( const int format, const QString& file )
210 Handle(TopTools_HSequenceOfShape) shapes;
215 shapes = importBREP( file );
218 shapes = importIGES( file );
221 shapes = importSTEP( file );
224 } catch ( Standard_Failure ) {
230 bool Translate::exportModel( const int format, const Handle(AIS_InteractiveContext)& ic )
233 QString fileName = selectFileName( format, false );
234 if ( fileName.isEmpty() )
237 Handle(TopTools_HSequenceOfShape) shapes = getShapes( ic );
238 if ( shapes.IsNull() || !shapes->Length() )
241 QApplication::setOverrideCursor( Qt::WaitCursor );
242 bool stat = exportModel( format, fileName, shapes );
243 QApplication::restoreOverrideCursor();
248 bool Translate::exportModel( const int format, const QString& file, const Handle(TopTools_HSequenceOfShape)& shapes )
255 status = exportBREP( file, shapes );
258 status = exportIGES( file, shapes );
261 status = exportSTEP( file, shapes );
264 status = exportSTL( file, shapes );
267 status = exportVRML( file, shapes );
270 } catch ( Standard_Failure ) {
276 Handle(TopTools_HSequenceOfShape) Translate::getShapes( const Handle(AIS_InteractiveContext)& ic )
278 Handle(TopTools_HSequenceOfShape) aSequence;
279 Handle(AIS_InteractiveObject) picked;
280 for ( ic->InitCurrent(); ic->MoreCurrent(); ic->NextCurrent() )
282 Handle(AIS_InteractiveObject) obj = ic->Current();
283 if ( obj->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
285 TopoDS_Shape shape = Handle(AIS_Shape)::DownCast(obj)->Shape();
286 if ( aSequence.IsNull() )
287 aSequence = new TopTools_HSequenceOfShape();
288 aSequence->Append( shape );
295 Selects a file from standard dialog acoording to selection 'filter'
297 QString Translate::selectFileName( const int format, const bool import )
299 TranslateDlg* theDlg = getDialog( format, import );
301 int ret = theDlg->exec();
303 qApp->processEvents();
306 QStringList fileNames;
307 if ( ret != QDialog::Accepted )
310 fileNames = theDlg->selectedFiles();
311 if (!fileNames.isEmpty())
314 if ( !QFileInfo( file ).completeSuffix().length() )
316 QString selFilter = theDlg->selectedNameFilter();
317 int idx = selFilter.indexOf( "(*." );
320 QString tail = selFilter.mid( idx + 3 );
321 int idx = tail.indexOf( " " );
323 idx = tail.indexOf( ")" );
324 QString ext = tail.left( idx );
326 file += QString( "." ) + ext;
333 TranslateDlg* Translate::getDialog( const int format, const bool import )
336 myDlg = new TranslateDlg( 0, 0, true );
341 QString formatFilter = QObject::tr( QString( "INF_FILTER_FORMAT_%1" ).arg( format ).toLatin1().constData() );
342 QString allFilter = QObject::tr( "INF_FILTER_FORMAT_ALL" );
345 filter.append( formatFilter );
346 filter.append( "\t" );
350 filter.append( allFilter );
351 filter.append( "\t" );
354 cout << filter.toLatin1().constData() << endl;
355 QStringList filters = filter.split( "\t" );
356 myDlg->setNameFilters ( filters );
360 myDlg->setWindowTitle( QObject::tr( "INF_APP_IMPORT" ) );
361 ((QFileDialog*)myDlg)->setFileMode( QFileDialog::ExistingFile );
365 myDlg->setWindowTitle( QObject::tr( "INF_APP_EXPORT" ) );
366 ((QFileDialog*)myDlg)->setFileMode( QFileDialog::AnyFile );
369 QString datadir = (QString (qgetenv ("CASROOT").constData()) + QObject::tr( QString("INF_PATH_%1").arg( format ).toLatin1().constData() ) );
378 myDlg->addMode( STEPControl_ManifoldSolidBrep, QObject::tr( "INF_BREP_MOIFOLD" ) );
379 myDlg->addMode( STEPControl_FacetedBrep, QObject::tr( "INF_BREP_FACETED" ) );
380 myDlg->addMode( STEPControl_ShellBasedSurfaceModel, QObject::tr( "INF_BREP_SHELL" ) );
381 myDlg->addMode( STEPControl_GeometricCurveSet, QObject::tr( "INF_BREP_CURVE" ) );
389 // ----------------------------- Import functionality -----------------------------
391 Handle(TopTools_HSequenceOfShape) Translate::importBREP( const QString& file )
393 Handle(TopTools_HSequenceOfShape) aSequence;
395 BRep_Builder aBuilder;
397 Standard_Boolean result = BRepTools::Read( aShape, (Standard_CString)file.toLatin1().constData(), aBuilder );
400 aSequence = new TopTools_HSequenceOfShape();
401 aSequence->Append( aShape );
406 Handle(TopTools_HSequenceOfShape) Translate::importIGES( const QString& file )
408 Handle(TopTools_HSequenceOfShape) aSequence;
409 IGESControl_Reader Reader;
410 int status = Reader.ReadFile( (Standard_CString)file.toLatin1().constData() );
412 if ( status == IFSelect_RetDone )
414 aSequence = new TopTools_HSequenceOfShape();
415 Reader.TransferRoots();
416 TopoDS_Shape aShape = Reader.OneShape();
417 aSequence->Append( aShape );
422 Handle(TopTools_HSequenceOfShape) Translate::importSTEP( const QString& file )
424 Handle(TopTools_HSequenceOfShape) aSequence;
426 STEPControl_Reader aReader;
427 IFSelect_ReturnStatus status = aReader.ReadFile( (Standard_CString)file.toLatin1().constData() );
428 if ( status == IFSelect_RetDone )
430 //Interface_TraceFile::SetDefault();
431 bool failsonly = false;
432 aReader.PrintCheckLoad( failsonly, IFSelect_ItemsByEntity );
434 int nbr = aReader.NbRootsForTransfer();
435 aReader.PrintCheckTransfer( failsonly, IFSelect_ItemsByEntity );
436 for ( Standard_Integer n = 1; n <= nbr; n++ )
438 bool ok = aReader.TransferRoot( n );
439 int nbs = aReader.NbShapes();
440 if ( ok == true && nbs > 0 )
442 aSequence = new TopTools_HSequenceOfShape();
443 for ( int i = 1; i <= nbs; i++ )
445 TopoDS_Shape shape = aReader.Shape( i );
446 aSequence->Append( shape );
454 // ----------------------------- Export functionality -----------------------------
456 bool Translate::exportBREP( const QString& file, const Handle(TopTools_HSequenceOfShape)& shapes )
458 if ( shapes.IsNull() || shapes->IsEmpty() )
461 TopoDS_Shape shape = shapes->Value( 1 );
462 return BRepTools::Write( shape, (Standard_CString)file.toLatin1().constData() );
465 bool Translate::exportIGES( const QString& file, const Handle(TopTools_HSequenceOfShape)& shapes )
467 if ( shapes.IsNull() || shapes->IsEmpty() )
470 IGESControl_Controller::Init();
471 IGESControl_Writer writer( Interface_Static::CVal( "XSTEP.iges.unit" ),
472 Interface_Static::IVal( "XSTEP.iges.writebrep.mode" ) );
474 for ( int i = 1; i <= shapes->Length(); i++ )
475 writer.AddShape ( shapes->Value( i ) );
476 writer.ComputeModel();
477 return writer.Write( (Standard_CString)file.toLatin1().constData() );
480 bool Translate::exportSTEP( const QString& file, const Handle(TopTools_HSequenceOfShape)& shapes )
482 if ( shapes.IsNull() || shapes->IsEmpty() )
485 TranslateDlg* theDlg = getDialog( -1, false );
486 STEPControl_StepModelType type = (STEPControl_StepModelType)theDlg->getMode();
490 IFSelect_ReturnStatus status;
492 if ( type == STEPControl_FacetedBrep && !checkFacetedBrep( shapes ) )
494 myInfo = QObject::tr( "INF_FACET_ERROR" );
498 STEPControl_Writer writer;
499 for ( int i = 1; i <= shapes->Length(); i++ )
501 status = writer.Transfer( shapes->Value( i ), type );
502 if ( status != IFSelect_RetDone )
506 status = writer.Write( (Standard_CString)file.toLatin1().constData() );
510 case IFSelect_RetError:
511 myInfo = QObject::tr( "INF_DATA_ERROR" );
513 case IFSelect_RetFail:
514 myInfo = QObject::tr( "INF_WRITING_ERROR" );
516 case IFSelect_RetVoid:
517 myInfo = QObject::tr( "INF_NOTHING_ERROR" );
520 return status == IFSelect_RetDone;
523 bool Translate::exportSTL( const QString& file, const Handle(TopTools_HSequenceOfShape)& shapes )
525 if ( shapes.IsNull() || shapes->IsEmpty() )
529 BRep_Builder builder;
530 builder.MakeCompound( res );
532 for ( int i = 1; i <= shapes->Length(); i++ )
534 TopoDS_Shape shape = shapes->Value( i );
535 if ( shape.IsNull() )
537 myInfo = QObject::tr( "INF_TRANSLATE_ERROR_INVALIDSHAPE" );
540 builder.Add( res, shape );
543 StlAPI_Writer writer;
544 writer.Write( res, (Standard_CString)file.toLatin1().constData() );
549 bool Translate::exportVRML( const QString& file, const Handle(TopTools_HSequenceOfShape)& shapes )
551 if ( shapes.IsNull() || shapes->IsEmpty() )
555 BRep_Builder builder;
556 builder.MakeCompound( res );
558 for ( int i = 1; i <= shapes->Length(); i++ )
560 TopoDS_Shape shape = shapes->Value( i );
561 if ( shape.IsNull() )
563 myInfo = QObject::tr( "INF_TRANSLATE_ERROR_INVALIDSHAPE" );
566 builder.Add( res, shape );
569 VrmlAPI_Writer writer;
570 writer.Write( res, (Standard_CString)file.toLatin1().constData() );
575 bool Translate::checkFacetedBrep( const Handle(TopTools_HSequenceOfShape)& shapes )
578 for ( int i = 1; i <= shapes->Length(); i++ )
580 TopoDS_Shape shape = shapes->Value( i );
581 for ( TopExp_Explorer fexp( shape, TopAbs_FACE ); fexp.More() && !err; fexp.Next() )
583 Handle(Geom_Surface) surface = BRep_Tool::Surface( TopoDS::Face( fexp.Current() ) );
584 if ( !surface->IsKind( STANDARD_TYPE( Geom_Plane ) ) )
587 for ( TopExp_Explorer eexp( shape, TopAbs_EDGE ); eexp.More() && !err; eexp.Next() )
589 Standard_Real fd, ld;
590 Handle(Geom_Curve) curve = BRep_Tool::Curve( TopoDS::Edge( eexp.Current() ), fd, ld );
591 if ( !curve->IsKind( STANDARD_TYPE( Geom_Line ) ) )