Rename and save using iLogic

Rename and save using iLogic

Anonymous
Not applicable
14,061 Views
30 Replies
Message 1 of 31

Rename and save using iLogic

Anonymous
Not applicable

Hi

 

I am having a bit of trouble finding the correct code for some automated ilogic in inventor 2011.

 

I am trying to achieve an automated rename and save process for an assembly file as well as the variable parts in the file.

 

an example would be a steel column...

 

Open ilogic steel column, run rules to change column length and section size...

 

then save a copy to workspace, but also to a specific folder in the workspace..

 

also rename the column file and save.

 

i have this for file saving -


SyntaxEditor Code Snippet

Test=InputBox("Add File name", "Please Add your file name", "Prefix number - File - .iam")

ThisDoc.Document.SaveAs(ThisDoc.WorkspacePath()&Test, True)

Which works fine to save the new assembly, but i need the same for the variable column within the assembly, and also to save to a specific folder not just the workspace top level.

 

Any help would be greatly appreciated

 

Regards

 

Lee

 

 

14,062 Views
30 Replies
Replies (30)
Message 21 of 31

Anonymous
Not applicable

Tony,

 

Using either SaveAs or SaveCopyAs, at the end, it will still generate two files. The only difference is which version will be the active file at the end. So using the SaveAs will generate 12345 A.ipt and 12345 A.idw. But if you have a reason for using SaveCopyAs instead, all you have to do is change the "False" argument in the SaveAs syntax to True. This will make it use the "SaveCopyAs". Also if you are using SaveCopyAs you will probably need to look at the code for updating the drawing because the active drawing now will still be the original drawing. The code to update the drawing is also replacing the model document with the new one so if the current drawing is the original, it won't be correct. You will probably have to close the original drawing after the SaveCopyAs and then open the new drawing. Hope that helps.

 

Dan

Message 22 of 31

Anonymous
Not applicable

@Anonymous

 

its works amazing and I need a small addition to this, I what the macro to ask me the location to place all the files in a new location or folder, can this be done? Thanks for your program.

0 Likes
Message 23 of 31

antoniobaron
Contributor
Contributor

Thanks mate this piece of code was a fantastic master piece!

 

It helped me to achieved the replacement of those components created with "With Make Components" and place an automatic part name on each file, inclusive sub assemblies and sub components.

 

I just needed to create a conditional that omites the base component.

 

Cheers

 

0 Likes
Message 24 of 31

jarod.long
Explorer
Explorer

I am also trying to figure out if there is a way to save and rename but just add a prefix or suffix to a already saved part in a assembly. 

I believe I use danvang's Save and rename assembly code and just manually add a prefix/ suffix but it just takes so much time.

 

Thank you,

Jarod

 

0 Likes
Message 25 of 31

Anonymous
Not applicable

Jarod,

 

Can you help me understand what you are looking for? From what I understand, you want to put a prefix or suffix to the file name. Do you want that prefix/suffix to be dynamic? Meaning that based on a certain criteria, it will use a certain prefix/suffix. Or will the prefix/suffix be a fixed value? Or will the prefix/suffix be a prompted value? All is doable. Just want clarification about your desired workflow.

 

DanV

0 Likes
Message 26 of 31

antoniobaron
Contributor
Contributor

I think you can create some sort of conditional, if the file name starts with or doesn't contain this then replace file name and trigger it when document is opened with the exception of content centre files....  however, for such complexity you may need to create some sort of index generator, I don't know if that would be that easy. Look I personally recommend you to forget about placing names in the file name, otherwise you will be spending your entire life trying to update these names for a specific job or client. Use numbers and use these numbers as part number example 55-11001 (55: Client 11: Project Type and 001 Consecutive) that makes life easier handling inventor in every aspect.

I think Vault handles similar operation with the name generator, have a look on how it works and see if a similar option would suit your needs. 


0 Likes
Message 27 of 31

Anonymous
Not applicable

Here's how you can put a prefix/suffix to the file name. You will be prompted for the Prefix and Suffix. BTW, it's been awhile since I looked at this code. I added what needs to happen but I haven't tested this yet. But hopefully it helps or leads you to what you are looking to do.

 

Sub Main
'get the filepath of this document
Dim sFilePath As String= ThisDoc.Path & "\"
'get the active document
doc = ThisDoc.Document
'declare variable for drawing
Dim oDestinationDoc As DrawingDocument
'declare variable for new name and original name
Dim sNewName As String
Dim sOrgName As String = doc.DisplayName

Dim strPrefix As String = ""

Dim strSuffix As String = ""

'get the new filename from the input box
sNewName = InputBox("What is the new file name for " & doc.DisplayName & "?", "New File Name", doc.DisplayName)

strPrefix = InputBox("What is the prefix?", "Prefix Value", strPrefix)

strSuffix = InputBox("What is the suffix?", "Suffix Value", strSuffix)


'if the new file name is blank then use the same name
If sNewName = "" Then
sNewName = sOrgName
End If

'if the user also types in the extension then remove the extension.
If sNewName.EndsWith(".ipt") = True Then
sNewName = sNewName.substring(0,sNewName.length-4)
End If
sNewName = strPrefix & sNewName & strSuffix 
iProperties.Value("Project", "Part Number") = sNewName '***this updates the part number value in the model

'if there is an change in the name then do the following.
If doc.DisplayName <> sNewName & ".ipt" Then
'check to see the file already exist. If the file does not exist then... If it does exist, do nothing
If System.IO.File.Exists(sFilePath & sNewName & ".ipt") = False Then
'set the old file name. will need this to find the idw. no extension
sOrgName = sOrgName.substring(0,sOrgName.Length-4)
'if it does not exist then create the file
doc.saveas(sFilePath & sNewName & ".ipt",False)
'call sub to update the drawing file.
Call UpdateDrawing(sFilePath, sOrgName, sNewName)
End If
End If
End Sub

Private Sub UpdateDrawing (ByVal Path As String, ByVal oldName As String, ByVal newName As String)
'declare variable for drawing document
Dim oDestinationDoc As DrawingDocument
'declare variable for the original idw.
Dim sFileName As String = Path & oldName & ".idw"

Try
'if the original idw file exist then.....
If System.IO.File.Exists(sFileName) Then
'open the original idw
oDestinationDoc = ThisApplication.Documents.Open(sFileName)
'save the original idw as the new file name. The new idw will be the same name as the ipt
oDestinationDoc.saveas(Path & newName & ".idw",False)

'next we need to update the new idw to look at the new ipt.
'declare variables
Dim oDocDescriptor As DocumentDescriptor
oDocDescriptor = oDestinationDoc.ReferencedDocumentDescriptors.Item(1)

Dim oFileDescriptor As FileDescriptor
oFileDescriptor = oDocDescriptor.ReferencedFileDescriptor

'replace the original ipt file with the new ipt in the new idw.
oFileDescriptor.ReplaceReference(Path & newName & ".ipt")

Dim oPropSet As PropertySet = oDestinationDoc.PropertySets.Item("Design Tracking Properties")
oPropSet.item("Part Number").Value = newName '***this updates the part number value in the drawing

'update the drawing
oDestinationDoc.Update()
'close the drawing
oDestinationDoc.Close
End If
Catch
'if there's an issue, close the drawing and show error
oDestinationDoc.Close
MessageBox.Show("Cannot update the drawing")
End Try

End Sub

0 Likes
Message 28 of 31

jarod.long
Explorer
Explorer

@Anonymous The initial prompt looks correct, giving the option for both prefix and suffix, but the code has a error.

 

I will have to look into it, but I appreciate you giving me a starting template. If I find a way to fix this error I will post it in the forum. 

0 Likes
Message 29 of 31

aronmatheus
Advocate
Advocate

Congratulations @Anonymous for me your code stayed perfect! I would know if had to utilize your code and link parameters utilized to construct the assembly after changing the name of the parties?

0 Likes
Message 30 of 31

aronmatheus
Advocate
Advocate

Hi @Anonymous your ruler has to put the file idw to?

0 Likes
Message 31 of 31

Anonymous
Not applicable

Hi Aron,

 

sorry it's been a while that I fiddled around with iLogic.

My best guess would be to put the *.idw into the same directory as the assembly. If I remember correctly Inventor himself is assuming that the idw is named the same as the assembly if you click on "open drawing" in assembly environment.

So I believe that's what the code is doing too, It assumes the drawing is named exactly the same as the assembly/part.

Man I am not even working with Inventor anymore since 2015 so I could be completely wrong, that's just what I could scratch out of the back of my head ... hope it helps in some way ; )

0 Likes