Error: (Exception from HRESULT: 0x80070057 (E_INVALIDARG))

Error: (Exception from HRESULT: 0x80070057 (E_INVALIDARG))

blaroseLB2EQ
Participant Participant
1,617 Views
3 Replies
Message 1 of 4

Error: (Exception from HRESULT: 0x80070057 (E_INVALIDARG))

blaroseLB2EQ
Participant
Participant

Hi all! 

I have a rule that creates holes on a part that is alreay created. My problem is that I get an error after running the rule and filling all the user inputs. Also I can see that points are created but the holes are never created. This is the error I receive. Translated to Invalid parameter/argument. I can't seem to find my error 😞 Help would be appreciated! Thanks in advance.

blaroseLB2EQ_0-1679942854715.png

 

Also, here's the code: 

 

Public Module InputFunction
    Public Function GetUserInput(prompt As String, defaultValue As Object) As Object
        Dim input As String = InputBox(prompt, "User Input", defaultValue)
        If input <> "" Then
            Return input
        Else
            Return Nothing
        End If
    End Function
End Module

Sub Main()
    Dim oPartDoc As PartDocument
    oPartDoc = ThisDoc.Document

    Dim oCompDef As PartComponentDefinition
    oCompDef = oPartDoc.ComponentDefinition

    ' Input parameters
    Dim n As Integer = CInt(GetUserInput("Enter number of holes", 0))
    Dim diameter As Double = CDbl(GetUserInput("Enter diameter of holes", 0))
    Dim xCoords(n - 1) As Double
    Dim yCoords(n - 1) As Double
	Debug.Print(n)
	Debug.Print(diameter)

    ' Loop to get hole coordinates
    For i As Integer = 0 To n - 1
        xCoords(i) = CDbl(GetUserInput("Enter X-coordinate for hole " & i + 1, 0))
        yCoords(i) = CDbl(GetUserInput("Enter Y-coordinate for hole " & i + 1, 0))
    Next

    ' Create sketch and holes
    Dim oSketch As PlanarSketch

    For Each oSketch In oCompDef.Sketches ' delete any previous test sketch
        If oSketch.Name = "Points Sketch" Then oSketch.Delete()
    Next

    Dim oWorkPlane As WorkPlane = oCompDef.WorkPlanes.Item("XY Plane")
    oSketch = oCompDef.Sketches.Add(oWorkPlane)
    oSketch.Name = "Points Sketch"

    Dim holeCount As Integer = n
    Dim oHoleDiameter As Double = diameter
    Dim holeX(n - 1) As Double
    Dim holeY(n - 1) As Double

    For i As Integer = 0 To n - 1
        holeX(i) = xCoords(i)
        holeY(i) = yCoords(i)
        Dim oPoint As Point2d
        oPoint = ThisApplication.TransientGeometry.CreatePoint2d(holeX(i), holeY(i))
        oSketch.SketchPoints.Add(oPoint)
    Next

    Dim oHoleFeatures As HoleFeatures
    oHoleFeatures = oCompDef.Features.HoleFeatures

    For i As Integer = 1 To n
        Dim oHoleFeature As HoleFeature
LINE 68 oHoleFeature = oHoleFeatures.AddDrilledByThroughAllExtent(oSketch.SketchPoints.Item(i), oHoleDiameter, kSymmetricExtentDirection)
    Next
End Sub

 

0 Likes
Accepted solutions (2)
1,618 Views
3 Replies
Replies (3)
Message 2 of 4

A.Acheson
Mentor
Mentor
Accepted solution

You could try to place the sketch points into a object collection then used one hole feature for all points. See API sample here

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 3 of 4

JMGunnar
Collaborator
Collaborator
Accepted solution

rename hole or use @A.Acheson exampel

 

	Dim holeFeat As HoleFeature = oCompDef.Features.HoleFeatures.AddDrilledByThroughAllExtent( _
					oHoleCenters, HoleDiameter,kSymmetricExtentDirection)
				holeFeat.Name = "CutHole" & i

 Best Johan @blaroseLB2EQ 

0 Likes
Message 4 of 4

JMGunnar
Collaborator
Collaborator
Do not forget too remove
"Cuthole"

For ii=1 To 10
DeleteFeatures("CutHole" & ii)
Next

Sub DeleteFeatures(FeatureName As String)

Dim oPartDoc As PartDocument = ThisDoc.Document
Dim oCompDef As PartComponentDefinition = oPartDoc.ComponentDefinition

For Each item In oCompDef.Features.HoleFeatures
If item.Name = FeatureName Then
Try
item.delete
Catch
End Try
End If
Next
0 Likes