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!

Point on face - accessing face from point

Status
Not open for further replies.

NirVidP

Industrial
May 10, 2010
45
IN
The key questions:
1. How can we access, using API (no interactive User selection), a specific face of a Pad feature?
2. How can we get the Object (to which the reference was created) from the Reference? This is reverse of CreateReferenceFromObject.

What I am trying to do:
I am trying to programmatically select a face of a Pad and calculate its area using measurable. (I have been able to successfully do it manually, and using Interactive selection in code. But I want to avoid interactive User selection).

What I have done so far:
For identifying a specific face, I have added a point (HybridShapePointOnSurface) on it. Using code I am able to access this point. But I am unable to get the face on which that point resides.

The point has a Surface property that returns a Reference object which causes error in Area property of Measurable object. I believe, the Measurable object needs the native object and not a Reference object.

So the question - how can we get Object from Reference.

Alternatives:
There can be other alternatives like assigning a different color to the face.

But it would be nice to know if this alternative is also available.

NB: I tried to record the manual process, but nothing was recorded.

Suggestions are welcome.






 
Replies continue below

Recommended for you

Hi NirVidP.

>2. How can we get the Object (to which the reference was created) from the Reference? This is reverse of CreateReferenceFromObject.
There is a GSMGetObjectFromReference function.
Link
 
Thank you, kantoku.

I did browse the Help, but overlooked prefix GSM.
 
I tried GSMGetObjectFromReference function, but it fails. Not sure if it has anything to do with settings or Workbenches.
BTW, does anybody know about 'an aternative way to get referenced object via Selection. It is for sure slower nonetheless reliable' that was mentioned in the above specified link.
 
Since OffsetSurface was created from HybridShapePointOnSurface.SurFece, I tried to create a macro to acquire the area.
(I think there are two types of reference object)

Code:
'CATPart only
Option Explicit

Sub CATMain()

    'select Geometrical Set
    Dim msg As String
    msg = "Please select a Geometrical Set(PointOnSurface) / ESC-Cancel"
    
    Dim hBdy As HybridBody
    Set hBdy = SelectItem(msg, Array("HybridBody"))
    If hBdy Is Nothing Then Exit Sub
    
    'PointOnSurface List
    Dim pnts As Collection
    Set pnts = GetPointOnSurfaceList(hBdy)
    If pnts Is Nothing Then
        MsgBox "PointOnSurface not found!", vbOKOnly + vbExclamation
        Exit Sub
    End If
    
    'area
    msg = GetArea(pnts)
    MsgBox msg

End Sub

Private Function GetArea( _
    ByVal lst As Collection) _
    As String
    
    Dim doc As PartDocument
    Set doc = CATIA.ActiveDocument
    
    Dim pt As part
    Set pt = doc.part
    
    Dim fact As HybridShapeFactory
    Set fact = doc.part.HybridShapeFactory
    
    Dim spa As SPAWorkbench
    Set spa = doc.GetWorkbench("SPAWorkbench")
    
    Dim mes As Measurable
    
    Dim infos As Collection
    Set infos = New Collection
    
    Dim area As Double
    
    Dim ref As Reference
    
    Dim dmy As HybridShapeOffset
    
    Dim pnt As HybridShapePointOnSurface
    
    For Each pnt In lst
        Set dmy = GetZeroOffset(pnt.Surface, pt) 'create TempOffsetSurface
        Set ref = pt.CreateReferenceFromObject(dmy)
        
        Set mes = spa.GetMeasurable(ref)
        infos.Add pnt.Name & " : " & mes.area
        area = area + mes.area
        
        fact.DeleteObjectForDatum ref 'delete TempOffsetSurface
    Next

    GetArea = Join(lst2ary(infos), vbCrLf) & vbCrLf _
        & "total area : " & area
    
End Function

Private Function GetZeroOffset( _
    ByVal ref As Reference, _
    ByVal pt As part) _
    As HybridShapeOffset

    Dim fact As HybridShapeFactory
    Set fact = pt.HybridShapeFactory

    Dim surf As HybridShapeOffset
    Set surf = fact.AddNewOffset(ref, 0#, False, 0.01)

    pt.UpdateObject surf
    
    Set GetZeroOffset = surf
    
End Function

Private Function lst2ary( _
    lst As Collection) _
    As Variant
    
    Dim ary() As Variant
    ReDim ary(lst.Count - 1)
    
    Dim i As Long
    For i = 0 To lst.Count - 1
        ary(i) = lst.Item(i + 1)
    Next
    
    lst2ary = ary
    
End Function

Private Function GetPointOnSurfaceList( _
    ByVal hBdy As HybridBody) _
    As Collection
    
    Dim lst As Collection
    Set lst = New Collection
    Dim shp As HybridShape
    
    For Each shp In hBdy.HybridShapes
        If IsPointOnSurface(shp) Then
            lst.Add shp
        End If
        
    Next
    
    If lst.Count < 1 Then
        Set GetPointOnSurfaceList = Nothing
    Else
        Set GetPointOnSurfaceList = lst
    End If
    
End Function

'Because it can not be judged by the typename function
Private Function IsPointOnSurface( _
    ByVal shp As HybridShape) _
    As Boolean
    
    Dim p As HybridShapePointOnSurface
    
    On Error Resume Next
    
        Set p = shp
    
    On Error GoTo 0
    
    IsPointOnSurface = IIf(p Is Nothing, False, True)
    
End Function

Private Function SelectItem( _
    ByVal msg As String, _
    ByVal filter As Variant) _
    As AnyObject
        
    Dim Sel As Variant
    Set Sel = CATIA.ActiveDocument.selection
    
    Sel.Clear
    Select Case Sel.SelectElement2(filter, msg, False)
        Case "Cancel", "Undo", "Redo"
            Exit Function
    End Select
    Set SelectItem = Sel.Item(1).Value
    Sel.Clear
    
End Function
 
Thank you kantoku, this worked.

1. I added a statement to create offset plane to the surface reference and it worked.
2. I was still puzzled as to why GSMGetObjectFromReference function did not work. After little exploration I found the answer:
2.1 The GSMGetObjectFromReference function is meant to return an object from a direct reference to that object and can’t resolve an object from a BREP reference. (Ref.: 2.2 Help: 'Reference (Object)represents an object pointing to another object. This other object can be either a wireframe GeometricElement object such as a plane or a line (Direct Object reference), or a boundary representation object such as a face, a vertex or an edge(BRep reference).'
So, the references can be of two types - Direct object reference or Brep refernce.
And since the Surface reference is a Brep reference (and NOT direct Object reference), it can't used in GSMGetObjectFromReference function to get the Object.

So, the issue that may need answer sometime in future is - How to get object from its BRep reference?

Ooooph!! It was a pretty good learning experience!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top