【问题标题】:Modifying a count code to run independently from a sub in CATIA修改计数代码以独立于 CATIA 中的子程序运行
【发布时间】:2020-04-28 16:09:48
【问题描述】:

我正在修改大量现有代码,在尝试完成之后,我对 VBA 的了解完全超出了我的想象。我的编码经验主要是在 Python 中,我很难理解对象结构以及 VBA 中可接受和不可接受的内容。

我正在尝试在用户选择的给定项目上修改用户添加的属性(“属性”菜单下的“添加其他属性”)。这段代码,作为一个独立的,将做我正在寻找的,将它集成到我现有的代码中是困难的。如何将以下代码修改为我可以使用的东西,这样它就不必在它自己的子代码中了?

Sub CATMain()

    GetNextNode CATIA.ActiveDocument.Product

End Sub



Sub GetNextNode(oCurrentProduct As Product)

    Dim oCurrentTreeNode As Product
    Dim i As Integer

    ' Loop through every tree node for the current product
    For i = 1 To oCurrentProduct.Products.Count
        Set oCurrentTreeNode = oCurrentProduct.Products.Item(i)

        ' Determine if the current node is a part, product or component
        If IsPart(oCurrentTreeNode) = True Then
            MsgBox oCurrentTreeNode.PartNumber & " is a part"

        ElseIf IsProduct(oCurrentTreeNode) = True Then
            MsgBox oCurrentTreeNode.PartNumber & " is a product" & i

        Else
            MsgBox oCurrentTreeNode.PartNumber & " is a component"
        End If


        ' if sub-nodes exist below the current tree node, call the sub recursively
        If oCurrentTreeNode.Products.Count > 0 Then
            GetNextNode oCurrentTreeNode
        End If

        If oCurrentTreeNode.Products.Count = 0 Then
            oCurrentTreeNode.ReferenceProduct.UserRefProperties.Item(1).Value = "Yippee!!!!!"
        End If

   Next


End Sub

这是我迄今为止的尝试,当我将它放入当前文本时,它似乎被忽略了。该计划是替换我们修改现有属性的当前方式,以便它能够读取 CATIA 树并修改单个零件和产品。此外,我尝试添加一个 createstring 来为不存在的用户创建一个新的用户属性。这将返回一个错误,指出程序需要一个 =。非常感谢任何帮助。

Dim oCurrentProduct As Product
Dim oCurrentTreeNode As Product
Dim i As Integer

' Loop through every tree node for the current product
For i = 1 To oCurrentProduct.Products.Count
    Set oCurrentTreeNode = oCurrentProduct.Products.Item(i)

    ' Determine if the current node is a part, product or component
    If IsPart(oCurrentTreeNode) = True Then
        MsgBox oCurrentTreeNode.PartNumber & " is a part"

    ElseIf IsProduct(oCurrentTreeNode) = True Then
        MsgBox oCurrentTreeNode.PartNumber & " is a product" & i

    Else
       MsgBox oCurrentTreeNode.PartNumber & " is a component"
    End If


    ' if sub-nodes exist below the current tree node, call the sub recursively
    If oCurrentTreeNode.Products.Count > 0 Then
        GetNextNode oCurrentTreeNode
    End If

    If oCurrentTreeNode.Products.Count = 0 Then
       oCurrentTreeNode.ReferenceProduct.UserRefProperties.Item(1).Value = "Yippee!!!!!"
       oCurrentTreeNode.ReferenceProduct.UserRefProperties.CreateString(Value, "Input")
    End If

Next

【问题讨论】:

  • 为什么只在没有子产品的情况下设置值?您不想在确定对象类型的 IF 分支中设置属性设置代码吗?例如,您只想为零件、产品或组件设置属性?
  • 啊,很好。我非常专注于尝试使输入起作用,以至于我没有意识到这一点。谢谢!
  • 我重读了你写的东西。无论父母或孩子拥有什么,我都希望只能分配给 1 个产品。

标签: vba catia


【解决方案1】:

看起来 CreateString 返回一个属性对象。尝试像这样使用它:

' Use this line where you want to make a new property
SetUserProperty oCurrentTreeNode.ReferenceProduct, "Input", "Yippee!!!!!"


Public Sub SetUserProperty(ByVal myProduct As Product, ByVal code as String, ByVal newValue as String)
    Dim myUserProperties As Object 'As Parameters
    Dim myUserProperty As StrParam

    Set myUserProperties = myProduct.UserRefProperties
    Set myUserProperty = myUserProperties.CreateString(code, "")
    myUserProperty.ValuateFromString newValue
End Sub 

【讨论】:

  • 我不得不说,我对潜艇如何使用你刚刚给我的东西一无所知。我了解发生了什么,但没有意识到可以以这种方式使用它们。
  • 哦,这就是我所说的将您的问题分解为多个部分的意思。这是过程编程。您的 CATMain 应该读起来像一个步骤列表,每个步骤都是一个子或函数。当一切都被分解成这样的部分时,应该没有重复的代码。
  • 我清理了我们所有的代码,并使用您提供帮助的代码以我的方式重写它。这个程序已经有 10 多年的历史了,编写它的人有很多......编码中的冗余。我不完全理解您如何调出代码,例如用于创建新属性的 SetUserProperty 行。我假设设置子后()中的代码是参数声明,然后输入名称和属性以使其运行?
  • 是否可以修改 SetUserPropery CATIA.ActiveDocument.product 以针对根级别?当前代码只会在子级别上创建字符串和修改。另外,如何将代码放入 cmets 中?我基于 mini-markdown 格式尝试的一切都不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-22
  • 2012-08-27
  • 1970-01-01
  • 2020-01-21
  • 2011-05-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多