Assimp and Common Dialog Box - Strings - C++

Started by
11 comments, last by mynameisnafe 9 years, 10 months ago

Hi all,

I've been playing with Assimp: I've used it before, hardcoding file paths. However now I'm getting a complete file path from one of these Common Dialog Box things with


 GetOpenFileName()

http://msdn.microsoft.com/en-gb/library/windows/desktop/ms646829(v=vs.85).aspx#open_file

I'm then jumping through some (CString-shaped) hoops to format the string for the call below:


Assimp::Importer importer;
const aiScene *scene = importer.ReadFile(filename, aiProcessPreset_TargetRealtime_Fast);
//aiProcessPreset_TargetRealtime_Fast has the configs we'll need// nickthecoder.com

The problem is that scene is NULL. I'm guessing this is because of the flilepath and my incorrect formatting.

- what steps do I need to go through to ensure the filepath is good for assimp?

Hope to hear from you soon :)

Advertisement

Well, for starters, since you assume that your CString hoops are the culprit, you might show that particular code.

And to make it even easier for us, show the path before and after. This should make it trivial to point out any problem.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

Sounds reasonable :)

My Hoops are straight out of Stack Overflow results of google searches.

This was called wstrtostr() on SO by the Answerer I beleive - this is so I can turn a

CString into a std::string (....trying to standardize on std::string type in a multi-byte character set build)

This fn looks reasobable enough


std::string FunkyString(const std::wstring &wstr)
{
    std::string strTo;
    
    char *szTo = new char[wstr.length() + 1];
    szTo[wstr.size()] = '\0';
    
    WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, szTo, (int)wstr.length(), NULL, NULL);
    
    strTo = szTo;
    delete[] szTo;


    return strTo;
}

//
// Swap '//' for '\\' in the given string
//
CString SlashFix(CString strBang)
{
    int n = strBang.Replace(_T("//"), _T("\\"));
    return strBang;
}

Then here's my Filename getter: Please excuse the poor variable names etc


std::string App::DoOpenFileDialog()
{
	std::string fName;
	OPENFILENAME ofn;       // common dialog box structure
	char szFile[260];       // buffer for file name
	HANDLE hf ;              // file handle

	// Initialize OPENFILENAME
	ZeroMemory(&ofn, sizeof(ofn));
	...

	// Display the Open dialog box. 

	if( GetOpenFileName(&ofn) == TRUE ) 
	{	
		hf = CreateFileW (ofn.lpstrFile,  GENERIC_READ, 0, (LPSECURITY_ATTRIBUTES) NULL,
						OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, (HANDLE) NULL);

		CString file;
                file.Format(L"%s", ofn.lpstrFile);
		file = SlashFix(file);

		fName = FunkyString((LPCWSTR)file);
		OutputDebugString(file);
	}
	else
		fName = ("error!");

	return fName;
}

I also have this which lets me find out if a file exists - more stack overflow:


static bool FileExists( std::string path )
{
	std::ifstream i(path.c_str());
	return (i != 0);
}

And this will return false with any path from my dialog and 'CHoops',

However it works for files that are in subdirectories of the project dir - if there are no spaces in the name. So I think I need to escape the space characters?

If need more code please ask, however return value of DoOpenFileDialog() goes pretty much straight to the assimp call


case ID_FILE_OPEN:
{
	// returns full path
	std::string fName = DoOpenFileDialog();
				
	// deosn't work- string not fomratted correctly
        bool bReturnsFalse = Scatterbrain::FileExists(fName);
				
	//works: (this file is here and can be found from this space-less path);
	bool bReturnsTrue = Scatterbrain::FileExists("Models\\Cessna\\Cessna172.obj");

	MeshList meshes;
	GLFactory* f = GLFactory::Create();
				
        // loads with assimp
	if( f->LoadMeshListFromFile(fName, &meshes) )
	{
		OutputDebugString(L"Wow");
		// Make GL Meshes
	}
			OutputDebugString(L"OpenFile: ");
}
break;

Hope this helps :)

Before and After Strings copied from Output Window (I'm using VS2012 if it matters):

C:\Users\Nathan\Documents\Visual Studio 2012\DarkStar\DarkStar\Models\Cessna\Cessna172.obj

C:\Users\Nathan\Documents\Visual Studio 2012\DarkStar\DarkStar\Models\Cessna\Cessna172.obj

As I said, I think it's the spaces,

C:\Users\Nathan\Documents\Visual Studio 2012\DarkStar\DarkStar\Models\Cessna\Cessna172.obj

Code from DoOpenFileDialog() used to produce


	if( GetOpenFileName(&ofn) == TRUE ) 
	{	
		hf = CreateFileW (ofn.lpstrFile,  GENERIC_READ, 0, (LPSECURITY_ATTRIBUTES) NULL,
						OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, (HANDLE) NULL);

		CString file;
		file.Format(L"%s", ofn.lpstrFile);
		OutputDebugString(file);
		file = SlashFix(file);
		fName = FunkyString((LPCWSTR)file);

		OutputDebugString(file);
	}

On another note - I did try and 'replaces the spaces' with the SlashFix function with


 n = strBang.Replace("\ ", "\\ ");

to no apparent avail except this warning:


c:\users\nathan\documents\visual studio 2012\darkstar\darkstar\appdialog.cpp(27): warning C4129: ' ' : unrecognized character escape sequence

The problem is that scene is NULL. I'm guessing this is because of the flilepath and my incorrect formatting.

Have you actually checked what is the error?


if(!scene) std::cout << importer.GetErrorString() << std::endl;


On another note - I did try and 'replaces the spaces' with the SlashFix function with

n = strBang.Replace("\ ", "\\ ");
Not sure what you're trying to do here, but "\ " is not a valid escape sequence. You probably wanted just " ".
Edit. Damn, I really hate this buggy quotation thingie on this forum. Whatever.

Derp


The problem is that scene is NULL. I'm guessing this is because of the flilepath and my incorrect formatting.

Have you actually checked what is the error?


if(!scene) std::cout << importer.GetErrorString() << std::endl;


On another note - I did try and 'replaces the spaces' with the SlashFix function with

n = strBang.Replace("\ ", "\\ ");
Not sure what you're trying to do here, but "\ " is not a valid escape sequence. You probably wanted just " ".

I have tried looking at the error:


if(!scene)
{
	const char* pszErr = aiGetErrorString();
	MessageBox(NULL, (LPCWSTR)pszErr, L"Error: Assimp", NULL);
	return false;
}

Yields an empty string and message box.

On spaces replaces:


C:\Users\Nathan\Documents\Visual Studio 2012\DarkStar\DarkStar\Models\Cessna\Cessna172.obj
C:\Users\Nathan\Documents\Visual\ Studio\ 2012\DarkStar\DarkStar\Models\Cessna\Cessna172.obj

Is what I get in the output but in the debugger it looks like

C:\\Users\Nathan\\Documents\\Visual Studio\\ 2012\\DarkStar\\DarkStar\\Models\\Cessna\\Cessna172.obj

I'm sure, looking at that this, that I don't know what I'm doing preparing this string. My lord I feel a fool!

I've looked back at another project using hardcoded paths with assimp and they're of the form

"../Models/Cessna/Cessna172.obj"

I think I've got enough to play with for a while but I may get stuck..

You shouldn't need to escape those spaces anyway. Maybe try hard coding the filename to the absolute path and see if that works.


filename = "C:\\Users\\Nathan\\Documents\\Visual Studio 2012\\DarkStar\\DarkStar\\Models\\Cessna\Cessna172.obj";

Derp

RIght - I'm close now.

I would of thought so too?

I tried the following and found a file with the hardcoded path and didn't when selecting the same file in the DIalog


CString SlashFix(CString strBang)
{
	int n = strBang.Replace(_T("\\"), _T("//"));
		n += strBang.Replace(_T(" "), _T("\\ "));

	return strBang;
}

// With this doing my testing

.....
   bool bDialogOpen = true;
	std::string path;
				
	if(bDialogOpen)			                        
		path = DoOpenFileDialog();                      // SlashFix() broken
	else
		path = "Models//Cessna\ 172//Cessna172.obj";	// works
				
	bool bSanity = Scatterbrain::FileExists(path);
				
	MeshList meshes;
	GLFactory* f = GLFactory::Create();

	if( f->LoadMeshListFromFile(path, &meshes) )
		OutputDebugString(L"// Make GL Meshes");
...

I'll try now with the full path hardcoded..

Meanwhile, as an aside pondering, if file.cpp opens writes to and saves a file, it would be created in the directory of that file, right?

Okay so the path hardcoded like this passes the FileExists test - also with slashes going the other way:


path = "C:\\Users\\Nathan\\Documents\\Visual Studio 2012\\DarkStar\\DarkStar\\Models\\Cessna\\Cessna172.obj";

So what's in the CString that I'm creating that I can't see, that I'm messing up when I SlashFix() ?

It appears you're simply trying to get a file path from the user and load the file with Assimp. If so, you shouldn't be creating any file of your own. Try loading a std::string directly from the GetOpenFileName fileName buffer, and call Assimp. This assumes (as you imply) you've set the Character Set property to "Use Multi-Byte Character Set"


    char fileName[256], fileTitle[256];
    fileName[0] = 0;
    fileTitle[0] = 0;
    std::string userFilePath;

    OPENFILENAME ofn;
    ZeroMemory(&ofn, sizeof(OPENFILENAME));

    ofn.lStructSize = sizeof(OPENFILENAME);
    ofn.lpstrFilter = "All files (*.*)\0*.*\0\0";
    ofn.nFilterIndex = 1;
    ofn.lpstrFile = fileName;
    ofn.nMaxFile = 256;
    ofn.Flags = OFN_FILEMUSTEXIST;
    BOOL response = GetOpenFileNameA(&ofn);
    if (!response)
    {
        return false;
    }
    userFilePath = fileName;
    scene = importer.ReadFile(userFilePath, ... [your flags]);

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.


It appears you're simply trying to get a file path from the user and load the file with Assimp. If so, you shouldn't be creating any file of your own. Try loading a std::string directly from the GetOpenFileName fileName buffer, and call Assimp. This assumes (as you imply) you've set the Character Set property to "Use Multi-Byte Character Set"


- precisely ..

So, what you're saying is that this line in DoFileOpenDialog()

hf = CreateFileW (ofn.lpstrFile, GENERIC_READ, 0, (LPSECURITY_ATTRIBUTES) NULL,
                        OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, (HANDLE) NULL);

is preventing me from then opening the file with ifstream later (Scatterbrain::FileExists(..) ) and Assimp after that ( .ReadFile(...) )

Why did I not think of that, given it says "LPSECURITY_ATTRIBUTES" in capital flippin' letters?

?

See, now that is something I hadn't thought of - I don't know that much about Win32 / MFC, just the basics like "Give me something I can draw on without using glut or GLFW"; this project is me trying to do some sort of editor.

This topic is closed to new replies.

Advertisement