how to load an STL file using QMesh



  • Hi all,

    I'm really new to Qt, using it only just a couple of weeks.

    I'm trying to find a way to load STL (stereolithography) files and display them. I've checked the documentation but there is no example of how to implement it.

    Is there any tutorial or past example that I could study for doing this?

    Thank you in advance for any help.



  • @Dimis I'd say you can have a look at the variety of Qt 3D examples to get used to the framework, and then you may also take a look at some previous posts: here and here.


  • Moderators

    @Dimis
    I personally haven't used it yet, but by looking at the docs you should use the source property

    QMesh mesh = ...;
    mesh.setSource( QUrl::fromLocalFile("path/to/local/file.stl") );
    


  • Thank you for your responses. I'll give a try.



  • I've sort it out. Just for anyone interested in this, my code for loading STL files is below.

    QGuiApplication a(argc, argv);
    QUrl data = QUrl::fromLocalFile("C:/Qt/Examples/objloader2/cadtrainingblock24asc.stl");

    Qt3DExtras::Qt3DWindow view;
    
    Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity;
    Qt3DCore::QEntity *flyingwedge = new Qt3DCore::QEntity(rootEntity);
    
    Qt3DExtras::QPhongMaterial *material = new Qt3DExtras::QPhongMaterial();
    material->setDiffuse(QColor(254, 254, 254));
    
    Qt3DRender::QMesh *flyingwedgeMesh = new Qt3DRender::QMesh;
    flyingwedgeMesh->setMeshName("FlyingWedge");
    flyingwedgeMesh->setSource(data);
    flyingwedge->addComponent(flyingwedgeMesh);
    flyingwedge->addComponent(material);
    
    Qt3DRender::QCamera *camera = view.camera();
    camera->lens()->setPerspectiveProjection(40.0f, 16.0f/9.0f, 0.1f, 1000.0f);
    camera->setPosition(QVector3D(0, 0, 40.0f));
    camera->setViewCenter(QVector3D(0, 0, 0));
    
    Qt3DCore::QEntity *lightEntity = new Qt3DCore::QEntity(rootEntity);
    Qt3DRender::QPointLight *light = new Qt3DRender::QPointLight(lightEntity);
    light->setColor("white");
    light->setIntensity(0.8f);
    lightEntity->addComponent(light);
    
    Qt3DCore::QTransform *lightTransform = new Qt3DCore::QTransform(lightEntity);
    lightTransform->setTranslation(QVector3D(60, 0, 40.0f));
    lightEntity->addComponent(lightTransform);
    
    Qt3DExtras::QOrbitCameraController *camController = new Qt3DExtras::QOrbitCameraController(rootEntity);
    camController->setCamera(camera);
    
    view.setRootEntity(rootEntity);
    view.show();
    
    return a.exec();

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.