Check a file already exists in Save As Dialog box - c++, opencascade - visual-c++

I want to create a .stl file for a particular shape where each face of that shape has a different patch name like face1,face 2 etc. I have done this by overriding the StlAPI_Writer and RWStl classes in opencascade. I have used file.Append method instead of file.Build method to do so.
But I have a problem when I save the .stl file in an already existing file, it appends data to the existing one which is incorrect. I want to delete the existing data in the file and append new data face by face for a given shape.
Please help me on this.

You can use this simple function:
#include <sys/stat.h>
#include <string>
using namespace std;
bool FileExists(string strFilename) {
struct stat stFileInfo;
bool blnReturn;
int intStat;
// Attempt to get the file attributes
intStat = stat(strFilename.c_str(),&stFileInfo);
if(intStat == 0) {
// We were able to get the file attributes
// so the file obviously exists.
blnReturn = true;
} else {
// We were not able to get the file attributes.
// This may mean that we don't have permission to
// access the folder which contains this file. If you
// need to do that level of checking, lookup the
// return values of stat which will give you
// more details on why stat failed.
blnReturn = false;
}
return(blnReturn);
}
I assume you use the SaveFileDialogue class. In this case you can handle the return result of the dialogue like this:
if ( saveFileDialog.ShowDialog() == ::DialogResult::OK ) {
if ( FileExist(saveFileDialog.FileName) ) {
// erase the file
}
// write the code using the Append function
}
This should work, however a easier variant must be accessible if you use something else than Append (something like Write or maybe even Append but with a parameter that specifies to rewrite the file)
HTH, JP

Related

nodejs import file and use functions natively

How can I import/require a file, and then use the functions in the file natively?
Say I have file 1:
const file2 = require("./file2.js")
const text = "hello"
file2.print()
And in file 2 I have:
module.exports = {
print:()=>{
console.log(text)
}
}
I want to be able to use functions from another file as if they were in the original file, retaining the variables and objects created in the first file, is this possible?
No, the modules are separate, unless you resort to assigning your variables into the global object and hoping that you can keep track of them without going insane. Don't do that.
Either
pass the data you need around (the best option most of the time), or
maybe add a third module containing the shared state you need and require() it from both file 1 and file 2
No!
But
The regular pattern of shared context is that you create a context and share it. The most simple form of it is something like this:
//In file 1 -->
let myContext = {
text: 'hello'
}
file2.print(myContext);
//In file 2 -->
module.exports = {
print:(ctx)=>{
console.log(ctx.text)
}
}
However
JS has some inbuilt support for context. Something like this:
//In file 1 -->
let myContext = {
text: 'hello'
}
let print = file2.print.bind(myContext);
print();
//In file 2 -->
module.exports = {
print: function(){
console.log(this.text)
}
}
Notice the removal of the argument and changing the arrow function to a function expression.

Export and Use same function in node js

I have exported a function to set random color, which returns me a random color for every call.
like,
exports.getRandomColor = function(index) {
//return random color
}
If I put function like this I can be able to get access to that function from another file. But not inside the same file. (i.e) This getRandomColor is not get accessed in the same file,
It works good if I use
function getRandomColor(index) {
//return random color
}
The problem is I cannot able to access this on another file. Suggestions are welcome.
Thanks in advance
So you can have the next in your file
// define named function
function getRandomColor(index) {
return random color
}
// assign it to the exports, like below
exports.getRandomColor = getRandomColor
Then you can reuse this function whenever you want.

Dll making with C++

I'm been trying to start doing a plug-in for a program called "Euroscope" for quite some time and i still can't do anything. I even read a C++ book and nothing, it's too difficult to start.
The question i'm going to ask is a little bit specific and it's going to be difficult to explain but i'm tired of trying to solve this by my own so here it comes.
I have a class that i imported with a bunch of function prototypes in the header called "EuroscopePlugIn".
My principal .cpp is this:
void CPythonPlugInScreen::meu()
{
//loop over the planes
EuroScopePlugIn::CAircraft ac;
EuroScopePlugIn::CAircraftFlightPlan acfp;
CString str;
CPythonPlugIn object;
for(ac=GetPlugIn()->AircraftSelectFirst();
ac.IsValid();
ac=GetPlugIn()->AircraftSelectNext(ac))
{
EuroScopePlugIn::CAircraftPositionData acpos=ac.GetPosition();
const char *c=ac.GetCallsign();
object.printtofile_simple_char(*c);
object.printtofile_simple_int(ac.GetState());
};
object.printtofile_simple_int(ac.GetVerticalSpeed());
object.printtofile_simple_int(acfp.GetFinalAltitude());
cout<<acfp.GetAlternate();
}
the "printtofile_simple_int" and "printtofile_simple_char" are defined is the class CPythonPlugIn like this:
void printtofile_simple_int(int n){
ofstream textfile;
textfile.open("FP_simple_int.txt");
textfile<<(n);
textfile.close();
So i open the program, load the .dll i created with Build->Solution and it does nothing, the .txt files aren't even created and even the cout produces nothing.
I will give you some of the prototype infos on the header file "EuroScopePlugIn.h" in case you need them to understand my micro program. If you need other,ask me and i'll put it here
//---GetPlugIn-----------------------------------------------------
inline CPlugIn * GetPlugIn ( void )
{
return m_pPlugIn ;
} ;
&
CAircraft AircraftSelectFirst ( void ) const ;
//-----------------------------------------------------------------
// Return :
// An aircraft object instance.
//
// Remark:
// This instance is only valid inside the block you are querying.
// Do not save it to a static place or into a member variables.
// Subsequent use of an invalid extracted route reference may
// cause ES to crash.
//
// Description :
// It selects the first AC in the list.
//-----------------------------------------------------------------
&
int GetFinalAltitude ( void ) const ;
//-----------------------------------------------------------------
// Return :
// The final requested altitude.
//-----------------------------------------------------------------
Please guys i need help to start with the plug-in making, from that point on with a methodology of trial and error i'll be on my way. I'm just finding it extremely hard to start...
Thank you very much for the help

How to read a custom path of a file in c++?

My file is located in C:\\Input\\pStep.p21 . i want to open that file in my cpp program. How can i do it? I am using char* inputPath="C:\\Input\\pStep.p21"; but its not finding my file in program. How to get current working directory in VC++?. Its working if try to save p21 file but failing if i read it.
my code in CAA:
#include<CATSDM_Services>
#include<SdaiModel.h>
#include<CATIUniCodeString>
---
---
main()
{
CATIUniCodeString inputPath("C:\\Input\\pStep.p21");
HRESULT hr=S_OK;
SdaiModel edxModel=Null;
//this method reads express schema name, input p21 file and sdaimodel
hr=CreateModelFromFile(inputPath,"parts",edxModel);
if(FAILED(hr))
{
cout<<"Model created succesfully";
}
else
{
cout<<"Failed";
}
}
Sorry for not seeing the question earlier.
From what I am seeing in your code, the test is wrong. The FAILED() macro denotes incorrect execution. Thus from you code, if you see "Failed" on the console, it actually means the execution of the method ran OK.
Change your code to something like:
if ( SUCCEEDED(hr) )
{
cout<<"Model created succesfully";
}
else
{
cout<<"Failed";
}

How to load text file from Resources in VC++?

I'm writing a DLL in VC++ and I need to use external text file. I have something like this right now:
ifstream file;
string line;
file.open("C:\\Users\\Me\\Desktop\\textfile.txt");
getline(file,line);
file.close();
I understand that I can have this file in Resources right?
I added my text file by going to "ResourceView" -> "Add Resource" -> "Import". I chose my text file and created custom resource type.
How can I access to this file to use similarly to the code above ? I have Resource.h but what should I do with it?
Can I edit my resource text file too?
Assumptions:
The resource type is "TEXT"
The resource ID is MY_RESOURCE_ID (this would be a constant from your resource.h)
You have stored the hInstance for your EXE or DLL (whichever contains the resource) into m_hInstance.
Loading a resource into memory and getting a pointer to it is done like this:
HRSRC hResource = FindResource(m_hInstance, MAKEINTRESOURCE(MY_RESOURCE_ID), L"TEXT");
if (hResource)
{
HGLOBAL hLoadedResource = LoadResource(m_hInstance, hResource);
if (hLoadedResource)
{
LPVOID pLockedResource = LockResource(hLoadedResource);
if (pLockedResource)
{
DWORD dwResourceSize = SizeofResource(m_hInstance, hResource);
if (0 != dwResourceSize)
{
// Use pLockedResource and dwResourceSize however you want
}
}
}
}
Note: You do not have to unload or unlock the resource on 32-bit or 64-bit vesions of Windows, and if you obtain the resource again you will not leak memory; you always get the same piece of memory.
For updating a resource, see Updating Resources in MSDN.

Resources