Discussion Groups

Simulation Moldflow Insight

Reply
Contributor
romslayer
Posts: 13
Registered: ‎07-06-2012
Accepted Solution

Application Programming Interface (VBA & Moldflow)

521 Views, 11 Replies
11-29-2012 02:07 AM

Hello,

I'm a french student. And I have a little problem.

I want to make a VBA Macro who launch Fill/Pack analisys and when the Fill/Pack analysis is finished the Macro launch the Fill/Pack/Warp analysis.

I want to do this, because when I launch Fill/Pack/Warp, I use Premium license during all the analysis time and with this Macro I can use Basic license during Fill/Pack analsis time and Premium License during only Warp analysis time.

 

I managed to make a macro that change the sequence analysis but this macro can't wait the end of Fill/Pack analysis for change to Fill/Pack/Warp analysis. I just can put one time (like 20sec) to wait but I can't know when the Fill/Pack analysis is finished.

 

If someone has an answer

 

thanks

 

Romain GUY

Please use plain text.
Product Support
nordhb
Posts: 463
Registered: ‎03-23-2009

Re: Application Programming Interface (VBA & Moldflow)

11-30-2012 01:16 AM in reply to: romslayer

Hi,
you can actually check the status of a job in job manager via macro.

You have to find the job, either by loop through the jobs and find the model or find by name and check status, wait and repeat loop.
When no longer finding job or getting the status in job manager, it is completed, you exit the loop and you can continue and relaunch.

Please, see Help - API Reference, and search JobManager and/or JobMgr
(to the right of question mark, there is a small arrow down to click and select API Reference)

Hope this helps.
Regards,

Berndt



Berndt Nordh
Please use plain text.
Contributor
romslayer
Posts: 13
Registered: ‎07-06-2012

Re: Application Programming Interface (VBA & Moldflow)

11-30-2012 03:12 AM in reply to: nordhb

hello,

I have find the API reference and JobMgr. Thanks, I didn't know that help existed.

But, I don't arrived to use the AnalysisStatus. I don't know, what's aIndex and the i-th analysis.

I have post my code here, if you have a response about that.

 

String AnalysisStatus

(

long 

aIndex

 ) 

 

Gets the status of the i-th analysis

Parameters:

 

aIndex 

index of analysis

Returns:

Status of the i-th analysis, which can be one of the following.

  • New
  • Pending
  • Running
  • Completed
  • Failed
Private Sub Btndeb_Click()

Dim rep As String
rep = completed
Dim a As String

'launch the Flow analysis
Set Synergy = CreateObject("synergy.Synergy")
Synergy.SetUnits "Metric"
Set Studydoc = Synergy.Studydoc()
Studydoc.AnalysisSequence "Flow"
Set Studydoc = Synergy.Studydoc()
Studydoc.AnalyzeNow False, True

'Take the name of the study
Dim StudyName As String
Set Studydoc = Synergy.Studydoc()
StudyName = Studydoc.StudyName
StudyName = Left(StudyName, Len(StudyName) - 4)
Studydoc.Close

'Wait the end of the flow analysis
Do
Set Studydoc = Synergy.Studydoc()
a = StudyDoc.AnalsisStatus
Loop While a<>rep

'Open the study
Set Project = Synergy.Project
Project.OpenItemByName StudyName, "Study"

'Launch the Fill/Pack/Warp analysis
Set Studydoc = Synergy.Studydoc()
Studydoc.AnalysisSequence "Flow|Warp"
Set Studydoc = Synergy.Studydoc()
Studydoc.AnalyzeNow False, True
Studydoc.Close

End Sub

Regards,

 

Romain GUY

Please use plain text.
Product Support
nordhb
Posts: 463
Registered: ‎03-23-2009

Re: Application Programming Interface (VBA & Moldflow)

11-30-2012 04:06 AM in reply to: romslayer

Hi,

to check the first analysis sequence is 0, so

a = StudyDoc.AnalysisStatus(0)

(there was a typo in code)

 

Then set

rep = "Completed"

so string comparison is right.

 

This should make it run.

 

Regards,

Berndt

 



Berndt Nordh
Please use plain text.
Contributor
romslayer
Posts: 13
Registered: ‎07-06-2012

Re: Application Programming Interface (VBA & Moldflow)

11-30-2012 05:39 AM in reply to: nordhb

hi,

 

The code doesn't work. I think we can't use :

 

Do
Set StudyDoc = Synergy.StudyDoc
a = StudyDoc.AnalysisStatus(0)
Loop While a <> rep

 

if I close the study with StudyDoc.Close before.

May be used, AnalysisStatus with StudyName ?

Because, in the Job manager, I have 3 server and four job launch all the time. With AnalysisStatuts(0) , I can't distinguish the different job.

 

Regards

 

Romain GUY

Please use plain text.
Product Support
nordhb
Posts: 463
Registered: ‎03-23-2009

Re: Application Programming Interface (VBA & Moldflow)

11-30-2012 06:02 AM in reply to: romslayer

Hi,

right, StudyDoc.AnalysisStatus(0) check the status of the active study in Synergy.

If closed, it will fail, as no available active study.

 

I believe you have to use another approach. To find status of jobs from job manager.

Look up Job Class Reference in API Help.

But something like this:

Set JobManager = Synergy.JobManager()

 

Jobs starts such as:

i = 1

Number_Of_Jobs = JobManager.NumberOfJobs

Set job = JobManager.GetJob(i) 

where you count through jobs in job server.

 

Job_ModelName = Job.ModelName()
JobStudyName = Left(Job_ModelName, Len(Job_ModelName) - 4)

 

compare study names and find
Job_Status = Job.Status()

You have to code above to suit your macro.

 

Regards,

Berndt

 



Berndt Nordh
Please use plain text.
Contributor
romslayer
Posts: 13
Registered: ‎07-06-2012

Re: Application Programming Interface (VBA & Moldflow)

11-30-2012 06:48 AM in reply to: nordhb

Hi,

 

I understand what you want to do. And I have make this.

But now, if I use :

Set JobManager = Synergy.JobManager()

I can't use AnalysisStatus because you can only use this function with Studydoc.

How I can find the time when my study is finish without AnalysisStatus ?

 

Private Sub Btndeb_Click()

Dim rep As String
rep = "Completed"
Dim a As String
Dim i As Single
Dim Number As Single
Dim Name As String

'launch the Flow analysis
Set Synergy = CreateObject("synergy.Synergy")
Synergy.SetUnits "Metric"
Set StudyDoc = Synergy.StudyDoc()
StudyDoc.AnalysisSequence "Flow"
Set StudyDoc = Synergy.StudyDoc()
StudyDoc.AnalyzeNow False, True

'Take the name of the study
Dim StudyName As String
Set StudyDoc = Synergy.StudyDoc()
StudyName = StudyDoc.StudyName
StudyName = Left(StudyName, Len(StudyName) - 4)
StudyDoc.Close

'Find the number of study
i = 1
Set JobManager = Synergy.JobManager()
Number = JobManager.NumberOfJobs

'Find the good study
For i = 1 To Number
Set Job = JobManager.GetJob(i)
Job_ModelName = Job.ModelName()
Name = Left(Job_ModelName, Len(Job_ModelName) - 4)
If Name = StudyName Then GoTo ici
i = i + 1
Next

ici:

'Wait the end of the flow analysis
Do
Set JobManager = Synergy.JobManager()
a = JobManager.AnalysisStatus(i)Loop While a <> rep

'Open the study
Set Project = Synergy.Project
Project.OpenItemByName StudyName, "Study"

'Launch the Fill/Pack/Warp analysis
Set StudyDoc = Synergy.StudyDoc()
StudyDoc.AnalysisSequence "Flow|Warp"
Set StudyDoc = Synergy.StudyDoc()
StudyDoc.AnalyzeNow False, True
StudyDoc.Close

End Sub

 

Regards,

 

Romain GUY

Please use plain text.
Product Support
nordhb
Posts: 463
Registered: ‎03-23-2009

Re: Application Programming Interface (VBA & Moldflow)

12-03-2012 02:08 AM in reply to: romslayer

Hi,
you can get the job status from job.
For job "i" such as:
Set Job = JobManager.GetJob(i)
Job_ModelName = Job.ModelName()
Job_Status = Job.Status()


For the job you check the status, until job no longer in job manager, meaning status is nothing.
Something like this:

Do 
'Find the number of study
  i = 1
  Set JobManager = Synergy.JobManager()
  Number = JobManager.NumberOfJobs
  Job_Status =""
  
'Find the good study
  While (i < Number + 1)
    Set Job = JobManager.GetJob(i)
    Job_ModelName = Job.ModelName()
    Name = Left(Job_ModelName, Len(Job_ModelName) - 4)
    If Name = StudyName Then 
      Job_Status = Job.Status()
    Else
      Job_Status =""
    End If
    i = i + 1
  Wend

  ' you can wait to loop to check status
  ' Application.Wait(Now + TimeValue("0:00:10")) 
  ' or
  ' wscript.sleep 3000 ' wait 3s, 3000ms

Loop While Job_Status <> ""

Hope this helps.

 

Regards,

Berndt 

 



Berndt Nordh
Please use plain text.
Contributor
romslayer
Posts: 13
Registered: ‎07-06-2012

Re: Application Programming Interface (VBA & Moldflow)

12-03-2012 04:53 AM in reply to: nordhb

Hi

 

Ok, I understand !

I did change and now he run. But I can't put "Application.Wait(Now + TimeValue("0:00:10")) " in the loop because if the calcul finish when the application wait, the calcul diseapear in the Job Manager and the loop loose the Completed Status.

 

At the end , I obtained :

 

Private Sub Btndeb_Click()

Dim rep As String
rep = "Completed"
Dim a As String
Dim i As Single
Dim Number As Single
Dim Name As String
Dim Job_Status As String
Dim Job_ModelName As String
rep = "Completed"

'launch the Flow analysis
Set Synergy = CreateObject("synergy.Synergy")
Synergy.SetUnits "Metric"
Set StudyDoc = Synergy.StudyDoc()
StudyDoc.AnalysisSequence "Flow"
Set StudyDoc = Synergy.StudyDoc()
StudyDoc.AnalyzeNow False, True

'Take the name of the study
Dim StudyName As String
Set StudyDoc = Synergy.StudyDoc()
StudyName = StudyDoc.StudyName
StudyName = Left(StudyName, Len(StudyName) - 4)
StudyDoc.Close


Do
'Find the number of study
i = 1
Set JobManager = Synergy.JobManager()
Number = JobManager.NumberOfJobs
Job_Status = ""

'Finf the good study
    While (i < Number + 1)
        Set Job = JobManager.GetJob(i)
        Job_ModelName = Job.ModelName()
        Name = Left(Job_ModelName, Len(Job_ModelName) - 4)
        If Name = StudyName Then
            Job_Status = Job.Status()
            GoTo ici
        Else
            Job_Status = ""
        End If
        i = i + 1
    Wend
Loop While Job_Status <> ""

ici:

Do
Job_Status = Job.Status()
Loop While Job_Status <> rep


'Open the study
Set Project = Synergy.Project
Project.OpenItemByName StudyName, "Study"

'Launch the Fill/Pack/Warp analysis
Set StudyDoc = Synergy.StudyDoc()
StudyDoc.AnalysisSequence "Flow|Warp"
Set StudyDoc = Synergy.StudyDoc()
StudyDoc.AnalyzeNow False, True
StudyDoc.Close

End Sub

 I am grateful for your help

 

See you soon on the forum

 

Regards

 

Romain GUY

Please use plain text.
Product Support
nordhb
Posts: 463
Registered: ‎03-23-2009

Re: Application Programming Interface (VBA & Moldflow)

12-03-2012 05:18 AM in reply to: romslayer

Hi,
thank you. Just some comments.
I think the reason is you look for "Completed".
I mean, when job is completed it is no longer in Job Manager, and when job is completed the job status will be empty " ".
That is why job status also is set to empty "", when job is no longer in job manager.
I am not sure if it interfere with other things you do, but I think you can remove the ici exit, and second Do-loop.

Also, you might need to add a little wait time, but I am not sure.
I think the job will start before you close study, so the job_status will hold data.
Otherwise you can add a short wait.
Suggestion:

Application.Wait(Now + TimeValue("0:00:10"))  '*** Add this line to get the job some time to start, if needed

Do
'Find the number of study
  i = 1
  Set JobManager = Synergy.JobManager()
  Number = JobManager.NumberOfJobs
  Job_Status = ""

'Finf the good study
    While (i < Number + 1)
        Set Job = JobManager.GetJob(i)
        Job_ModelName = Job.ModelName()
        Name = Left(Job_ModelName, Len(Job_ModelName) - 4)
        If Name = StudyName Then
            Job_Status = Job.Status()
            GoTo ici '*** Remove this line
        Else
            Job_Status = ""
        End If
        i = i + 1
    Wend
  Application.Wait(Now + TimeValue("0:00:10"))  '*** Add this line for wait
Loop While Job_Status <> ""

ici: '*** Remove this line

Do  '*** Remove this line
Job_Status = Job.Status()  '*** Remove this line
Loop While Job_Status <> rep '*** Remove this line , do not need to check rep "Completed" 

If you are all set with your script as is, you can ignore my suggestions.

 

Regards,
Berndt

 



Berndt Nordh
Please use plain text.