cubygt
Aerospace
- Feb 10, 2017
- 10
I have the following VBA code that will loop through an assembly and assign user reference properties to each product and part (different properties depending on if it's a part or a product). A couple of issues are the "Measure Inertia" command happens to each part instance, I only need to do it once for each part number, and if the "Measure Inertia" is not named "InertiaVolume.1" the formula is not recognized. I need to be able to use the "Measure Inertia" no matter what the name is and only instantiate it once for each part number. Any help is very much appreciated...
Code:
Sub CATMain()
GetNextNode CATIA.ActiveDocument.Product
End Sub
Sub GetNextNode(oCurrentProduct)
Dim oCurrentTreeNode As Product
Dim oCurrentTreeNodePart As Part
Dim StrNomenclature, StrDesignation, StrWindows As String
Dim i As Integer
Dim mBody As Body
'Loop through every tree node for the current product
For i = 1 To oCurrentProduct.Products.Count
Set oCurrentTreeNode = oCurrentProduct.Products.Item(i)
Set oParameters = oCurrentTreeNode.ReferenceProduct.UserRefProperties
StrWindows = oCurrentTreeNode.ReferenceProduct.Parent.FullName
On Error Resume Next
'Determine if the current node is a part, product or component
If Right(StrWindows, 4) = "Part" Then
Set oCurrentTreeNodePart = oCurrentTreeNode.ReferenceProduct.Parent.Part
Set mBody = oCurrentTreeNodePart.MainBody
Set oPartProdParam = oCurrentTreeNodePart.Parameters
Set oPartRelations = oCurrentTreeNodePart.Relations
While oParameters.Count > 0
oParameters.Remove (1)
Wend
Set oPartSel = CATIA.ActiveDocument.Selection
oPartSel.Clear
oPartSel.Add oCurrentTreeNodePart
AppActivate ("CATIA V5")
SendKeys "c:" & "FrmActivate" & Chr(13), True
CATIA.StartCommand ("Measure Inertia")
CATIA.RefreshDisplay = True
SendKeys "{Tab 6}", True
SendKeys "{Enter}", True
CATIA.RefreshDisplay = True
CATIA.StartCommand "Collapse All"
Set Xdim = oPartProdParam.GetItem("BBLx")
Set Ydim = oPartProdParam.GetItem("BBLy")
Set Zdim = oPartProdParam.GetItem("BBLz")
oParameters.CreateString "DRAWING/PART NO.", oCurrentTreeNode.PartNumber & "_SHT_1"
oParameters.CreateString "MATERIAL/SPECIFICATION", mBody.Name
If Xdim.Value < Ydim.Value And Xdim.Value < Zdim.Value Then
Set paramDimension1 = oParameters.CreateDimension("SURFACE AREA", "AREA", 0#)
Set Xdim = oPartRelations.CreateFormula("AREA", "", paramDimension1, "InertiaVolume.1\BBLy * InertiaVolume.1\BBLz")
ElseIf Ydim.Value < Xdim.Value And Ydim.Value < Zdim.Value Then
Set paramDimension1 = oParameters.CreateDimension("SURFACE AREA", "AREA", 0#)
Set Ydim = oPartRelations.CreateFormula("AREA", "", paramDimension1, "InertiaVolume.1\BBLx * InertiaVolume.1\BBLz")
ElseIf Zdim.Value < Xdim.Value And Zdim.Value < Ydim.Value Then
Set paramDimension1 = oParameters.CreateDimension("SURFACE AREA", "AREA", 0#)
Set Zdim = oPartRelations.CreateFormula("AREA", "", paramDimension1, "InertiaVolume.1\BBLx * InertiaVolume.1\BBLy")
End If
oCurrentTreeNodePart.Update
ElseIf Right(StrWindows, 7) = "Product" Then
While oParameters.Count > 0
oParameters.Remove (1)
Wend
oParameters.CreateString "DRAWING/PART NO.", oCurrentTreeNode.PartNumber & "_SHT_1"
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
Next
End Sub
Thanks,
Culbygt
Code:
Sub CATMain()
GetNextNode CATIA.ActiveDocument.Product
End Sub
Sub GetNextNode(oCurrentProduct)
Dim oCurrentTreeNode As Product
Dim oCurrentTreeNodePart As Part
Dim StrNomenclature, StrDesignation, StrWindows As String
Dim i As Integer
Dim mBody As Body
'Loop through every tree node for the current product
For i = 1 To oCurrentProduct.Products.Count
Set oCurrentTreeNode = oCurrentProduct.Products.Item(i)
Set oParameters = oCurrentTreeNode.ReferenceProduct.UserRefProperties
StrWindows = oCurrentTreeNode.ReferenceProduct.Parent.FullName
On Error Resume Next
'Determine if the current node is a part, product or component
If Right(StrWindows, 4) = "Part" Then
Set oCurrentTreeNodePart = oCurrentTreeNode.ReferenceProduct.Parent.Part
Set mBody = oCurrentTreeNodePart.MainBody
Set oPartProdParam = oCurrentTreeNodePart.Parameters
Set oPartRelations = oCurrentTreeNodePart.Relations
While oParameters.Count > 0
oParameters.Remove (1)
Wend
Set oPartSel = CATIA.ActiveDocument.Selection
oPartSel.Clear
oPartSel.Add oCurrentTreeNodePart
AppActivate ("CATIA V5")
SendKeys "c:" & "FrmActivate" & Chr(13), True
CATIA.StartCommand ("Measure Inertia")
CATIA.RefreshDisplay = True
SendKeys "{Tab 6}", True
SendKeys "{Enter}", True
CATIA.RefreshDisplay = True
CATIA.StartCommand "Collapse All"
Set Xdim = oPartProdParam.GetItem("BBLx")
Set Ydim = oPartProdParam.GetItem("BBLy")
Set Zdim = oPartProdParam.GetItem("BBLz")
oParameters.CreateString "DRAWING/PART NO.", oCurrentTreeNode.PartNumber & "_SHT_1"
oParameters.CreateString "MATERIAL/SPECIFICATION", mBody.Name
If Xdim.Value < Ydim.Value And Xdim.Value < Zdim.Value Then
Set paramDimension1 = oParameters.CreateDimension("SURFACE AREA", "AREA", 0#)
Set Xdim = oPartRelations.CreateFormula("AREA", "", paramDimension1, "InertiaVolume.1\BBLy * InertiaVolume.1\BBLz")
ElseIf Ydim.Value < Xdim.Value And Ydim.Value < Zdim.Value Then
Set paramDimension1 = oParameters.CreateDimension("SURFACE AREA", "AREA", 0#)
Set Ydim = oPartRelations.CreateFormula("AREA", "", paramDimension1, "InertiaVolume.1\BBLx * InertiaVolume.1\BBLz")
ElseIf Zdim.Value < Xdim.Value And Zdim.Value < Ydim.Value Then
Set paramDimension1 = oParameters.CreateDimension("SURFACE AREA", "AREA", 0#)
Set Zdim = oPartRelations.CreateFormula("AREA", "", paramDimension1, "InertiaVolume.1\BBLx * InertiaVolume.1\BBLy")
End If
oCurrentTreeNodePart.Update
ElseIf Right(StrWindows, 7) = "Product" Then
While oParameters.Count > 0
oParameters.Remove (1)
Wend
oParameters.CreateString "DRAWING/PART NO.", oCurrentTreeNode.PartNumber & "_SHT_1"
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
Next
End Sub
Thanks,
Culbygt