示例#1
0
文件: brep.cpp 项目: cciechad/brlcad
/**
 *			R T _ B R E P _ I M P O R T 5
 */
int
rt_brep_import5(struct rt_db_internal *ip, const struct bu_external *ep, register const fastf_t *mat, const struct db_i *dbip)
{
    ON::Begin();
    TRACE1("rt_brep_import5");
    struct rt_brep_internal* bi;
    BU_CK_EXTERNAL(ep);
    RT_CK_DB_INTERNAL(ip);
    ip->idb_major_type = DB5_MAJORTYPE_BRLCAD;
    ip->idb_type = ID_BREP;
    ip->idb_meth = &rt_functab[ID_BREP];
    ip->idb_ptr = bu_malloc(sizeof(struct rt_brep_internal), "rt_brep_internal");

    bi = (struct rt_brep_internal*)ip->idb_ptr;
    bi->magic = RT_BREP_INTERNAL_MAGIC;

    RT_MemoryArchive archive(ep->ext_buf, ep->ext_nbytes);
    ONX_Model model;
    ON_TextLog dump(stderr);
    //archive.Dump3dmChunk(dump);
    model.Read(archive, &dump);

    if (model.IsValid(&dump)) {
	ONX_Model_Object mo = model.m_object_table[0];
	// XXX does openNURBS force us to copy? it seems the answer is
	// YES due to the const-ness
	bi->brep = new ON_Brep(*ON_Brep::Cast(mo.m_object));
	return 0;
    } else {
	return -1;
    }
}
static void read_file( const char* filename, ON_Object*& pObject )
{
  // see example_read.cpp for information about read 3dm files
  // This code will only read the file created by write_file().
  // This code should not be used as a model for reading general 3dm files.


  ONX_Model model;
  model.Read( filename );

  if ( model.m_object_table.Count() > 0 )
  {
    pObject = const_cast<ON_Object*>(model.m_object_table[0].m_object);
    model.m_object_table[0].m_object = 0; // so ~ONX_Model will not delete object
  }

}
示例#3
0
int main(int argc, const char *argv[])
{
  // If you are using OpenNURBS as a Windows DLL, then you MUST use
  // ON::OpenFile() to open the file.  If you are not using OpenNURBS
  // as a Windows DLL, then you may use either ON::OpenFile() or fopen()
  // to open the file.

  int argi;
  if (argc < 2)
  {
    Usage(argv[0]);
    return 0;
  }

  // Call once in your application to initialze opennurbs library
  ON::Begin();

  int version = 0; // write current Rhino file

                   // default dump is to stdout
  ON_TextLog dump_to_stdout;
  ON_TextLog* dump = &dump_to_stdout;

  ON_String input;
  ON_String output;
  ON_String logfile;

  for (argi = 1; argi < argc; argi++)
  {
    ON_String arg(argv[argi]);

    if (arg.Left(10).CompareOrdinal("--version=", true) == 0)
    {
      arg = arg.Mid(10);
      version = atoi(arg);
      continue;
    }

    if (arg.Left(2).CompareOrdinal("/v", true) == 0 || arg.Left(2).CompareOrdinal("-v", true) == 0)
    {
      argi++;
      const char* sversion = argv[argi];
      version = atoi(sversion);
      continue;
    }

    if (arg.Left(6).CompareOrdinal("--log=", true) == 0)
    {
      arg = arg.Mid(6);
      logfile = arg;
      continue;
    }

    if (input.IsEmpty())
    {
      input = arg;
      if (false == ON_FileStream::Is3dmFile(input, true))
      {
        input = ON_String::EmptyString;
        break;
      }
      continue;
    }

    if (output.IsEmpty())
    {
      output = arg;
      continue;
    }

    // Invalid command line parameter
    input = ON_String::EmptyString;
    output = ON_String::EmptyString;
    break;
  }

  if (input.IsEmpty() || output.IsEmpty())
  {
    Usage(argv[0]);
    return 1;
  }


  dump->Print("\nOpenNURBS Archive File:  %s\n", static_cast<const char*>(input) );

  // open file containing opennurbs archive
  FILE* archive_fp = ON_FileStream::Open3dmToRead(input);
  if (nullptr == archive_fp)
  {
    dump->Print("  Unable to open file.\n");
    return 1;
  }

  dump->PushIndent();

  // create achive object from file pointer
  ON_BinaryFile archive(ON::archive_mode::read3dm, archive_fp);

  // read the contents of the file into "model"
  ONX_Model model;
  bool rc = model.Read(archive, dump);
  // close the file
  ON::CloseFile(archive_fp);

  if (false == rc)
  {
    dump->Print("Errors during reading.\n");
    return 1;
  }

  if (HasErrorsOrWarnings(dump, "reading input file"))
    return 1;
  
  // print diagnostic
  dump->Print("Successfully read.\n");
  
  // Write file
  model.m_sStartSectionComments = "Converted by example_convert.exe";
  bool outrc = model.Write(output, version, dump);
  if (HasErrorsOrWarnings(dump, "writing output file"))
    return 1;
  
  if (outrc)
  {
    dump->Print("model.Write(%s) succeeded.\n", static_cast<const char*>(output));
    ONX_Model model2;
    if (model2.Read(output, dump))
    {
      dump->Print("model2.Read(%s) succeeded.\n", static_cast<const char*>(output));
      if (HasErrorsOrWarnings(dump, "verifying output file"))
        return 1;

      if (!logfile.IsEmpty())
      {
        FILE* fp = ON::OpenFile(logfile, "w");
        ON_TextLog log(fp);
        model2.Dump(log);
        ON::CloseFile(fp);
      }

    }
    else
    {
      dump->Print("model2.Read(%s) failed.\n", static_cast<const char*>(output));
    }

    dump->PopIndent();
  }

  // OPTIONAL: Call just before your application exits to clean
  //           up opennurbs class definition information.
  //           Opennurbs will not work correctly after ON::End()
  //           is called.
  ON::End();

  return 0;
}
int main( int argc, const char *argv[] )
{
    // If you are using OpenNURBS as a Windows DLL, then you MUST use
    // ON::OpenFile() to open the file.  If you are not using OpenNURBS
    // as a Windows DLL, then you may use either ON::OpenFile() or fopen()
    // to open the file.

    int argi;
    if ( argc < 2 )
    {
        printf("Syntax: %s [-out:outputfilename.txt] file1.3dm file2.3dm ...\n",argv[0] );
        return 0;
    }

    // Call once in your application to initialze opennurbs library
    ON::Begin();

    // default dump is to stdout
    ON_TextLog dump_to_stdout;
    ON_TextLog* dump = &dump_to_stdout;
    FILE* dump_fp = 0;

    ONX_Model model;

    for ( argi = 1; argi < argc; argi++ )
    {
        const char* arg = argv[argi];

        // check for -out or /out option
        if ( ( 0 == strncmp(arg,"-out:",5) || 0 == strncmp(arg,"/out:",5) )
                && arg[5] )
        {
            // change destination of dump file
            const char* sDumpFilename = arg+5;
            FILE* text_fp = ON::OpenFile(sDumpFilename,"w");
            if ( text_fp )
            {
                if ( dump_fp )
                {
                    delete dump;
                    ON::CloseFile(dump_fp);
                }
                dump_fp = text_fp;
                dump = new ON_TextLog(dump_fp);
            }
            continue;
        }

        const char* sFileName = arg;

        dump->Print("\nOpenNURBS Archive File:  %s\n", sFileName );

        // open file containing opennurbs archive
        FILE* archive_fp = ON::OpenFile( sFileName, "rb");
        if ( !archive_fp )
        {
            dump->Print("  Unable to open file.\n" );
            continue;
        }

        dump->PushIndent();

        // create achive object from file pointer
        ON_BinaryFile archive( ON::read3dm, archive_fp );

        // read the contents of the file into "model"
        bool rc = model.Read( archive, dump );

        // close the file
        ON::CloseFile( archive_fp );

        // print diagnostic
        if ( rc )
            dump->Print("Successfully read.\n");
        else
            dump->Print("Errors during reading.\n");

        // see if everything is in good shape
        if ( model.IsValid(dump) )
            dump->Print("Model is valid.\n");
        else
        {
            model.Polish();
            if ( model.IsValid() )
            {
                dump->Print("Model is valid after calling Polish().\n");
            }
            else
            {
                dump->Print("Model is not valid.\n");
            }
        }

        /*
        int oi = 14;
        if ( oi >=0 && oi < model.m_object_table.Count() )
        {
          dump->Print("m_object_table[%d].m_object:\n",oi);
          dump->PushIndent();
          model.m_object_table[oi].m_object->Dump(*dump);
          dump->PopIndent();
        }
        */

        int version = 0; // write current Rhino file

        ON_String outfile = sFileName;
        int len = outfile.Length() - 4;
        outfile.SetLength(len);
        outfile += "_roundtrip.3dm";
        bool outrc = model.Write( outfile, version, "roundtrip", dump );
        if ( outrc )
        {
            dump->Print("model.Write(%s) succeeded.\n",outfile.Array());
            ONX_Model model2;
            if ( model2.Read( outfile, dump ) )
            {
                dump->Print("model2.Read(%s) succeeded.\n",outfile.Array());
                if ( model2.IsValid(dump) )
                {
                    dump->Print("Model2 is valid.\n");
                }
                else
                {
                    dump->Print("Model2 is not valid.\n");
                }
                /*
                if ( oi >=0 && oi < model2.m_object_table.Count() )
                {
                  dump->Print("m_object_table[%d].m_object:\n",oi);
                  dump->PushIndent();
                  model2.m_object_table[oi].m_object->Dump(*dump);
                  dump->PopIndent();
                }
                */
            }
            else
            {
                dump->Print("model2.Read(%s) failed.\n",outfile.Array());
            }
        }
        else
            dump->Print("model.Write(%s) failed.\n",outfile.Array());

        // destroy this model
        model.Destroy();

        dump->PopIndent();
    }

    if ( dump_fp )
    {
        // close the text dump file
        delete dump;
        ON::CloseFile( dump_fp );
    }

    // OPTIONAL: Call just before your application exits to clean
    //           up opennurbs class definition information.
    //           Opennurbs will not work correctly after ON::End()
    //           is called.
    ON::End();

    return 0;
}
/*
Returns:
  True if .3dm file was successfully read into an ONX_Model class.
*/
static bool ReadFileHelper( 
  const wchar_t* sFileName,
  bool bVerboseTextDump,
  bool bChunkDump,
  ON_TextLog& dump
  )
{
  if ( bChunkDump )
  {
    return Dump3dmFileHelper(sFileName,dump);
  }

  ONX_Model model;

  dump.Print("\nOpenNURBS Archive File:  %ls\n", sFileName );

  // open file containing opennurbs archive
  FILE* archive_fp = ON::OpenFile( sFileName, L"rb");
  if ( !archive_fp ) 
  {
    dump.Print("  Unable to open file.\n" );
    return false;
  }

  dump.PushIndent();

  // create achive object from file pointer
  ON_BinaryFile archive( ON::read3dm, archive_fp );

  // read the contents of the file into "model"
  bool rc = model.Read( archive, &dump );

  // close the file
  ON::CloseFile( archive_fp );

  // print diagnostic
  if ( rc )
    dump.Print("Successfully read.\n");
  else
    dump.Print("Errors during reading.\n");

  // see if everything is in good shape
  if ( model.IsValid(&dump) )
  {
    dump.Print("Model is valid.\n");
  }
  else
  {
    dump.Print("Model is not valid.\n");
  }

  // create a text dump of the model
  if ( bVerboseTextDump )
  {
    dump.PushIndent();
    model.Dump(dump);
    dump.PopIndent();
  }

  // destroy this model
  model.Destroy();

  dump.PopIndent();

  return rc;
}
CRhinoCommand::result CCommandSampleImportMeshes::RunCommand( const CRhinoCommandContext& context )
{
  CWnd* pMainWnd = CWnd::FromHandle(RhinoApp().MainWnd());
  if (0 == pMainWnd)
    return CRhinoCommand::failure;
 
  CRhinoGetFileDialog gf;
  gf.SetScriptMode(context.IsInteractive() ? FALSE : TRUE);
  BOOL rc = gf.DisplayFileDialog(CRhinoGetFileDialog::open_rhino_only_dialog, 0, pMainWnd);
  if (!rc)
    return CRhinoCommand::cancel;
 
  ON_wString filename = gf.FileName();
  filename.TrimLeftAndRight();
  if (filename.IsEmpty())
    return CRhinoCommand::nothing;

  if (!CRhinoFileUtilities::FileExists(filename))
  {
    RhinoApp().Print(L"File not found\n");
    return CRhinoCommand::failure;
  }

  FILE* archive_fp = ON::OpenFile(filename, L"rb");
  if (0 == archive_fp)
  {
    RhinoApp().Print(L"Unable to open file\n");
    return CRhinoCommand::failure;
  }

  ON_BinaryFile archive(ON::read3dm, archive_fp);

  ONX_Model model;
  rc = model.Read(archive) ? TRUE : FALSE;

  ON::CloseFile( archive_fp );

  if (!rc)
  {
    RhinoApp().Print(L"Error reading file\n");
    return CRhinoCommand::failure;
  }

  int num_imported = 0;
  for (int i = 0; i < model.m_object_table.Count(); i++)
  {
    const ONX_Model_Object& model_object = model.m_object_table[i];
    const ON_Mesh* mesh = ON_Mesh::Cast(model_object.m_object);
    if (0 != mesh)
    {
      // CRhinoDoc::AddMeshObject makes a copy of the input mesh
      context.m_doc.AddMeshObject(*mesh);
      num_imported++;
    }
  }

  if (0 == num_imported)
    RhinoApp().Print(L"No meshes imported\n");
  else if (1 == num_imported)
    RhinoApp().Print(L"1 mesh imported\n");
  else
    RhinoApp().Print(L"%d meshes imported\n", num_imported);

  context.m_doc.Redraw();

  return CRhinoCommand::success;
}