Changing file name of a open file

Changing file name of a open file

m.rymut
Advocate Advocate
643 Views
7 Replies
Message 1 of 8

Changing file name of a open file

m.rymut
Advocate
Advocate

Hi,

I was wondering if there is a way to change a name of open file?

Lets say i have an assembly and it consists of part 1 and part 2.

I have a rule that makes a new file from a part , copying it and changing its name using custom iproperty.

But what i get is 2 files -  part2.ipt and custom.ipt, and assembly is still using part 2 and not custom.ipt.

Is there a way to change the name of an open file? Or i can only use save file as with a new name?

 

Code i am using:

customPropertySet = ThisDoc.Document.PropertySets.Item("Inventor User Defined Properties")

Dim Nr_fasady As String
Dim Nr_elementu As String
Dim Nr_katalogowy As String
Dim Długość As String
Dim Kolor As String

question = MessageBox.Show("Czy zapisywany plik to część?" & vbLf & "Wybierz Tak dla części oraz Nie dla złożenia", "Rodzaj pliku", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
					If question = vbYes Then

Nr_fasady = InputBox("Proszę wpisać Nr fasady:", "Potrzebne informacje", "")
Nr_elementu = InputBox("Proszę wpisać Nr elementu:", "Potrzebne informacje", "")
Kolor = InputBox("Proszę wpisać kolor elementu:", "Potrzebne informacje", "")

Dim iprop(2) As String
iprop(1) = "Kod elementu (Nr katalogowy)"
iprop(2) = "Długość"


For k = 1 To 2
Dim prop(k) As String
    Try
        prop(k) = iProperties.Value("Custom", iprop(k))
    Catch
        'Assume error means not found
        customPropertySet.Add("", iprop(k))
        iProperties.Value("Custom", iprop(k)) = "null"
    End Try
Next


For j = 1 To 2
    Dim var(j) As String
    If iProperties.Value("Custom", iprop(j)) = "null" Then
	question = MessageBox.Show("Wartość dla: " & iprop(j) & " Nie została określona, czy chcesz określić?", "IProperty nie określone", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
					If question = vbYes Then
        var(j) = InputBox("Proszę wpisać wartość dla: " & iprop(j), "Warning", "")
        iProperties.Value("Custom", iprop(j)) = var(j)
					End If
	Else
		var(j) = iProperties.Value("Custom", iprop(j))
    End If
	
Next

Nr_katalogowy = iProperties.Value("Custom", iprop(1))
Długość = iProperties.Value("Custom", iprop(2))



Dim Name As String
Name = ThisDoc.Path & "\" & Nr_fasady & "_" & Nr_elementu & "_" & Nr_katalogowy & "_" & Długość & "_" & Kolor & ".ipt"
ThisDoc.Document.SaveAs(Name, True)

End If

					If question = vbNo Then
						
Nr_fasady = InputBox("Proszę wpisać Nr fasady:", "Potrzebne informacje", "")
Nr_elementu = InputBox("Proszę wpisać Nr elementu:", "Potrzebne informacje", "")

Dim Name As String
Name = ThisDoc.Path & "\" & Nr_fasady & "_" & Nr_elementu & ".iam"
ThisDoc.Document.SaveAs(Name, True)
End If 

Sorry if the code is messy but it is my first code that i have writen myself, important part is here: 

Dim Name As String
Name = ThisDoc.Path & "\" & Nr_fasady & "_" & Nr_elementu & ".iam"
ThisDoc.Document.SaveAs(Name, True)

As you can see, name of the file is Nr_fasady and Nr_elementu, but it creates a new .iam or .ipt file ( depending on original file) and instead of that i would want it to change the name of the original file.

0 Likes
644 Views
7 Replies
Replies (7)
Message 2 of 8

WCrihfield
Mentor
Mentor

Hi @m.rymut.  It is difficult to follow your code because much of it is in a language that I am unfamiliar with, and I don't know the purpose or goal of the code.  Normally you can not change a file's name while that file is currently open.  If you do manage to change a file's name while it is open, it will cause problems & computer errors.  Why would you want to do this?  Could you please explain in more detail what you are trying to accomplish?

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 8

A.Acheson
Mentor
Mentor

To keep references connected you can do this from a few places, In the assembly or from the design assistant tool. 

 

1. Design assistant:

    Close all open files, Open assembly, right click on part file and rename. 

2. In the assembly:

    You will need to do a save as and then a replace. You will end up with the original file on disk but you could delete this or move to another folder to keep the workspace clean. 

 

Two methods for this:

 2.1 Use the built in command button available in productivity tab as a manual button. Here is ilogic version of  that. This one would be the easiest as a the user is presented with a dialogue for renaming. 

 

2.2 Alternatively use separate routines to get the part occurrence in question save as and create a new file and then perform a replace of this file in the assembly.  The link here shows how to use the replace function. 

 

3. Of course you can just simply rename the part from windows and open the assembly and reconnect the broken file reference, this can get messy if you have a lot of files and you may also want to change iProperties at the same time so design assistant would be better.

 

Depending on your workflow you may choose any one of these methods. If you want any more help post up some more information on your workflow. 

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

m.rymut
Advocate
Advocate
What this code does is when the rule is run, it asks the user to input some information via input box, so user gets a message " Please input lenght:", then another, " please input color" and so on, then it takes some informations from custom iproperties, and if there is no custom iproperty, it makes one and asks the user to input the information, so after asking via inputbox, then it checks iproperties and takes Norm number from it, if there is no iproperty named Norm number, then it makes one and asks the user to input Norm number.
After it gets all the information, it saves copy of the model as lenght_color_Finish_Norm number.iam or .ipt depending on the file type.

What i wanted to change is so that it does not save a copy with the information in file name, but just changes the file name, because if i have a file in an assembly, then when i run the rule and make a copy with proper name the part in the assembly has wrong name.

I make a part, put in in the assembly with name part1.ipt, then i want to run the rule and get proper part name, but i would also want to change the part in the assembly so insted of part1.ipt i would get lenght_color_Finish_Norm number.ipt
0 Likes
Message 5 of 8

gerrardhickson
Collaborator
Collaborator

I agree with @A.Acheson's 2.2 option. Create a temp copy of the file, save it in the name you want, then redirect file references to the new name. It's not pretty, but I don't know of any other way.

0 Likes
Message 6 of 8

m.rymut
Advocate
Advocate
Is is possible to use code to automatically replace file in the assembly?
So i wont have to do it by hand every time
0 Likes
Message 7 of 8

pball
Mentor
Mentor

Here is some code that will do a save and replace on all instances of a selected part.

 

https://forums.autodesk.com/t5/inventor-ilogic-api-vba-forum/vba-save-and-replace-all-instances-of-a...

Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style
0 Likes
Message 8 of 8

m.rymut
Advocate
Advocate
Ok, thanks, i will try combining it with mine so that i can save and replace but also get a proper name of the file