iLogic, Save Placed Part in Active Assembly File Location

iLogic, Save Placed Part in Active Assembly File Location

Anonymous
Not applicable
1,702 Views
4 Replies
Message 1 of 5

iLogic, Save Placed Part in Active Assembly File Location

Anonymous
Not applicable

I couldn't seem to find exactly what I was looking for. For Piping, I am trying to insert an adaptive pipe part from a part library into a top level assembly and then, with iLogic, save the new Pipe part in the same location as the assembly (not the original pipe part location which is located in a custom parts library).

 

What I have so far works perfectly as far as automatically naming the file and everything, but it is saving to a subfolder where the original Pipe part is located. How can I get the top assembly file location and save there instead?

 

I have found similar posts, but I couldn't quite get those suggestions to work for me. Some guidence would be much appreciated. Below is the code I have so far.

 

Thanks

Dim oNewFileName As String
Dim oNewFileNameAndPath As String
Dim oPipeSuffix As Integer = 1

'Get active document path. In this case, the Custom Part Library part file, not the top assembly file location...
'How do I get the active assembly location which I am placing the part???
oActiveFileLoc=ThisDoc.Path

'Initialize File Name and Path
oNewFileName = "P" & oPipeSuffix
oNewFileNameAndPath = oActiveFileLoc & "\Pipe\" & oNewFileName & ".ipt"

'Check if File name already exists, increment suffix until filename is available (e.g. P1 x 4.5, P2 x 4.5, etc)
While System.IO.File.Exists(oNewFileNameAndPath)
		oPipeSuffix = oPipeSuffix + 1
		oNewFileName = "P" & oPipeSuffix
		oNewFileNameAndPath = oActiveFileLoc & "\Pipe\" & oNewFileName & ".ipt"
End While

'For debugging
MsgBox("File location" & vbLf & oActiveFileLoc & vbLf & vbLf & "File name" & vbLf & oNewFileName)

'Save document with the new available file name - Will later be set to "False" (Save as/load new).
ThisDoc.Document.SaveAs(oNewFileNameAndPath, True)
0 Likes
1,703 Views
4 Replies
Replies (4)
Message 2 of 5

rjay75
Collaborator
Collaborator

The reason you are saving in the current location of the assembly is your are performing all your steps in the context of the part you're saving and placing. To stay in the context of the assembly start the rule from the assembly. "ThisDoc.Path" always refers to the document the rule is run from.

 

I've created a blog post demonstrating this by selecting a component document, saving it as a new document, placing the new document in the active assembly.

 

To use this for specifically for the type of files you're working with change the portion

 

        Dim newCompName As String = _
GetFileNameDialog(ThisDoc.Path, "Select location to save component.", True)

 

to set newCompName to your own file naming method.

 

0 Likes
Message 3 of 5

Anonymous
Not applicable

Thanks rjay! I have not yet had a chance to try out your code.... So the idea is that you write the rule at the assembly level and then run that rule anytime you want to place a part in this manner? Do you by any chance have a video or step-by-step how to implement and utilize this rule? Maybe it will be more clear once I get some time back on this to test it out.

 

Anyway, that sounds pretty close to what I'm trying to do. Ideally though, the behavior I was looking for was to be able to place the part as any other (with the Place command), select the parameter values with a popup form, then with iLogic "create" the part (so to speak) and save that active copy into the top assembly folder.

 

Upon further inspection, I found that the code I posted doesn't work exactly as I described - It works when editing at the component level, but not when Placing in the assembly. I have a threaded nipple that would have been a better example. I'll attach that part below for reference.

 

When I get the opportunity, I will try out your rule and let you know if it is what I am looking for.

 

Thanks again!

0 Likes
Message 4 of 5

rjay75
Collaborator
Collaborator

What you describe is the work flow of an iPart with custom columns in a factory. However your dialog is just a little bit more sophisticated and implemented easier using a form. So I modified the ilogic code to watch for an occurrence to placed while running and run a rule.

 

In your nipple model add a rule named "Show_Config". Add this to the text of the rule.

 

iLogicForm.Show("Configuration")

 

This will allow you to display the form by calling the rule from another model.

 

Here's the code modified to run the rule "Show_Config" after placing a part the first time.

 

 

SyntaxEditor Code Snippet

Sub Main()
    'Check to make sure in an assembly or exit.
    If Not ThisDoc.Document.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then Return
    'Get Location of existing component.
    Dim compFileName As String = GetFileNameDialog(Nothing, "Select component to place.", False)
    'Check to see if a file has been selected.    
    If Not String.IsNullOrEmpty(compFileName)
        'Get new File Name of part to be saved and placed.
        Dim newCompName As String = GetFileNameDialog(ThisDoc.Path, "Select location to save component.", True)
        'Check to see if save location has been selected.    
        If Not String.IsNullOrEmpty(newCompName)
            'Open old document.
            Dim compFileDoc As Document = ThisApplication.Documents.Open(compFileName, False)
            'SaveCopyAs of document to new compont document.
            compFileDoc.SaveAs(newCompName, True)
            'Close old component document.(Skip Save)
            compFileDoc.Close(True)
            'Place new component in Assembly.
            'Set Event to get last placed component
            Dim asmEvents As AssemblyEvents = ThisApplication.AssemblyEvents
            'Set Handler what to do on new occurrence
            AddHandler asmEvents.OnNewOccurrence, AddressOf asm_NewOccurrence
            ThisApplication.CommandManager.PostPrivateEvent(PrivateEventTypeEnum.kFileNameEvent, newCompName)
            Dim oCtrlDef  As ControlDefinition = ThisApplication.CommandManager.ControlDefinitions.Item("AssemblyPlaceComponentCmd")
            oCtrlDef.Execute()
            RemoveHandler asmEvents.OnNewOccurrence, AddressOf asm_NewOccurrence
        End If
    End If
End Sub

'Event Handler to call rule on new placement.
Sub asm_NewOccurrence(DocumentObject As AssemblyDocument, Occurrence As ComponentOccurrence, _
    BeforeOrAfter As EventTimingEnum, Context As NameValueMap, ByRef HandlingCode As HandlingCodeEnum)
    If BeforeOrAfter = EventTimingEnum.kAfter Then
        iLogicVb.RunRule(Occurrence.Name, "Show_Config")
        HandlingCode = HandlingCodeEnum.kEventHandled
    Else
        HandlingCode = HandlingCodeEnum.kEventNotHandled
    End If
End Sub

'Utility Function to get the name of a component
Function GetFileNameDialog(startDir As String, dlgTitle As String, doSave As Boolean) As String
    Dim fileName As String
    ' Create a new FileDialog object.
    Dim oFileDlg As Inventor.FileDialog = Nothing
    'Create Inventor's File Dialog
    ThisApplication.CreateFileDialog(oFileDlg)
    ' Define the filter to select part and assembly files or any file.
    oFileDlg.Filter = "Inventor Files (*.ipt)|*.ipt"
    ' Define the part and assembly files filter to be the default filter.
    oFileDlg.FilterIndex = 1
    ' Set the title for the dialog.
    oFileDlg.DialogTitle = dlgTitle
    ' Set the initial directory that will be displayed in the dialog.
    oFileDlg.InitialDirectory = If(Not String.IsNullOrEmpty(startDir) And System.IO.Directory.Exists(startDir), _
        startDir, ThisDoc.Path)
    ' Set the flag so an error will be raised if the user clicks the Cancel button.
    oFileDlg.CancelError = True
    ' Show the open dialog.  The same procedure is also used for the Save dialog.
    ' The commented code can be used for the Save dialog.
    Try
        If(doSave) Then
            oFileDlg.ShowSave()
        Else
            oFileDlg.ShowOpen()
        End If
    Catch
        'MessageBox.Show("User cancelled out of dialog", "Replace Reference")
    Finally
        fileName = oFileDlg.FileName
    End Try
    Return fileName
End Function

This uses event handlers to watch for the placement of a part and react accordingly.

0 Likes
Message 5 of 5

rjay75
Collaborator
Collaborator

This is a something I never considered before but I can find it useful. I originally attempted to modify your Nipple model to be an iPart. But it's slightly more sophisticated than that. Then I saw that configuration upon placing could be useful.

 

Here's a new blog post I made cleaning up the code so it can be used with any type of model.

 

Hope it's useful.

0 Likes