Inventor ilogic Stuck in a Save Loop

Inventor ilogic Stuck in a Save Loop

ThomasSwanson
Advocate Advocate
927 Views
7 Replies
Message 1 of 8

Inventor ilogic Stuck in a Save Loop

ThomasSwanson
Advocate
Advocate

Hey all,

I have managed to program my self into a corner here.  I wanted to add the file save location on the title block.  Wipped up a quick ilogic code and set it to fire before save.  This worked but you had to save it twice because when the rule runs before save the file hasn't been given a location on the disc or a file name.  So simple fix, I set the event trigger to fire the rule after save.  This works except if I close the drawing and don't accecpt the prompt to save the file my changes don't stick.  So I added a save command to the rule to save the changes as it runs.  The issue is now when the ilogic rule runs it fires off the save command which fires off the after save even trigger, which fires off the save command........and down the rabbit hole I go.

 

I tried setting the rule to file when an iproperty changes also, but my rule makes a custom iproperty so that starts the whole loop again.

 

Dim filelocation As String
Dim Vault_Location As String 
Dim GETSheetName As String 
Dim save_state As String




Try
	'set save_state
	MessageBox.Show ("starting the try loop: " & save_state)
	save_state = iProperties.Value("Custom", "Save_State")
	'save_state = True
	MessageBox.Show("end of try: "& save_state)
Catch
	' Assume error means not found so create it
	save_state = "True"
	iProperties.Value("Custom", "Save_State") = save_state
	MessageBox.Show("catching setting to True: " & save_state)
End Try


filelocation = ThisDoc.PathAndFileName(False)
	
If InStr(filelocation, ":") Then

	MessageBox.Show("Path and filename:" & filelocation)
'       ' Extract the Vault location from the combined file path and part name
'		'filelocation variable looks like this "C:/Vault/desgins/Project/..." - we want "Project/..."
	GetSheetName = Mid (filelocation, 3, Len(filelocation)-2)
	MessageBox.Show("Modified Path and filename:" & GetSheetName)
	iProperties.Value("Custom", "Vault_Location") = GetSheetName
Else
	' Couldn't find the ":", so just return the whole string
 	GetSheetName = filelocation
	iProperties.Value("Custom", "Vault_Location") = GetSheetName
End If

MessageBox.Show("before if: " & save_state)
If save_state = "True" Then
	'this flag means this function needs to save the drawing again

	save_state = "False"
	iProperties.Value("Custom", "Save_State") = save_state
	ThisDoc.Save
	
	MessageBox.Show("IF statement:"& save_state)
Else
	'this function has already run, so we don't need to save again
	save_state = "False"
	iProperties.Value("Custom", "Save_State") = save_state
	
End If
0 Likes
928 Views
7 Replies
Replies (7)
Message 2 of 8

adam.nagy
Autodesk Support
Autodesk Support

Hi Thomas,

 

"I wanted to add the file save location on the title block.  "

Why don't you simply use the Filename and Path property in the title block?

 

FileNameAndPath.png

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Cheers,



Adam Nagy
Autodesk Platform Services
0 Likes
Message 3 of 8

ThomasSwanson
Advocate
Advocate

Sorry for the late reply on this but I have been swamped.

 

Yes I can use that file/Path Iproperty but it gives the full path  C:\Vault\desgins\workfolder\project\part.ipt.

They want the file path on the drawing so the guy running the laser cutter will pull the correct digitial file from the paper copy he receives out of vault.   Thats why I wrote the rule to chop off the front of the file path so it would read \workfolder\project\part.ipt  Make sense?

 

 

Is there any property that will show the vault file path?  

 

0 Likes
Message 4 of 8

Balaji_Ram
Alumni
Alumni

Hi Thomas,

 

Thanks for clarifying more details.

 

Will it not be possible to call "ThisDoc.Save" only if the file hasn't been saved previously ?

 

In that case, even if the iLogic rule has been set to run after each save, the save would not be called the second time

and avoid getting into a loop.

 

The "ThisDoc.PathAndFileName" does not return a file name if the document has not been saved.

So checking its return value should help your iLogic code to decide if it needs a Save.

 

Regards,

Balaji

 

 



Balaji
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 5 of 8

adam.nagy
Autodesk Support
Autodesk Support

I missed that.

Well, I'm not aware of a variable for that so you'll have to do it your own way then.

 

If you get into an endless loop then you would need a variable which stores that you are calling save so that you won't call it again.

Something like

If Not bSaving Then

  bSaving = True

  Save

  bSaving = False

End If

 

You might have to create a document paraneter for that. Not sure.

 

The best would be if you did this whole thing from an addin, then you could update things just before the save takes place.



Adam Nagy
Autodesk Platform Services
0 Likes
Message 6 of 8

ThomasSwanson
Advocate
Advocate

This is exactly what I did in the beginning of the code with the Try and Catch loop.

 

I am looking for a variable called save_state If its not there then I create it, and set it to True so the rule will run.

After the rule runs I set save_state to false so it will not run next time. 

 

 

OK I think I might have found my problem.  Its the TRY and Catch Loop.

If the Ipropety already exists and it is set to False the Try half of that loop sets it back to True which allows the rule to run.

0 Likes
Message 7 of 8

ThomasSwanson
Advocate
Advocate

I think I fixed it.   When you call a variable and a custom iproperty the same name it makes debugging a little tricky.

I went back and added another variable called Test that pulls the state of the custom iproperty called Saved_State.

This test varible is this ran through the existing IF loop to see if needs to save again.  Before it was tied to the save_state varible that was in the rule. 

I've added some comments to the rule also so its a little easier to read what I am doing.

 

Dim filelocation As String
Dim Vault_Location As String 
Dim GETSheetName As String 
Dim save_state As String




Try
	'This is looking for the Iproperty called Save_State
	'MessageBox.Show ("starting the try loop: " & save_state)
	save_state = iProperties.Value("Custom", "Save_State")
	'MessageBox.Show("end of try: "& save_state)
Catch
	'If the Save_State Variable doesn't exist this will create it and set it to True.
	save_state = "True"
	iProperties.Value("Custom", "Save_State") = save_state
	MessageBox.Show("catching setting to True: " & save_state)
End Try

'This calles the Active File
filelocation = ThisDoc.PathAndFileName(False)

	
'This IF statement looks for the : in the file path if found it will remove the needed amount of spaces from the file path	
If InStr(filelocation, ":") Then

	'MessageBox.Show("Path and filename:" & filelocation)
'       ' Extract the Vault location from the combined file path and part name
'		'filelocation variable looks like this "C:/Vault/desgins/Project/..." - we want "Project/..."
	GetSheetName = Mid (filelocation, 3, Len(filelocation)-2)
	'MessageBox.Show("Modified Path and filename:" & GetSheetName)
	iProperties.Value("Custom", "Vault_Location") = GetSheetName
Else
	' Couldn't find the ":", so just return the whole string
 	GetSheetName = filelocation
	iProperties.Value("Custom", "Vault_Location") = GetSheetName
End If

'MessageBox.Show("before if: " & iProperties.Value("Custom", "Save_State"))

'created a variable here to store the value of the Save_State iproperty. This test variable is then used to see if the rule needs to be run again
Dim test As String
test = iProperties.Value("Custom", "Save_State")
If test = "True" Then
	'this flag means this function needs to save the drawing again

	save_state = "False"
	iProperties.Value("Custom", "Save_State") = save_state
	ThisDoc.Save
	
	'MessageBox.Show("IF statement:"& save_state)
Else
	'this function has already run, so we don't need to save again
	save_state = "False"
	iProperties.Value("Custom", "Save_State") = save_state
	
End If
0 Likes
Message 8 of 8

adam.nagy
Autodesk Support
Autodesk Support

Glad to hear it 🙂



Adam Nagy
Autodesk Platform Services
0 Likes