Saving Imported Files

Saving Imported Files

NachoShaw
Advisor Advisor
465 Views
6 Replies
Message 1 of 7

Saving Imported Files

NachoShaw
Advisor
Advisor

Hey

 

Looking for some pointers. I am importing files file from a step file. As they load into Inventor, a folder is automatically created called 'Imported Components' and all of the temporary files are loaded into it.  This is not the destination location for the files, they need to be in another folder called Inventor/Components. I set up a method to traverse the assembly and save all of the components to the new location with a filepath using SaveAs(SavePath, False) and then replace the current occurrence in the assembly with the newly saved version. However its not working as expected.

 

I added a method to delete any files that may already exist in the destination folder first to prevent any issue that i had where it wouldnt overwrite an existing file.

 

The files appear to be saving but not replacing in the assembly. Furthermore, if my assembly has more than one occurrence it fails on the replace. I have tried oOcc.Replace(Savepath, False) & oOcc.Replace(Savepath, True) both of which throw an error. I trapped the error and the component doesnt replace.

 

Also worth noting, the Assembly is the last object to be saved

 

Does anyone have a working save method that would fit this instance?

 

 

Thanks

 

 

Nacho
Automation & Design Engineer

Inventor automation Programmer (C#, VB.Net / iLogic)
Furniture, Sheet Metal, Structural, Metal fab, Tradeshow, Fabrication, CNC

EESignature


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.


0 Likes
466 Views
6 Replies
Replies (6)
Message 2 of 7

NachoShaw
Advisor
Advisor

UPDATE

 

I have also tried this which doesnt replace the reference.

 

For Each oFD As Inventor.FileDescriptor In CurrDoc.File.ReferencedFileDescriptors
                    If oFD.FullFileName = oDoc.FullFileName Then
                        oFD.ReplaceReference(SavePath)
                        CurrDoc.Update2()
                        Exit For
                    End If
                Next

The file is saved to to the correct location using Savepath, i have double checked.

The condition of oFD.FullFileName = oDoc.FullFileName is met, i went through each iteration

 

Nacho
Automation & Design Engineer

Inventor automation Programmer (C#, VB.Net / iLogic)
Furniture, Sheet Metal, Structural, Metal fab, Tradeshow, Fabrication, CNC

EESignature


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.


0 Likes
Message 3 of 7

NachoShaw
Advisor
Advisor

Hey

 

My Current code is this

Public Sub SaveComponents(CurrDoc As Inventor.AssemblyDocument)
        UpdateProcessList("Saving Components", True, False)
        Call TraverseAssemblySave(CurrDoc.ComponentDefinition.Occurrences)
    End Sub

    Private Sub TraverseAssemblySave(Occurrences As Inventor.ComponentOccurrences, Optional ByRef SubAssy As Inventor.AssemblyDocument = Nothing)
        Dim CurrDoc As AssemblyDocument = _invApp.ActiveEditDocument

        For Each oOcc As Inventor.ComponentOccurrence In Occurrences
            Dim oDoc As Inventor.Document = oOcc.ReferencedFileDescriptor.ReferencedDocument
            Dim oPropSets As Inventor.PropertySets = oDoc.PropertySets
            Dim oPropSet As Inventor.PropertySet = oPropSets.Item("Inventor User Defined Properties")
            Dim oPropCompType As Inventor.Property = oPropSet.Item("ComponentType")
            Dim CompType As String = oPropCompType.Value

            Dim BrowserName As String = oOcc.Name
            Dim Result As String = BrowserName.Split(":"c)(0)

            Dim FileType As String = String.Empty
            Select Case oDoc.DocumentType
                Case Inventor.DocumentTypeEnum.kAssemblyDocumentObject
                    FileType = ".iam"
                Case Inventor.DocumentTypeEnum.kPartDocumentObject
                    FileType = ".ipt"
            End Select

            Dim SavePath As String = PathRoot & ComponentTypeName(CompType) & "\" & Result & FileType

            _invApp.SilentOperation = True
            Try
                DeleteIfExists(SavePath)
                oDoc.SaveAs(SavePath, True)
                oDoc.Close()
            Catch ex As Exception

            End Try

            Dim SaveAssy As Inventor.AssemblyDocument = Nothing
            If SubAssy Is Nothing Then
                SaveAssy = SubAssy
            Else
                SaveAssy = CurrDoc
            End If
            Try
                Stop
                Dim oFD As Inventor.FileDescriptor = CurrDoc.File.ReferencedFileDescriptors.Item(oDoc.FullFileName)
                oFD.ReplaceReference(SavePath)
                CurrDoc.Update2()

            Catch ex As Exception
            End Try

            _invApp.SilentOperation = False

            If oOcc.DefinitionDocumentType = Inventor.DocumentTypeEnum.kAssemblyDocumentObject Then
                Call TraverseAssemblySave(oOcc.SubOccurrences)
            End If
        Next
    End Sub

 

I am able to replace the components, but only at top level. This is most likely because i need to access tthe sub level assemblies to replace their components. Problem is, i can see how to access them using the code above? This is the TraverseAssembly method from the API and when recalling the method with oOcc.SubOccurrences, it doesnt pass the assembly with it. I tried passing the assembly but it didnt really work that well.

 

Is the the right approach or should i attempt a different method of replacing the components? I need the Occurrence.Name in order to get the save path (its added early in the process to the browsernode)

 

thanks

Nacho
Automation & Design Engineer

Inventor automation Programmer (C#, VB.Net / iLogic)
Furniture, Sheet Metal, Structural, Metal fab, Tradeshow, Fabrication, CNC

EESignature


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.


0 Likes
Message 4 of 7

NachoShaw
Advisor
Advisor

Update

 

i added the assembly document to the traverse assembly in order to process sub assembly components

 

If oOcc.DefinitionDocumentType = Inventor.DocumentTypeEnum.kAssemblyDocumentObject Then
Dim RefDoc As AssemblyDocument = oOcc.Definition.Document
                Call TraverseAssemblySave(RefDoc, oOcc.SubOccurrences)
            End If

 

That appears to resolve the save sub component issue. however i still have an issue....

 

If i save the parent assembly first, i am able to save all of the components to the destination folder and replace in the Parent Assembly so that once the process has finished, all of the assembly components have been replaced with the correct components.

 

If i save the components firsts using the exact same method but leave the parent assembly save until last, all of the components remain the the origin folder and none of them are replaced. Why?

 

When i step through the replace process, the paths are all correct and there are no errors so why would the reference file be different? The components are still saved to the destination folder, just not replaced..

 

This issue introduces 2 save processes into my method. Save01 at the beginning, save02 at the end which adds a good minute to the total process time.

 

Lastly

 

if i have multiple occurrences of the same component, it becomes read only.. What is the best way to handle this?

 

save.jpg

 

Thanks

Nacho
Automation & Design Engineer

Inventor automation Programmer (C#, VB.Net / iLogic)
Furniture, Sheet Metal, Structural, Metal fab, Tradeshow, Fabrication, CNC

EESignature


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.


0 Likes
Message 5 of 7

NachoShaw
Advisor
Advisor

I guess the question is, Do any of these work if the assembly has not yet been saved or does the assembly need to be saved in order for these to work?

Dim oFD As Inventor.FileDescriptor = oAsmDoc.File.ReferencedFileDescriptors.Item(oDoc.FullFileName)
oFD.ReplaceReference(SavePath)
oAsmDoc.Update2()
For Each oFD As Inventor.FileDescriptor In oAsmDoc.File.ReferencedFileDescriptors
   If oFD.FullFileName = oDoc.FullFileName Then
      oFD.ReplaceReference(SavePath)
      oAsmDoc.Update2(True)
      Exit For
   End If
Next

 If i save first, i get a folder full of components i dont need, if i dont save first, nothing happens

 

Thanks

 

Nacho
Automation & Design Engineer

Inventor automation Programmer (C#, VB.Net / iLogic)
Furniture, Sheet Metal, Structural, Metal fab, Tradeshow, Fabrication, CNC

EESignature


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.


0 Likes
Message 6 of 7

WCrihfield
Mentor
Mentor

It kind of sounds like you could use the "iLogic Design Copy" tool to do what you're doing.

Have you ever used it before?

To access it, close all Inventor documents, but leave Inventor open. Then it's on the Tools tab / iLogic panel.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 7 of 7

NachoShaw
Advisor
Advisor

Hi

 

The Design Copy tool is too clumbsy and tedious to use. Also it wouldnt fit what i am doing which is-

 

  • import step into inventor
  • get an idNumber that is stored within the file name
  • traverse a large XML document and get all properties based on the idNumber
  • Assign a material that is part of the XML data
  • Rename the file based on an XML property data
  • save to a new location

 

its a lot more involved than that but its the general idea

 

Thanks

 

Nacho
Automation & Design Engineer

Inventor automation Programmer (C#, VB.Net / iLogic)
Furniture, Sheet Metal, Structural, Metal fab, Tradeshow, Fabrication, CNC

EESignature


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.


0 Likes