Get folder


(F Leon Marquez95) #1

Hello, I have a question regarding the way to obtain the folder where I save my bitmaps from my program in order to save a csv file. The point is that when I save the bitmap I can choose the folder where I save my picture, but when I want to save my csv file, I have to set the folder in the program, so I would like to get the folder or make the program ask me for a folder to save. Thank you. My code is the following.

#include "StdAfx.h"
#include "TestPlugIn.h"
#include <iostream>
#include <fstream>
#include <math.h>
#include <algorithm>
using namespace std;

#include <Windows.h>
#include <stdio.h>

#include <list>
#include <string>
#include <sstream>
#include <vector>

static class CCommandTest theTestCommand;

CRhinoCommand::result CCommandTest::RunCommand( const CRhinoCommandContext& context )
{
  
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
CRhinoView* view = ::RhinoApp().ActiveView();
  if( !view )
    return CRhinoCommand::failure;


	ON::display_mode dm = view->ActiveViewport().DisplayMode();
  if( dm != ON::shaded_display )
  {
    view->ActiveViewport().SetDisplayMode( ON::shaded_display );
	context.m_doc.ViewModified(view);
    view->Redraw();
  }
	///////////////////////////////////////////////////////////////////
	
	CWnd* pMainWnd = CWnd::FromHandle( RhinoApp().MainWnd() );
  if( 0 == pMainWnd )
	  CRhinoCommand::nothing;

  CRhinoGetFileDialog gf;
  gf.SetScriptMode( context.IsInteractive() ? FALSE : TRUE );

  ////////////////////--------------------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  ////////---------------select the name of the bitmap--------------------------------------------------//////////////////////////////

  const wchar_t* file_name;
  
  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

  BOOL rc = gf.DisplayFileDialog( CRhinoGetFileDialog::save_bitmap_dialog, file_name, pMainWnd );
  if( !rc )
	  CRhinoCommand::cancel;

  ON_wString filename = gf.FileName();
  filename.TrimLeftAndRight();
  if( filename.IsEmpty() )
	  CRhinoCommand::failure;

  if( view )
  {
    CRect rect;
    view->GetClientRect( rect );

    CRhinoDib dib;
    if( dib.CreateDib(rect.Width(), rect.Height(), 24, true) )
    {
      // Set these flags as you wish.
      BOOL bIgnoreHighlights = TRUE;
      BOOL bDrawTitle = FALSE;
      BOOL bDrawConstructionPlane = FALSE;
      BOOL bDrawWorldAxes = FALSE;

      CRhinoObjectIterator it( CRhinoObjectIterator::normal_or_locked_objects,
                               CRhinoObjectIterator::active_and_reference_objects
                               );

      if( view->ActiveViewport().DisplayMode() == ON::wireframe_display )
      {
        context.m_doc.DrawToDC( it, dib, dib.Width(), dib.Height(),
          view->ActiveViewport().View(),
          bIgnoreHighlights,
          bDrawTitle,
          bDrawConstructionPlane,
          bDrawWorldAxes
          );
      }
      else
      {
        context.m_doc.RenderToDC( it, dib, dib.Width(), dib.Height(),
          view->ActiveViewport().View(),
          bIgnoreHighlights,
          bDrawTitle,
          bDrawConstructionPlane,
          bDrawWorldAxes,
          view->ActiveViewport().GhostedShade()
          );
      }

      dib.WriteToFile( filename );
    }
  }


ofstream myfile;
      myfile.open ("D:/Myfolder/example.csv");
      myfile << "This is the first cell in the first column\n";
      myfile << "450\n";
      myfile << "csv\n";
      myfile << "6\n";
      myfile << "colon\n";
      myfile.close();

(Dale Fugier) #2

Hi @f.leon.marquez95,

To prompt the user for a folder in C++ in Windows, you’d normally use the SHBrowseForFolder function.

If you won’t want to the complication of using this function, you can use the Rhino SDK’s CRhinoUiDirDialog class.

To use this class in Rhino 6, make sure you’ve defined the RHINO_SDK_MFC pre-processor macro in stdafx.h.

#define RHINO_SDK_MFC

Then, you can do something like this;

CString caption = L"Select output folder";
CString title = L"TestLeon";

ON_wString folder;
RhinoApp().RhinoDirectoryManager().GetDefaultDirectory(CRhinoDirectoryManager::ftOpen, folder);
CString path = folder;
  
CWnd* pParent = CWnd::FromHandle(RhinoApp().MainWnd());

CRhinoUiDirDialog dialog(caption, title, path, pParent);
int rc = dialog.DoModal();
if (rc == IDOK)
  RhinoApp().Print(L"%s\n", (const wchar_t*)dialog.GetPathName());

– Dale


(F Leon Marquez95) #3

Will the same be correct for Rhino 5?


(Dale Fugier) #4

Yes…

– Dale


(F Leon Marquez95) #5

Thank you very much, it worked perfectly