3 #include "Application.h"
5 #include <Standard_WarningsDisable.hxx>
12 #include <QFileDialog>
13 #include <QApplication>
15 #include <QStyleFactory>
16 #include <Standard_WarningsRestore.hxx>
18 #include <AIS_Shape.hxx>
19 #include <AIS_InteractiveObject.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 )
253 case FormatBREP: return exportBREP( file, shapes );
254 case FormatIGES: return exportIGES( file, shapes );
255 case FormatSTEP: return exportSTEP( file, shapes );
256 case FormatSTL: return exportSTL ( file, shapes );
257 case FormatVRML: return exportVRML( file, shapes );
259 } catch ( Standard_Failure ) {
265 Handle(TopTools_HSequenceOfShape) Translate::getShapes( const Handle(AIS_InteractiveContext)& ic )
267 Handle(TopTools_HSequenceOfShape) aSequence;
268 Handle(AIS_InteractiveObject) picked;
269 for ( ic->InitSelected(); ic->MoreSelected(); ic->NextSelected() )
271 Handle(AIS_InteractiveObject) obj = ic->SelectedInteractive();
272 if ( obj->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
274 TopoDS_Shape shape = Handle(AIS_Shape)::DownCast(obj)->Shape();
275 if ( aSequence.IsNull() )
276 aSequence = new TopTools_HSequenceOfShape();
277 aSequence->Append( shape );
284 Selects a file from standard dialog acoording to selection 'filter'
286 QString Translate::selectFileName( const int format, const bool import )
288 TranslateDlg* theDlg = getDialog( format, import );
290 int ret = theDlg->exec();
292 qApp->processEvents();
295 QStringList fileNames;
296 if ( ret != QDialog::Accepted )
299 fileNames = theDlg->selectedFiles();
300 if (!fileNames.isEmpty())
303 if ( !QFileInfo( file ).completeSuffix().length() )
305 QString selFilter = theDlg->selectedNameFilter();
306 int idx = selFilter.indexOf( "(*." );
309 QString tail = selFilter.mid( idx + 3 );
310 idx = tail.indexOf( " " );
312 idx = tail.indexOf( ")" );
313 QString ext = tail.left( idx );
315 file += QString( "." ) + ext;
322 TranslateDlg* Translate::getDialog( const int format, const bool import )
325 myDlg = new TranslateDlg( 0, 0, true );
330 QString formatFilter = QObject::tr( QString( "INF_FILTER_FORMAT_%1" ).arg( format ).toLatin1().constData() );
331 QString allFilter = QObject::tr( "INF_FILTER_FORMAT_ALL" );
334 filter.append( formatFilter );
335 filter.append( "\t" );
339 filter.append( allFilter );
340 filter.append( "\t" );
343 std::cout << filter.toLatin1().constData() << std::endl;
344 QStringList filters = filter.split( "\t" );
345 myDlg->setNameFilters ( filters );
349 myDlg->setWindowTitle( QObject::tr( "INF_APP_IMPORT" ) );
350 ((QFileDialog*)myDlg)->setFileMode( QFileDialog::ExistingFile );
354 myDlg->setWindowTitle( QObject::tr( "INF_APP_EXPORT" ) );
355 ((QFileDialog*)myDlg)->setFileMode( QFileDialog::AnyFile );
365 myDlg->addMode( STEPControl_ManifoldSolidBrep, QObject::tr( "INF_BREP_MOIFOLD" ) );
366 myDlg->addMode( STEPControl_FacetedBrep, QObject::tr( "INF_BREP_FACETED" ) );
367 myDlg->addMode( STEPControl_ShellBasedSurfaceModel, QObject::tr( "INF_BREP_SHELL" ) );
368 myDlg->addMode( STEPControl_GeometricCurveSet, QObject::tr( "INF_BREP_CURVE" ) );
376 // ----------------------------- Import functionality -----------------------------
378 Handle(TopTools_HSequenceOfShape) Translate::importBREP( const QString& file )
380 Handle(TopTools_HSequenceOfShape) aSequence;
382 BRep_Builder aBuilder;
383 TCollection_AsciiString aFilePath = file.toUtf8().data();
384 Standard_Boolean result = BRepTools::Read( aShape, aFilePath.ToCString(), aBuilder );
387 aSequence = new TopTools_HSequenceOfShape();
388 aSequence->Append( aShape );
393 Handle(TopTools_HSequenceOfShape) Translate::importIGES( const QString& file )
395 Handle(TopTools_HSequenceOfShape) aSequence;
396 TCollection_AsciiString aFilePath = file.toUtf8().data();
398 IGESControl_Reader Reader;
399 int status = Reader.ReadFile(aFilePath.ToCString() );
401 if ( status == IFSelect_RetDone )
403 aSequence = new TopTools_HSequenceOfShape();
404 Reader.TransferRoots();
405 TopoDS_Shape aShape = Reader.OneShape();
406 aSequence->Append( aShape );
411 Handle(TopTools_HSequenceOfShape) Translate::importSTEP( const QString& file )
413 Handle(TopTools_HSequenceOfShape) aSequence = new TopTools_HSequenceOfShape;
414 TCollection_AsciiString aFilePath = file.toUtf8().data();
415 STEPControl_Reader aReader;
416 IFSelect_ReturnStatus status = aReader.ReadFile( aFilePath.ToCString() );
417 if ( status != IFSelect_RetDone )
422 //Interface_TraceFile::SetDefault();
423 bool failsonly = false;
424 aReader.PrintCheckLoad( failsonly, IFSelect_ItemsByEntity );
426 int nbr = aReader.NbRootsForTransfer();
427 aReader.PrintCheckTransfer( failsonly, IFSelect_ItemsByEntity );
428 for ( Standard_Integer n = 1; n <= nbr; n++ )
430 aReader.TransferRoot( n );
433 int nbs = aReader.NbShapes();
436 for ( int i = 1; i <= nbs; i++ )
438 TopoDS_Shape shape = aReader.Shape( i );
439 aSequence->Append( shape );
446 // ----------------------------- Export functionality -----------------------------
448 bool Translate::exportBREP( const QString& file, const Handle(TopTools_HSequenceOfShape)& shapes )
450 if ( shapes.IsNull() || shapes->IsEmpty() )
453 TopoDS_Shape shape = shapes->Value( 1 );
455 const TCollection_AsciiString anUtf8Path (file.toUtf8().data());
457 return BRepTools::Write( shape, anUtf8Path.ToCString() );
460 bool Translate::exportIGES( const QString& file, const Handle(TopTools_HSequenceOfShape)& shapes )
462 if ( shapes.IsNull() || shapes->IsEmpty() )
465 IGESControl_Controller::Init();
466 IGESControl_Writer writer( Interface_Static::CVal( "XSTEP.iges.unit" ),
467 Interface_Static::IVal( "XSTEP.iges.writebrep.mode" ) );
469 for ( int i = 1; i <= shapes->Length(); i++ )
470 writer.AddShape ( shapes->Value( i ) );
471 writer.ComputeModel();
473 const TCollection_AsciiString anUtf8Path (file.toUtf8().data());
475 return writer.Write( anUtf8Path.ToCString() );
478 bool Translate::exportSTEP( const QString& file, const Handle(TopTools_HSequenceOfShape)& shapes )
480 if ( shapes.IsNull() || shapes->IsEmpty() )
483 TranslateDlg* theDlg = getDialog( -1, false );
484 STEPControl_StepModelType type = (STEPControl_StepModelType)theDlg->getMode();
488 IFSelect_ReturnStatus status;
490 if ( type == STEPControl_FacetedBrep && !checkFacetedBrep( shapes ) )
492 myInfo = QObject::tr( "INF_FACET_ERROR" );
496 STEPControl_Writer writer;
497 for ( int i = 1; i <= shapes->Length(); i++ )
499 status = writer.Transfer( shapes->Value( i ), type );
500 if ( status != IFSelect_RetDone )
504 const TCollection_AsciiString anUtf8Path (file.toUtf8().data());
506 status = writer.Write( anUtf8Path.ToCString() );
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" );
519 case IFSelect_RetStop:
520 case IFSelect_RetDone:
523 return status == IFSelect_RetDone;
526 bool Translate::exportSTL( const QString& file, const Handle(TopTools_HSequenceOfShape)& shapes )
528 if ( shapes.IsNull() || shapes->IsEmpty() )
532 BRep_Builder builder;
533 builder.MakeCompound( res );
535 for ( int i = 1; i <= shapes->Length(); i++ )
537 TopoDS_Shape shape = shapes->Value( i );
538 if ( shape.IsNull() )
540 myInfo = QObject::tr( "INF_TRANSLATE_ERROR_INVALIDSHAPE" );
543 builder.Add( res, shape );
546 StlAPI_Writer writer;
548 const TCollection_AsciiString anUtf8Path (file.toUtf8().data());
550 writer.Write( res, anUtf8Path.ToCString() );
555 bool Translate::exportVRML( const QString& file, const Handle(TopTools_HSequenceOfShape)& shapes )
557 if ( shapes.IsNull() || shapes->IsEmpty() )
561 BRep_Builder builder;
562 builder.MakeCompound( res );
564 for ( int i = 1; i <= shapes->Length(); i++ )
566 TopoDS_Shape shape = shapes->Value( i );
567 if ( shape.IsNull() )
569 myInfo = QObject::tr( "INF_TRANSLATE_ERROR_INVALIDSHAPE" );
572 builder.Add( res, shape );
575 VrmlAPI_Writer writer;
577 const TCollection_AsciiString anUtf8Path (file.toUtf8().data());
579 writer.Write( res, anUtf8Path.ToCString() );
584 bool Translate::checkFacetedBrep( const Handle(TopTools_HSequenceOfShape)& shapes )
587 for ( int i = 1; i <= shapes->Length(); i++ )
589 TopoDS_Shape shape = shapes->Value( i );
590 for ( TopExp_Explorer fexp( shape, TopAbs_FACE ); fexp.More() && !err; fexp.Next() )
592 Handle(Geom_Surface) surface = BRep_Tool::Surface( TopoDS::Face( fexp.Current() ) );
593 if ( !surface->IsKind( STANDARD_TYPE( Geom_Plane ) ) )
596 for ( TopExp_Explorer eexp( shape, TopAbs_EDGE ); eexp.More() && !err; eexp.Next() )
598 Standard_Real fd, ld;
599 Handle(Geom_Curve) curve = BRep_Tool::Curve( TopoDS::Edge( eexp.Current() ), fd, ld );
600 if ( !curve->IsKind( STANDARD_TYPE( Geom_Line ) ) )