Adding Vault saved location to title block.

Adding Vault saved location to title block.

ThomasSwanson
Advocate Advocate
788 Views
6 Replies
Message 1 of 7

Adding Vault saved location to title block.

ThomasSwanson
Advocate
Advocate

Hey guys, 

 

Working on a custom code to put the Vault file location into the title block. 

I have cheated a little bit buy using the save location on C: and then chopping off the first few characters (C:\vault\) inorder to get the file path to read the same as it would show in vault.  

 

The issues I am running into is the fact that the file has to be saved first  in order for it to create a physical location on the hard drive.  This rule runs perfect executing it using the after save event trigger.  Except that you cannot check it into the Vault afterwards because the rule dirtys the file.  Its a know issue https://knowledge.autodesk.com/support/inventor-products/troubleshooting/caas/sfdcarticles/sfdcartic...

 

So I followed the advice of the knowledge base post and moved it to a before save rule, and added a save as command to prompt the user to save the file before Inventor issued its save.  The problem here is the file has moved from its temp location with my save as command so it causes the save command from Inventor to kick out an error.

 

If this wasn't enough someone brought it to my attention that if you do a copy design then file location would be wrong in Vault until someone checked the file out and back in.

 

Here is the most revised code with it being configured to run with a before save event trigger.

Sub Main
'define the active document
oDoc = ThisDoc.Document
'create a file dialog box
Dim oFileDlg As inventor.FileDialog = Nothing
InventorVb.Application.CreateFileDialog(oFileDlg)


'check file type and set dialog filter
If oDoc.DocumentType = kPartDocumentObject Then
oFileDlg.Filter = "Autodesk Inventor Part Files (*.ipt)|*.ipt"
Else If oDoc.DocumentType = kAssemblyDocumentObject Then
oFileDlg.Filter = "Autodesk Inventor Assembly Files (*.iam)|*.iam"
'Else If oDoc.DocumentType = kDrawingDocumentObject Then
'oFileDlg.Filter = "Autodesk Inventor Drawing Files (*.idw)|*.idw"
Else If oDoc.DocumentType = kDrawingDocumentObject Then
oFileDlg.Filter = "Autodesk Inventor Drawing Files (*.dwg)|*.dwg"
End If


'set the directory to open the dialog at
oFileDlg.InitialDirectory = ThisDoc.WorkspacePath()
'set the file name string to use in the input box
oFileDlg.FileName = iProperties.Value("Project", "Part Number")


'work with an error created by the user backing out of the save 
oFileDlg.CancelError = True
On Error Resume Next
'specify the file dialog as a save dialog (rather than a open dialog)
oFileDlg.ShowSave()


'catch an empty string in the imput
If Err.Number <> 0 Then
MessageBox.Show("No File Saved.", "iLogic: Dialog Canceled")
ElseIf oFileDlg.FileName <> "" Then
MyFile = oFileDlg.FileName
'save the file 
oDoc.SaveAs(MyFile, False) 'True = Save As Copy & False = Save As
End If

'Fileloc

End Sub 

Sub Fileloc

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)
MessageBox.Show("Filelocation",filelocation)

	
'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

End Sub
0 Likes
789 Views
6 Replies
Replies (6)
Message 2 of 7

ThomasSwanson
Advocate
Advocate
Sorry for Posting this twice. The website told me that it had encountered an error while posting so I hit the button again.
0 Likes
Message 3 of 7

wayne.brill
Collaborator
Collaborator

Hi Thomas,

 

I am not finding a way to get around the problem with the "Before Save Document" trigger.

 

I am not sure of your workflow but how about using an "After Open Document" event trigger? I tested this with this simple rule:

 

   iProperties.Value("Project", "Revision Number") = "ABC"
   ThisDoc.Document.Update

When I open the document from Vault the iProperty is updated and the new value is reflected in the titleblock. 

 

The title block would only have the incorrect value the first time it was checked into Vault. (not sure if this would be an issue) Maybe having a default value for that iProperty in the template file would help. (something like "Path will be set when checked out")

 

When a file is checked out from vault it would be saved to the local drive and your rule should be able to get the path and assign it to the iProperty. After this it should be correct in Vault and when it is checked out. 

 

Thanks,

Wayne

 

 



Wayne Brill
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 4 of 7

ThomasSwanson
Advocate
Advocate

Wayne,

 

I cannont make the changes stick.  They will only stay if I run a save command again, or hit yes to saving the document when closing it.

 

If you run the code as a local rule, and using the after save trigger, it seems to behave better and will not leave the file in a dirty state allowing vault checkin.  If it is run as an external rule it will give errors and leave the file in a dirty state.

 

I had someone else here at work look at my code and we were able to greatly simplify it.

Dim fl As String
fl = ThisDoc.PathAndFileName(True)
If InStr(fl, ":") Then
    iProperties.Value("Custom", "Vault_Location") = Mid (fl, 3, Len(fl))
Else
     iProperties.Value("Custom", "Vault_Location") = fl
End If
ThisDoc.Save

 

0 Likes
Message 5 of 7

wayne.brill
Collaborator
Collaborator

Hi Thomas,

 

If there is not an event trigger and I manually run the rule it works and no error occurs. If I enable the "After Save Document" event trigger and manually run the rule it will error on this line:

 

ThisDoc.Save

 

"Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))"

 

I also get this error when I save the document if there is an "After Save Document" running the rule after saving.

 

I believe this error occurs to avoid an infinite loop. The after save event trigger would try to run the rule again when the document is saved by the rule.

 

In my previous reply I suggested using the "After Open Document" event trigger. Is this not an option?

 

Thanks,

Wayne



Wayne Brill
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 6 of 7

ThomasSwanson
Advocate
Advocate

Ok in playing with it today I might have figured out something. 

 

When you do an initial save, it is a SAVE AS command that Inventor issues and you get the  HRESULT: 0x80004005 (E_FAIL)) error.  But when you save it a second time that error doesn't appear. 

 

 

This could be move to an on open trigger, but if its a new file how would any information get populated without saving and reopening all the files.

0 Likes
Message 7 of 7

wayne.brill
Collaborator
Collaborator

Hi Thomas,

 

Do the users need to see where the file is going to be on the local drive when they are looking at a view the file in Vault? If so they could right click on the file and select "Go To Working folder".

 

When they open the file from vault your rule should run using the "After Document Open" event trigger and the location should be set in the title block.

 

Thanks,

Wayne

 

 

 

 



Wayne Brill
Developer Technical Services
Autodesk Developer Network

0 Likes