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
I was hoping to see more replies to your post, as I'm encountering similar difficulties with a "Configurator" I am working on.
The biggest limitation seems to come from the ThisDoc.Document.Saveas, which doesn't permit you to "save as" any components within your assembly (ie, you can only Save As your top level assembly where the rule is). There is also a FileCopy command that one could conceivaby use on components, but that only copies out the part file as it was last configured and saved. I haven't found a way to SaveAs the newly configured part as stored in RAM, which seems to be what you desire.
As for file save locations, be advised that you can structure them either relatively (as you already are) or absolutely.
For example:
Relative location in a subfolder within your workspace named after your new assembly number. Note that I changed the code to instruct the user just to addd the file name without the extension. The if statement creates a new subfolder specific to your new subassembly if it doesn't already exist.
Test=InputBox("Add File name", "Please Add your file name", "Prefix number - File")
If (Not System.IO.Directory.Exists(Test)) Then
System.IO.Directory.CreateDirectory(Test)
End If
ThisDoc.Document.SaveAs(Test & "\" &Test &".iam", True)
or
Absolute location (assuming the destination folder already exists):
Test=InputBox("Add File name", "Please Add your file name", "Prefix number - File")
ThisDoc.Document.SaveAs("C:\Workspace\newfiles\" &Test &".iam", True)
Hope this helps a bit and that it prompts some more discussion.
Here's some code that I wrote that may help with what you are looking at doing. This is only for a single part and it's drawing. I do have one that's for the assembly, parts, assembly drawing and part drawings. Copy and paste this into a rule. Hope this helps.
~~~~~
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
'get the new filename from the input box
sNewName = InputBox("What is the new file name for " & doc.DisplayName & "?", "New File Name", doc.DisplayName)
'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
'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")
'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
Do you have any more info on your code for "the assembly, parts, assembly drawing, and part drawings"? It sounds interesting and should be what Lee is looking for.
Also, where did you learn about the additional coding that iLogic can handle? The Snippets built into the Rule Editor are pretty limited, but it seems like Inventor can understand and use much more. Can you point Lee and me towards some useful references?
For example, I notice that you dimension oDestinationDoc as a "DrawingDocument", but what kind of syntax would one use for an ipt or iam file?
Here is the code for the assembly. place this in a rule in the assembly file. There is one thing to be aware of, because i am using the displayname to get the name of the file, you will need to display file extensions for known file types in Windows Explorer.
The syntax that I am using is from the Inventor API.
Hope this helps.
Sub Main()
Dim Path As String = ThisDoc.Path & "\"
Dim FileChange As Boolean = False
InvDoc = ThisDoc.Document
Dim refDocs As DocumentsEnumerator = InvDoc.AllReferencedDocuments
Dim refDoc As Document
For Each refDoc in refDocs
'MessageBox.show(refDoc.DisplayName)
'input box to enter new file name
NewFileName = InputBox("What is the new file name for " & refDoc.DisplayName & "?", "New File Name", refDoc.DisplayName)
'NewFileName = InputBox("What is the new file name for " & refDoc.DisplayName & "?", "New File Name", iProperties.Value(refDoc.DisplayName, "Project", "Part Number"))
'if the new file name is blank then use the same name
If NewFileName = "" Then
NewFileName = refDoc.DisplayName
End If
'if the user also types in the extesion then remove the extension.
If NewFileName.EndsWith(".ipt") = True Then
NewFileName = NewFileName.substring(0,NewFileName.length-4)
'MessageBox.show(NewFileName)
End If
'if there is an change in the name then do the following.
If refDoc.DisplayName <> NewFileName & ".ipt" Then
'check to see the file already exist. if it does then replace.
If System.IO.File.Exists(Path & NewFileName & ".ipt") Then
Component.Replace(refDoc.DisplayName, Path & NewFileName & ".ipt", True)
Else
Dim oldName As String = refDoc.DisplayName
'if it does not exist then recreate the file
refDoc.saveas(Path & NewFileName & ".ipt",False)
Call UpdateDrawing(Path, oldName, NewFileName)
End If
FileChange = True
End If
Next
If FileChange = True Then
NewAssemblyName = InputBox("What is the new file name for the assembly?", "New Assembly File Name", iProperties.Value("Project", "Part Number"))
If NewAssemblyName <> "" Then
Dim oldName As String = ThisDoc.FileName(True) 'include extension
ThisDoc.Document.SaveAs(Path & NewAssemblyName & ".iam" , False)
Call UpdateDrawing(Path, oldName, NewAssemblyName)
End If
End If
iLogicVb.UpdateWhenDone = True
End Sub
Private Sub UpdateDrawing (ByVal Path As String, ByVal oldName As String, ByVal newName As String)
Dim oDestinationDoc As DrawingDocument
Dim strFileExtension As String = oldName.substring(oldName.Length-4,4)
oldName = oldName.substring(0,oldName.Length-4)
Dim sFileName As String = Path & oldName & ".idw"
Try
If System.IO.File.Exists(sFileName) Then
oDestinationDoc = ThisApplication.Documents.Open(sFileName)
oDestinationDoc.saveas(Path & newName & ".idw",False)
Dim oDocDescriptor As DocumentDescriptor
oDocDescriptor = oDestinationDoc.ReferencedDocumentDescriptors.Item(1)
Dim oFileDescriptor As FileDescriptor
oFileDescriptor = oDocDescriptor.ReferencedFileDescriptor
oFileDescriptor.ReplaceReference(Path & newName & strFileExtension)
oDestinationDoc.Update()
oDestinationDoc.Close
End If
Catch
oDestinationDoc.Close
MessageBox.Show("error")
End Try
End Sub
Hi I have found this info useful but would like to modify it slightly so that it saves the .ipt files and .idw files to a new folder location based on iproperties within the model. I can achive this on the .ipt level with the altered code but the drawing does not copy to the new folder location.
Format:HTML Format Version:1.0 StartHTML: 165 EndHTML: 30566 StartFragment: 314 EndFragment: 30534 StartSelection: 314 EndSelection: 314
SyntaxEditor Code Snippet
SubMain'get the filepath of this documentDimsFilePathAsString="C:\Vault Workspace\Customers"&"\"&Customer&"\"&Job_Tag&"\"&SalesOrderNumber&"\"'get the active documentdoc=ThisDoc.Document'declare variable for drawingDimoDestinationDocAsDrawingDocument'declare variable for new name and original nameDimsNewNameAsStringDimsOrgNameAsString=doc.DisplayName'get the new filename from the input boxsNewName=InputBox("What is the new file name for "&Gauge&" ga "&Material_type&" - "&Part_Name&"?", "New File Name", Gauge&" ga "&Material_type&" - "&Part_Name)'if the new file name is blank then use the same nameIfsNewName=""ThensNewName=sOrgNameEndIf'if the user also types in the extension then remove the extension.IfsNewName.EndsWith(".ipt")=TrueThensNewName=sNewName.substring(0,sNewName.length-4)EndIf'if there is an change in the name then do the following.Ifdoc.DisplayName<>sNewName&".ipt"Then'check to see the file already exist. If the file does not exist then... If it does exist, do nothingIfSystem.IO.File.Exists(sFilePath&sNewName&".ipt")=FalseThen'set the old file name. will need this to find the idw. no extensionsOrgName=sOrgName.substring(0,sOrgName.Length-4)'if it does not exist then create the filedoc.saveas(sFilePath&sNewName&".ipt",False)'call sub to update the drawing file.CallUpdateDrawing(sFilePath, sOrgName, sNewName)EndIfEndIfEnd Sub
PrivateSubUpdateDrawing(ByValPathAsString, ByValoldNameAsString, ByValnewNameAsString)'declare variable for drawing documentDimoDestinationDocAsDrawingDocument'declare variable for the original idw.DimsFileNameAsString=Path&oldName&".idw"Try'if the original idw file exist then.....IfSystem.IO.File.Exists(sFileName)Then'open the original idwoDestinationDoc=ThisApplication.Documents.Open(sFileName)'save the original idw as the new file name. The new idw will be the same name as the iptoDestinationDoc.saveas(Path&newName&".idw",False)'next we need to update the new idw to look at the new ipt.'declare variablesDimoDocDescriptorAsDocumentDescriptoroDocDescriptor=oDestinationDoc.ReferencedDocumentDescriptors.Item(1)DimoFileDescriptorAsFileDescriptoroFileDescriptor=oDocDescriptor.ReferencedFileDescriptor'replace the original ipt file with the new ipt in the new idw.oFileDescriptor.ReplaceReference(Path&newName&".ipt")'update the drawingoDestinationDoc.Update()'close the drawingoDestinationDoc.CloseEndIfCatch'if there's an issue, close the drawing and show erroroDestinationDoc.CloseMessageBox.Show("Cannot update the drawing")EndTry
End Sub
Hi
Im struggeling with what it seems you guys might have a solution for...!
Short.: i would like to copy specific parts, in my assy. (allso a configurable assy), they are specific by name and location..! also the target location is always the same...
My current thread is here..:
Even though this is an old tread i hope some of you are still available to help 🙂
/LSA
I replied to you in your other post.
danvang.
I have been using this code with slight modifications for some time and have had great results. I have it running on 2 machines seamlessly but I have a third machine setup that i can't get to function properly. the code is exactly the same as the other 2 machines. The issue i am having is with the coping of the drawing to the new location. I have added message box code (see below) to determine where the code fails and have determined that it is this line "If System.IO.File.Exists(sOldFileName) Then"
Try
'if the original idw file exist then.....
If System.IO.File.Exists(sOldFileName) Then
MessageBox.Show("I got past the if statment")
Any help in resolving this issue would be much appreciated.
Not sure why it would not work but here are some troubleshooting methods you can try.
1. Place a message box to show sOldFileName before the if. Run the rule and check to see if the syntax for sOldFileName is correct.
2. The person who is logged in on the third computer, does the person have access to the directory?
3. In Windows Explorer, show the file extensions for known file types.
If it still does not work then attach your code and I can take a look at it.
Dan
Hi Dan,
thanks for posting your code. It helped me a lot in solving my problem.
I changed your code for my specific task. I want to copy an assembly to a workspace path while ignoring content center parts. So here is my code for helping others to achieve their goals.
Sub Main() Dim Path As String = ThisDoc.WorkspacePath & "\" Dim oldPath As String = ThisDoc.Path & "\" MessageBox.Show(Path, "Workspace-Pfad") MessageBox.Show(oldPath, "alter Workspace-Pfad") oInvDoc = ThisDoc.Document Dim refDocs As DocumentsEnumerator = oInvDoc.AllReferencedDocuments Dim refDoc As Document For Each refDoc In refDocs 'MessageBox.Show(refDoc.Type) 'MessageBox.Show(refDoc.DocumentType) 'detect If document Is Assembly And save As Assembly If refDoc.DocumentType = 12291 Then 'define new Name for copy of the referenced document NewFileName = InputBox("Geben Sie den neuen Namen für die Baugruppe " & refDoc.DisplayName &" ein!","New File Name", refDoc.DisplayName) 'check For blank filename And Set To DisplayName If NewFileName = "" Then NewFileName = refDoc.DisplayName End If 'check For extension In name And remove If there If NewFileName.EndsWith(".iam") = True Then NewFileName = NewFileName.Substring(0,NewFileName.length-4) 'MessageBox.Show(NewFileName) End If refDoc.SaveAs(Path & NewFileName & ".iam",False) Else 'initialize object for PartComponentDefinition to access property if part is member of content center Dim oPartCompDef As PartComponentDefinition oPartCompDef = refDoc.ComponentDefinition If Not oPartCompDef.IsContentMember Then NewFileName = InputBox("Geben Sie den neuen Namen für das Bauteil " & refDoc.DisplayName &" ein!","New File Name", refDoc.DisplayName) 'check For blank filename And Set To DisplayName If NewFileName = "" Then NewFileName = refDoc.DisplayName End If 'check For extension In name And remove If there If NewFileName.EndsWith(".ipt") = True Then NewFileName = NewFileName.substring(0,NewFileName.length-4) 'MessageBox.Show(NewFileName) End If refDoc.SaveAs(Path & NewFileName & ".ipt",False) Else MessageBox.Show("Die Komponente " & refDoc.DisplayName & "ist ein Content Center Part und wird nicht kopiert !") End If End If Next NewAssemblyName = InputBox("Wie lautet der neue Name für die Haupt-Baugruppe?", "Neuer Baugruppenname", ThisDoc.FileName(True)) If NewAssemblyName = "" Then NewAssemblyName = ThisDoc.FileName(True) If NewAssemblyName.EndsWith(".iam") = True Then NewAssemblyName = NewAssemblyName.Substring(0,NewAssemblyName.length-4) End If 'MessageBox.Show(NewAssemblyName,"Alter Baugruppenname") ThisDoc.Document.SaveAs(Path & NewAssemblyName & ".iam" ,False) Else 'MessageBox.Show(NewAssemblyName, "Neuer Baugruppenname") If NewAssemblyName.EndsWith(".iam") = True Then NewAssemblyName = NewAssemblyName.Substring(0,NewAssemblyName.length-4) End If ThisDoc.Document.SaveAs(Path & NewAssemblyName & ".iam",False) End If Dim ctrl As ControlDefinition ctrl = ThisApplication.CommandManager.ControlDefinitions("AppSaveAllCmd") ctrl.Execute() End Sub
What I need to do is copy the related drawings. My question here is about your structure of code, you´re using a sub for the drawing part. Can I use functions in my rule that I can call in the Main sub() and if I can what options do I have to set that this is working.
best wishes
P.S: Had to delete my first post cause the code was pretty much unreadable. Anyone knows what´s the best method to copy and paste code into this forum?
Falkmassmann,
If your are asking if you can use functions in an iLogic rule, the answer is yes. Just remember that with a Function you have to return something. A Sub you do not. Here's a quick example of using a function in an iLogic rule. Hope that helps.
Sub Main()
Dim MessageValue As Integer = 1
Dim MessageNote As String
MessageNote = TestMessageFunction(MessageValue)
MessageBox.Show(MessageNote)
End Sub
Private Function TestMessageFunction(ByVal MessageValue As Integer) As String
Dim MessageType As String
If MessageValue = 1 Then
MessageType = "Test note 1"
Else
MessageType = "Test note other"
End If
Return MessageType
End Function
Thank you Dan,
that cleared things up.
I´ve run into another minor problem which I can´t resolve.
In my code I´m reading the old displayname and write it back to the copied files because I´ve an iLogic script that controls components in the assembly.
Actually it just links parameters from the main assembly to components in the assembly, if I change displaynames those linked connections won´t work anymore.
When I try to write back the displayname from the main assembly it´s always missing the ".iam" part at the end even if I put it in manually.
It´s not a big issue, I will probably leave it as it is since it won´t affect the link to the parameters.
All the best
Falk
Yes, I noticed that it do, but for some reason the idw won't update parts with the new parts.
All assembly views are updated, but my parts list, and (single part view) is not updated)
But I suppose it's something with some custom configurations on our company's templates.
Thanks for reply 🙂
Hi Danvang,
Your rename and save code looks like a great time saver when updating the file name for a part and drawing.
I've tested your code in a part file (template) no problem with the part file number updating but have been unable to get the drawing file number to update. I'm wondering if there are some other rules that need to be saved to the drawing template or some specific "Copy Model iproperties Settings"?
Thanks in advance
Tony.
Tony,
When I originally wrote this, I didn't recall seeing that the part numbers weren't updating. I'm wondering over the years something changed because the code is simply doing a SAVEAS which if it's done Inventor should update the part number. That or I just over looked it as well. But thanks for point that out. Here is what you can probably do to update the part number in the model and also the part number in the associated drawing. Hope this helps.
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
'get the new filename from the input box
sNewName = InputBox("What is the new file name for " & doc.DisplayName & "?", "New File Name", doc.DisplayName)
'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
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