Macro for saving image samples overwrites images

Macro for saving image samples overwrites images

Anonymous
Not applicable
1,546 Views
11 Replies
Message 1 of 12

Macro for saving image samples overwrites images

Anonymous
Not applicable
Hi all First of all thank you for looking to this topic. I'm creating a macro for automaticaly saving images, however I don't want it to overwrite the image, even if the figure taken is from the same file/project/result. Can anyone help me please? "SetLocale("en-us") Set Synergy = CreateObject("synergy.Synergy") Synergy.SetUnits "Metric" Set PlotManager = Synergy.PlotManager() Set Viewer = Synergy.Viewer() Viewer.SaveImage "C:\Images_moldflow\taken_picture.jpg"" Greetings
0 Likes
Accepted solutions (1)
1,547 Views
11 Replies
Replies (11)
Message 2 of 12

bernor_mf
Advisor
Advisor

Hi,

you can check if a file exists and do operation using code such as:

 

Set objFSO = CreateObject("Scripting.FileSystemObject")

If objFSO.FileExists("C:\Images_moldflow\taken_picture.jpg") Then
    Wscript.Quit '** this exit script so enter other operation...
Else
    Wscript.Echo "The file does not exist."
End If

Hope this helps.

 

Regards,

Berndt

( If my comments are accepted, provide "Kudos" as appreciation. If your request is answered/resolved, please click the "Accept as Solution" button. Thanks.)
0 Likes
Message 3 of 12

Anonymous
Not applicable
Hi @bernor_mf Thank you for your reply, it is already a starting point! However it happens that I have a non fixed number of images that can go from 1 until 99999 for example. I would like to know what is the last .jpeg saved and save the next image on that folder with the last number+1 (if the last one is "3.jpeg" I want to save the next one with "4.jpeg". -Search the highest number of the images present in the folder. -Convert that number into a number. Extract the number from the ".jpeg" part -Increment 1 to that number -Convert that to a string -Save the image with that name. Could you please give me some help? Greetings
0 Likes
Message 4 of 12

mppkumar
Advisor
Advisor

Hi

 

Try with For loop

 

For i= 1 to 10
"SetLocale("en-us")
Set Synergy = CreateObject("synergy.Synergy")
Synergy.SetUnits "Metric"
Set PlotManager = Synergy.PlotManager()
Set Viewer = Synergy.Viewer()
Viewer.SaveImage "C:\Images_moldflow\taken_picture" + "i" + ".jpg"
i = i + 1
Next

 

But this for loop works till 10 images

 

 

 

Thanks
M P Pradeep Kumar


If my views / comments acceptable, provide "Kudos" as appreciation, if answers your query, please click the "Accept as Solution"
Message 5 of 12

bernor_mf
Advisor
Advisor

Hi @Anonymous,

another idea is to simplify and just count the number of jpg-files in folder.

Then +1 for next jpg to save.

Would that resolve the issue?

Such as:

Option Explicit
Dim fso, count, src, folder, file
Set fso = CreateObject("Scripting.FileSystemObject")
src="c:\temp\jpgs"
Set folder = fso.GetFolder(src)
count = 0
For Each file In folder.files
  If LCase(fso.GetExtensionName(file)) = "jpg" Then
    count = count + 1
  End If
Next
MsgBox "Number of jpg: " & count ,,"Information" 

You can get the full file name from: fso.GetFileName(File)

 

Hope this helps.

Regards,

Berndt

 

( If my comments are accepted, provide "Kudos" as appreciation. If your request is answered/resolved, please click the "Accept as Solution" button. Thanks.)
0 Likes
Message 6 of 12

bernor_mf
Advisor
Advisor

Hi @andrevalega,

I created a piece of code that looks for all jpg files in folder, check if numeric and returns the highest integer + 1:

 

Option Explicit
Dim fso, count, src, folder, file, str
Dim str2, intNum
Set fso = CreateObject("Scripting.FileSystemObject")
src="c:\temp\jpgs"
Set folder = fso.GetFolder(src)
count = 0
For Each file In folder.files
  If LCase(fso.GetExtensionName(file)) = "jpg" Then
    count = count + 1
	str=Cstr(fso.getbasename(file.name))
	If IsNumeric(str) Then
	  intNum = CInt(str)
	End If
  End If
Next
intNum = intNum + 1
MsgBox "Number of jpg files : " & count & "  Next Integer: " & intNum ,, "Information" 

Hope this helps.

 

Regards,

Berndt

( If my comments are accepted, provide "Kudos" as appreciation. If your request is answered/resolved, please click the "Accept as Solution" button. Thanks.)
0 Likes
Message 7 of 12

Anonymous
Not applicable
Hi @bernor_mf Your code was very helpful, thank you. However since I don't code every day, there is a detail that I can't understand: "str=Cstr(fso.getbasename(file.name)) If IsNumeric(str) Then intNum = CInt(str) End If " What guarantees do you have that the variable intNum will get the higher number present, and not one in the file, for example 5 if there are 10 pictures? Does VB access it in an ascending way? Greetings
0 Likes
Message 8 of 12

bernor_mf
Advisor
Advisor
Accepted solution

Hi @Anonymous,
thanks for pointing that out. I overlooked that vb is not sorting as seen in Windows Explorer... Smiley Sad

Pictures from left:

jpg files in Windows explorer ; jpg integer files in script by default ; jpg integer files in script - sorted

 

jpg folder.JPGjpg script default.JPGjpg script sorted.JPG

 


Revised the code to read jpg-files which are numbered, then sorting ascending.:

 

Option Explicit
Dim fso, count, countInt, src, folder, file, str, intNum
Dim AL : Set AL = CreateObject("System.Collections.ArrayList")
Set fso = CreateObject("Scripting.FileSystemObject")
src="c:\temp\jpgs"
Set folder = fso.GetFolder(src)
count = 0
countInt = 0
For Each file In folder.files
  If LCase(fso.GetExtensionName(file)) = "jpg" Then
    count = count + 1
    str=Cstr(fso.getbasename(file.name))
    If IsNumeric(str) Then
      countInt= countInt + 1
      intNum = CInt(str)
      AL.Add intNum
    End If
  End If
Next
AL.Sort
intNum = AL(AL.count-1) + 1
MsgBox "Number of jpg files : " & count & "  integer files: " & countInt & "  Next Integer: " & intNum ,, "Information"

 

Results in:

jpg script result.JPG

 

I think this should do the job.

I am neither an every day programmer, but do some Moldflow API for fun Smiley Happy

Hope this helps.
Regards,
Berndt

( If my comments are accepted, provide "Kudos" as appreciation. If your request is answered/resolved, please click the "Accept as Solution" button. Thanks.)
Message 9 of 12

Anonymous
Not applicable
Problem solved @bernor_mf Thank you very much for your help and to all others! Can you advice any site for vba (moldflow api right?) tutorials? Greetings
0 Likes
Message 10 of 12

bernor_mf
Advisor
Advisor

Hi @Anonymous,

happy to hear it resolved the issue.

 

See the AKN (Autodesk Knowledege Network) article:

 

How to find Moldflow API documentation and reference

 

It has some additional links:

Moldflow Application Programming Interface (API)
Moldflow API Examples

 

Hope this helps.

Regards,

Berndt

 

Kudos me if you find posts good. Smiley Happy

( If my comments are accepted, provide "Kudos" as appreciation. If your request is answered/resolved, please click the "Accept as Solution" button. Thanks.)
0 Likes
Message 11 of 12

Anonymous
Not applicable
@bernor_mf I am already using that, I thought that there were some others. Now that I can save images as I want, I'm trying to get the program to save the image that the user is viewing with the name of the plot, for example, if the user is viewing the fill time, I want the program to save the picture as "Fill.jpg" for example. I saw this "function" getName: Set Viewer = Synergy.Viewer() Set PlotManager = Synergy.PlotManager() Set strName = PlotManager.GetName() ' @ later to be saved as Viewer.SaveImage "C:\Images" + strName + ".jpg" MsgBox strName However I'm getting an error that states that PlotManager doesn't have this property. Am I doing something wrong? Greetings
0 Likes
Message 12 of 12

bernor_mf
Advisor
Advisor

Hi @Anonymous,

you should use the Viewer.ActivePlot() from Synergy.Viewer

Coded an example for you:

 

 

Set Synergy = CreateObject("Synergy.Synergy")
Synergy.SetUnits "METRIC"
Set Viewer = Synergy.Viewer
If Viewer Is Nothing Then
   MsgBox "Please, select a study.",,"Error"
   WScript.Quit
End If
Set Plot = Viewer.ActivePlot()
If Plot Is Nothing Then
   MsgBox "Please, select a result.",,"Error"
   WScript.Quit
End If
ResultName = Plot.GetName
Viewer.SaveImage "C:\Temp\Images\" & ResultName & ".jpg"

 

Hope this helps.

Regards,

Berndt

 

( If my comments are accepted, provide "Kudos" as appreciation. If your request is answered/resolved, please click the "Accept as Solution" button. Thanks.)
0 Likes