oHoleFeature.SetAffectedBodies API VB.NET

oHoleFeature.SetAffectedBodies API VB.NET

florian_wenzel
Advocate Advocate
794 Views
9 Replies
Message 1 of 10

oHoleFeature.SetAffectedBodies API VB.NET

florian_wenzel
Advocate
Advocate

Hi,

 

inventor 2022

API with Visual Studio

 

i try to make a HoleFeature with selecting Surface Body.

I have a PartDocument with 2 Bodies.

I want to make the Holes in Surface Body item(2)

 

I have a Code, and it works with only with 1 SurfaceBody.

When i have 2 Bodies, than a got Error.

 

This is the Code for 1 Body:

 

Imports System.Runtime.InteropServices
Imports Inventor
Imports Microsoft.Win32


Module CommandFunctionButton_06

    Public Sub CommandFunctionfweButton_06()

        Dim oPartDoc As PartDocument = g_inventorApplication.ActiveDocument
        Dim oCompDef As PartComponentDefinition = oPartDoc.ComponentDefinition
        Dim oTO As TransientObjects = g_inventorApplication.TransientObjects
        Dim oTG As TransientGeometry = g_inventorApplication.TransientGeometry
        Dim oRefComponents As ReferenceComponents = oCompDef.ReferenceComponents


        Dim oFace As Face = CType(g_inventorApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceFilter, "Pick a Face"), Face)

        If oFace IsNot Nothing Then
            For Each oEdgeloop As EdgeLoop In oFace.EdgeLoops
                If Not oEdgeloop.IsOuterEdgeLoop Then
                    Dim oEdgeColl As EdgeCollection = oTO.CreateEdgeCollection
                    For Each oEdge In oEdgeloop.Edges
                        oEdgeColl.Add(oEdge)

                    Next

                    Dim oWorkpoint As WorkPoint = oCompDef.WorkPoints.AddAtCentroid(oEdgeColl)
                    Dim oWorkAxis As WorkAxis = oCompDef.WorkAxes.AddByNormalToSurface(oFace, oWorkpoint, False)
                    Dim oWorkPlane As WorkPlane = oCompDef.WorkPlanes.AddByNormalToCurve(oWorkAxis, oWorkpoint, False)

                    Dim oWorkSketch As PlanarSketch = oCompDef.Sketches.AddWithOrientation(oWorkPlane, oWorkAxis, True, True, oWorkpoint, True)

                    Dim oHoleCenters As ObjectCollection
                    oHoleCenters = oTO.CreateObjectCollection
                    oHoleCenters.Add(oWorkSketch.SketchPoints.Add(oTG.CreatePoint2d(0, 0)))

                    Dim oHoleFeature As HoleFeature
                    oHoleFeature = oCompDef.Features.HoleFeatures.AddDrilledByDistanceExtent(oHoleCenters, "10", "15", PartFeatureExtentDirectionEnum.kPositiveExtentDirection, True, )


                End If
            Next
        End If


    End Sub

End Module

 

 

 

This is the Code for 2 Bodies:

 

Imports System.Runtime.InteropServices
Imports Inventor
Imports Microsoft.Win32


Module CommandFunctionButton_05
    Public Sub CommandFunctionfweButton_05()

        Dim oPartDoc As PartDocument = g_inventorApplication.ActiveDocument
        Dim oCompDef As PartComponentDefinition = oPartDoc.ComponentDefinition
        Dim oTO As TransientObjects = g_inventorApplication.TransientObjects
        Dim oTG As TransientGeometry = g_inventorApplication.TransientGeometry
        Dim oRefComponents As ReferenceComponents = oCompDef.ReferenceComponents


        Dim oFace As Face = CType(g_inventorApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceFilter, "Pick a Face"), Face)

        If oFace IsNot Nothing Then
            For Each oEdgeloop As EdgeLoop In oFace.EdgeLoops
                If Not oEdgeloop.IsOuterEdgeLoop Then
                    Dim oEdgeColl As EdgeCollection = oTO.CreateEdgeCollection
                    For Each oEdge In oEdgeloop.Edges
                        oEdgeColl.Add(oEdge)

                    Next

                    Dim oWorkpoint As WorkPoint = oCompDef.WorkPoints.AddAtCentroid(oEdgeColl)
                    Dim oWorkAxis As WorkAxis = oCompDef.WorkAxes.AddByNormalToSurface(oFace, oWorkpoint, False)
                    Dim oWorkPlane As WorkPlane = oCompDef.WorkPlanes.AddByNormalToCurve(oWorkAxis, oWorkpoint, False)


                    Dim oWorkSketch As PlanarSketch = oCompDef.Sketches.AddWithOrientation(oWorkPlane, oWorkAxis, True, True, oWorkpoint, True)


                    Dim oSurfaceBody2 As SurfaceBody
                    oSurfaceBody2 = oCompDef.SurfaceBodies.Item(2)

                    Dim oObjectColl As ObjectCollection
                    oObjectColl = oTO.CreateObjectCollection

                    oObjectColl.Add(oSurfaceBody2)

                    Dim oHoleCenters As ObjectCollection
                    oHoleCenters = oTO.CreateObjectCollection
                    oHoleCenters.Add(oWorkSketch.SketchPoints.Add(oTG.CreatePoint2d(0, 0)))

                    Dim oHoleFeature As HoleFeature
                    oHoleFeature = oCompDef.Features.HoleFeatures.AddDrilledByDistanceExtent(oHoleCenters, "10", "15", PartFeatureExtentDirectionEnum.kPositiveExtentDirection, True, )

                    Call oHoleFeature.SetAffectedBodies(oObjectColl)



                End If
            Next
        End If


    End Sub

End Module

 

florianwenzelEJNZZ_0-1651452945958.png

it dont change the Bodies ?

How to use the oHoleFeature.SetAffectedBodies?

 

florianwenzelEJNZZ_0-1651453191547.png

Error: Wrong Parameter

 

 

Where is the Problem?

 

Thanks for any Sugestion

 

 

0 Likes
Accepted solutions (2)
795 Views
9 Replies
Replies (9)
Message 2 of 10

basautomationservices
Advocate
Advocate

Can you create the hole manually? You're not trying to cut air? It looks in the screenshot as if there is no solid where you are making a hole.

Contact me for custom app development info@basautomationservices.com. Follow below links to view my Inventor appstore apps.

Free apps: Smart Leader | Part Visibility Utility | Mate Origins

Paid apps: Frame Stiffener Tool | Constrain Plane Toggle | Property Editor Pro


0 Likes
Message 3 of 10

florian_wenzel
Advocate
Advocate

Hi,

this the Point, i want select a Face from Body(1) to Take the Center Position.

But the Hole should be in Body(2)

 

In the Previe, i only want to show, that even when i use the command:

Call oHoleFeature.SetAffectedBodies(oObjectColl)

in the DialogBox is still selected the Body(1)

 

 

 

0 Likes
Message 4 of 10

basautomationservices
Advocate
Advocate

Can you share your part file and what face you are using?  I tried it out with this code/part in VBA, and its fine. 

 

Sub CreateHole()

Dim prt As PartDocument
Set prt = ThisApplication.ActiveDocument

Dim sk As Sketch
Set sk = prt.ComponentDefinition.Sketches.Item(2)

Dim skPoint As SketchPoint
Set skPoint = sk.SketchPoints.Item(1)

Dim pCol As ObjectCollection
Set pCol = ThisApplication.TransientObjects.CreateObjectCollection()
Call pCol.Add(skPoint)

Dim hf As HoleFeature
Set hf = prt.ComponentDefinition.Features.HoleFeatures.AddDrilledByDistanceExtent( _
pCol, "10", "15", kPositiveExtentDirection, False)

Dim bodycol As ObjectCollection
Set bodycol = ThisApplication.TransientObjects.CreateObjectCollection()

'Call bodycol.Add(prt.ComponentDefinition.SurfaceBodies.Item(1))
Call bodycol.Add(prt.ComponentDefinition.SurfaceBodies.Item(2))

Call hf.SetAffectedBodies(bodycol)

End Sub

 

basautomationservices_0-1651473917696.png

 

When changing the second solid so the hole cannot cut it, I get a broken feature, but no wrong parameter error.

 

basautomationservices_1-1651474176710.png

 

 

Contact me for custom app development info@basautomationservices.com. Follow below links to view my Inventor appstore apps.

Free apps: Smart Leader | Part Visibility Utility | Mate Origins

Paid apps: Frame Stiffener Tool | Constrain Plane Toggle | Property Editor Pro


Message 5 of 10

Michael.Navara
Advisor
Advisor

Here is fixed module code with comments, what was changed. You need only to change the order of creating bodies collection and hole creation.

 

Module CommandFunctionButton_05
    Public g_inventorApplication As Application
    Public Sub CommandFunctionfweButton_05()



        Dim oPartDoc As PartDocument = g_inventorApplication.ActiveDocument
        Dim oCompDef As PartComponentDefinition = oPartDoc.ComponentDefinition
        Dim oTO As TransientObjects = g_inventorApplication.TransientObjects
        Dim oTG As TransientGeometry = g_inventorApplication.TransientGeometry
        Dim oRefComponents As ReferenceComponents = oCompDef.ReferenceComponents


        Dim oFace As Face = CType(g_inventorApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceFilter, "Pick a Face"), Face)

        If oFace IsNot Nothing Then
            For Each oEdgeloop As EdgeLoop In oFace.EdgeLoops
                If Not oEdgeloop.IsOuterEdgeLoop Then
                    Dim oEdgeColl As EdgeCollection = oTO.CreateEdgeCollection
                    For Each oEdge In oEdgeloop.Edges
                        oEdgeColl.Add(oEdge)

                    Next

                    Dim oWorkpoint As WorkPoint = oCompDef.WorkPoints.AddAtCentroid(oEdgeColl)
                    Dim oWorkAxis As WorkAxis = oCompDef.WorkAxes.AddByNormalToSurface(oFace, oWorkpoint, False)
                    Dim oWorkPlane As WorkPlane = oCompDef.WorkPlanes.AddByNormalToCurve(oWorkAxis, oWorkpoint, False)


                    Dim oWorkSketch As PlanarSketch = oCompDef.Sketches.AddWithOrientation(oWorkPlane, oWorkAxis, True, True, oWorkpoint, True)


                    'Must be defined AFTER hole creation
                    'Dim oSurfaceBody2 As SurfaceBody
                    'oSurfaceBody2 = oCompDef.SurfaceBodies.Item(2)

                    'Dim oObjectColl As ObjectCollection
                    'oObjectColl = oTO.CreateObjectCollection

                    'oObjectColl.Add(oSurfaceBody2)

                    Dim oHoleCenters As ObjectCollection
                    oHoleCenters = oTO.CreateObjectCollection
                    oHoleCenters.Add(oWorkSketch.SketchPoints.Add(oTG.CreatePoint2d(0, 0)))


                    Dim oHoleFeature As HoleFeature
                    oHoleFeature = oCompDef.Features.HoleFeatures.AddDrilledByDistanceExtent(oHoleCenters, "10", "15", PartFeatureExtentDirectionEnum.kPositiveExtentDirection, True, )

                    'This is recommennded hole creation. The previous one is only for backward compatibility
                    'Dim oHoleDef = oCompDef.Features.HoleFeatures.CreateSketchPlacementDefinition(oHoleCenters)
                    'oHoleFeature = oCompDef.Features.HoleFeatures.AddDrilledByDistanceExtent(oHoleDef, "10", "15", PartFeatureExtentDirectionEnum.kPositiveExtentDirection, True, )

                    Dim oSurfaceBody2 As SurfaceBody
                    oSurfaceBody2 = oCompDef.SurfaceBodies.Item(2)

                    Dim oObjectColl As ObjectCollection
                    oObjectColl = oTO.CreateObjectCollection

                    oObjectColl.Add(oSurfaceBody2)
                    Call oHoleFeature.SetAffectedBodies(oObjectColl)

                End If
            Next
        End If
    End Sub
End Module

 

Message 6 of 10

florian_wenzel
Advocate
Advocate

Hi,

 

wrong Parameter.

unKnow Error:

florianwenzelEJNZZ_1-1651475221127.png

 

and Without debugging, the it make the Hole in Body(2)

But give me Error:

florianwenzelEJNZZ_0-1651475817833.png

 

0 Likes
Message 7 of 10

Michael.Navara
Advisor
Advisor
Accepted solution

The collection of edgeloops (oFace.EdgeLoops) may be changed during the holes creation. I recommend you to create inputs first (without surface modification) and then create the holes. 

 

Module CommandFunctionButton_05
    'FOR MY TESTS ONLY
    'Public g_inventorApplication As Application
    Public Sub CommandFunctionfweButton_05()



        Dim oPartDoc As PartDocument = g_inventorApplication.ActiveDocument
        Dim oCompDef As PartComponentDefinition = oPartDoc.ComponentDefinition
        Dim oTO As TransientObjects = g_inventorApplication.TransientObjects
        Dim oTG As TransientGeometry = g_inventorApplication.TransientGeometry
        Dim oRefComponents As ReferenceComponents = oCompDef.ReferenceComponents


        Dim oFace As Face = CType(g_inventorApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceFilter, "Pick a Face"), Face)

        If oFace Is Nothing Then Return

        'Get hole centers collection for specific face first
        Dim holeCentersList As New List(Of ObjectCollection)
        For Each oEdgeloop As EdgeLoop In oFace.EdgeLoops
            If oEdgeloop.IsOuterEdgeLoop Then Continue For
            Dim oHoleCenters As ObjectCollection = GetHoleCenters(oCompDef, oFace, oEdgeloop)
            holeCentersList.Add(oHoleCenters)
        Next

        'Create hole features
        For Each oHoleCenters In holeCentersList
            Dim oHoleFeature As HoleFeature

            'This is recommennded hole creation. The previous one is only for backward compatibility
            Dim oHoleDef = oCompDef.Features.HoleFeatures.CreateSketchPlacementDefinition(oHoleCenters)
            oHoleFeature = oCompDef.Features.HoleFeatures.AddDrilledByDistanceExtent(oHoleDef, "10", "15", PartFeatureExtentDirectionEnum.kPositiveExtentDirection, True, )

            Dim oSurfaceBody2 As SurfaceBody
            oSurfaceBody2 = oCompDef.SurfaceBodies.Item(2)

            Dim oObjectColl As ObjectCollection
            oObjectColl = oTO.CreateObjectCollection

            oObjectColl.Add(oSurfaceBody2)
            Call oHoleFeature.SetAffectedBodies(oObjectColl)
        Next

    End Sub

    Private Function GetHoleCenters(oCompDef As PartComponentDefinition, oFace As Face, oEdgeloop As EdgeLoop) As ObjectCollection
        Dim oTO As TransientObjects = g_inventorApplication.TransientObjects
        Dim oTG As TransientGeometry = g_inventorApplication.TransientGeometry

        Dim oEdgeColl As EdgeCollection = oTO.CreateEdgeCollection
        For Each oEdge In oEdgeloop.Edges
            oEdgeColl.Add(oEdge)

        Next

        Dim oWorkpoint As WorkPoint = oCompDef.WorkPoints.AddAtCentroid(oEdgeColl)
        Dim oWorkAxis As WorkAxis = oCompDef.WorkAxes.AddByNormalToSurface(oFace, oWorkpoint, False)
        Dim oWorkPlane As WorkPlane = oCompDef.WorkPlanes.AddByNormalToCurve(oWorkAxis, oWorkpoint, False)


        Dim oWorkSketch As PlanarSketch = oCompDef.Sketches.AddWithOrientation(oWorkPlane, oWorkAxis, True, True, oWorkpoint, True)

        Dim oHoleCenters As ObjectCollection
        oHoleCenters = oTO.CreateObjectCollection
        oHoleCenters.Add(oWorkSketch.SketchPoints.Add(oTG.CreatePoint2d(0, 0)))
        Return oHoleCenters
    End Function
End Module

 

Message 8 of 10

florian_wenzel
Advocate
Advocate

still Error:

 

List(Of ObjectCollection)

Object not define

florianwenzelEJNZZ_0-1651478088235.png

 

0 Likes
Message 9 of 10

Michael.Navara
Advisor
Advisor
Accepted solution

You need to import namespace

Imports System.Collections.Generic

 

 

 

Message 10 of 10

florian_wenzel
Advocate
Advocate

ok, its Working 🙂

Thanks very much!

 

florianwenzelEJNZZ_0-1651478805746.png

 

 

0 Likes