int QilexDoc::doc_new_kinematic_hand(ct_new_kinematic_chain *data)
{
   int error = 0;
   int tipus = 0;
   void * buffer ; //char *buffer;
   char *buftemp = (char*)malloc(1024);

   SoOutput out;

   size_t sizeModel = 0;

   SoSeparator *kinechain = new SoSeparator;
   SoSeparator *kinetest = new SoSeparator;

   Rchain_hand *kineengine = new Rchain_hand();

   SoTransform *pos_rot = new SoTransform;
   SbVec3f joinax;

   joinax.setValue(SbVec3f(data->x,data->y,data->z));
   pos_rot->translation.setValue(joinax);
   pos_rot->rotation.setValue(SbVec3f(data->axeX, data->axeY, data->axeZ), (float) rad((double) data->angle));

   kinechain = readFile(data->QsModelFile.latin1(), tipus);

   if (kinechain == NULL) // no object read
   { return 1; }
   else  // ok, there's no object with the same name
   {
      error = kineengine->init_dat(data->QsDatFile.latin1()); //

      if (error == 0)
      {
         kinechain->ref();
         kinetest = (SoSeparator*)SoNode::getByName(data->QsName.latin1());

         if (kinetest==NULL)
         {
            //we need to put it in a buffer to write the xml file
            // if is Ok
            SoOutput out;
            out.setBuffer(buftemp, 1024, reallocCB);

            SoWriteAction wa1(&out);
            wa1.apply(kinechain);

            out.getBuffer(buffer, sizeModel);

            kinechain->insertChild(pos_rot, 0);
         }
         error = doc_insert_kinematic_hand(kineengine, kinechain);
      }
   }

   if (error==0)
   {
      writeXML_kineelement((char *)buffer, sizeModel, tipus, data, kineengine);
   }
   return error;
}
//////////////////////////////////////////////////////////////////////////////
//
//  Description:
//    Convert the passed path list to a byte stream.
//    binaryFormat determines whether to copy the data in binary (TRUE) or
//    ascii (FALSE) format.
//
//  Use: public
//
void
SoByteStream::convert(SoPathList *pathList, SbBool binaryFormat)
//
//////////////////////////////////////////////////////////////////////////////
{
    // clear out any old data
    if (data != NULL) {
	free(data);
	data = NULL;
	numBytes = 0;
    }
	
    if ((pathList == NULL) || (pathList->getLength() == 0))
	return;

    // Write all the paths in the path list into an in-memory buffer
    SoWriteAction wa;
    SoOutput *out = wa.getOutput();
    out->setBinary(binaryFormat);
    out->setBuffer(malloc(128), 128, realloc);

    for (int i = 0; i < pathList->getLength(); i++) {
    	wa.apply((*pathList)[i]);
    }

    // Point to the byte stream data
    void *buf;
    size_t size;

    out->getBuffer(buf, size);
    data = buf;
    numBytes = (uint32_t) size;
    isRaw = FALSE;
}
int QilexDoc::doc_new_grasping_object(ct_new_grasping_object *data)
{
   int error = 0;
   int tipus = 0;

   void *buffer; //char *buffer;
   char *buftemp = (char*)malloc(1024);
   size_t sizeModel = 0;;

   SoSeparator *object = new SoSeparator;
   SoSeparator *objecttest = new SoSeparator;


   SoTransform *pos_rot = new SoTransform;
   SbVec3f joinax;
   SbVec3f joingrasp0;
   SbVec3f joingrasp1;
   SbVec3f joingrasp2;
   SbVec3f joingrasp3;

   joinax.setValue(SbVec3f(data->x,data->y,data->z));
   pos_rot->translation.setValue(joinax);
   pos_rot->recenter(joinax);
   pos_rot->rotation.setValue(SbVec3f(data->axeX, data->axeY, data->axeZ), (float) rad((double) data->angle));
   object = readFile(data->QsModelFile.latin1(), tipus);


   if (object == NULL) // no object read
   {
      error = 1 ;
   }
   else  // ok, there's no object with the same name
   {
      error = read_grasp_points(data);

      SoMaterial *bronze = new SoMaterial;
      bronze->ambientColor.setValue(0.33,0.22,0.27);
      bronze->diffuseColor.setValue(0.78,0.57,0.11);
      bronze->specularColor.setValue(0.99,0.94,0.81);
      bronze->shininess=0.28;

      SoSphere *grasp_sphere = new SoSphere;
      grasp_sphere->radius=7.0;

      SoFont *font = new SoFont;
      font->size.setValue(28);
      font->name.setValue("Times-Roman");
      SoSeparator *grasp_sep0 = new SoSeparator;
      SoTransform *grasp_transf0 = new SoTransform;
      SoSeparator *text0 = new SoSeparator;
      SoText2 *label_text0 = new SoText2;
      SoSeparator *grasp_sep1 = new SoSeparator;
      SoTransform *grasp_transf1 = new SoTransform;
      SoSeparator *text1 = new SoSeparator;
      SoText2 *label_text1 = new SoText2;
      SoSeparator *grasp_sep2 = new SoSeparator;
      SoTransform *grasp_transf2 = new SoTransform;
      SoSeparator *text2 = new SoSeparator;
      SoText2 *label_text2 = new SoText2;
      SoSeparator *grasp_sep3 = new SoSeparator;
      SoTransform *grasp_transf3 = new SoTransform;
      SoSeparator *text3 = new SoSeparator;
      SoText2 *label_text3 = new SoText2;

      //for (int i=0;i<data->num_point;i++)
      //{
       joingrasp0.setValue(SbVec3f(data->grasp_points[0].px,data->grasp_points[0].py,data->grasp_points[0].pz));
       grasp_transf0->translation.setValue(joingrasp0);
       grasp_transf0->recenter(joingrasp0);
       label_text0->string=" 1";
       text0->addChild(font);
       text0->addChild(label_text0);
       grasp_sep0->addChild(bronze);
       grasp_sep0->addChild(grasp_transf0);
       grasp_sep0->addChild(grasp_sphere);
       grasp_sep0->addChild(text0);
       //grasp_sep0->addChild(line0);
       joingrasp1.setValue(SbVec3f(data->grasp_points[1].px,data->grasp_points[1].py,data->grasp_points[1].pz));
       grasp_transf1->translation.setValue(joingrasp1);
       grasp_transf1->recenter(joingrasp1);
       label_text1->string=" 2";
       text1->addChild(font);
       text1->addChild(label_text1);
       grasp_sep1->addChild(bronze);
       grasp_sep1->addChild(grasp_transf1);
       grasp_sep1->addChild(grasp_sphere);
       grasp_sep1->addChild(text1);
       joingrasp2.setValue(SbVec3f(data->grasp_points[2].px,data->grasp_points[2].py,data->grasp_points[2].pz));
       grasp_transf2->translation.setValue(joingrasp2);
       grasp_transf2->recenter(joingrasp2);
       label_text2->string=" 3";
       text2->addChild(font);
       text2->addChild(label_text2);
       grasp_sep2->addChild(bronze);
       grasp_sep2->addChild(grasp_transf2);
       grasp_sep2->addChild(grasp_sphere);
       grasp_sep2->addChild(text2);
       joingrasp3.setValue(SbVec3f(data->grasp_points[3].px,data->grasp_points[3].py,data->grasp_points[3].pz));
       grasp_transf3->translation.setValue(joingrasp3);
       grasp_transf3->recenter(joingrasp3);
       label_text3->string=" 4";
       text3->addChild(font);
       text3->addChild(label_text3);
       grasp_sep3->addChild(bronze);
       grasp_sep3->addChild(grasp_transf3);
       grasp_sep3->addChild(grasp_sphere);
       grasp_sep3->addChild(text3);
       //object->addChild(grasp_sep);
      //}
      if (error == 0)
      {
      object->ref();
      objecttest = (SoSeparator*)SoNode::getByName(data->QsName.latin1());

      if (objecttest==NULL)
      {

         SoOutput out;
         out.setBuffer(buftemp, 1024, reallocCB);

         SoWriteAction wa1(&out);
         wa1.apply(object);

         out.getBuffer(buffer, sizeModel);

         object->setName(data->QsName.latin1());
	 //grasp_object->addChild(model_object);
	 object->addChild(grasp_sep0);
	 object->addChild(grasp_sep1);
	 object->addChild(grasp_sep2);
	 object->addChild(grasp_sep3);
         object->insertChild(pos_rot, 0);

         view->addObjectCell(object);
         error = 0;

         //writeXML_geomelement((char *)buffer, sizeModel, tipus, data);
	 //S'ha de canviar!!!!!
      }
      else
      {
         object->unref();
         error = 3;
      }
      }
   }
   return error;
}
int QilexDoc::doc_new_geometric_object(ct_new_geometric_object *data)
{
   int error = 0;
   int tipus = 0;

   void *buffer; //char *buffer;
   char *buftemp = (char*)malloc(1024);
   size_t sizeModel = 0;;

   SoSeparator *object = new SoSeparator;
   SoSeparator *objecttest = new SoSeparator;


   SoTransform *pos_rot = new SoTransform;
   SbVec3f joinax;


   joinax.setValue(SbVec3f(data->x,data->y,data->z));
   pos_rot->translation.setValue(joinax);
   pos_rot->recenter(joinax);
   pos_rot->rotation.setValue(SbVec3f(data->axeX, data->axeY, data->axeZ), (float) rad((double) data->angle));
   object = readFile(data->QsModelFile.latin1(), tipus);


   if (object == NULL) // no object read
   {
      error = 1 ;
   }
   else  // ok, there's no object with the same name
   {
      object->ref();
      objecttest = (SoSeparator*)SoNode::getByName(data->QsName.latin1());

      if (objecttest==NULL)
      {

         SoOutput out;
         out.setBuffer(buftemp, 1024, reallocCB);

         SoWriteAction wa1(&out);
         wa1.apply(object);

         out.getBuffer(buffer, sizeModel);

         object->setName(data->QsName.latin1());
         object->insertChild(pos_rot, 0);

         view->addObjectCell(object);
         error = 0;

         writeXML_geomelement((char *)buffer, sizeModel, tipus, data);
      }
      else
      {
         object->unref();
         error = 3;
      }
   }

   return error;
}