Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations KootK on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ending a 'StartCommand' command

Status
Not open for further replies.

CheeseMaker13

Aerospace
Sep 25, 2012
42
Hi Guys,

I'm currently writing a script to extract Inertial Data from a product of numerous parts.

The problem I'm coming up against is that I need to run CATIA.StartCommand ("Measure Inertia") and once I've got all the data from the Parameters fields (E.g. "Gx = oProduct.Parameters.Item("Gx").Value") in VBA I need to then close it, select the next part and run the command again.

Currently, without closing the Inertia Window, I get a load of Gx Parameters and it picks the first that it finds.

Is there a way to either:
A) Close the Measure Inertia window.
B) Extract CofG, Volume and Stock Size data (Volume, Gx, Gy, Gz, BBLx, BBLy, BBLz) without using the Inertia Window.

I've researched A) to death including trying to hit enter to close the window.

B) I'm going to attempt now but I have a feeling I've attempted that before.

Thanks!!

 
Replies continue below

Recommended for you

Hi Ferdo,

Had considered that, but there are multiple instances of parts which only show their true CofG when in the Product.

I've managed to find some code that closes the window for me:


ut the problem now is, even with DoEvents multiple times, it runs too fast.

For example, if I step through it manually, each part in excel will have the correct data.

If I automatically do it, two, three or four lines will have the same data on it.

It's like CATIA isn't closing the window (and hence clearing the parameters) quick enough :/

Any ideas on commands apart from Sleep (1000) or DoEvents that would cure this problem?? :/
 
I've got same problem, in some way, procedure Start'n'Wait helps me with not executing next command line till previous line wasn't done, but you have to create external shell file, which doesn't support CATIA's library objects - I've used SendKeys function. It's quiet tricky, sometimes SendKeys are not typed correctly in Catia window

Code:
Public Sub ShellandWait(ByVal ProcessPath As String)
        Dim objProcess As System.Diagnostics.Process
        Try
            objProcess = New System.Diagnostics.Process()
            objProcess.StartInfo.FileName = ProcessPath
            objProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal
            objProcess.Start()

            'Wait until the process passes back an exit code 
            objProcess.WaitForExit()

            'Free resources associated with this process
            objProcess.Close()
        Catch
            MessageBox.Show("Could not start process " & ProcessPath, "Error")
        End Try
    End Sub


Code:
'...
AppActivate "CATIA V5"
StartCMD = "wscript D:\ShellFile.vbs "
Call ShellandWait(StartCMD)
'...


D:\ShellFile.vbs code:
Code:
Set WshShell = WScript.CreateObject("WScript.Shell")
WScript.sleep 100
WshShell.SendKeys "c:Measure Inertia" + "{ENTER}", True


B) Extract CofG, Volume and Stock Size data (Volume, Gx, Gy, Gz, BBLx, BBLy, BBLz) without using the Inertia Window.
B) I'm going to attempt now but I have a feeling I've attempted that before.
Try export Inertia Measure to txt file, of course the measure must be created with MainProduct selected. In txt file You'll get all parts and products listed with theirs properties - mass, COG, BBL's, etc... You only have to write code to retrieve those informations and set as parameters

Anyway, I think, the best solution is to create UserRefProperties which are connected through formula to measured parameters. Then You'll have to run Your macro only once, and all parameters would be always up-to-date.

more informations and sample code: (12. post)


Lukasz

LukaszSz. Poland, Warsaw University of Technology, Faculty of Power and Aeronautical Engineering : MEchanical Engineering. BsC - 2013
 
Hi Lukasz,

Thanks. This works in Alt+F11 yes? (The VBA edit window - not catscript?)

Reason being I get an error on this:

Code:
Public Sub ShellandWait(ByVal ProcessPath As String)
        Dim objProcess As System.Diagnostics.Process
        Try
            objProcess = New System.Diagnostics.Process()
            objProcess.StartInfo.FileName = ProcessPath
            objProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal
            objProcess.Start()

            'Wait until the process passes back an exit code 
            objProcess.WaitForExit()

            'Free resources associated with this process
            objProcess.Close()
        Catch
            MessageBox.Show("Could not start process " & ProcessPath, "Error")
        End Try
    End Sub

"Dim objProcess As System.Diagnostics.Process" gets a "Compile error: User-defined type not defined"

If I delete that line I get 'Sub or Function not defined' on the "Try" line.

Thoughts?
 
Place at header:

Code:
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
 Public Const PROCESS_QUERY_INFORMATION = &H400 
 Public Const STILL_ACTIVE = &H103

LukaszSz. Poland, Warsaw University of Technology, Faculty of Power and Aeronautical Engineering : MEchanical Engineering. BsC - 2013
 
I'm very sorry, I've posted wrong code and headers without trying it (I have copied previous function and headers from google instead of my macro because I was away from my personal computer)

Here is working code:

Code:
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess _
    As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle _
    As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
 

Private Sub ShellAndWait(ByVal program_name As String, _
                         Optional ByVal window_style As VbAppWinStyle = vbNormalFocus, _
                         Optional ByVal max_wait_seconds As Long = 0)
Dim lngProcessId As Long
Dim lngProcessHandle As Long
Dim datStartTime As Date
Const WAIT_TIMEOUT = &H102
Const SYNCHRONIZE As Long = &H100000
Const INFINITE As Long = &HFFFFFFFF

    ' Start the program.
    On Error GoTo ShellError
    lngProcessId = Shell(program_name, window_style)
    On Error GoTo 0
    
    DoEvents

    ' Wait for the program to finish.
    ' Get the process handle.
    lngProcessHandle = OpenProcess(SYNCHRONIZE, 0, lngProcessId)
    If lngProcessHandle <> 0 Then
        datStartTime = Now
        Do
          If WaitForSingleObject(lngProcessHandle, 250) <> WAIT_TIMEOUT Then
            Exit Do
          End If
          DoEvents
          If max_wait_seconds > 0 Then
            If DateDiff("s", datStartTime, Now) > max_wait_seconds Then Exit Do
          End If
        Loop
        CloseHandle lngProcessHandle
    End If
    Exit Sub
    
ShellError:
End Sub

LukaszSz. Poland, Warsaw University of Technology, Faculty of Power and Aeronautical Engineering : MEchanical Engineering. BsC - 2013
 
Hi mate,

Thanks.

Will get back to you and post some of the code (when it is working!!)
 
Hi,

I suppose first code posted is in vb.net so is normal to get errors in vba.

Just for my curiosity, why do you need the CoG for each part instance in the assembly (if I understood correctly you want the CoG for each instance in his place, isn't it?). If you get the CoG for the whole assy calculated by CATIA is not enough?

Regards
Fernando

 
More accurate inertia calculations require more detailed data.

Assuming the CofG is lumped into one point isn't as good as all the parts separated.

I've reworked the code now so pausing is no longer needed.

Essentially now I open up the top level product, search for each item in the spreadsheet and then export all the data I want back to it.

Slow, but it works.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor