Delete Workplanes (Exception form HRESULT: 0x80070057(E_INVALIDARG))

Delete Workplanes (Exception form HRESULT: 0x80070057(E_INVALIDARG))

Anonymous
Not applicable
1,464 Views
6 Replies
Message 1 of 7

Delete Workplanes (Exception form HRESULT: 0x80070057(E_INVALIDARG))

Anonymous
Not applicable

I have a form with a button.

 

I am trying to delete all workplanes in a part, but i get an exception?

 

Wrong Parameter.(Exception form HRESULT: 0x80070057(E_INVALIDARG))

 

if i click the button multible times the workplanes are deleted else only some are deleted.

 

what is wrong in this code. Please help with a workaround.

 

Running VBExpress 2010 and Inventor 2011

 

 Dim oApp As Inventor.Application

        oApp = GetObject(, "Inventor.Application")

        Dim oPartdoc As PartDocument

        oPartdoc = oApp.ActiveDocument

        Dim oWorkPlanes As WorkPlanes

        oWorkPlanes = oPartdoc.ComponentDefinition.WorkPlanes

        Dim i As Integer

        Dim oWorkplane As WorkPlane

        For i = 1 To oWorkPlanes.Count

            Try

                If i > 3 Then

                    oWorkplane = oWorkPlanes.Item(i)

                    'MsgBox(oWorkplane.Name)

                    oWorkplane.Delete()

                End If

            Catch ex As ArgumentException

                MsgBox(ex.Message)

                'GoTo DoThis

            Catch ex As Exception

                'MsgBox(ex.Message)

            Finally

            End Try

            'DoThis:

        Next

    End Sub

 

regards Kent boettger

0 Likes
Accepted solutions (2)
1,465 Views
6 Replies
Replies (6)
Message 2 of 7

YuhanZhang
Autodesk
Autodesk

The exception happens when you are going to get a work plane with an invalid index. Say you have 5 work planes, and the first 3 work planes are the built-in ones so you could not delete them, and you want to delete the last 2 custom work planes. And you use an index [i] for work plane, so when i = 4, you will delete the forth work plane, then the i will be 5, but because you deleted a work plane, now there are only 4 work planes, and index 5 is out of the range.

 

So solution can be like below, i.e. delete the work planes from the last one to the forth:

 

For i = oWorkPlanes.Count To 4

 



If this solves the problem please click ACCEPT SOLUTION so other people can find it easily.



Rocky Zhang
Inventor API PD
Manufacturing Solutions
Autodesk, Inc.

0 Likes
Message 3 of 7

Anonymous
Not applicable

The code example you provided does not work, if i replace the code part.

 

'For i = 3 To oWorkPlanes.Count

 

For i = oWorkPlanes.Count To 4

 

nothing goes anymore? Please help on this issue

 

Regards.

 

Kent boettger

0 Likes
Message 4 of 7

ekinsb
Alumni
Alumni
Accepted solution

There are a few ways to approach this, but the issue is the one that was already described.  That is the contents of the collection are changing as you delete items and you need to take that into account when you use the Item property to get an object by index.

 

Here's a version of the program that works for me.  It's deleting the the 4th work plane for every iteration.

 

Public Sub DeleteWorkPlanes()
    Dim partDoc As PartDocument
    Set partDoc = ThisApplication.ActiveDocument
   
    Dim i As Integer
    For i = 4 To partDoc.ComponentDefinition.WorkPlanes.count
        Dim wp As WorkPlane
        Set wp = partDoc.ComponentDefinition.WorkPlanes.Item(4)
       
        wp.Delete
    Next
End Sub


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes
Message 5 of 7

YuhanZhang
Autodesk
Autodesk
Accepted solution

Sorry for a mistake, that I should add a Step of -1 in that code, so correct code is as below:

 

 

For i = oWorkPlanes.Count To 4 Step -1

 

Also you can take use of Mr. Brian's method to delete the forth work plane every time.

 



If this solves the problem please click ACCEPT SOLUTION so other people can find it easily.



Rocky Zhang
Inventor API PD
Manufacturing Solutions
Autodesk, Inc.

0 Likes
Message 6 of 7

Anonymous
Not applicable

Hello Brian and yuhanzhang, its always nice when you are around 🙂

 

I see the problem you describe.

 

My problem is actually that a have a part with 10 - 20 different workplanes.

 

Based on some input in my addin some of them has to be deleted.

I Figure the way I tried to approach this by running thru the workplanes until now is the wrong way.

I figure I have to make a selection set of the workplanes I need to delete, and the delete the workplanes I added to the selection set?

 

Here is an example: I have a part with 10 workplanes.

 

I want to count how many workplanes that are in the part, and then add - lets say "workplane 1" "Workplane 5" and Workplane 8" to a selection set, next I want to delete any workplane I added to the selection set.

 

Is this the right way?

 

Do you have a little code that shows how to do this?

 

Regards Kent Boettger

0 Likes
Message 7 of 7

Anonymous
Not applicable

Thanks for your solutions, brian & yuhanzhang but after some thinking i ended up doing it this way, again thanks for your help Smiley Wink

 

If ComboIPlace.Text = "Top - left" Then

 

            Dim oWorkPlanes As WorkPlanes

 

            oWorkPlanes = oPartdoc.ComponentDefinition.WorkPlanes

 

            Dim oWorkplane As WorkPlane

 

            oWorkplane = oWorkPlanes.Item("Work Plane6")

            oWorkplane.Delete(False)

            oWorkplane = oWorkPlanes.Item("Work Plane8")

            oWorkplane.Delete(False)

            oWorkplane = oWorkPlanes.Item("Work Plane15")

            oWorkplane.Delete(False)

            oWorkplane = oWorkPlanes.Item("Work Plane16")

            oWorkplane.Delete(False)

            oWorkplane = oWorkPlanes.Item("Work Plane17")

            oWorkplane.Delete(False)

            oWorkplane = oWorkPlanes.Item("Work Plane19")

            oWorkplane.Delete(False)

        End If

 

regards Kent boettger

0 Likes