Inventor General

Reply
Valued Contributor
twinz3950
Posts: 66
Registered: ‎02-14-2013
Message 1 of 5 (373 Views)
Accepted Solution

SAVE COPY AND REPLACE WITH I-LOGIC

373 Views, 4 Replies
12-18-2013 07:28 AM

I have searched the forums here but could not really find an answer.  I have an assembly that I am working on that will be used as a configurator, driven by I-logic. Everything is work fine but there is one part off the assembly that will be custom to each new assembly.  Is there a way to use I logic  to open the ipt file save it as copy with different name and replace the original in the assembly. I know this can be done manual through the productivity tab within the assembly environment, but I am trying to automate the process so the end user will just have to follow prompts that will be given by i-logic.

Valued Contributor
twinz3950
Posts: 66
Registered: ‎02-14-2013
Message 2 of 5 (354 Views)

Re: SAVE COPY AND REPLACE WITH I-LOGIC

12-18-2013 04:17 PM in reply to: twinz3950

OK I have this code but I do not need it to go  through all the parts, is there any way to modify this so that it only looks for a part call "shell:1" with in the assembly?

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

 

Distinguished Mentor
pcrawley
Posts: 666
Registered: ‎05-23-2002
Message 3 of 5 (321 Views)

Re: SAVE COPY AND REPLACE WITH I-LOGIC

12-23-2013 12:47 PM in reply to: twinz3950

I'm not in front of Inventor right now, so I can't check any of this, but using my (badly) calibrated eyeball, try the following.  (I note that the code references "display name" rather than filename - so if you rename anything in your browser, this is likey to fail!)

 

 

InvDoc = ThisDoc.Document

 Dim refDocs As DocumentsEnumerator = InvDoc.AllReferencedDocuments

 Dim refDoc As Document

 

 For Each refDoc in refDocs

 'MessageBox.show(refDoc.DisplayName)


If refDoc.DisplayName = "shell:1" then

'This will find only "shell:1" and execute all the following code.  

'If the refDoc.DisplayName is not "Shell:1" it'll jump to the red "End If" and proceed to check the next component.

 

 '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 a 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

   End If

 Next

 

'Not sure if you need any of the following as it appears to be building new sub-assemblies - and you are only replacing a part.

'If you don't need it, comment it out using a  '  at the beginning of each line.

 

' 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 'I'm not sure about this line.  

'It could exit the iLogic code at this point.  I've only ever used it to end a sub-routine, but none has been defined.  

'Someone who understands code better than me will tell you what it does!

 

'And this next part is to do with Drawings?  Again, you can probably ditch this too.

 

Private Sub UpdateDrawing (ByVal Path As StringByVal oldName As StringByVal 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

 

Peter
Valued Contributor
twinz3950
Posts: 66
Registered: ‎02-14-2013
Message 4 of 5 (312 Views)

Re: SAVE COPY AND REPLACE WITH I-LOGIC

12-23-2013 04:26 PM in reply to: twinz3950

thank you so much :smileyhappy:

Distinguished Mentor
pcrawley
Posts: 666
Registered: ‎05-23-2002
Message 5 of 5 (303 Views)

Re: SAVE COPY AND REPLACE WITH I-LOGIC

12-23-2013 05:37 PM in reply to: twinz3950

Wow - you mean it worked?!!

Hope the rest of the project worked out too.

 

Glad I could help.

Peter

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community


Inventor Exchange Apps

Created by the community for the community, Autodesk Exchange Apps for Autodesk Inventor helps you achieve greater speed, accuracy, and automation from concept to manufacturing.

Connect with Inventor

Twitter

Facebook

Blogs

Pinterest

Youtube