API save a view from Robot on desktop in PNG format

API save a view from Robot on desktop in PNG format

jazlik
Contributor Contributor
297 Views
5 Replies
Message 1 of 6

API save a view from Robot on desktop in PNG format

jazlik
Contributor
Contributor

hello!

I looked through a few topics that are connected with the topic here but nothing seems to work for me.

I would like to save a view in png format on desktop. I am using Robot API 2024. My code is like this:


IRobotView3 view = _viewMngr.GetView(1) as IRobotView3;

view.ParamsDisplay.Set(IRobotViewDisplayAttributes.I_VDA_OTHER_HIDE_NODES, !showMesh);
view.ParamsDisplay.Set(IRobotViewDisplayAttributes.I_VDA_FE_FINITE_ELEMENTS, showMesh);
view.Selection.Get(IRobotObjectType.I_OT_CASE).FromText("1");
view.Redraw(1);
view.Projection = IRobotViewProjection.I_VP_3DXYZ;
view.Window.Activate();
string filePath = "myPathToFolderOnDesktop";
string directory = Path.GetDirectoryName(filePath);
if (!Directory.Exists(directory)) Directory.CreateDirectory(directory);
view.Redraw(1);
var exported = view.Printable.SaveToFile(filePath, IRobotOutputFileFormat.I_OFF_PNG);
 
even though exported is true, my exported file in png is 0kb. Did somebody get similar problem?

When I am exporting to .rtf or other extension everything works. Though the quality of rft is not very good.
0 Likes
298 Views
5 Replies
Replies (5)
Message 2 of 6

Stephane.kapetanovic
Mentor
Mentor

hi @jazlik 

static void SavePngViewToDesktop() {
    string DesktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
    string EntireFileName = Path.Combine(DesktopPath, "Capture_1.png");

    RobotApplication RobApp = new();
    var View3 = (IRobotView3)RobApp.Project.ViewMngr.GetView(1);
    View3.Projection = I_VP_3DXYZ;

    RobotMultiSelection vSelect = View3.Selection;
    vSelect.Get(I_OT_CASE).FromText("1");

    var array = new[] { I_VDA_OTHER_HIDE_NODES, I_VDA_FE_FINITE_ELEMENTS };
    array.ToList().ForEach(p => View3.ParamsDisplay.Set(p, true));

    View3.Window.Activate(); View3.Redraw(1);
    bool exported = View3.Printable.SaveToFile(EntireFileName, I_OFF_PNG);
}

Get Active View Index 

RobotViewMngr viewMngr = new RobotApplication().Project.ViewMngr;
int idx = Enumerable.Range(1, viewMngr.ViewCount).FirstOrDefault(i => viewMngr.GetView(i).Window.IsActive != 0);
var View3 = (IRobotView3)viewMngr.GetView(idx);

Maximize Window 

RobotWindow Window = View3.Window; Window.Activate(); Window.State = I_WS_MAXIMIZE;

Best Regards

 

Stéphane Kapetanovic

Did you find this post helpful? If it gave you one or more solutions,
don't forget to accept the solution and leave a < like !
EESignature
Message 3 of 6

jazlik
Contributor
Contributor

Hey Stephane!
Thank you for your reply 🙂
Nevertheless, I must say that still with that my .png image is 0 kb. Maybe I am doing something wrong at the beginning of the whole process. I tried Thread sleep but it did not work.
Have you ever had similar issues?

0 Likes
Message 4 of 6

Stephane.kapetanovic
Mentor
Mentor

hi @jazlik 

This must be related to your project, the sequence in which you produce your views, or the file environment.

The method provided doesn't seem to be the issue and works perfectly here.

  • To begin, try a new project and try my function.
  • To continue, open your file in safe mode or backward compatibility mode and start shooting.
  • Increment the file number with each shot.

Best Regards

Stéphane Kapetanovic

Did you find this post helpful? If it gave you one or more solutions,
don't forget to accept the solution and leave a < like !
EESignature
0 Likes
Message 5 of 6

jazlik
Contributor
Contributor

- When I am calling just your function the screen is black but has some "weight" like 7-8 kB. Though it is better than having empty .png with 0kB:

view1.png


- I am creating a GUID for each new picture so they are all unique I think.
- Opening model in safe mode did not change anything.

In this case I think that the answer is that something is wrong my project, but I am not able to find what it could be. Do you have something on your mind?

0 Likes
Message 6 of 6

Stephane.kapetanovic
Mentor
Mentor

hi @jazlik 

All code follows API logic and the file format is the same on all computers. So it's either your installation or your project. For the latter, open another project or create one with just a beam and run the macro.

Best Regards

Stéphane Kapetanovic

Did you find this post helpful? If it gave you one or more solutions,
don't forget to accept the solution and leave a < like !
EESignature
0 Likes