Error 'parameter is incorrect' when replacing a subassembly

Error 'parameter is incorrect' when replacing a subassembly

jberns-newell
Contributor Contributor
358 Views
2 Replies
Message 1 of 3

Error 'parameter is incorrect' when replacing a subassembly

jberns-newell
Contributor
Contributor

Community,

 

I am creating an assembly with a configurable subassembly.

The top-level assembly does not initially contain the subassembly.

After making choices, the user clicks the button on the form to run the rule (place the subassembly).

 

The initial run successfully places the subassembly.

The second run fails.

 

The rule looks for the existence of the subassembly. If found, delete from top-level assembly.
If a local file exists, it is deleted too.

 

During the second run, this error occurs:

The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))

 

Here is the partial code. Error occurs on line 24 in the code below

		' Normalized name
		sNormalizedName = "Return Rails"
		If ComponentExists(ThisApplication.ActiveDocument, sNormalizedName) Then
			' Delete component
			Components.Delete(sNormalizedName)
		End If
		
		
		''' Return Rails ---------------------------------------------------------------------------
		' Component name
		sCompName = "\\eng\Inventor_Data\Templates\NMC_ASSEMBLY.iam"
		' Delete any local file
		If System.IO.Directory.Exists(sWorkspaceTempPath) AndAlso _
			System.IO.File.Exists(sWorkspaceTempPath & sNormalizedName & ".iam") Then
			System.IO.File.Delete(sWorkspaceTempPath & sNormalizedName & ".iam")
		End If
		' Create component from template
		Dim oReturnRailDoc As AssemblyDocument = ThisApplication.Documents.Add( _
			kAssemblyDocumentObject, _
			sCompName,
			False
			)
		' Default filename to use when saved
		oReturnRailDoc.FullFileName = sWorkspaceTempPath & sNormalizedName & ".iam"
		''' E R R O R    O C C U R S   O N   L I N E   A B O V E   U P O N   S E C O N D   R U N
		' Add the occurrence
		Dim oReturnRailOcc As ComponentOccurrence = oAsmCompDef.Occurrences.Add(oReturnRailDoc.FullDocumentName, oMatrix)
		' Browser name
		oReturnRailOcc.Name = sNormalizedName
		' Not grounded
		oReturnRailOcc.Grounded = False
		' Constraints
		Constraints.AddFlush("ReturnRail_YZ-Assy_YZ", "", "YZ Plane", sNormalizedName, "YZ Plane")
		Constraints.AddFlush("ReturnRail_XZ-Assy_XZ", "", "XZ Plane", sNormalizedName, "XZ Plane")
		Constraints.AddFlush("ReturnRail_XY-Assy_XY", "", "XY Plane", sNormalizedName, "XY Plane")
		iProperties.Value(sNormalizedName, "Custom", "SortPriority") = "6"
		oReturnRailDoc.Close(True)
		oReturnRailDoc.ReleaseReference

 

Since it works the first time, I know the parameters are valid. After I delete, I suspect the file is lingering in memory, which causes the error on second run.

I welcome any insight and suggestion to correct the issue.


Regards,
Jerry

0 Likes
359 Views
2 Replies
Replies (2)
Message 2 of 3

JelteDeJong
Mentor
Mentor

It surprised me that setting a file name as you do on line 24 worked at all. Why don't you save the file with the correct file name? I would change line 24 into this:

oReturnRailDoc.SaveAs(sWorkspaceTempPath & sNormalizedName & ".iam", False)

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes
Message 3 of 3

JBerns
Advisor
Advisor

@JelteDeJong (Jelte),

 

I did not want to perform a SaveAs yet during the subassembly update, since the design may still be changing.

 

My code is based on an example from @JhoelForshav :

https://forums.autodesk.com/t5/inventor-programming-ilogic/how-to-create-a-new-component-in-an-assem... 

 

Upon reviewing my code, I found my mistake. After deleting the subassembly, I forgot to close all invisible documents. This solution was also provided by Jhoel.

https://forums.autodesk.com/t5/inventor-programming-ilogic/removing-components-from-quot-internal-qu... 

 

' Normalized name
sNormalizedName = "Return Rails"
If ComponentExists(ThisApplication.ActiveDocument, sNormalizedName) Then
   ' Delete component
   Components.Delete(sNormalizedName)
   ' Close invisible docs
   ThisApplication.Documents.CloseAll(True)
End If

 

Code appears to be working now as expected. Thanks all for your contributions.

 

Regards,

Jerry

 

 

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional