Compare old and new file names after Save As

Compare old and new file names after Save As

Nejc.Leban
Advocate Advocate
2,471 Views
32 Replies
Message 1 of 33

Compare old and new file names after Save As

Nejc.Leban
Advocate
Advocate

Hi everyone.

I would like to change some values in a document when it is saved as new file, either with Save As or Save&Replace. Let's say, I have an assembly named 12345-00-000-00 it is checked into vault and is read-only. I then save it as new file named 54321-00-000-00 to create a new product. After it saves it as a new file, I would like to automatically (without pressing any other button or running a rule manually other than Save As or Save&Replace) change the Author and Designer to current User: 

ThisApplication.ActiveDocument.PropertySets.Item("Inventor Summary Information").Item("Author").Value = ThisApplication.GeneralOptions.UserName

 Does anyone know how one would get this to work?

 

Best regards,

Nejc

 

 

0 Likes
Accepted solutions (4)
2,472 Views
32 Replies
Replies (32)
Message 2 of 33

Andrii_Humeniuk
Advisor
Advisor

Add this code to the internal rules and the rule to "Event Triggers" in the "Before Save Document" section.

 

 

Dim sName As String = ThisApplication.GeneralOptions.UserName
iProperties.Value("Summary", "Author") = sName
iProperties.Value("Project", "Designer") = sName

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 3 of 33

Nejc.Leban
Advocate
Advocate

The file is read-only, it doesn't allow changes.

0 Likes
Message 4 of 33

Nejc.Leban
Advocate
Advocate

It also wouldn't work with Save&Replace, because

iProperties.Value("Summary", "Author")

 would change the values of current active document and not the saved and replaced document.

 

 

0 Likes
Message 5 of 33

WCrihfield
Mentor
Mentor

If you need to get a reference to the 'other' document, why not use

Dim oOtherDocument As Document = ThisApplication.Documents.Open(sOtherDocumentFullDocumentName, False)

Then use similar API code to alter iProperties

oOtherDocument.PropertySets..Item("Inventor Summary Information").Item("Author").Value = ThisApplication.GeneralOptions.UserName

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 6 of 33

Nejc.Leban
Advocate
Advocate

What is the value of sOtherDocumentFullDocumentName? Imagine that I perform a Save As or Save&Replace and I want to change the properties of that new saved (and replaced) file? Is there a way to get the file name from the save as dialogue window?

SaveAs.png

0 Likes
Message 7 of 33

WCrihfield
Mentor
Mentor

Sorry, been busy at work.  Since this appeared to be a code related topic, I just assumed the SaveAs action would be  done by code too, instead of with the manual process with the file dialog.  When done entirely by code, you know the file's name before you save it, and after you save it, because you had to specify the new file name.  There are events we can monitor for the regular Save and the SaveCopyAs actions, but not for the SaveAs action.  This is likely because the SaveAs action causes you to loose the reference to the 'other' document.  Below are a couple links to the events I am talking about, but I am not sure they will be of much use to you in this situation.  When properly utilizing those events, you can sometimes get useful information from the 'Context' (NameValueMap), but only the information it has available in that timing frame (before or after), and only in those two situations (Save, SaveCopyAs).

ApplicationEvents.OnSaveDocument Event

DocumentEvents.OnSave Event

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 8 of 33

Nejc.Leban
Advocate
Advocate

Yeah, I've been at this for a while now and couldn't get a solution, so my expectations weren't too high.

Thank you for the help, I appreciate it nonetheless.

0 Likes
Message 9 of 33

JelteDeJong
Mentor
Mentor

Maybe a bit odd thought but why don't you save your 12345-00-000-00  assembly as a template? If you do that you can create new files without the problems of read-only flags. In that case, rules like the one from @Andrii_Humeniuk 

Dim sName As String = ThisApplication.GeneralOptions.UserName
iProperties.Value("Summary", "Author") = sName
iProperties.Value("Project", "Designer") = sName

Although I would use an external rule for this type of situation. It will make things simpler if you want to change the behaviour in the future

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

Message 10 of 33

Frederick_Law
Mentor
Mentor

Might need addin which listen to OnSave event.

I have one to catch new file save by checking FileSaveCounter.

Not sure if it'll catch a SaveAs.

 

Code doesn't work right now.  2023 changed something.  I can't get new file name.

    Private Sub M_ApplicationEvents_OnSaveDocument(
        oDoc As _Document, BeforeOrAfter As EventTimingEnum, Context As NameValueMap,
        ByRef HandlingCode As HandlingCodeEnum) Handles M_ApplicationEvents.OnSaveDocument

      Dim oPartDoc As PartDocument

      'Cannot get new filename Before
      'Try: Check Context for file name
      If BeforeOrAfter = EventTimingEnum.kBefore Then
        'Check if new file, kAfter FileSaveCounter = 1
        If oDoc.FileSaveCounter = 0 Then
          ' MsgBox(oDoc.FullFileName)
          If oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
            'Use Context.Item(1)
            If (InStr(oDoc.DisplayName, "Master", CompareMethod.Text) Or InStr(oDoc.DisplayName, "Layout", CompareMethod.Text)) > 0 Then
              oPartDoc = oDoc
              oPartDoc.ComponentDefinition.BOMStructure = BOMStructureEnum.kReferenceBOMStructure
              oPartDoc.ComponentDefinition.Parameters.UserParameters.Item("Length").ExposedAsProperty = False
              oPartDoc.ComponentDefinition.Parameters.UserParameters.Item("Width").ExposedAsProperty = False
              oPartDoc.ComponentDefinition.Parameters.UserParameters.Item("Area").ExposedAsProperty = False
              'IV2023: oPartDoc.Save, Save2 fire OnSave again
              'oPartDoc.Save()
            End If
          End If
        End If
      End If
    End Sub

 

Message 11 of 33

Andrii_Humeniuk
Advisor
Advisor

Hi @Nejc.Leban . You need to add this rule to external rules. Next, you need to run a read-only document and run this rule. It SaveAs your document and replaces the author and designer.

Dim oDoc As Document = ThisApplication.ActiveDocument
Dim oFileDlg As Inventor.FileDialog = Nothing
InventorVb.Application.CreateFileDialog(oFileDlg)
oFileDlg.Filter = "Inventor Files (*.iam;*.ipt)|*.iam;*.ipt|All Files (*.*)|*.*"
oFileDlg.FileName = System.IO.Path.GetFileName(oDoc.FullFileName)
oFileDlg.ShowSave()
If oFileDlg.FileName Is Nothing Or oFileDlg.FileName = "" Then Exit Sub
oDoc.SaveAs(oFileDlg.FileName, True)
If System.IO.File.Exists(oFileDlg.FileName) Then
	Dim sName As String = ThisApplication.GeneralOptions.UserName
	Dim oChildDoc As Document = ThisApplication.Documents.Open(oFileDlg.FileName, True)
	oChildDoc.PropertySets.Item("Inventor Summary Information").Item("Author").Value = sName
	oChildDoc.PropertySets.Item("Design Tracking Properties").Item("Designer").Value = sName
	oChildDoc.Save()
End If

 

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 12 of 33

yuzeaa
Advocate
Advocate
Accepted solution

try this ilogic code 

Class ThisRule
Private WithEvents oApplicationEvents As ApplicationEvents
Sub main
	If SharedVariable.Exists("ApplicationEvent") Then Exit Sub
	oApplicationEvents = ThisApplication.ApplicationEvents
	sharedVariable("ApplicationEvent") = oApplicationEvents
End Sub
Private Sub oAPP_onsave(DocumentObject As Document,
                        BeforeOrAfter As EventTimingEnum,
						Context As NameValueMap,
						ByRef HandlingCode As HandlingCodeEnum) _
						Handles oApplicationEvents.OnSaveDocument
	If BeforeOrAfter = EventTimingEnum.kBefore Then Exit Sub
	If Context(0) <> Context(1) Then 
		'MsgBox(Context(1) & " was saved as " & Context(0))
		Dim doc = ThisApplication.Documents.Open(Context(0),False)
		doc.PropertySets.Item("Inventor Summary Information").Item("Author").Value = ThisApplication.GeneralOptions.UserName 
	End If 
End Sub
End Class

 

Message 13 of 33

Nejc.Leban
Advocate
Advocate

@JelteDeJong the problem is, I only need this when using older assemblies as reference. When starting from scratch, with a new file, it's not a problem.
@Andrii_Humeniuk thanks, but I need the code to run on the built-in SaveAs function, not a separate one.
@Frederick_Law thanks for your input, I haven't tried the code though, since @yuzeaa 's code doesn't need an add-in and it's actually working perfectly,
so thanks @yuzeaa !!

0 Likes
Message 14 of 33

Nejc.Leban
Advocate
Advocate

@yuzeaa 

I would also like to change the Creation Date value to "today". When running a rule directly, this code works, but when I insert this line into your code, it only changes the Author, the date remains the same.

Dim doc = ThisApplication.ActiveDocument
'doc.PropertySets.Item("Inventor Summary Information").Item("Author").Value = ThisApplication.GeneralOptions.UserName
'doc.PropertySets.Item("Design Tracking Properties").Item("Designer").Value = ThisApplication.GeneralOptions.UserName
doc.PropertySets.Item("Design Tracking Properties").Item("Creation Time").Value = DateTime.Now.ToString("d")


 Any ideas why?

0 Likes
Message 15 of 33

yuzeaa
Advocate
Advocate
Accepted solution

your code is right,just restart your Inventor and run the rule again.

Class ThisRule
Private WithEvents oApplicationEvents As ApplicationEvents
Sub main
	If SharedVariable.Exists("ApplicationEvent") Then Exit Sub
	oApplicationEvents = ThisApplication.ApplicationEvents
	sharedVariable("ApplicationEvent") = oApplicationEvents
End Sub
Private Sub oAPP_onsave(DocumentObject As Document,
                        BeforeOrAfter As EventTimingEnum,
						Context As NameValueMap,
						ByRef HandlingCode As HandlingCodeEnum) _
						Handles oApplicationEvents.OnSaveDocument
	If BeforeOrAfter = EventTimingEnum.kBefore Then Exit Sub
	If Context(0) <> Context(1) Then 
		'MsgBox(Context(1) & " was saved as " & Context(0))
		Dim doc = ThisApplication.Documents.Open(Context(0),False)
		doc.PropertySets.Item("Inventor Summary Information").Item("Author").Value = ThisApplication.GeneralOptions.UserName 
	    doc.PropertySets.Item("Design Tracking Properties").Item("Creation Time").Value = DateString
	End If 
End Sub
End Class
0 Likes
Message 16 of 33

Nejc.Leban
Advocate
Advocate

That's great, thank you! 

0 Likes
Message 17 of 33

yuzeaa
Advocate
Advocate

Hi,@Nejc.Leban 

I found some problems that this code will be triggered when you save iam file after "SaveandReplace" command  executed. the context(1) is iam file,the context(0) is ipt file . Even this cannot be detected by you.So I change my code to make sure this rule runs only when really saveas option.  You can't save A as B which B is already exists on disk.

Class ThisRule
Private WithEvents oApplicationEvents As ApplicationEvents
Dim  isSaveAS As Boolean = False
Sub main
	If SharedVariable.Exists("ApplicationEvent") Then Exit Sub
	oApplicationEvents = ThisApplication.ApplicationEvents
	sharedVariable("ApplicationEvent") = oApplicationEvents
End Sub
Private Sub oAPP_onsave(DocumentObject As Document,
                        BeforeOrAfter As EventTimingEnum,
						Context As NameValueMap,
						ByRef HandlingCode As HandlingCodeEnum) _
						Handles oApplicationEvents.OnSaveDocument
	If BeforeOrAfter = EventTimingEnum.kBefore Then 
		If Context(0) <> Context(1) AndAlso  Not IO.File.Exists(context(0)) Then isSaveAS = True  'check really saveas option
	End If 
	
	If BeforeOrAfter = EventTimingEnum.kAfter Then
		If Not isSaveAS Then Exit Sub
		'MsgBox(Context(1) & " was saved as " & Context(0))
		Dim doc = ThisApplication.Documents.Open(Context(0),False)
		doc.PropertySets.Item("Inventor Summary Information").Item("Author").Value = ThisApplication.GeneralOptions.UserName 
	    doc.PropertySets.Item("Design Tracking Properties").Item("Creation Time").Value = DateString
		isSaveAS = False 'reset False
    End If
End Sub
End Class

 

Message 18 of 33

Nejc.Leban
Advocate
Advocate

I'm not sure I understand what you're trying to say. I've tried the code with Save&Replace and it worked exactly how I want it to work. I tried saving an assembly with Save As and it changed the values correctly. Then I inserted another assembly (let's call it SubAssembly.iam) and tried a Save&Replace on the SubAssembly.iam. I checked the SubAssembly iProperties, and the values were changed. Then I inserted a part into the main assembly (call it Part1.ipt) and tried the Save&Replace and it worked fine there too.

0 Likes
Message 19 of 33

yuzeaa
Advocate
Advocate

After you saveandreplace A to B in iam file ,when you save iam file everytime, the rule fires again and B'properties changed  once again .If you saveandreplace 10 ipt files,this rule will executed 10 times  every time you save iam file which is not needed.  So I said you can not detect this mistake because the result of B'properties always right.You can cancel the following comment  in rule  and see how and when the rule runs.

MsgBox(Context(1) & " was saved as " & Context(0))

 

Message 20 of 33

Nejc.Leban
Advocate
Advocate

OK, thank you.

I ran into another problem though... I have this rule in an external rules folder and set to run Before Save Document. It works great for what I want, but when I'm working with Frame Generator and I insert Frame on 2 sketch lines with same lengths, I get an error: "A frame member could not be created. The template file for DIN 2395 ... structural steel could not be located in the Content Center library. If I don't run your rule, everything works fine like before.

 

Do you maybe have a solution for that or do you know why it happens?

0 Likes