API to extract Filepath and Filename in C#

API to extract Filepath and Filename in C#

GGB.BHB
Enthusiast Enthusiast
1,400 Views
6 Replies
Message 1 of 7

API to extract Filepath and Filename in C#

GGB.BHB
Enthusiast
Enthusiast

Hi

I have tried to use the API guide to put together some C# code to call the file path and name of the active model.  Unfortunately I cannot get it to work 😞 

Please can you show me how it should be done?

Many thanks

0 Likes
Accepted solutions (1)
1,401 Views
6 Replies
Replies (6)
Message 2 of 7

Rafal.Gaweda
Autodesk Support
Autodesk Support

Hi @GGB.BHB 

 

Once I made it this way:

 

            string FN = Robot.robApp.Project.FileName;
            string Dir = FN.Substring(0, FN.LastIndexOf("\\"));
            string FileName = FN.Substring(FN.LastIndexOf("\\")+1);


Rafal Gaweda
Message 3 of 7

GGB.BHB
Enthusiast
Enthusiast

Thank you.  This is what I tried with what you sent through.  It seems to almost be there but I don't know how to assign IRobotProject ROBOT to the current open instance of robot!  How do I do that?

Many thanks

 

public static void GetROBOTfilename(RobotApplication robot, out string filename)

{

filename = "BLANK";

IRobotProject ROBOT;

string FN = ROBOT.FileName;

string ModelFilePath = FN.Substring(0, FN.LastIndexOf("\\"));

filename = FN.Substring(FN.LastIndexOf("\\") + 1);

}

 

0 Likes
Message 4 of 7

Rafal.Gaweda
Autodesk Support
Autodesk Support

Hi @GGB.BHB 


 I don't know how to assign IRobotProject ROBOT to the current open instance of robot!  How do I do that?

 


API interface is automatically linked with only one robot (no matter how many robots is running) = the last one opened. You can not change it.

You can load the project by API.

 



Rafal Gaweda
0 Likes
Message 5 of 7

GGB.BHB
Enthusiast
Enthusiast

Hi Rafal

Thank you for explaining that.  Have now got the code to run up to a point but it doesn't appear to get the name of the file.  The 'RobotOM,IRobotProject.FileName.get returned =null' although it seems to have got the Project and the RobotApplication - See highlighted red box in screen shot attached

Any suggestions?

Many thanks

 

0 Likes
Message 6 of 7

Rafal.Gaweda
Autodesk Support
Autodesk Support

Hi @GGB.BHB 

 

Check how many Robots is running...

            if (Process.GetProcessesByName("Robot").Length > 1)
            {
                MessageBox.Show("More than 1 Robot is running");
            }

https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.process.getprocesses?view=netframewor...

 



Rafal Gaweda
0 Likes
Message 7 of 7

GGB.BHB
Enthusiast
Enthusiast
Accepted solution

Hi Rafal

That seems to have sorted it Smiley Happy

The code now looks like this and it works!

Thank you

public static void GetROBOTfilename(RobotApplication robot, out string ModelFilePath, out string ModelFileName)

{

if (System.Diagnostics.Process.GetProcessesByName("Robot").Length > 1)

{ Console.WriteLine("More than one instance of Robot is running. CLOSE AND RESTART"); }

else { Console.WriteLine("Single instance of Robot is running"); }

string FN = robot.Project.FileName;

ModelFilePath = FN.Substring(0, FN.LastIndexOf("\\"));

ModelFileName = FN.Substring(FN.LastIndexOf("\\") + 1);

}

 

0 Likes