Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't offset plane from user selected surface

Status
Not open for further replies.

das583

Aerospace
May 5, 2016
4
US
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
 
Replies continue below

Recommended for you

Hello,

Disclaimer: I didn' test the code above, just read it and some observations:
1. ref1 does not exist in the part you want to have the plane created. You have two options:
A) either create a reference to it by its name using the full path to the selected item and its display name (this is complicated and prone to error)
or
B) simply create an Extract in the document where the selected face belongs and copy-paste it from there in your part.

2. I rarely use your method for measuring distance between entities. I use SPAWorkbench on the active document and then a function like this:

Code:
Set TheSPAWorkbench = CATIA.ActiveDocument.GetWorkbench("SPAWorkbench")
Set TheMeasurable = TheSPAWorkbench.GetMeasurable(object1)
MinimumDistance = TheMeasurable.GetMinimumDistance(object2)

Hope it helps,
Calin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top