3MFConsortium / lib3mf

lib3mf is an implementation of the 3D Manufacturing Format file standard
http://3mf.io
BSD 2-Clause "Simplified" License
227 stars 89 forks source link

Failing to write a 3mf generated from OpenNurbs meshes. #366

Closed Sernior closed 2 months ago

Sernior commented 2 months ago

Hi, I am not sure if I am missing something or not but I have a problem with 3mf files generated starting from a vector<ON_Mesh*>.

        std::vector<ON_Mesh*> lMeshes = GetMeshes();

    auto wrapper = Lib3MF::CWrapper::loadLibrary();

    auto model = wrapper->CreateModel();
    {
        for (size_t i = 0; i < lMeshes.size(); ++i)
        {
            if (lMeshes[i])
            {
                ON_Mesh* mesh = lMeshes[i];

                auto vertices = mesh->m_V;

                auto faces = mesh->m_F;

                auto meshObject = model->AddMeshObject();

                for (int j = 0; j < mesh->VertexCount(); j++)
                {
                    auto vertex = vertices[j];
                    Lib3MF::sPosition p = { vertex.x, vertex.y, vertex.z };
                    meshObject->AddVertex(p);
                }

                for (int j = 0; j < mesh->FaceCount(); j++)
                {
                    auto face = faces[j];
                    Lib3MF::sTriangle t = { face.vi[0], face.vi[1], face.vi[2] };
                    meshObject->AddTriangle(t);
                }
            }
        }
    }

    auto writer = model->QueryWriter(LIB3MF_FILE_TYPE);

    if (writer)
    {
        writer->WriteToFile(lFullPath.toStdString());
    }

Now these meshes are valid. If I draw them I can see them or if I write them with any other format, but when I generate the 3mf file with this code I cannot open it with 3D builder nor Rhino. Oddly enough this website https://imagetostl.com/ can still open and show me the meshes of the 'invalid' 3mf files. At this point I am not sure if I am missing something.

vijaiaeroastro commented 2 months ago

@Sernior Do you have a build item for these mesh objects in your code ?

You need to have a

model->AddBuildItem(meshObject.get(), wrapper->GetIdentityTransform());

after you are done adding all the vertices and triangles of a given mesh. Give that a try and let me us know if it is still a problem

Sernior commented 2 months ago

It works perfectly now thank you. My bad, I did not expect after model->AddMeshObject(), AddBuildItem to be required unless you want to apply a Transform. Thank you.