OBJ FileIO in Rhino6

Hi, I’m currently working at mesh exporting in Rhino6 by C++ and encountered same problem.

How to export a selected mesh as wavefront .obj file to target path?

Currently I’m using code below to import single OBJ file as mesh and export selected mesh as OBJ file.

Import OBJ

   CString objFileName = thirdPartyWriteObj();
   script.Format(L"_-Import \"%ls\" _Enter", static_cast<const wchar_t*>(objFileName ));

	RhinoApp().RunScript(RhinoApp().ActiveDoc()->RuntimeSerialNumber(), static_cast<const wchar_t*>(script), 0);

RunScript() sometimes start excuting before thirdPartyWriteObj() finished, resulting in imported OBJ file errors.

  1. Is there any way to control the excuting sequence when there are RhinoScripts in code?
  2. is there any way to import OBJ files without using scripts?

Export selected mesh as OBJ

    pMeshObj->Select(true, true, true);
	ON_wString script2 = L"_-Export testExport.obj";
	RhinoApp().RunScript(RhinoApp().ActiveDoc()->RuntimeSerialNumber(), static_cast<const wchar_t*>(script2), 0);
	

By script2, I can only export pMeshObj at Desktop with selecting exporting parameters manually in command line.

  1. How to export file without command line parameter selection?
  2. How to export file to target path?
  3. Is there any way to export a simplest OBJ file? (Say, only vertex coordinates and face connectivitys without normals and uvs). If not, how to access mesh data to implement a custom WriteOBJ method?

Hi @Liu2,

Use the Enter command.

ON_wString script;
script.Format(L"_-Export \"%ls\" _Enter", static_cast<const wchar_t*>(filename));
RhinoApp().RunScript(context.m_doc.RuntimeSerialNumber(), static_cast<const wchar_t*>(script), 0);

– Dale

Thanks Dale.
I’m working on a Rhino6 plugin which is support Rhino mesh export and OBJ file import.

I know Rhino provides export and import scripts, but RunScript() sometimes excutes early.
eg.
Export early than mesh data prepared.
Import early than view setting prepared.

So I have implement a WriteOBJ() method myself to export mesh.

Question:

  • Is there any way to control the excuting order of RunScript()?
  • If not, how to read a OBJ file through ON_BinaryFile?

Can you provide me sample source code, that I can run here, that repeats this?

No.

The openNURBS library only reads and writes .3dm files.

Let’s figure out your RunScript issue.

– Dale

Thanks for you answer.

After my code review, I think It is cause by numerical issue from my liner algebra operation.

RunScript() is fine.