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) :
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
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.
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.