Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Run a VBscript in a parameter formula

Status
Not open for further replies.

ArnaudG

Mechanical
Dec 5, 2019
25
Hi all,
I created a VBscript (CATVBS file) that works fine to recover the mass of a part and put it inside a custom mass parameter.

I noticed that in parameter formulas it should be possible to run a specific script with the command VBscript->Run(). However I don't understand how it works. I tried to use the full path of my script, only its name, its full name (name + extension) but it won't work. Moreover I don't understand what should be the arguments of the Run command.

How could I use this script (maybe with some modifications, for instance using a Function returning the mass instead of a Sub writing the value in a parameter) in order to run it inside a custom parameter and maybe get the value in this parameter ? The help is not very detailed for stuff like that.

Here's the code (remember it's a catvbs) which will send the calculated mass to a mass parameter called CalculatedWeight (it must exist in your part if you want it to work).

Code:
Option Explicit

Sub CATMain()

'On Error Resume Next

Dim oDoc
Set oDoc = CATIA.ActiveDocument

Dim oPart
Set oPart = oDoc.Part

If (Err.Number <>0) Then
	MsgBox "Cette macro fonctionne avec un fichier CATPart ouvert uniquement"
	Exit Sub
End If

Err.Clear

Dim oBody
Dim objSPAWorkbench 
Dim objInertia
Dim oWeight 

Set oBody = oPart.MainBody
Set objSPAWorkbench = oPart.Parent.GetWorkbench("SPAWorkbench")
Set objInertia = objSPAWorkbench.Inertias.Add(oBody)

If (Err.Number = 0) Then
  oWeight = objInertia.Mass
Else
  oWeight = 1
  MsgBox "Impossible de déterminer la masse du corps principal." & vbCr & "Masse définie à 1."
End If

oPart.Parameters.Item("CalculatedWeight").Value = oWeight

End Sub
 
Replies continue below

Recommended for you

hi Arnaud;
your script assumes there is a parameter called CalculatedWeight. you might consider adding this parameter if it is not there in the first place.
in KWA (knowledge Advisor), there is "Macros with arguments" button inside Actions ribbon. (it has a little film clap with VB on it)
that will put your script inside the tree-branch (with default VB Scripts.1). Just copy-paste your posted script, don't include the sub CATMain line, since it is already provided.
By the way, you can now see how to pass in arguments into the script :)
anyways, to run the script, you can create a reaction based on a parameter change i.e create a boolean, call it "run script" and put it to False.
add a reaction, source Selection, choose your freshly created boolean parameter...
Available events: Value change
Action, Edit Action... (if you want, you can directly write your script here, as VB action), but I want to demonstrate how to use the "run" command!
so, choose Knowledgeware action
Inside your Action editor, double click on your script, it will end up in the editor
in the dictionary, bottom left, look for the Messages and Macros, and investigate whats there; all different kinds of ways to start a macro :)
there is LaunchMacroFromFile(file:String):VoidType (to answer your question how to write in path to your script)
but there is also VB Script -> Run(valueOrFeature:ObjectType, ...):VoidType
click on it! and Voila! you almost done
I usually add a line to change your boolean back to false...
`Run Script` = false





regards,
LWolf
 
Thanks for the long answer.
Indeed my part has already the parameter created (otherwise I would have created it in the script). However I'm trying not to use KWA licence (because if i need to use the knowledge advisor, I just write a rule and that's it) but then maybe it's not possible to launch the script from its path.

I also tried to launch a VBA macro but it doesn't work either.

So do you think it's possible to use VBscript->Run() on a file script or a VBA script inside the active VBA project, without the KWA license ?
 
By the way I found what I need in a law (not a formula) using a basic useless law with two formal parameters (because it's needed in a law) and then I was able to use other tricks to launch my macro with LaunchMacroFromFile.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor