Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

In C++ Open a File and Read Write Integer Double String Data then Close

7 REPLIES 7
SOLVED
Reply
Message 1 of 8
tperam
3348 Views, 7 Replies

In C++ Open a File and Read Write Integer Double String Data then Close

In C++ File Open Read Write Integer Double String Close
 
In Fusion 360 using C++ I would like to,
 
Open a Read Only text file and Read Integer, Double and String Data, then Close it.
 
Open a Write text File and Write Integer, Double and String Data, then Close it.
 
Thurai Moorthy
7 REPLIES 7
Message 2 of 8
BrianEkins
in reply to: tperam

The Fusion 360 API doesn't have any functionality to open and read or write to files.  However, the reason it doesn't have any functionality is that the programming languages themselves (Python and C++) both support that functionality.  So you can use the standard C++ libraries to read and write files.  Here's one introduction.

 

http://www.cplusplus.com/doc/tutorial/files/  

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 3 of 8
tperam
in reply to: BrianEkins

 

I Tried the following  C++ Code, which did not work.

 

Can you post a sample code that works in Fusion 360 please?

 

Thanks,

Thurai


#include <Core/CoreAll.h>
#include <Fusion/FusionAll.h>
#include <CAM/CAMAll.h>
#include <stdio.h>
#include <io.h>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <assert.h>
#include <math.h>
#include <memory.h>
#include <errno.h>
//#include <iostream>
#include <complex>
 
using namespace adsk::core;
using namespace adsk::fusion;
using namespace adsk::cam;
using namespace std;

Ptr<Application> app;
Ptr<UserInterface> ui;

extern "C" XI_EXPORT bool run(const char* context)
{
 app = Application::get();
 if (!app)
  return false;
 ui = app->userInterface();
 if (!ui)
  return false;
 ui->messageBox("Hello script");
 /* create a file */
 
/* Test Writing to a file */  
 ofstream outflm;
 outflm.open("outflm.txt");
 double a = 10.0;
 char aStr[16]; // string which will contain the number
 sprintf_s ( aStr, "%f", a ); // %d makes the result be a decimal integer
 string SaStr;
    SaStr += "Test aStr";
    SaStr += " ";
    SaStr += aStr;
    ui->messageBox(SaStr);
    outflm << SaStr;
  outflm.close();
 
   char str[100];  
  
   ifstream inflm;
   inflm.open("inflm.txt");
   inflm.getline(str, 100, '#');
   ui->messageBox("str", str);
   string x = "fred";
      int d = 3;
   char dStr[16]; // string which will contain the number
   sprintf_s ( dStr, "%d", d ); // %d makes the result be a decimal integer
      
   ui->messageBox("Test messageBox");
       
    string msg;
    msg = "Test 2";
    msg += " ";
    msg += x;
    msg += " ";
    msg += dStr;
    ui->messageBox(msg);
 
 return true;
}
#ifdef XI_WIN
#include <windows.h>
BOOL APIENTRY DllMain(HMODULE hmodule, DWORD reason, LPVOID reserved)
{
 switch (reason)
 {
 case DLL_PROCESS_ATTACH:
 case DLL_THREAD_ATTACH:
 case DLL_THREAD_DETACH:
 case DLL_PROCESS_DETACH:
  break;
 }
 return TRUE;
}
#endif // XI_WIN
Message 4 of 8
tperam
in reply to: BrianEkins

Hi Brian Ekins,

                          The reason I would like to do  file input output in Fusion 360 is to have the capability of having the data for hundreds or more  of sketches in a file.

 

Is it possible to import the common C++ Library file input output capability  into Fusion 360?

 

Thanks.

Message 5 of 8
goyals
in reply to: tperam

I tried your code and it worked for me. May I know what problem you are seeing with your code. The best way to create C++ Script/Add-in is to start with default C++ project. Please look at the following link 

http://help.autodesk.com/view/fusion360/ENU/?guid=GUID-9701BBA7-EC0E-4016-A9C8-964AA4838954

 

This will set VS project settings correctly and you can modify it later for your work. You can use any C++ library for your script.



Shyam Goyal
Sr. Software Dev. Manager
Message 6 of 8
tperam
in reply to: goyals

Hi Goyals,

                 I Tried the code again today.

 

I followed the steps in the link you sent.

 

It Compiles and Runs.

 

But does not read the data from the input file and does not write the data into the output file.

 

Input File(inflm.txt) Data is:

abcd1234
efgh5678

 

The messageBox did not display the above line.

 

After running, nothing got written into the Output file (outflm.txt).

 

Thanks,

Thurai

 

 

 

 

 

 

 

Message 7 of 8
goyals
in reply to: tperam

I am hoping that after compiling the code, you are running the script from "Scripts and Add-ins" dialog. Rather than giving just the file name, please try to pass full path like 'D://Outflm.txt'. My guess is that script is not able to locate these text files on your machine.



Shyam Goyal
Sr. Software Dev. Manager
Message 8 of 8
tperam
in reply to: goyals

Hi Goyals,

                I Added the full path as you suggested.

 

Then it worked Okay. 

 

Great and Thanks.

 

 

The new code is as below and I called it TestFile3.cpp

 


#include <Core/CoreAll.h>
#include <Fusion/FusionAll.h>
#include <CAM/CAMAll.h>
using namespace adsk::core;
using namespace adsk::fusion;
using namespace adsk::cam;
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <io.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <assert.h>
#include <math.h>
#include <memory.h>
#include <errno.h>
using namespace std;
 
Ptr<Application> app;
Ptr<UserInterface> ui;
extern "C" XI_EXPORT bool run(const char* context)
{
 app = Application::get();
 if (!app)
  return false;
 ui = app->userInterface();
 if (!ui)
  return false;
 ui->messageBox("Hello script");
 // Read File
  string line;
  ifstream myfile ("C://Users//tpera//AppData//Roaming//Autodesk//Autodesk Fusion 360//API//Scripts//TestFile3//infile.txt");
  if (myfile.is_open())
   {
 ui->messageBox(" Opened infile.txt");
    while ( getline (myfile,line) )
    {
      cout << line << '\n';
   line +="\n";
   ui->messageBox(line);
    }
   
 myfile.close();
  }
  else
  {
   ui->messageBox(" Unable to Open infile.txt");
  }
 
 
 // writing on a text file
     string x = "fred";
     int d = 3;
   //string dStr;
   //std::string dAsString = std::to_string(d);
   char dStr[16]; // string which will contain the number
   sprintf_s ( dStr, "%d", d ); // %d makes the result be a decimal integer
      
   string msg;
    msg = "Test Write File";
    msg += " ";
    msg += x;
    msg += " ";
    msg += dStr;
    ui->messageBox(msg);

  ofstream myfile2 ("C://Users//tpera//AppData//Roaming//Autodesk//Autodesk Fusion 360//API//Scripts//TestFile3//outfile.txt");
  if (myfile2.is_open())
  {
    //myfile2 << "This is a line.\n";
    //myfile2 << "This is another line.\n";
    myfile2 << msg;
 //myfile2.put('A');
 //myfile2.write(msg);
  
    ui->messageBox(" Opened outfile.txt");
    myfile2.close();
  }
  else
  {
 ui->messageBox("Unable to open outfile.txt");
  }

 return true;
}
#ifdef XI_WIN
#include <windows.h>
BOOL APIENTRY DllMain(HMODULE hmodule, DWORD reason, LPVOID reserved)
{
 switch (reason)
 {
 case DLL_PROCESS_ATTACH:
 case DLL_THREAD_ATTACH:
 case DLL_THREAD_DETACH:
 case DLL_PROCESS_DETACH:
  break;
 }
 return TRUE;
}
#endif // XI_WIN
 
infile.txt:
abcd1234
efgh5678
 
outfile.txt:
Test Write File fred 3
 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report