delete face from part with the Inventor API

By Xiaodong Liang

Issue

Is there an example showing how to delete faces from a part using the Inventor API?

Solution

It is possible to delete the face from a part using the Inventor API. This is done by adding the faces to the DeleteFaceFeatures. A FaceCollection object is created and the faces to be deleted are added to the collection. Finally the collection is added to the DeleFaceFeature object.

VBA code:

Sub deleteSideFaces_VBA()

   ‘remove all the side faces
   Dim aDoc As Document
   Dim pFets As PartFeatures
   Dim cdef As ComponentDefinition
   Dim oExtrude As ExtrudeFeatures
   Dim oAllface As Face
   Set aDoc = _
        ThisApplication.ActiveDocument
   Set cdef = _
        aDoc.ComponentDefinition
   Set pFets = _
        cdef.Features
   Set oExtrude = _
        pFets.ExtrudeFeatures
   ‘FaceCollection to hold the faces to delete
   Dim oFacecoll As FaceCollection
   Set oFacecoll = _
    ThisApplication.TransientObjects.CreateFaceCollection
   Dim oFaces As Faces
   Set oFaces = _
    oExtrude.Item(1).SideFaces
   ‘Add all the sideface of the part in
   ‘ to the face collection object
   For Each oAllface In oFaces
        oFacecoll.Add oAllface
   Next
   ‘delete the faces by adding into the deleteFaceFeatures
   If oFacecoll.Count > 0 Then
        pFets.DeleteFaceFeatures.Add oFacecoll, False
   End If
End Sub

 

VB.NET code:

Sub deleteSideFaces()         ' assume we have had Inventor application    '  _InvApplication             'remove all the side faces           Dim aDoc As Document =             _InvApplication.ActiveDocument                  Dim cdef As ComponentDefinition =                 aDoc.ComponentDefinition             Dim pFets As PartFeatures =             cdef.Features             Dim oExtrude As ExtrudeFeatures =                 pFets.ExtrudeFeatures             'FaceCollection to hold the faces to delete          Dim oFacecoll As FaceCollection        oFacecoll =             _InvApplication.TransientObjects.CreateFaceCollection             Dim oFaces As Faces        oFaces = oExtrude.Item(1).SideFaces                 Dim oAllface As Face         'Add all the sideface of the part in         ‘to the face collection object           For Each oAllface In oFaces            oFacecoll.Add(oAllface)        Next             'delete the faces by adding into the deleteFaceFeatures           If oFacecoll.Count > 0 Then            pFets.DeleteFaceFeatures.Add(oFacecoll, False)        End If    End Sub

Comments

3 responses to “delete face from part with the Inventor API”

  1. Hello Xiaodong Liang,
    Thank you for the sample code for DeleteFaceFeatures.
    Well, we are working on a requirement in which a part file contains multiple shells (multiple solids). We want to get the mass properies of each shell independently.
    For example in a part, we have 4 shells, to get mass properties of 1st shell, we want to remove other 3 shells. This can be achieved using DeleteFaceFeatures.
    We have written the functionality as follows:
    1. Collect the information for each shell – which other shell’s faces should be deleted
    2. The above information is stored in a collection
    3. Then for each shell, other 3 shells are deleted and mass property is obtained.
    But many times we are getting error message which says:
    Run-time Error ‘-2147467259(80004005)’:
    Method ‘Add’ of object ‘DeleteFaceFeatures’ failed
    The issue is definitely reproduced if we open the part file and do some operation like rotate and then run the program, otherwise it is intermittent issue.
    The error message comes while creating the DeleteFaceFeature for second shell
    Actually our program is in C++ and we always get error at following line:
    Result = pDeleteFaceFeatures->Add(pFaceCollection, Heal, &pDeleteFace);
    Result is always E_FAIL. The logic is exactly same as written in VBA.
    Please advise what could be the reason for the same.
    We could not attach the sample part file and it can be created by drawing 4 rectangles in a 2D sketch and extruding the same.
    Sub DeleteFaceFeatureFunction()
    Dim oDoc As PartDocument
    Set oDoc = ThisApplication.ActiveDocument
    ‘oDoc.Activate
    Dim oFace As Face
    Dim TotalFaceShells As Integer
    ‘Get total shell count
    TotalFaceShells = oDoc.ComponentDefinition.SurfaceBodies.Item(1).FaceShells.Count
    ‘Get all the shells
    Dim oShells As FaceShells
    Set oShells = oDoc.ComponentDefinition.SurfaceBodies.Item(1).FaceShells
    Dim shellCnt As Integer
    shellCnt = 1
    Dim iInnerShellCnt As Integer
    Dim oShellFaceColl As New Collection
    ‘For each shell get which all other shells are to be removed
    While (shellCnt <= TotalFaceShells)
    Dim oFaceColl As FaceCollection
    Set oFaceColl = ThisApplication.TransientObjects.CreateFaceCollection
    iInnerShellCnt = 1
    While (iInnerShellCnt <= TotalFaceShells)
    If (shellCnt = iInnerShellCnt) Then
    ‘If both shells are same, skip
    Else
    Dim oFaceShell As FaceShell
    Set oFaceShell = oShells.Item(iInnerShellCnt)
    For Each oFace In oFaceShell.Faces
    Call oFaceColl.Add(oFace)
    Next
    End If
    iInnerShellCnt = iInnerShellCnt + 1
    Wend
    Call oShellFaceColl.Add(oFaceColl, Str(shellCnt))
    shellCnt = shellCnt + 1
    Wend
    shellCnt = 1
    ‘oDoc.Activate
    ‘For each shell delete other shells and get the area
    While (shellCnt <= TotalFaceShells)
    Set oFaceColl = oShellFaceColl.Item(shellCnt)
    Dim oDeleteFace As DeleteFaceFeature
    Set oDeleteFace = oDoc.ComponentDefinition.Features.DeleteFaceFeatures.Add(oFaceColl, False)
    ‘Get area
    Dim area As Double
    area = oDoc.ComponentDefinition.MassProperties.area
    Debug.Print area
    ‘Delete the created feature
    oDeleteFace.Delete True, True, True
    shellCnt = shellCnt + 1
    Wend
    End Sub
    Thanks & Regards,
    Mangesh

  2. badrinth chinni Avatar
    badrinth chinni

    hie,
    You got a solution for this ?
    I am trying to generate a ilogic using shell feature. But do can you help me to select the faces to be deleted in this shell features using ilogic ?
    I have named all my faces of the geometry.
    Thank you for your help.

  3. Best thing is to check on the Inventor Customization forum: https://forums.autodesk.com/t5/inventor-customization/bd-p/120

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading