Save and replace parts with new names I choose in assembly

Save and replace parts with new names I choose in assembly

zaneFBY4D
Explorer Explorer
635 Views
1 Reply
Message 1 of 2

Save and replace parts with new names I choose in assembly

zaneFBY4D
Explorer
Explorer

I have this code where it will save the assembly with the name of my choosing and then auto set names for the parts in the assembly and replace the names of the parts in the assembly with the new auto set names. I want it to do the same thing except I want to be able to choose the name and location of the parts instead of it auto assigning (save as) while still replacing the names in the assembly. 

 

If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
	MsgBox("This rule '" & iLogicVb.RuleName & "' only works for Assembly Documents.",vbOK, "WRONG DOCUMENT TYPE")
	Return
End If

Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument

Dim oFileDlg As Inventor.FileDialog = Nothing
ThisApplication.CreateFileDialog(oFileDlg)
oFileDlg.Filter = "Autodesk Inventor Assembly Files (*.iam)|*.iam"
oFileDlg.InitialDirectory = ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath
oFileDlg.FileName = IO.Path.GetFileName(oADoc.FullFileName).TrimEnd("."c,"i"c,"a"c,"m"c)
oFileDlg.DialogTitle = "Specify New Name & Location For Copied Assembly"
oFileDlg.CancelError = True

On Error Resume Next
oFileDlg.ShowSave
If Err.Number <> 0 Then
	MsgBox("No File Saved.", vbOKOnly, "DIALOG CANCELED")
ElseIf oFileDlg.FileName <> "" Then
	oNewFileName = oFileDlg.FileName
	oADoc.SaveAs(oNewFileName, False)
End If

oADoc = Nothing

InventorVb.DocumentUpdate()

oADoc = ThisApplication.ActiveDocument

Dim oLast3Chars As String
For Each oRefDoc As Document In oADoc.AllReferencedDocuments
	ThisApplication.Documents.Open(oRefDoc.FullFileName,False)
	oLast3Chars = Left(Right(oRefDoc.FullFileName, 7), 3)
	If oRefDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
		oRefDoc.SaveAs(Left(oADoc.FullFileName, Len(oADoc.FullFileName) -4) & oLast3Chars & ".ipt", True)
	ElseIf oRefDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
		oRefDoc.SaveAs(Left(oADoc.FullFileName, Len(oADoc.FullFileName) -4) & oLast3Chars & ".iam", True)
	End If
	oRefDoc.Close
Next

Dim oOccDoc As Document
Dim oOccNewFileName As String
For Each oOcc As ComponentOccurrence In oADoc.ComponentDefinition.Occurrences
	oOccDoc = oOcc.Definition.Document
	oLast3Chars = Left(Right(oOccDoc.FullFileName, 7), 3)
	If oOccDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
		oOccNewFileName = Left(oADoc.FullFileName,Len(oADoc.FullFileName)-4) & oLast3Chars & ".ipt"
	ElseIf oOccDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
		oOccNewFileName = Left(oADoc.FullFileName,Len(oADoc.FullFileName)-4) & oLast3Chars & ".iam"
	End If
	oOcc.Replace(oOccNewFileName, True)
Next

 

0 Likes
636 Views
1 Reply
Reply (1)
Message 2 of 2

yan.gauthier
Advocate
Advocate

to do this, you need a savefile dialog. I recommend using one from windowsAPI.

 

Declare this outside any sub or function

 

Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
    ByVal hwnd As LongPtr, _
    ByVal lpOperation As String, _
    ByVal lpFile As String, _
    ByVal lpParameters As String, _
    ByVal lpDirectory As String, _
    ByVal nShowCmd As Long) As LongPtr
    ' EX: Call reuseLib.ShellExecute(Application.hwnd, "printto", strPathAndFilename, "my printer name", vbNullString, 0)
    
    

    Public Declare PtrSafe Function GetSaveFileName Lib "comdlg32.dll" Alias _
            "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long
      
    Public Type OPENFILENAME
        lStructSize As Long
        hwndOwner As LongPtr
        hInstance As LongPtr
        lpstrFilter As String
        lpstrCustomFilter As String
        nMaxCustFilter As Long
        nFilterIndex As Long
        lpstrFile As String
        nMaxFile As Long
        lpstrFileTitle As String
        nMaxFileTitle As Long
        lpstrInitialDir As String
        lpstrTitle As String
        flags As Long
        nFileOffset As Integer
        nFileExtension As Integer
        lpstrDefExt As String
        lCustData As LongPtr
        lpfnHook As LongPtr
        lpTemplateName As String
    End Type

 

then create a function to return a filename :

 

Public Function SaveMyFile(strTitle As String, strFileName As String, strPath As String) As String

    Dim OpenFile    As OPENFILENAME
    Dim lReturn     As Long
    Dim boolReturn As Boolean
    
    Dim buff As String * 256
    buff = strFileName
    
   
    
    OpenFile.lpstrFilter = "Inventor Files (*.iam;*.ipt)|*.iam;*.ipt|All Files (*.*)|*.*"
    OpenFile.nFilterIndex = 1
    OpenFile.hwndOwner = 0
    OpenFile.lpstrFile = buff 'String(257, 0)
    #If VBA7 Then
        OpenFile.nMaxFile = LenB(OpenFile.lpstrFile) - 1
        OpenFile.lStructSize = LenB(OpenFile)
    #Else
        OpenFile.nMaxFile = Len(OpenFile.lpstrFile) - 1
        OpenFile.lStructSize = Len(OpenFile)
    #End If
    OpenFile.lpstrFileTitle = OpenFile.lpstrFile
    OpenFile.nMaxFileTitle = OpenFile.nMaxFile
    OpenFile.lpstrInitialDir = strPath
    OpenFile.lpstrTitle = strTitle
    OpenFile.flags = 0
    boolReturn = GetSaveFileName(OpenFile)
  
    If Not boolReturn Then
        SaveMyFile = ""
    Else
        SaveMyFile = Trim(Left(OpenFile.lpstrFile, InStr(1, OpenFile.lpstrFile, vbNullChar) - 1))
    End If
  
End Function

 

now you can call this function to get an savefiledialog.

 

Dim filename As String
filename = SaveMyFile("dialogTitle", "FileNameProposition", "%userprofile%\desktop")

n.b.: the third parameter is the path the dialog will start from.

0 Likes