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!

VBA Create a Fog Law and add formal parameters

Status
Not open for further replies.

ArnaudG

Mechanical
Dec 5, 2019
25
Hi,
I've been able to create a law with VBA code and add Length parameters. However I need to use a VOLUME parameter and it doesn't work.
I can do it manually (create the law and manually add the volume formal parameter) and my law is working. However when I do it with VBA, it seems that I can only create a LENGTH type formal parameter. If I use VOLUME it makes Catia crash (be aware of that and save your code before).

Lets say I have a CATPart opened. Here's a simplified version of what I'm doing (I skip the check if it's a part or not, I do that in another function) :

Code:
Sub CreateLaw()
Dim dDate As Variant
Dim oDoc As Document 'document actif
Dim oPart As Part 'part du document actif
Dim oRels As Relations 'relations du document actif

Dim oLaw As Law 'loi pour le calcul de la masse via macro CATscript
Dim sLawName As String 'nom de la loi qui sera créée
Dim sLawComment As String '1ère ligne de la loi
Dim sLawBody As String 'texte de la loi
Dim sLawPrm1 As String '1er paramètre formel de la loi (should be a volume)
Dim sLawPrm2 As String '2e paramètre formel de la loi (longueur)

dDate = Now()
sLawName = "Law_CalculatedMass"
sLawPrm1 = "fLawLongIn"
sLawPrm2 = "fLawLongOut"
'------------- texte de la loi -------------
sLawComment = "/*Law created by MaterialMacro " & dDate & "*/"
sLawBody = sLawComment & vbCrLf _
& "if " & sLawPrm1 & "<>0mm " & vbCrLf _
& "{ " & sLawPrm2 & "=1mm " & vbCrLf _
& "}"

'MsgBox sLawBody

Set oDoc = CATIA.ActiveDocument


Set oPart = oDoc.Part
Set oRels = oPart.Relations

'on regarde si la loi existe déjà en se basant sur son nom
If oRels.Count <> 0 Then
    Dim obj As Object
    For Each obj In oRels
        If obj.Name = sLawName Then
        bLawExists = True
        Set oLaw = obj
        Exit For
        End If
    Next
End If

'si elle existe on la supprime
If bLawExists = True Then
    oRels.Remove oLaw.Name
    Set oLaw = Nothing
End If

'On (re)crée la loi et ses paramètres formels
Set oLaw = oRels.CreateLaw(sLawName, _
"Récupération de la masse via macro CATscript", sLawComment)
oLaw.AddFormalParameter sLawPrm2, "LENGTH"
oLaw.AddFormalParameter sLawPrm1, "[highlight #FCE94F][b]LENGTH[/b][/highlight]"
oLaw.Modify sLawBody

'--------------- NETTOYAGE -------------
Set oDoc = Nothing
Set oRels = Nothing
Set oLaw = Nothing
Set obj = Nothing
End Sub

So if I change the highlighted LENGTH by VOLUME (which is correct according to the help and the possible magnitude values), Catia crashes.
Note that this code (like it is now) should work and the law should have no error in it. Of course if you're able to add a VOLUME formal parameter, the law will fail because of the units used inside for
Code:
if " & sLawPrm1 & "<>0mm

But if you're able to create a formal parameter of type VOLUME that would save me some weird workaround that I'm going to do.

To be more precise, I can't create a rule because it would require a KWA license that I don't have. So I'm using knowledgeware inside a law. However I need this law to be valuated each time the volume changes (or the material). I can't base the law on a single length so when I did this manually to try my idea, I was able to create a vollume formal parameter and this law was used in a formula which was using the smartvolume(PartBody) formula (and other stuff). If I can't create with VBA the law as intended, I will need to create another fantom parameter to check the volume and divide it by a fake area in order to get a length. This should work but it's nasty.
 
Replies continue below

Recommended for you

OK, I can still be interested in the solution to create a formal parameter of type VOLUME because it could be useful to other people but I found my solution. I tried simply to add my smartVolume function inside the law and it works. In my case I have to launch a CATscript so here's the law :

Code:
if fLawLongIn<>0mm 
        fLawLongOut=1mm 

if smartVolume(`Corps principal` ) <>0
	LaunchMacroFromFile("D:\Tests Catia\V5\Macros\Macro1.CATScript")

It's stupid but it works and great thing with that is that fLawLongOut and fLawLongIn are LENGTH and I don't even have to use the law in a formula to make it work, so even better than what I expected at first because I save the creation of the formula and a useless parameter.
Going to check with VBA but I just have to change my code for the sLawBody and it should be OK.
 
Actually, it seems that LENGTH is the only mangnitude that works.
That is without a doubt a bug that should be reported to Dassault Systemes (you do it via your VAR).
 
That's what I thought, thanks.

I have another question about laws and writing it through a macro. You may have understood that I'm french and mostly working with french version of Catia. So the parameter names and other stuff are localized but they contain some special characters like the space in "corps principal" or the accent on "Matériau".
If I write my law as I did in my code above, the name of the main body "Corps principal" is not recognized, even if I put the ` character (which is what Catia does when I select it manually). Another bug or should I do another way around ?

Right know it's hard coded but I intend to recover the name of the main body inside the macro (same thing for the material parameter) because sometimes the body can be renamed. If you have an idea I'm all ears, if not I'll just rename the "Matériau" parameter in "Material" (because I control when the material will be applied so I can easily rename the parameter as soon as the material is applied) and for the body I'll just check if it contain special characters and rename it without those characters (because I don't want to fully change the name if it was already renamed by the user so that he barely notices the change but there is still a risk that the user renames again the body).
 
To be honest I faced language issues only when I implemented WinAPI applications. There was always a way around, such as getting main body in script not by name, but with Part.MainBody property.

Generating Knowledgeware code from script though tends to be complex and unreliable, so it's better to avoid this approach entirely if possible.

Take a look at User Feature and Power Copy mechanisms. They can be parameterized and then called in any language locale in a safe way.
 
I need a law because I must be able to update some properties if the body or material changes and I can't use a knowledgeware rule.

So as it didn't work with the spaces and accents, I decided to rename the main body and the Material parameter to avoid using "`" (even though I'm still getting the body name from the macro with part.mainbody.name)( and now it work fine and the law is doing what it's intended to do. Thanks again for the tips.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor