load ifc

 

void slotLoadRecentIfcFileClicked()
{
    QPushButton* btn_load = (QPushButton*)sender();
    if( !btn_load )
    {
        return;
    }
    
    int row = m_combo_recent_files->currentIndex();
    if( row < 0 || row >= m_combo_recent_files->count() )
    {
        return;
    }
    
    m_io_widget->setDisabled( true );
    if( row < m_recent_files.size() )
    {
        QString file_name = m_recent_files.at( row );
        slotLoadIfcFile( file_name );
    }
    m_io_widget->setDisabled(false);
}

 

void slotLoadIfcFile( QString& path_in )
{
    // redirect message callbacks
    m_system->getGeometryConverter()->setMessageCallBack( this, &TabReadWrite::messageTarget );
    m_system->getModelReader()->setMessageCallBack( this, &TabReadWrite::messageTarget );
    m_system->getModelWriter()->setMessageCallBack( this, &TabReadWrite::messageTarget );
    

    slotTxtOut( QString( "loading file: " ) + path_in );
    QApplication::processEvents();
    clock_t millisecs = clock();
    m_system->notifyModelCleared();
    m_txt_out->clear();
    QSettings settings(QSettings::UserScope, QLatin1String("IfcPlusPlus"));

    if( !QFile::exists(path_in) )
    {
        slotTxtOutError( QString("file ") + path_in + QString(" does not exist\n") );

        // remove all non-existing files from recent files combo
        for( int i=0; i<m_recent_files.size(); )
        {
            const QString& recent_file = m_recent_files[i];
            if( !QFile::exists(recent_file) )
            {
                m_recent_files.takeAt( i );
            }
            else
            {
                ++i;
            }
        }
        settings.setValue("recentFiles",m_recent_files );
        updateRecentFilesCombo();
        return;
    }
    else
    {
        // move to top of recent files list
        int i = m_recent_files.indexOf( path_in );
        if( i > 0 )
        {
            QString current_path = m_recent_files.takeAt( i );
            m_recent_files.insert( 0, current_path );
            m_recent_files.removeDuplicates();
            settings.setValue("recentFiles",m_recent_files );
            updateRecentFilesCombo();
        }
        else
        {
            m_recent_files.insert( 0, path_in );
            m_recent_files.removeDuplicates();
            settings.setValue("recentFiles",m_recent_files );
            updateRecentFilesCombo();
        }
    }

    try
    {
        shared_ptr<LoadIfcFileCommand> cmd_load( new LoadIfcFileCommand( m_system ) );
        std::wstring path_str = path_in.toStdWString();
        cmd_load->setFilePath( path_str );
        cmd_load->doCmd();
    }
    catch( OutOfMemoryException& e)
    {
        slotTxtOutError( e.what() );
    }
    catch( BuildingException& e )
    {
        slotTxtOutError( e.what() );
    }
    catch(std::exception& e)
    {
        slotTxtOutError( e.what() );
    }

    m_viewer->update();
    
    osgViewer::View* main_view = m_viewer->getMainView();
    if( main_view )
    {
        osgGA::CameraManipulator* camera_manip = main_view->getCameraManipulator();
        OrbitCameraManipulator* orbit_manip = dynamic_cast<OrbitCameraManipulator*>( camera_manip );
        if( orbit_manip )
        {
            osg::BoundingSphere bs = m_system->getModelNode()->computeBound();
            orbit_manip->zoomToBoundingSphere( bs );
        }
    }

    clock_t time_diff = clock() - millisecs;
    int num_entities = m_system->getIfcModel()->getMapIfcEntities().size();
    slotTxtOut( tr("File loaded: ") + QString::number(num_entities) + " entities in " + QString::number( round(time_diff*0.1)*0.01 ) + " sec."  );

    m_system->notifyModelLoadingDone();
    slotProgressValue( 1.0, "" );
}

 

bool doCmd()
{
    if( m_file_path.length() == 0 )
    {
        return false;
    }

    // first remove previously loaded geometry from scenegraph
    osg::ref_ptr<osg::Switch> model_switch = m_system->getModelNode();
    SceneGraphUtils::clearAllChildNodes( model_switch );
    m_system->clearSelection();

    // reset the IFC model
    shared_ptr<GeometryConverter> geometry_converter = m_system->getGeometryConverter();
    geometry_converter->clearMessagesCallback();
    geometry_converter->resetModel();
    std::stringstream err;

    try
    {
        // load file to IFC model
        m_system->getModelReader()->loadModelFromFile( m_file_path, geometry_converter->getBuildingModel() );

        // convert IFC geometric representations into Carve geometry
        geometry_converter->convertGeometry();

        // convert Carve geometry to OSG
        shared_ptr<ConverterOSG> converter_osg( new ConverterOSG( geometry_converter->getGeomSettings() ) );
        converter_osg->setMessageTarget( geometry_converter.get() );
        converter_osg->convertToOSG( geometry_converter->getShapeInputData(), model_switch );

        // in case there are IFC entities that are not in the spatial structure
        const std::map<int, shared_ptr<BuildingObject> >& objects_outside_spatial_structure = geometry_converter->getObjectsOutsideSpatialStructure();
        if( objects_outside_spatial_structure.size() > 0 )
        {
            osg::ref_ptr<osg::Switch> sw_objects_outside_spatial_structure = new osg::Switch();
            sw_objects_outside_spatial_structure->setName( "IfcProduct objects outside spatial structure" );

            converter_osg->addNodes( objects_outside_spatial_structure, sw_objects_outside_spatial_structure );
            if( sw_objects_outside_spatial_structure->getNumChildren() > 0 )
            {
                model_switch->addChild( sw_objects_outside_spatial_structure );
            }
        }
    }
    catch( OutOfMemoryException& e)
    {
        throw e;
    }
    catch( BuildingException& e )
    {
        err << e.what();
    }
    catch( std::exception& e )
    {
        err << e.what();
    }
    catch( ... )
    {
        err << "loadModelFromFile, createGeometryOSG failed" << std::endl;
    }

    try
    {
        if( model_switch )
        {
            bool optimize = true;
            if( optimize )
            {
                osgUtil::Optimizer opt;
                opt.optimize(model_switch);
            }

            // if model bounding sphere is far from origin, move to origin
            const osg::BoundingSphere& bsphere = model_switch->getBound();
            if( bsphere.center().length() > 10000 )
            {
                if( bsphere.center().length()/bsphere.radius() > 100 )
                {
                    std::unordered_set<osg::Geode*> set_applied;
                    SceneGraphUtils::translateGroup( model_switch, -bsphere.center(), set_applied );
                }
            }
        }
    }
    catch(std::exception& e)
    {
        err << e.what();
    }

    geometry_converter->clearInputCache();
    
    if( err.tellp() > 0 )
    {
        throw BuildingException( err.str().c_str() );
    }

    return true;
}

 

posted @ 2019-07-25 09:55  西北逍遥  阅读(325)  评论(0编辑  收藏  举报