CHAI3DCHAI3D

normal problem with loadFromFile and a variable path

More
18 Jul 2017 18:36 #1

hello, i was following the example 27-multiframes to learn how to load a multimesh object,but when i try to use the loadFromFile or cLoadFileSTL with a variable path (i sent the path to the console to see if it is correct) it doesn't load the model.

Originally i get the path from a dialog box:

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

	// Initialize OPENFILENAME
	ZeroMemory(&ofn, sizeof(ofn));
	ofn.lStructSize = sizeof(ofn);
	ofn.hwndOwner = hwnd;
	ofn.lpstrFile = szFile;
	// Set lpstrFile[0] to '\0' so that GetOpenFileName does not 
	// use the contents of szFile to initialize itself.
	ofn.lpstrFile[0] = '\0';
	ofn.nMaxFile = sizeof(szFile);
	ofn.lpstrFilter = "obj(*.obj)\0*.obj\0stl(*.stl)\0*.stl\0";
	ofn.nFilterIndex = 2;
	ofn.lpstrFileTitle = NULL;
	ofn.nMaxFileTitle = 0;
	ofn.lpstrInitialDir = NULL;
	ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

	// Display the Open dialog box.
	if (GetOpenFileName(&ofn)==TRUE)
	{ 
			hf = CreateFile(ofn.lpstrFile, GENERIC_READ,0,(LPSECURITY_ATTRIBUTES) NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,(HANDLE) NULL);

			std::ostringstream ost;
//			ost<< szFile;
			ost<< ofn.lpstrFile;
			std::string s = ost.str();
			return s;
	}
	
	return "";
}


the problem:
std::string file_path;
file_path=import_File();
const char *path=file_path.c_str();
fileload=chai3d::cLoadFileSTL( model,path);  gives false

or this is still false:
std::string file_path;
file_path=import_File();
fileload=chai3d::cLoadFileSTL( model,file_path);  gives false

if i use this is correct:
std::string file_path="C:/Users/..../Desktop/model.stl";
fileload=chai3d::cLoadFileSTL( model,file_path);

but i need a variable path T_T.. can you help me?

Please Log in or Create an account to join the conversation.

More
19 Jul 2017 11:17 #2

Solved.
The problem wasn't the path itself, but the dialog box that was locking the file; so the solution was adding "CloseHandle(hf);" before "return s;"

Please Log in or Create an account to join the conversation.