3DX Development Chronicles

#ekl #bestpractice #kac

cover document

Note that you will need a registered account to access the file.

#ekl #bestpractice #python

Until now, you could use Python via the COM interface for automation. There are wrappers for Catia V5 pycatia and for 3DEXPERIENCE experience. In the Templates Rules Designer, you can now create Python scripts using the EKL language. You can use external packages from PyPI (make sure to use the correct version), or even create your own packages. If you want to take it further, you can create a package that uses automation and call it from a Python script. Spoiler alert: the selection methods do not work.

Would you like to learn more? Then check out the best practices on Python scripting in 3DEXPERIENCE!

cover document

Note that you will need a registered account to access the file.

#3dx #v5 #ekl

A ternary operation in programming is a concise way to perform conditional evaluations. The ternary operator takes three operands and is typically used as a shorthand for an if-else statement.

Syntax

The general syntax of a ternary operation is:

condition ? expression_if_true ; expression_if_false

Explanation

  • condition: This is the expression that is evaluated. If it evaluates to true, the first expression is executed; if false, the second expression is executed.
  • expression if true: This expression is returned if the condition is true.
  • expression if false: This expression is returned if the condition is false.

Example

Create a Boolean parameter “switch” with value true Create a Point.1 with coordinates (10, 0, 0) Create a Point.2 with coordinates (-10, 0, 0) Create a parameter of type point and in the formula editor type:

switch == true ? Point.1 ; Point.2

You might need to select the points from the tree to be sure the complete path is caught

In this example, the condition checks if switch is true or false. If true, it assigns “Point.1” to the point parameter, otherwise, it assigns “Point.2”

It can be used directly in formulas, making it useful for assignments. For complex conditions, a rule or reaction can still be used.

#vba #v5 #3dx

Maybe you are working with Catia V5 and 3DEXPERIENCE on a daily basis depending on customers needs. Most of the API is common for both aplications but some of the key methods are different. In order to reuse the code for both aplications, you can check the version and depending on it follow a diference code path.

Dim CatiaVersion As Long
CatiaVersion = CATIA.System.Configuration.Version 

If CatiaVersion = 5 Then
    ' Put your V5 code here

ElseIf CatiaVersion = 6 Then
    ' Put your 3dx code here

End If

#vba #v5 #3dx

When files are moved or downloaded, ensuring their integrity is crucial. One popular method for verifying that a file has not been altered is through MD5 hashing.

What is MD5 Hashing?

MD5 (Message Digest Algorithm 5) is a cryptographic hash function that produces a 128-bit (16-byte) hash value, typically represented as a 32-character hexadecimal number. It takes an input (such as a file) and generates a unique hash. Even a tiny change in the file will result in a completely different hash.

Why is MD5 Important?

MD5 is widely used to verify file integrity. When you download a file, the provider often supplies an MD5 hash. After downloading, you can compute the hash of your copy and compare it to the original. If the hashes match, the file is intact; if not, it may be corrupted or tampered with.

How to Use MD5 for File Integrity: 1. Before Transfer: The sender computes the MD5 hash of the file and shares it. 2. After Transfer: The receiver computes the MD5 hash of the received file. 3. Comparison: If both hashes are identical, the file is unchanged.

How to Use MD5 for File Integrity

You can verify file integrity using MD5 in two common ways: with PowerShell or with VBA code.

Using PowerShell

PowerShell provides a simple command to compute the MD5 hash of a file:

Get-FileHash -Algorithm MD5 "C:\path\to\file"

This command outputs the MD5 hash, which you can compare with the expected value.

Using VBA Code

If you prefer to automate the process within Microsoft Office or other VBA-enabled environments, you can use a VBA module to compute the MD5 hash. For example, you might have a module like ComputeMD5.bas with a function to calculate the hash of a file:

On Error Resume Next

Dim Hash As String
Hash = ComputeMd5("C:\path\to\file")

If Err.Number <> 0 Then
        MsgBox "Error: " & Err.Description
Else
        MsgBox "MD5: " & Hash
End If

This approach is useful for integrating file integrity checks into your own automation workflows.

Limitations

While MD5 is fast and easy to use, it is not collision-resistant. For highly sensitive applications, stronger algorithms like SHA-256 are recommended. However, for basic file integrity checks, MD5 remains a practical choice.

Conclusion

MD5 hashing is a simple yet effective way to ensure files remain unchanged during transfers. By comparing hash values before and after moving or downloading files, users can quickly verify file integrity and prevent issues caused by corruption or tampering.

You can also check external files (settings, XML, TXT, etc.) of your application for unwanted modifications before using the app. This check will not prevent users from modifying the files, but if they do so, you will be able to log the issue and inform them to use an unmodified file.😉

ComputeMd5.bas

The original code has been extracted from this source and then refactored to make it more module-friendly and easier to understand.

' Checks if the specified file exists.
Private Function FileExists(FilePath As String) As Boolean
    On Error GoTo ErrorHandler
    FileExists = (Len(Dir(FilePath)) <> 0)
    Exit Function
ErrorHandler:
    FileExists = False
End Function

' Allocates a buffer for reading file data, rounding up to the nearest KB if needed.
Private Function AllocateBuffer(HFile As Integer, BlockSize As Long) As Byte()
    Const KB As Long = 1024
    ' Adjust block size to next multiple of 1024 if file is smaller than requested block size
    If LOF(HFile) < BlockSize Then
        BlockSize = ((LOF(HFile) + KB - 1) \ KB) * KB
    End If
    Dim Buffer() As Byte
    ReDim Buffer(0 To BlockSize - 1)
    AllocateBuffer = Buffer
End Function

' Computes the MD5 hash of a file stream using block-wise reading.
Private Function ComputeFileHash(HFile As Integer, BlockSize As Long) As Byte()
    Dim Md5Provider As Object
    Set Md5Provider = CreateObject("System.Security.Cryptography.MD5CryptoServiceProvider")

    Dim FileLen As Long
    FileLen = LOF(HFile)

    Dim NumBlocks As Long
    NumBlocks = FileLen \ BlockSize

    Dim RemainderSize As Long
    RemainderSize = FileLen Mod BlockSize

    Dim Buffer() As Byte
    ReDim Buffer(0 To BlockSize - 1)

    Dim i As Long
    ' Read and process each block
    For i = 1 To NumBlocks
        Get HFile, , Buffer
        Md5Provider.TransformBlock Buffer, 0, BlockSize, Buffer, 0
    Next i

    ' Process the final block
    If RemainderSize > 0 Then
        Get HFile, , Buffer
        Md5Provider.TransformFinalBlock Buffer, 0, RemainderSize
    Else
        Md5Provider.TransformFinalBlock Buffer, 0, 0
    End If

    ComputeFileHash = Md5Provider.Hash
    Md5Provider.Clear
    Set Md5Provider = Nothing
End Function

' Converts a byte array to a hexadecimal string.
Private Function BytesToHex(Buffer() As Byte) As String
    ' Converts a byte array to a hexadecimal string
    Dim HexStr As String: HexStr = ""
    Dim i As Long
    For i = LBound(Buffer) To UBound(Buffer)
        HexStr = HexStr & Right("0" & Hex(Buffer(i)), 2)
    Next i
    BytesToHex = HexStr
End Function

' Main function to compute the MD5 hash of a file and return it as a hex string.
Public Function ComputeMd5(FilePath As String) As String
    Const DEFAULT_BLOCK_SIZE As Long = 2 ^ 16

    If Not FileExists(FilePath) Then
        Err.Raise 53, , "File not found." & vbCr & FilePath
    End If

    Dim HFile As Integer
    HFile = VBA.FreeFile
    On Error GoTo ErrorHandler
    Open FilePath For Binary Access Read As HFile

    Dim HashBuffer() As Byte
    HashBuffer = ComputeFileHash(HFile, DEFAULT_BLOCK_SIZE)
    ComputeMd5 = BytesToHex(HashBuffer)

    If HFile <> 0 Then Close HFile
    Exit Function

ErrorHandler:
    On Error Resume Next
    If HFile <> 0 Then Close HFile
    Err.Raise 5, , "File could not be processed." & vbCr & FilePath
End Function

#vba #3dx

Information extracted from the User Assistance

In a collaborative space, managing access and permissions is essential. In the 3DEXPERIENCE platform, credentials comprise a collaborative space, a role and an organization, and define the content a user can access and the actions they can perform.

What are credentials?

When users log in, they select a collaborative space, role, and organization. This combination is called credentials. Credentials determine the user's access rights and the scope of content available to them.

Users may be authorized for multiple collaborative spaces but are only connected to one set of credentials at a time. While users can be assigned multiple roles within a collaborative space, this is generally discouraged due to the hierarchical nature of role access.

How roles work

Roles dictate what content users can access and what operations they can perform, such as searching, viewing, creating, or deleting content. Roles inherit the permissions of lower ones.

Example role hierarchy:

  • Reader: Can view content and create personal management items (Favorites, Personal Folders).
  • Contributor: Inherits Reader rights and can create evaluation content (Reviews, Simulations).
  • Author: Inherits Contributor rights and can create definition content (Requirements, CAD, EBOM Part, etc.).
  • Leader: Inherits Author rights and can create design resources (Libraries, Project Templates).

Restricted roles (e.g., Reader Restricted, Contributor Restricted) limit read access to content owned by the collaborative space and organization but otherwise function like their regular counterparts.

Administrative roles

Administrative roles provide broader access and management capabilities: – Owner: Can read all content in a collaborative space, create administrative resources, manage resources, and modify properties for repair or exception tasks. – Administrator: Inherits all Owner rights across all collaborative spaces, manages company resources, and can modify any property in the 3DEXPERIENCE 3D Space service. – Owner Restricted: Restricts read access to content owned by the collaborative space and organization, with the same capabilities as a regular Owner.

Use case

It makes sense to check the user's credentials before they use an application that modifies or adds geometry. The idea is to inform the user that, with the current credentials, it is not possible to save data. This avoids the situation where a user has been working for some time, only to find that they cannot save to the database. 🙁

Function to check if the active user write rights

Const VALID_WRITE_ROLES As String = "VPLMCreator,3DSRestrictedAuthor,VPLMProjectLeader,3DSRestrictedLeader"


' Checks if the currently connected user has write rights based on their role.
' Returns True if the user's role is in the list of valid write roles.
Function HasUserWriteRights() As Boolean

    On Error GoTo ErrorHandler

    Dim PnoService As PnoService
    Set PnoService = CATIA.GetSessionService("PnOService")

    Dim ConnectedPerson As Person
    Set ConnectedPerson = PnoService.Person

    Dim ActiveRole As String
    ActiveRole = ConnectedPerson.RoleID

    HasUserWriteRights = ValueInList(ActiveRole, Split(VALID_WRITE_ROLES, ","))

ErrorHandler:
    Exit Function

End Function


' Checks if a given value exists in the provided list (array).
' Returns True if found, otherwise False.
Private Function ValueInList(Value As String, List As Variant) As Boolean

    If Not IsArray(List) Then Exit Function

    If Len(Trim(Value)) = 0 Then Exit Function

    Dim i As Integer
    For i = LBound(List) To UBound(List)
        If StrComp(List(i), Value, vbBinaryCompare) = 0 Then
            ValueInList = True
            Exit For
        End If
    Next i

End Function


Sub CATMain()
    MsgBox HasUserWriteRights
End Sub

In a future post, we will learn how to check whether files are locked and who locked them.

#ekl #bestpractice

EKL is an interpreted, object-oriented programming language that builds upon the DS C++ automation programming language. It provides a robust development environment that includes knowledge packages, types, methods, and functions, enabling users to easily customize and automate task execution on both the client and server sides.

This feature already existed in Catia V5, but Dassault Systèmes has significantly enhanced the language for 3DEXPERIENCE.

In the following document, you will find the fundamentals of the language, details about the editor, instructions on how to use it in actions, and guidance on storing code in libraries, among other topics.

cover document

Note that you will need a registered account to access the file.

#vba #bestpractice

If you are a beginner or a seasoned developer, this guide will teach you how to use VBA, VSTA, and CATScript to access CAA Automation objects, enhance productivity, and customize applications for automating repetitive tasks.

This is a document you cannot miss if you are starting or transitioning from CATIA V5.

What you will learn:

  • Automation in 3DEXPERIENCE
  • Working with VBA/VSTA/CATScript Projects
  • VBA/VSTA Installation
  • Object Model Map
  • Troubleshooting
  • Debugging
  • Important Use Cases

cover document

Note that you will need a registered account to access the file.