0

I want to change an already working macro that lists the part number and name of the protections in a listbox.Now I also try to list length of each protection that I can find in my cable. I looked it up and people told me you cannot access electrical properties of a protection where I can get the lenght, therefore I need to make a workarround. First I need to create a parameter for each protection. Then create a formula (length on curve and 2 points)to feed the parameter. Then list the value in a listbox.

selection1.Search "CATElectricalSearch.Protection,all"

Dim i As Integer
Dim oInstProd As Product
Dim strpartno As String

For i = 1 To selection1.Count
Set oInstProd = selection1.Item(i).LeafProduct
strpartno = oInstProd.ReferenceProduct.PartNumber
    
    
'test
selection1.Item(1).Document.Activate
     
Dim part1 As Part
Set part1 = selection1.Item(1).Document.Part

Dim parameters1 As Parameters
Set parameters1 = part1.Parameters

On Error Resume Next
Err.Clear 'Clear any previous error messages
Set ParamV = parameters1.Item("Lungime")
If Err.Number = 0 Then
parameters1.Remove "Lungime"
Else
'TODO Stuff if parameter does not Exist
'create a new length type parameter, set its value to 0 for now
Dim length1 As Dimension
Set length1 = parameters1.CreateDimension("", "LENGTH", 0)
'if you want to rename the parameter
length1.Rename "Lungime"
            
Dim hybridBodies1 As HybridBodies
Set hybridBodies1 = part1.HybridBodies
Dim hybridBody1 As HybridBody
Set hybridBody1 = hybridBodies1.Item("External References")
Set hybridShapes1 = hybridBody1.HybridShapes
Dim reference1 As Reference
Set reference1 = hybridShapes1.Item(1) 'get curve

Dim reference2 As Reference
Set reference2 = hybridShapes1.Item(2) 'get first point

Dim reference3 As Reference
Set reference3 = hybridShapes1.Item(3) 'get second point
    
'create a new formula to link to the parameter
Dim relations1 As Relations
Set relations1 = part1.Relations

'make sure points are labeled MyEndPt1 and MyEndPt2 respectively
Dim formula1 As Formula
Set formula1 = relations1.CreateFormula("Formula.47", "", length1, "length( `External References\" & reference1.Name & "` ,`External References\" & reference2.Name & "` , `External References\" & reference3.Name & "` ) ")

'MsgBox length1.ValueAsString

End If

'end test
    
With UserFormTapeCheck.ListBox1
.AddItem
.List(i - 1, 0) = selection1.Item(i).LeafProduct.Name
.List(i - 1, 1) = strpartno
'test
.List(i - 1, 2) = length1.ValueAsString
'end test
End With
    
'test
relations1.Remove "Formula.47"
parameters1.Remove "Lungime"
'end test
Next

selection1.Clear

The macro works perfect if I only list the part number and leafproduct.name. I need to get inside the part. Think I am still inside the product and therefore macro can't get the parameters nor the hybrid bodies.

Mihai
  • 1
  • 1

2 Answers2

0

I manage to make to make it work. Found the way to get the part from a electrical component Set oPart = oInstProd.ReferenceProduct.Parent.Part

Dim productDocument1 As ProductDocument
Set productDocument1 = CATIA.ActiveDocument

Dim selection1 As Selection
Set selection1 = productDocument1.Selection

selection1.Search "CATElectricalSearch.Protection,all"

Dim i As Integer
Dim oInstProd As Product
Dim strpartno As String
Dim oPart As Part

For i = 1 To selection1.Count
    Set oInstProd = selection1.Item(i).LeafProduct
    strpartno = oInstProd.ReferenceProduct.PartNumber
    
    'test
    Set oPart = oInstProd.ReferenceProduct.Parent.Part

        Dim parameters1 As Parameters
        Set parameters1 = oPart.Parameters

        On Error Resume Next
        Err.Clear 'Clear any previous error messages
        Set ParamV = parameters1.Item("Lungime")
        If Err.Number = 0 Then
            parameters1.Remove "Lungime"
        Else
            'TODO Stuff if parameter does not Exist
            'create a new length type parameter, set its value to 0 for now
            Dim length1 As Dimension
            Set length1 = parameters1.CreateDimension("", "LENGTH", 0)
            'if you want to rename the parameter
            length1.Rename "Lungime"
            
            Dim hybridBodies1 As HybridBodies
            Set hybridBodies1 = oPart.HybridBodies
            Dim hybridBody1 As HybridBody
            Set hybridBody1 = hybridBodies1.Item("External References")
            Set hybridShapes1 = hybridBody1.HybridShapes
            Dim reference1 As Reference
            Set reference1 = hybridShapes1.Item(1) 'get curve

            Dim reference2 As Reference
            Set reference2 = hybridShapes1.Item(2) 'get first point

            Dim reference3 As Reference
            Set reference3 = hybridShapes1.Item(3) 'get second point
    
            'create a new formula to link to the parameter
            Dim relations1 As Relations
            Set relations1 = oPart.Relations

            'make sure points are labeled MyEndPt1 and MyEndPt2 respectively
            Dim formula1 As Formula
            Set formula1 = relations1.CreateFormula("Formula.47", "", length1, "length( `External References\" & reference1.Name & "` ,`External References\" & reference2.Name & "` , `External References\" & reference3.Name & "` ) ")

        End If

    'end test
    
    With UserFormTapeCheck.ListBox1
        .AddItem
        .List(i - 1, 0) = selection1.Item(i).LeafProduct.Name
        .List(i - 1, 1) = strpartno
        'test
        .List(i - 1, 2) = Round(Abs(length1.Value), 2)
        'end test
    End With
    
    'test
    relations1.Remove "Formula.47"
    parameters1.Remove "Lungime"
    'end test
Next

selection1.Clear
Mihai
  • 1
  • 1
0

Found another way to do it. In the relations there are some values saved , like: length of the electrical route and the distances from the each end of the route. With this 3 values you can calculate the length of the protection.

selection1.Search "CATElectricalSearch.Protection,all"

Dim i As Integer
Dim oInstProd As Product
Dim strpartno As String
Dim oPart As Part
Dim routelength As Double
Dim parameters1 As Parameters
Dim ParamPoint4 As Dimension
Dim ParamPoint3 As Dimension
Dim ParamTotal As Dimension



For i = 1 To selection1.Count
    Set oInstProd = selection1.Item(i).LeafProduct
    strpartno = oInstProd.ReferenceProduct.PartNumber
    
    'test
    Set oPart = oInstProd.ReferenceProduct.Parent.Part
 
    Set parameters1 = oPart.Parameters
    Set ParamPoint4 = parameters1.Item("ElecRouteBody.1\Point.4\Length.2")
    Set ParamPoint3 = parameters1.Item("ElecRouteBody.1\Point.3\Length.1")
    Set ParamTotal = parameters1.Item(oPart.Name & "\Elec_Length")       
    
    routelength = ParamTotal.Value - ParamPoint4.Value - ParamPoint3.Value
    
    With UserFormTapeCheck.ListBox1
        .AddItem
        .List(i - 1, 0) = selection1.Item(i).LeafProduct.Name
        .List(i - 1, 1) = strpartno
        'test
        .List(i - 1, 2) = Round(routelength, 2)
        'end test
    End With

I also encountered some small problems where the Elec_Length had a wrong value and I had to deleted & place the protection again for it to be updated and the macro to draw the correct values. For now I'm very pleased with how it turned out. Enjoy

Mihai
  • 1
  • 1