Write multiple object in a loop


#1

Hi,

I am quite new to the openNURBS and need some help with writing multiple cylinders or boxes to a 3DM file. I modified the example_brep.cpp file to write two cylinders but I see only the first cylinder in the resulting 3dm file. The final goal is to be able to read multiple cylinders of varying sizes and orientation and write them into a single 3dm file.

Thanks,

Vimal

#include "../opennurbs.h"
#include "../examples_linking_pragmas.h"
#include <map>
#include <string>
using namespace std;

static ON_Brep* MakeCylinder(const double Diameter,
                             const double Length,
                             const ON_3dPoint Origin,
                             const ON_3dVector Axis,
                             ON_TextLog& error_log )
{
    double Radius;
    
    // Radius of Cylinder
    Radius = 0.5*Diameter;
    
    // Base Plane on which the cylinder is drawn
    ON_Plane base(Origin,Axis);
    
    // Base Circle
    ON_Circle base_circle(base,Radius);
    
    // Base Cylinder
    ON_Cylinder cylinder(base_circle,Length);
    
    //Brep
    ON_Brep *brep = ON_BrepCylinder( cylinder, TRUE, TRUE );
    
    return brep;
}


//int main( int argc, const char *argv[] )
int main()
{

    // Mapping of Cylinder Axis
    std::map<std::string, ON_3dVector> Axis;
    Axis["-X"] = ON_3dVector(-1.0, 0.0, 0.0);
    Axis["X"]  = ON_3dVector(+1.0, 0.0, 0.0);
    Axis["-Y"] = ON_3dVector( 0.0,-1.0, 0.0);
    Axis["Y"]  = ON_3dVector( 0.0,+1.0, 0.0);
    Axis["-Z"] = ON_3dVector( 0.0, 0.0,-1.0);
    Axis["Z"]  = ON_3dVector( 0.0, 0.0,+1.0);
    
    // Initialze opennurbs library
    ON::Begin();

    // Error Log
    ON_TextLog error_log;
    
    // 3DM Model
    ONX_Model model;
  
    // Add Cylinder
    ONX_Model_Object& mo = model.m_object_table.AppendNew();
    ON_Brep* brep = new ON_Brep();
    
    {
    brep = MakeCylinder(5.0,40.0,ON_3dPoint(10.0,10.0,-20.0),Axis["-Z"],error_log);
    if (!brep) return 1;
    mo.m_object = brep;
    mo.m_bDeleteObject = true; // ~ONX_Model will delete brep
    brep = 0;
    }
    
    brep = MakeCylinder(8.0,10.0,ON_3dPoint(50.0,20.0,-20.0),Axis["-X"],error_log);
    if (!brep) return 1;
    mo.m_object = brep;
    mo.m_bDeleteObject = true; // ~ONX_Model will delete brep
    brep = 0;
    
    // version will be ON_BinaryArchive::CurrentArchiveVersion()
    int version = 0;
    
    // Output Model
    model.Polish();
    const char* filename = "my_brep.3dm";
    bool rc = model.Write(filename,
               version,
               __FILE__ "example_brep.cpp " __DATE__,
               &error_log
               );

    if (rc)
        printf("Wrote %s.\n",filename);
    else
        printf("Errors writing %s.\n",filename);

    // Clean up opennurbs class definition information.
    // Opennurbs will not work correctly after ON::End() is called.
    ON::End();

    return 0;
}

(Dale Fugier) #2

Hi Vimal,

You are only doing this once:

ONX_Model_Object& mo = model.m_object_table.AppendNew();

You will need to add a model object for every cylinder.

Does this help?

– Dale


#3

Hi Dale,

Thanks for clarifying this. I was able to figure this out after looking through the API documentation. I will post the solution in a bit. Please do let me know if that’s the best approach.

Thanks,

Vimal


#4
	for (i = 0; i < NObjects; i++) {

		fid_mcrout >> objLayerName;
		fid_mcrout >> objType;

		// Add new layer
		layer.SetLayerName(objLayerName.c_str());
		layer.SetVisible(true);
		layer.SetLocked(false);
		layer.SetLayerIndex(i);
		model.m_layer_table.Append(layer);

		// Add new object
		model.m_object_table.AppendNew();

		if (objType.compare(string("C")) == 0) {
			fid_mcrout >> COriginX >> COriginY >> COriginZ >> CDiameter >> CLength >> CAxis;
			brep = MakeCylinder(CDiameter, CLength, ON_3dPoint(COriginX, COriginY, COriginZ), Axis[CAxis], length_tol, error_log);
		}
		if (objType.compare(string("B")) == 0) {
			fid_mcrout >> BOriginX >> BOriginY >> BOriginZ >> Bdx >> Bdy >> Bdz;
			brep = MakeBox(Bdx, Bdy, Bdz, ON_3dPoint(BOriginX, BOriginY, BOriginZ), length_tol, error_log);
		}

		if (brep) {
			model.m_object_table[i].m_object = brep;
			model.m_object_table[i].m_bDeleteObject = true;
			model.m_object_table[i].m_attributes.m_layer_index = i;
			brep = 0;
		}
	}