Run Rule, create new .iam, Close original .iam

Run Rule, create new .iam, Close original .iam

J.VandeMerckt
Advocate Advocate
762 Views
11 Replies
Message 1 of 12

Run Rule, create new .iam, Close original .iam

J.VandeMerckt
Advocate
Advocate

Hi everyone

 

I have a Ilogic code which makes a new Assembly and opens that assembly.

I'm trying to setup a rule that closes the original assembly and forces it to not save.

 

I tried a bunch of codes but I can't figure out how to set it up.

Right now I'm trying this rule:

			Dim asmDoc As Document = currentfilename
			asmDoc.Close(False)

 I'm getting this error:

Error in rule: XP200Replace, in document: TB4001-1750-FU1CYE - Test.iam

Unable to cast object of type 'System.String' to type 'Inventor.Document'.

 

Can anyone help?

0 Likes
763 Views
11 Replies
Replies (11)
Message 2 of 12

A.Acheson
Mentor
Mentor

Hi @J.VandeMerckt 

The error message is correct you can't assign a string value to document. See link here to document object in API help, scroll to the bottom and look at the accessed from paragraph. Here you will get all the objects you can access the document object from.

 

You either need to have the document object earlier in your code using "ThisApplication.ActiveDocument" or "ThisDoc.Document" or retrieve it from memory or open it directly using "ThisApplication.Documents.Open(currentfilename,True)

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 12

J.VandeMerckt
Advocate
Advocate

Hi Acheson

 

I'm not quite sure how to use these rules.

I've only been trying iLogic for a short time and have never been thought how to program.

Could you try to explain a bit more?

Thank you

0 Likes
Message 4 of 12

A.Acheson
Mentor
Mentor

At the start of the rule you are in the assembly document and this is the one you want to close so use the following. 

Dim asmDoc As AssemblyDocument = ThisApplication.ActiveDocument

asmDoc.Close(False)

 Can you share the complete rule? If you are switching active document the above rule will  close the wrong document. If you want to be sure you have the right one before closing use the below messagebox to check the filepath.

 

Messagebox.Show(asmDoc.FullFileName)

 

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

J.VandeMerckt
Advocate
Advocate

Hi Acheson

 

At the end of my code.

A new document opens indeed. This document has the same name but with some extra info at the back.

 

Here the complete rule:

Sub main
	If TypeOf ThisDoc.Document Is AssemblyDocument Then
		Dim oAssDoc As AssemblyDocument = ThisDoc.Document
		Dim fso As Object = ThisApplication.FileManager.FileSystemObject
		Dim oPartOcc As ComponentOccurrence = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter, _
																					"Select your part...(Press ESC to cancel)")
		If oPartOcc Is Nothing Then Exit Sub
		If TypeOf oPartOcc.Definition Is PartComponentDefinition Then
			Dim oPartDef As PartComponentDefinition = oPartOcc.Definition
			Dim oPartDoc As PartDocument = oPartDef.Document
			Dim oUseParams As UserParameters = oPartDef.Parameters.UserParameters			
			Dim sLength As String = InputBox("Enter the length parameter:", "Parameter.", oUseParams.Item("FixedUnit_Length").Expression) 
			
			'Clean sLength so only numbers are left over
			Dim sLengthCleaned As String 
			For i As Integer = 0 To sLength.Length - 1
				If Char.IsDigit(sLength(i)) Or sLength(i) = "."c Then
 					 sLengthCleaned &= sLength(i)
				End If
 			Next
			Dim newLength As Double = Double.Parse(sLengthCleaned)
			If Not Double.TryParse(sLengthCleaned, newLength) Then Exit Sub
			
			
			Dim newCurve As String = GetNewParam(oUseParams, "FixedUnit_Curve", oPartDoc.DisplayName)
			
			If newCurve Is Nothing Then Exit Sub
			
			Dim sPath As String = System.IO.Path.GetDirectoryName(oPartDoc.FullFileName)
			Dim sFullName As String = sPath & "\" & System.IO.Path.GetFileNameWithoutExtension(oPartDoc.FullFileName) & " - " & Strings.Mid(newCurve, 2, Strings.Len(newCurve) - 2) & " - L" & newLength & "mm.ipt"
			
			If Not System.IO.File.Exists(sFullName) Then
				fso.copyFile(oPartDoc.FullDocumentName, sFullName, True)
				oPartOcc.Replace(sFullName, True)
				Dim oNewPart As PartDocument = GetNewComponent(oAssDoc, sFullName)
				Dim oNewParams As UserParameters = oNewPart.ComponentDefinition.Parameters.UserParameters
				
				If sActLength <> newLength Then ChangeParam(oNewParams, "FixedUnit_Length", newLength)
				If sActCurve <> newCurve Then ChangeParam(oNewParams, "FixedUnit_Curve", newCurve)
				
				oNewPart.Save()
			Else
				oPartOcc.Replace(sFullName, True)
				MessageBox.Show("Part Already exist", "Error")

			End If
			
			oAssDoc.Update()
			currentfilename = ThisDoc.FileName(False)

			'filepath same as the current ipt
			SetTheFilePathHere = ThisDoc.Path

			'Set the new file name
			SetTheFilenameHere = currentfilename & " - " & Strings.Mid(newCurve, 2, Strings.Len(newCurve) - 2) & " - L" & newLength & "mm"

			'Use the CommandManager object to execute a private event that sets the file name and path.
			ThisApplication.CommandManager.PostPrivateEvent(kFileNameEvent, SetTheFilePathHere &"\" & SetTheFilenameHere & ".iam")

			'Use the ControlDefinitions object to execute a command that saves the file with the new name and path.
			Dim oCtrlDef
			oCtrlDef = ThisApplication.CommandManager.ControlDefinitions.Item("AppFileSaveCopyAsCmd")
			oCtrlDef.Execute
			
			'Close original file without saving
			' Get a reference to a specific assembly
			Dim asmDoc As AssemblyDocument = ThisApplication.ActiveDocument
			Messagebox.Show(asmDoc.FullFileName)
			asmDoc.Close(False)
			
			'Open new file
			ThisDoc.Launch(SetTheFilePathHere & "\" & SetTheFilenameHere & ".iam")
			

		Else
			MessageBox.Show("Select component is not PartDocyment!", "Error",MessageBoxButtons.OK,MessageBoxIcon.Error)
		End If
	Else
		MessageBox.Show("Active document is not AssemblyDocyment!", "Error",MessageBoxButtons.OK,MessageBoxIcon.Error)
	End If
End Sub

Private Function GetNewParam(oUseParams As UserParameters, sNameParam As String, sPartName As String) As String
	Dim sListParams As New List(Of String)
	Dim sActParam As String
	Dim iActParam As Integer
	For Each oParam As Inventor.Parameter In oUseParams
		If oParam.Name = sNameParam Then
			sActParam = oParam.Expression
			sListParams.AddRange(oParam.ExpressionList.GetExpressionList())
			Exit For
		End If
	Next	
	If sActParam IsNot Nothing Then
		sListParams.Sort
		iActParam = sListParams.IndexOf(sActParam)
		If iActParam < 0 Then iActParam = 0
	Else
		MessageBox.Show("The selected component is missing a parameter: " & sNameParam, "Error!",MessageBoxButtons.OK,MessageBoxIcon.Error)
		Return Nothing
	End If

	Dim newParam As String = InputListBox(sPartName, sListParams, sListParams(iActParam), _
											Title := "List parameters.", ListName := "Select " & sNameParam & ":")
	If newParam Is Nothing Then Return Nothing
	Return newParam
End Function

Private Function ChangeParam(oUseParams As UserParameters, sNameParam As String, newExpr As String)
	For Each oParam As Inventor.Parameter In oUseParams
		If oParam.Name = sNameParam Then
			oParam.Expression = newExpr
		End If
	Next
End Function

Private Function GetNewComponent(oAssDoc As AssemblyDocument, oNameFile As String) As Document
	For Each oRefDoc As Document In oAssDoc.AllReferencedDocuments
		If oRefDoc.FullDocumentName = oNameFile Then
			Return oRefDoc
		End If
	Next
	Return Nothing
End Function

I've tried moving the rule to close the active document up. Instead of putting it at the end.

This doesn't help which is weird because your message box shows the right filepath and right filename.

 

Let me know what you think.

 

BR

Justin

0 Likes
Message 6 of 12

Andrii_Humeniuk
Advisor
Advisor

Hi @J.VandeMerckt . You need to replace line 69:

 

asmDoc.Close(False)

On:

oAssDoc.Close(True)

 

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

0 Likes
Message 7 of 12

J.VandeMerckt
Advocate
Advocate

Hi Andri

 

I thought the False line was to make sure the file doesn't get saved.

Or what is the difference between false and true.

 

BR

Justin

0 Likes
Message 8 of 12

Andrii_Humeniuk
Advisor
Advisor

SkipSave - True.
Снимок экрана 2023-05-02 111031.png

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

Message 9 of 12

J.VandeMerckt
Advocate
Advocate

Hi Andrii

 

Now it closes the file but I think I have other problems now.

The original file closes.

The new one opens but with this error:

JVandeMerckt_0-1683016447278.png

In my new assembly the Part has the correct name but one it doesn't have the correct case selected. (the lenght did change correctly.

 

It seems like I will have to let the rule run completely and close the original file after.

Is it possible to have a snippet where I can specify the document it has to close? Or is this not the correct way to do this.

 

0 Likes
Message 10 of 12

Andrii_Humeniuk
Advisor
Advisor

I have made some changes, please change your code to the new one from the .txt document and try if it works.

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

0 Likes
Message 11 of 12

J.VandeMerckt
Advocate
Advocate
Hi

No still the same error.
0 Likes
Message 12 of 12

J.VandeMerckt
Advocate
Advocate
Is it maybe a idea to let the entire rule run and write another rule that executes after the first rule?
That way the first rule can run its course.
0 Likes