What I'm trying to do is ask the user to select two planar surfaces and then create a plane that is halfway between them. I can get the correct distance and create the plane. However, it doesn't detect/forgets or is incompatible with the reference that I got from the first planar surface and is unable to create the plane (shown below in highlighted section). It seems like just one small bit needs to be fixed so any help would be appreciated.
On a side note I noticed that the distance computed doesn't always match if I use the "Measure Between" on the toolbar. I think it's because the distance computed is being measured from the surfaces while the the toolbar icon (selection modes: Any geometry, infinite) measures from planes. Thus, I would probably need to measure using planes on those surfaces but I'm not sure how to do that.
CODE:
Sub test()
Dim MyDoc As Document
Set MyDoc = CATIA.ActiveDocument
Dim MainProduct As Product
Set MainProduct = MyDoc.Product
Dim product1 As Product
Dim product2 As Product
Dim uSel 'as selection
Set uSel = CATIA.ActiveDocument.Selection
Dim InputObject(0)
InputObject(0) = "PlanarFace" '"Plane" if planes
uSel.Clear
oStatus = uSel.SelectElement2(InputObject, "Select first planar face", False)
Set product1 = uSel.Item(1).LeafProduct
Dim ref1 As Reference
Set ref1 = uSel.Item(1).Reference
uSel.Clear
oStatus = uSel.SelectElement2(InputObject, "Select second planar face", False)
Set product2 = uSel.Item(1).LeafProduct
Dim FirstGroup As Group
Dim cGroups As Groups
Set cGroups = CATIA.ActiveDocument.Product.GetTechnologicalObject("Groups")
Dim oGroup1 As Group
Dim oGroup2 As Group
Set oGroup1 = cGroups.Add
Set oGroup2 = cGroups.Add
Dim cDistances As Distances
Set cDistances = CATIA.ActiveDocument.Product.GetTechnologicalObject("Distances")
Dim NewDistance As Distance
Set NewDistance = cDistances.Add
oGroup1.AddExplicit product1
oGroup2.AddExplicit product2
NewDistance.FirstGroup = oGroup1
NewDistance.SecondGroup = oGroup2
NewDistance.ComputationType = catDistanceComputationTypeBetweenTwo
NewDistance.MeasureType = catDistanceMeasureTypeMinimum
NewDistance.Compute
MsgBox NewDistance.Value
Dim findThis As String
findThis = "MASTER"
Dim gPName As String
For i = 1 To MainProduct.Products.Count
If InStr(MainProduct.Products.Item(i).Name, findThis) > 0 Then
gPName = MainProduct.Products.Item(i).PartNumber
End If
Next
gPName = Left(gPName, Len(gPName) - 2) & ".CATPart"
Dim documents1 As documents
Set documents1 = CATIA.documents
Dim partDocument1 As PartDocument
Set partDocument1 = documents1.Item(gPName)
Dim part1 As Part
Set part1 = partDocument1.Part
Dim hybridBodies1 As HybridBodies
Set hybridBodies1 = part1.HybridBodies
Dim hybridShapeFactory1 As HybridShapeFactory
Set hybridShapeFactory1 = part1.HybridShapeFactory
Dim productDocument1 As productDocument
Set productDocument1 = CATIA.ActiveDocument
Dim selection1 As Selection
Set selection1 = productDocument1.Selection
selection1.Search "CATPrtSearch.OpenBodyFeature.Name=COG,all"
Dim hybridBody1 As HybridBody
'Set hybridBody1 = hybridBodies1.Add()
If selection1.Count > 0 Then
Set hybridBody1 = hybridBodies1.Item("COG")
Else
Set hybridBody1 = hybridBodies1.Add()
hybridBody1.Name = "COG"
End If
Dim hybridShapePlaneOffset2 As HybridShapePlaneOffset
Set hybridShapePlaneOffset2 = hybridShapeFactory1.AddNewPlaneOffset(ref1, NewDistance.Value / 2, True)
[highlight #FCE94F]hybridBody1.AppendHybridShape hybridShapePlaneOffset2[/highlight] '<== at this point it creates a plane in the geometrical set which has the correct offset but the "Reference" field shows "No selection"
part1.InWorkObject = hybridShapePlaneOffset2
part1.Update
End Sub
On a side note I noticed that the distance computed doesn't always match if I use the "Measure Between" on the toolbar. I think it's because the distance computed is being measured from the surfaces while the the toolbar icon (selection modes: Any geometry, infinite) measures from planes. Thus, I would probably need to measure using planes on those surfaces but I'm not sure how to do that.
CODE:
Sub test()
Dim MyDoc As Document
Set MyDoc = CATIA.ActiveDocument
Dim MainProduct As Product
Set MainProduct = MyDoc.Product
Dim product1 As Product
Dim product2 As Product
Dim uSel 'as selection
Set uSel = CATIA.ActiveDocument.Selection
Dim InputObject(0)
InputObject(0) = "PlanarFace" '"Plane" if planes
uSel.Clear
oStatus = uSel.SelectElement2(InputObject, "Select first planar face", False)
Set product1 = uSel.Item(1).LeafProduct
Dim ref1 As Reference
Set ref1 = uSel.Item(1).Reference
uSel.Clear
oStatus = uSel.SelectElement2(InputObject, "Select second planar face", False)
Set product2 = uSel.Item(1).LeafProduct
Dim FirstGroup As Group
Dim cGroups As Groups
Set cGroups = CATIA.ActiveDocument.Product.GetTechnologicalObject("Groups")
Dim oGroup1 As Group
Dim oGroup2 As Group
Set oGroup1 = cGroups.Add
Set oGroup2 = cGroups.Add
Dim cDistances As Distances
Set cDistances = CATIA.ActiveDocument.Product.GetTechnologicalObject("Distances")
Dim NewDistance As Distance
Set NewDistance = cDistances.Add
oGroup1.AddExplicit product1
oGroup2.AddExplicit product2
NewDistance.FirstGroup = oGroup1
NewDistance.SecondGroup = oGroup2
NewDistance.ComputationType = catDistanceComputationTypeBetweenTwo
NewDistance.MeasureType = catDistanceMeasureTypeMinimum
NewDistance.Compute
MsgBox NewDistance.Value
Dim findThis As String
findThis = "MASTER"
Dim gPName As String
For i = 1 To MainProduct.Products.Count
If InStr(MainProduct.Products.Item(i).Name, findThis) > 0 Then
gPName = MainProduct.Products.Item(i).PartNumber
End If
Next
gPName = Left(gPName, Len(gPName) - 2) & ".CATPart"
Dim documents1 As documents
Set documents1 = CATIA.documents
Dim partDocument1 As PartDocument
Set partDocument1 = documents1.Item(gPName)
Dim part1 As Part
Set part1 = partDocument1.Part
Dim hybridBodies1 As HybridBodies
Set hybridBodies1 = part1.HybridBodies
Dim hybridShapeFactory1 As HybridShapeFactory
Set hybridShapeFactory1 = part1.HybridShapeFactory
Dim productDocument1 As productDocument
Set productDocument1 = CATIA.ActiveDocument
Dim selection1 As Selection
Set selection1 = productDocument1.Selection
selection1.Search "CATPrtSearch.OpenBodyFeature.Name=COG,all"
Dim hybridBody1 As HybridBody
'Set hybridBody1 = hybridBodies1.Add()
If selection1.Count > 0 Then
Set hybridBody1 = hybridBodies1.Item("COG")
Else
Set hybridBody1 = hybridBodies1.Add()
hybridBody1.Name = "COG"
End If
Dim hybridShapePlaneOffset2 As HybridShapePlaneOffset
Set hybridShapePlaneOffset2 = hybridShapeFactory1.AddNewPlaneOffset(ref1, NewDistance.Value / 2, True)
[highlight #FCE94F]hybridBody1.AppendHybridShape hybridShapePlaneOffset2[/highlight] '<== at this point it creates a plane in the geometrical set which has the correct offset but the "Reference" field shows "No selection"
part1.InWorkObject = hybridShapePlaneOffset2
part1.Update
End Sub