Ending a 'StartCommand' command
Ending a 'StartCommand' command
(OP)
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!!
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!!





RE: Ending a 'StartCommand' command
One idea it would be to open parts in new window then you can use CATIA.StartCommand("Measure Inertia") if necessary, get what you want - there are a lot of examples here in forum or in CATIA Portable Script Center - then just close the new window without bothering for something else...
Regards
Fernando
https://picasaweb.google.com/102257836106335725208
https://picasaweb.google.com/103462806772634246699...
RE: Ending a 'StartCommand' command
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:
http://www.pbdr.com/vbtips/api/FindCloseAPI.htm
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?? :/
RE: Ending a 'StartCommand' command
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 SubCODE -->
D:\ShellFile.vbs code:
CODE -->
Set WshShell = WScript.CreateObject("WScript.Shell") WScript.sleep 100 WshShell.SendKeys "c:Measure Inertia" + "{ENTER}", TrueTry 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: http://www.eng-tips.com/viewthread.cfm?qid=341826 (12. post)
Lukasz
LukaszSz. Poland, Warsaw University of Technology, Faculty of Power and Aeronautical Engineering : MEchanical Engineering. BsC - 2013
RE: Ending a 'StartCommand' command
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?
RE: Ending a 'StartCommand' command
CODE -->
LukaszSz. Poland, Warsaw University of Technology, Faculty of Power and Aeronautical Engineering : MEchanical Engineering. BsC - 2013
RE: Ending a 'StartCommand' command
RE: Ending a 'StartCommand' command
Here is working code:
CODE --> catvba
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 SubLukaszSz. Poland, Warsaw University of Technology, Faculty of Power and Aeronautical Engineering : MEchanical Engineering. BsC - 2013
RE: Ending a 'StartCommand' command
Thanks.
Will get back to you and post some of the code (when it is working!!)
RE: Ending a 'StartCommand' command
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
https://picasaweb.google.com/102257836106335725208
https://picasaweb.google.com/103462806772634246699...
RE: Ending a 'StartCommand' command
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.