iLogic Design Copy - Programatically

iLogic Design Copy - Programatically

Anonymous
Not applicable
3,463 Views
6 Replies
Message 1 of 7

iLogic Design Copy - Programatically

Anonymous
Not applicable

We find ourselves using the iLogic Design Copy tool a lot in our designs.

 

We use it as a way to generate unique copies of a master assembly.

 

My question is, is there an easy way to programatically use this feature? Or is there a better way to generate copies using a script?

 

Apprecciate any advice,

Ben

0 Likes
3,464 Views
6 Replies
Replies (6)
Message 2 of 7

RodrigoEiras
Advocate
Advocate

 

Hi Ben,

 

I just received this post this morning. I think this is maybe what you need.

 

https://www.cadlinecommunity.co.uk/hc/en-us/articles/204549101

 

Best regards

 

 

 

0 Likes
Message 3 of 7

LukeDavenport
Collaborator
Collaborator

 

Hi Ben and Rodrigo,

Be careful - the code in my blog only performs a 'save as' on the assembly and drawing files, then replaces the reference to the old assembly with the new assembly in the drawing - it isn't a full 'deep copy' of the assembly (renaming and re-referencing all sub-components).

 

If (as I suspect) you do need a full 'deep copy' to create an entirely independent new assembly and drawing, there aren't any simple programmatic options I'm aware of. I'd start here from a user's point of view:

 

http://cadsetterout.com/inventor-tutorials/copy-an-autodesk-inventor-design/

 

Best regards,
Luke

0 Likes
Message 4 of 7

Anonymous
Not applicable

Hi Luke,

 

Yes, I looked at the script and it does not create a deep copy of the assembly, which is what we need.

 

What would you guys reccommend for using master files in a generated assembly, eg; several door assemblies based on the same design.

 

Can iAssemblies be used for the following example?

 

Untitled.png

  • Multiple unique instances of the assembly
  • Each instance may have varying parts (as shown above)
  • All parts generated from master parts

Is this possible with iAssemblies?

 

Cheers,

Ben

0 Likes
Message 5 of 7

Jef_E
Collaborator
Collaborator

Yes, there is, I just made my tool last week. (well i used a really great source and modified it a bit.)

 

You should look at: C:\Users\Public\Documents\Autodesk\Inventor 2014\SDK\UserTools\CopyDesign

This is the code from source, but to better understand it, you should run it in debug.

 

I modified the file saving location part.

 

Public Class Form1

    Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
        ' Get the full path of the folder of the existing design.
        dlgFolderBrowser.Description = "Select path of existing files."
        dlgFolderBrowser.ShowDialog()
        Dim filePath As String
        filePath = dlgFolderBrowser.SelectedPath

        ' Check to see that a path was specified.
        If filePath <> "" Then
            ' Set the text field to the specified path.
            txtDirectoryPath.Text = filePath

            ' Update the displayed path of the new project.
            UpdateNewProjectName()
        End If
    End Sub

    Private Sub txtDirectoryPath_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDirectoryPath.TextChanged
        ' Update the displayed path of the new project.
        UpdateNewProjectName()
    End Sub

    Private Sub txtNewName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtNewName.TextChanged
        ' Update the displayed path of the new project.
        UpdateNewProjectName()
    End Sub

    Private Sub UpdateNewProjectName()
        ' Strip off the last directory from the full path.
        Dim fullPath As String = txtDirectoryPath.Text
        fullPath = fullPath.Substring(0, fullPath.LastIndexOf("\") + 1)

        ' Update the displayed path of the new project.
        lblNewDesignPath.Text = fullPath & txtNewName.Text

        ' Update the pre-pend name for the file names to be copied.
        If chkPrependDesignNameToFileNames.Checked Then
            txtPrependDesignName.Text = txtNewName.Text
        End If
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        ' Disable the buttons while it's processing
        btnOK.Enabled = False
        btnCancel.Enabled = False

        Try
            ' Validate that the specified path for the existing project exists.
            If Not System.IO.Directory.Exists(txtDirectoryPath.Text) Then
                MsgBox("The specified path to the existing design does not exist.")
                Exit Sub
            End If

            ' Check to see if the specified path for the new project exists.
            If System.IO.Directory.Exists(lblNewDesignPath.Text) Then
                If MsgBox("The specified path for the new design already exists.  Any existing files in this directory may be overwritten.  Do you want to continue?", MsgBoxStyle.YesNo + MsgBoxStyle.Question) = MsgBoxResult.No Then
                    Exit Sub
                End If
            Else
                ' Create the output directory.
                Try
                    System.IO.Directory.CreateDirectory(lblNewDesignPath.Text)
                Catch ex As Exception
                    MsgBox("Unable to create the directory for the new design: """ & lblNewDesignPath.Text & """")
                    Exit Sub
                End Try
            End If

            ' Get the name of the existing design by getting the name of the directory from the full path.
            Dim fullPath As String = txtDirectoryPath.Text
            Dim existingDesignName As String = fullPath.Substring(fullPath.LastIndexOf("\") + 1)

            ' Get the name of the new design.
            Dim newDesignName As String = txtNewName.Text

            ' Get the full path of the base directory
            Dim baseDirectoryPath As String = fullPath.Substring(0, fullPath.LastIndexOf("\") + 1)

            ' Get the filenames of any drawings. 
            'Dim drawingFilenames() As String = System.IO.Directory.GetFiles(baseDirectoryPath & existingDesignName, "*.idw")
            Dim idwdrawingFilenames() As String = System.IO.Directory.GetFiles(baseDirectoryPath & existingDesignName, "*.idw")
            Dim dwgDrawingFilenames() As String = System.IO.Directory.GetFiles(baseDirectoryPath & existingDesignName, "*.dwg")

            Dim drawingFilenames(idwdrawingFilenames.Length + dwgDrawingFilenames.Length - 1) As String

            Dim i As Long
            For i = 0 To idwdrawingFilenames.Length - 1
                drawingFilenames(i) = idwdrawingFilenames(i)
            Next

            Dim j As Long
            For j = 0 To dwgDrawingFilenames.Length - 1
                drawingFilenames(idwdrawingFilenames.Length + j) = dwgDrawingFilenames(j)
            Next

            ' Create an instance of Apprentice.
            Dim apprenticeServer As New Inventor.ApprenticeServerComponent

            ' Iterate through the drawing documents.  This assumes that all other documents
            ' are somehow referenced by the drawings.  This can be an indirect reference.  For
            ' example, a part that is used in an assembly that is shown in the drawing is ok.
            Dim currentDrawingFilename As String
            For Each currentDrawingFilename In drawingFilenames
                ' Open the drawing in Apprentice.
                If currentDrawingFilename.Substring(currentDrawingFilename.Length - 3) = "dwg" Then
                    'Skip the file if it's not an Inventor DWG.
                    If Not apprenticeServer.FileManager.IsInventorDWG(currentDrawingFilename) Then
                        Continue For
                    End If

                End If

                Dim drawingDoc As Inventor.ApprenticeServerDocument
                drawingDoc = apprenticeServer.Open(currentDrawingFilename)

                ' Get the FileSaveAs object.
                Dim fileSaveAs As Inventor.FileSaveAs = apprenticeServer.FileSaveAs

                ' Add the drawing to the FileSaveAs object.
                Dim newDrawingFilename As String = drawingDoc.FullFileName
                newDrawingFilename = baseDirectoryPath + newDesignName + "\" + newDrawingFilename.Substring(fullPath.Length + 1)

                ' Prepend design name to filename
                If txtPrependDesignName.Text <> "" Then
                    Dim prependPos As Integer = newDrawingFilename.LastIndexOf("\") + 1
                    newDrawingFilename = newDrawingFilename.Substring(0, prependPos) + txtPrependDesignName.Text + "_" + newDrawingFilename.Substring(prependPos, newDrawingFilename.Length - prependPos)
                End If

                ' Delete the document in case it already exists.
                If System.IO.File.Exists(newDrawingFilename) Then System.IO.File.Delete(newDrawingFilename)

                fileSaveAs.AddFileToSave(drawingDoc, newDrawingFilename)

                ' Copy all the referenced OLE files and re-reference the new copied ones to the drawing document.
                Dim referencedOLEFileDes As Inventor.ReferencedOLEFileDescriptor
                For Each referencedOLEFileDes In drawingDoc.ReferencedOLEFileDescriptors
                    replaceOLEReferencedFiles(referencedOLEFileDes, existingDesignName, newDesignName)

                Next

                ' Get all of the documents referenced by this document.
                Dim allReferencedDocs As Inventor.ApprenticeServerDocuments
                allReferencedDocs = drawingDoc.AllReferencedDocuments

                Dim referencedDoc As Inventor.ApprenticeServerDocument
                For Each referencedDoc In allReferencedDocs
                    ' Get the full filename of the current document.
                    Dim designFilename As String = referencedDoc.FullFileName

                    ' Replace instances of the old design name with the new name.
                    designFilename = baseDirectoryPath + newDesignName + "\" + designFilename.Substring(fullPath.Length + 1)

                    ' Prepend design name to filename
                    If txtPrependDesignName.Text <> "" Then
                        Dim prependPos As Integer = designFilename.LastIndexOf("\") + 1
                        designFilename = designFilename.Substring(0, prependPos) + txtPrependDesignName.Text + "_" + designFilename.Substring(prependPos, designFilename.Length - prependPos)
                    End If

                    ' Check to see if the specified path for the file exists.
                    If Not System.IO.Directory.Exists(designFilename.Substring(0, designFilename.LastIndexOf("\") + 1)) Then
                        ' Create the output directory.
                        Try
                            System.IO.Directory.CreateDirectory(designFilename.Substring(0, designFilename.LastIndexOf("\") + 1))
                        Catch ex As Exception
                            MsgBox("Unable to create the directory for the file: """ & designFilename & """")
                            Exit Sub
                        End Try
                    End If

                    ' Add the current file to the FileSaveAs object.
                    fileSaveAs.AddFileToSave(referencedDoc, designFilename)

                    ' Delete the document in case it already exists.
                    If System.IO.File.Exists(designFilename) Then System.IO.File.Delete(designFilename)

                    ' Copy all the referenced OLE files and re-reference the new copied ones to the drawing document.
                    For Each referencedOLEFileDes In referencedDoc.ReferencedOLEFileDescriptors
                        replaceOLEReferencedFiles(referencedOLEFileDes, existingDesignName, newDesignName)

                    Next
                Next

                ' Do the save copy as of all the files.
                fileSaveAs.ExecuteSaveCopyAs()

                ' Close the drawing.
                For Each referencedDoc In allReferencedDocs
                    referencedDoc.Close()
                Next
                apprenticeServer.Close()
            Next

            MsgBox("Finished copying design.")
        Catch ex As Exception
            MsgBox("Unexpected error while processing files.")
        End Try

        btnOK.Enabled = True
        btnCancel.Enabled = True
    End Sub

    Private Sub chkPrependDesignNameToFileNames_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkPrependDesignNameToFileNames.CheckedChanged
        If chkPrependDesignNameToFileNames.Checked Then
            txtPrependDesignName.Text = txtNewName.Text
        Else
            txtPrependDesignName.Text = ""
        End If

    End Sub

    ' Copy and replace the OLE referenced files
    Private Sub replaceOLEReferencedFiles(ByRef referencedOLEFileDes As Inventor.ReferencedOLEFileDescriptor, ByVal existingDesignName As String, ByVal newDesignName As String)
        ' only linked OLE files will be copied and re-referenced to new design documents.
        If referencedOLEFileDes.OLEDocumentType = Inventor.OLEDocumentTypeEnum.kOLEDocumentLinkObject Then
            ' Get the full filename of the referenced OLE file.
            Dim originDesignOLEFileName As String = referencedOLEFileDes.FullFileName
            Dim designOLEFileName As String = originDesignOLEFileName
            Dim fullPath As String = txtDirectoryPath.Text
            ' Get the full path of the base directory
            Dim baseDirectoryPath As String = fullPath.Substring(0, fullPath.LastIndexOf("\") + 1)


            If System.IO.File.Exists(originDesignOLEFileName) Then
                ' Replace the old design name with new name.
                designOLEFileName = baseDirectoryPath + newDesignName + "\" + designOLEFileName.Substring(fullPath.Length + 1)

                If Not (System.IO.Directory.Exists(designOLEFileName.Substring(0, designOLEFileName.LastIndexOf("\")))) Then
                    System.IO.Directory.CreateDirectory(designOLEFileName.Substring(0, designOLEFileName.LastIndexOf("\")))
                End If

                ' Prepend design name to filename
                If txtPrependDesignName.Text <> "" Then
                    Dim prependPos As Integer = designOLEFileName.LastIndexOf("\") + 1
                    designOLEFileName = designOLEFileName.Substring(0, prependPos) + txtPrependDesignName.Text + "_" + designOLEFileName.Substring(prependPos, designOLEFileName.Length - prependPos)
                End If

                System.IO.File.Copy(originDesignOLEFileName, designOLEFileName, True)

                ' reference to new OLE files.
                referencedOLEFileDes.FileDescriptor.ReplaceReference(designOLEFileName)
            End If
        End If
    End Sub
End Class

 

If you use content center parts (which are not allowed for this code) see this link.

http://forums.autodesk.com/t5/inventor-customization/sdk-copy-design-amp-content-center-parts/m-p/58... 

 

If you need more info just just let me know.



Please kudo if this post was helpfull
Please accept as solution if your problem was solved

Inventor 2014 SP2
Message 6 of 7

Anonymous
Not applicable

Thanks Jef_E,

 

If it can replicate the assembly with new unique part names, then that is exactly what I am looking for.

 

I will try it over the next few days and let you know.

 

Cheers!

 

 

Ben

0 Likes
Message 7 of 7

Jef_E
Collaborator
Collaborator

Well, i hope that it does just that, let me know what the outcome is!

Keep in mind that it needs a .dwg or .idw file where the assembly is on to work.



Please kudo if this post was helpfull
Please accept as solution if your problem was solved

Inventor 2014 SP2
0 Likes