You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VBA转CATScript:实现Collection式字典键移除功能方案咨询

Replicating VBA Collection Behavior in VBScript/CATScript for Dictionary Key Removal

Hey there! As a fellow CATIA scripter, I get how frustrating it can be when VBA's handy tools don't translate directly to CATScript. Let's walk through the best way to replicate your original logic without relying on arrays, since you're new to this and want a clean, familiar approach.

The Core Issue

In VBA, your Collection was perfect here because it let you grab keys to remove by their position (index). VBScript doesn't have that built-in Collection (and CATIA's environment might not support the Microsoft.VisualBasic.Collection COM object), so we'll use a second Scripting.Dictionary to mimic that index-based access.

Optimal Solution: Simulate a Collection with a Dictionary

We'll create a "removal list" dictionary where:

  • The keys are sequential numbers (1, 2, 3...) just like a Collection's item indices
  • The values are the actual keys we need to delete from your main docsToSave dictionary

This gives you the exact index-based access you had with VBA's Collection, while staying fully compatible with CATScript. Here's the code:

' Initialize your main document dictionary
Dim docsToSave : Set docsToSave = CreateObject("Scripting.Dictionary")
' Add sample entries (replace with your actual data)
docsToSave.Add "Drawing1.CATDrawing", "C:\MyDocs\Drawing1.CATDrawing"
docsToSave.Add "Part1.CATPart", "C:\MyDocs\Part1.CATPart"
docsToSave.Add "Assembly1.CATProduct", "C:\MyDocs\Assembly1.CATProduct"

' Create a simulated Collection using a Dictionary for keys to remove
Dim toRemove : Set toRemove = CreateObject("Scripting.Dictionary")
' Add keys to remove—use toRemove.Count +1 to auto-increment the index
toRemove.Add toRemove.Count + 1, "Part1.CATPart"
toRemove.Add toRemove.Count + 1, "Assembly1.CATProduct"

' Loop through the simulated Collection and clean up the main dictionary
Dim i
For i = 1 To toRemove.Count
    Dim keyToRemove : keyToRemove = toRemove(i)
    ' Always check if the key exists first to avoid runtime errors
    If docsToSave.Exists(keyToRemove) Then
        docsToSave.Remove keyToRemove
    End If
Next

Why This Works

  • Index-based access: Just like your VBA code, toRemove(i) lets you pull the key to remove by its position in the list.
  • Error prevention: The Exists() check stops your script from crashing if a key was already deleted or never existed.
  • CATScript-friendly: Scripting.Dictionary is guaranteed to work in CATIA's scripting environment—no missing COM objects to worry about.

Simplified Alternative (If Order Doesn't Matter)

If you don't care about the order you remove keys, you can skip managing indices entirely. Store the keys to remove directly as keys in a second dictionary (values can be anything, like True):

Dim toRemove : Set toRemove = CreateObject("Scripting.Dictionary")
toRemove.Add "Part1.CATPart", True
toRemove.Add "Assembly1.CATProduct", True

' Loop through the removal list keys
Dim key
For Each key In toRemove.Keys
    If docsToSave.Exists(key) Then
        docsToSave.Remove key
    End If
Next

This is even cleaner if order isn't important, but the first solution is the closest match to your original VBA workflow.

内容的提问来源于stack exchange,提问作者Laurens Ruben

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:25:55