Save and replace parts in an assembly using iLogic

Save and replace parts in an assembly using iLogic

gcooper79
Advocate Advocate
8,997 Views
30 Replies
Message 1 of 31

Save and replace parts in an assembly using iLogic

gcooper79
Advocate
Advocate

Hi,

 

I posted this in the wrong forum yesterday so reposting here hoping someone can help.

 

I've just started experimenting with ilogic and I would like to understand how to perform the following task.

 

I have a master assembly called PS07-01 and multiple parts in the assembly called PS07-01-01, PS07-01-02 etc..

 

I would like to save a version of the assembly as a different name e.g. PS07-02 and when I do, the associated parts would also save with a new name PS07-02-01, PS07-02-02 etc..

 

I have looked through the forum and the closest code I could find was the one below by @CurtisWaguespack .  However, this code only worked if I selected the parts, I would rather it was automated.

 

Any help would be gratefully appreciated.

 

Graeme

 

On Error Resume Next
oSelect = ThisDoc.Document.SelectSet.Item(1)

'catch an empty string in the imput
If Err.Number <> 0 Then
MessageBox.Show("Please select a component before running this rule.", "iLogic")
Return
End If

' get a component by name.
Dim compOcc = Component.InventorComponent(oSelect.name) 


'define document
oDoc = compOcc.Definition.Document
'create a file dialog box
Dim oFileDlg As Inventor.FileDialog = Nothing
InventorVb.Application.CreateFileDialog(oFileDlg)

'check file type and set dialog filter
If oDoc.DocumentType = kPartDocumentObject Then
oFileDlg.Filter = "Autodesk Inventor Part Files (*.ipt)|*.ipt"
Else If oDoc.DocumentType = kAssemblyDocumentObject Then
oFileDlg.Filter = "Autodesk Inventor Assembly Files (*.iam)|*.iam"
End If

'set the directory to open the dialog at
oFileDlg.InitialDirectory = ThisDoc.WorkspacePath()
'set the file name string to use in the input box
oFileDlg.FileName = System.IO.Path.GetFileName(oDoc.FullFileName)

'work with an error created by the user backing out of the save
oFileDlg.CancelError = True
On Error Resume Next
'specify the file dialog as a save dialog (rather than a open dialog)
oFileDlg.ShowSave()

'catch an empty string in the imput
If Err.Number <> 0 Then
MessageBox.Show("No File Saved.", "iLogic: Dialog Canceled")
ElseIf oFileDlg.FileName <> "" Then
MyFile = oFileDlg.FileName
'save the file
oDoc.SaveAs(MyFile, False) 'True = Save As Copy & False = Save As
End If

 

 

Regards,
Graeme
0 Likes
Accepted solutions (1)
8,998 Views
30 Replies
Replies (30)
Message 21 of 31

WCrihfield
Mentor
Mentor

It's a little hard to tell from that error message what the problem is, but it has a lot of "System.IO" stuff in there, so I'm betting it doesn't like the line in my code where I'm specifying the "oFileDlg.FileName = ", because I'm using a "System.IO.Path.GetFileName()" function.  All I'm doing in that line is getting the file name of the active assembly document, without the path, and without the file extension.  There are many other ways to do this.  I don't recall why I chose to do it that way.

Try replacing the line:

oFileDlg.FileName = IO.Path.GetFileName(oADoc.FullFileName).TrimEnd("."c,"i"c,"a"c,"m"c)

with one of these:

oFileDlg.FileName = ThisDoc.FileName(False)
oFileDlg.FileName = Left(oADoc.FullFileName,Len(oADoc.FullFileName)-4)
oFileDlg.FileName = oADoc.FullFileName.Split("\")(oADoc.FullFileName.Split("\").Length-1).TrimEnd(".","i","a","m")

There are still several other ways to do this, but you get the idea.

As far as the last 4 lines of the error message, it looks like you were attempting add or run an external rule, and it something went wrong.  I'm not sure what that is about, but some people have a problem with the "iLogicVb.RuleName" within the first message in the rule.  You can try just deleting that whole portion of that message, and see if that helps.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 22 of 31

Anonymous
Not applicable

Thanks for responding! Unfortunately, the information you have provided did not correct the error message. I also noticed that when I run the rule and it generates a new assembly with the subassemblies having new part numbers but the parts in those subassembly still have the same part number as the original model. This being said, I want to change the sizes of the parts on the new assembly, without it changing the size on the original assembly. Hope this makes sense.

0 Likes
Message 23 of 31

WCrihfield
Mentor
Mentor

What operating system are you using? What year and version (LT, normal, Pro) of Inventor are you using?

Is the code in this post the only code in your rule, or is there more/other code within your rule?

Maybe try some error tracing techniques to see where the error is happening within the code.

If you don't have 2019 or later, you may not be able to use the iLogic logger, but you could do something simple like inserting a bunch of MsgBox(1) & MsgBox(2) & MsgBox(3) etc throughout the code, then when you run it, pay attention to the last number message it shows before it errors out.  This should give us a better idea where the error is happening for you.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 24 of 31

Anonymous
Not applicable

I am using Windows 10, 64-bit operating system with Inventor Professional 2019. I honestly am considering on creating a new rule and starting over from scratch. The over all goal I am trying to achieve is:

 

1. I want to make a master Assembly that contains subassemblies made up of created parts and content center parts.

2. I want to be able to save a copy of the master assembly in a new folder with a new name, and have all the subassemblies and parts reflect the new naming.

3. I want to be able to adjust the new copied assembly to certain projects (changing length, width, height, etc.) without it changing the master assembly.

4. Basically my goal is to make a template assembly to save time. Right now it takes me about two days to create a full model, and I want to create rules to were I can have a basic version of a model and be able to save a copy and adjust it without interfering with other versions.

 

Hope this helps and gives you a better idea of what I'm looking/hoping to do.

Message 25 of 31

WCrihfield
Mentor
Mentor

I believe I understand what you want, but it is a large and complex process.

Perhaps it would be best to make this request in a new post of your own, with a title something like 'how to copy an assembly', or something similar.  This is similar to a frequently requested ability.  I believe some people have made addins for this process.  There are many steps involved.  And the process of iterating down through an unknown number of levels of sub assemblies, itself can be a complex code to create.  I don't currently have a ready-made solution of my own to offer that would do what you are asking for, but I'm sure if you create your own post for it, there will be more people chiming in to help you out on this.  Good luck. 🙂

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 26 of 31

Anonymous
Not applicable

Thank you for all the information that you have provided. Yeah I think that is what I am going to have to do is create a different post and hope someone has a method to my madness lol.

0 Likes
Message 27 of 31

nvendeiro
Participant
Participant

I am developing a rule that can insert a parameterized assembly from an excel file into a given assembly.
In other words, the rule is executed, asks us to select the INTENDED model, will open the base assembly and will parameterize it with the data coming from excel.
Later, you should save it in a specific folder.

Note: If this model already exists, you should detect it and automatically place it in the assembly instead of creating it again.
I also intended that if we selected the CYLINDER and executed the RULE, it would be possible to change the model.

Example:
Model: 1000
Assembly: 1000.iam

Parts:
1) Cylinder Body: 1000(1) .ipt
2) Rod: 1000(2) .ipt

The parts must be put inside a folder at the same level of the assembly.

0 Likes
Message 28 of 31

aronmatheus
Advocate
Advocate

Hi @WCrihfield, I'm using this ruler. I have 10 parts, but 2 parts aren't saving in a folder.

 

'define the active document
oDoc = ThisDoc.Document

CurrentFileName = ThisDoc.PathAndFileName(False)

'create a file dialog box
Dim oFileDlg As Inventor.FileDialog = Nothing
InventorVb.Application.CreateFileDialog(oFileDlg)

Dim Filetype As String

'check file type and set dialog filter
If oDoc.DocumentType = kPartDocumentObject Then
oFileDlg.Filter = "Autodesk Inventor Part Files (*.ipt)|*.ipt"
Filetype = ".ipt"
ElseIf oDoc.DocumentType = kAssemblyDocumentObject Then
oFileDlg.Filter = "Autodesk Inventor Assembly Files (*.iam)|*.iam"
Filetype = ".iam"
ElseIf oDoc.DocumentType = kDrawingDocumentObject Then
oFileDlg.Filter = "Autodesk Inventor Drawing Files (*.idw)|*.idw"
Filetype = ".idw"
End If

'set the directory to open the dialog at
oFileDlg.InitialDirectory = ThisDoc.WorkspacePath()
'set the file name string to use in the input box
oFileDlg.FileName = ThisDoc.FileName(False) 'without extension

'work with an error created by the user backing out of the save
oFileDlg.CancelError = True
On Error Resume Next
'specify the file dialog as a save dialog (rather than a open dialog)
oFileDlg.ShowSave()

'catch an empty string in the imput
If Err.Number <> 0 Then
'MessageBox.Show("No File Saved.", "iLogic: Dialog Canceled")
ElseIf oFileDlg.FileName <> "" Then
MyFile = oFileDlg.FileName
'save the file
oDoc.SaveAs(MyFile, False) 'True = Save As Copy & False = Save As

NewDocPathName = ThisDoc.PathAndFileName(False)

'open original drawing
oDestinationDoc = ThisApplication.Documents.Open(CurrentFileName & ".idw")
oDestinationDoc.saveas(NewDocPathName & ".idw",False)
oDestinationDoc.Close

'open new drawing
oDestinationDoc = ThisApplication.Documents.Open(NewDocPathName & ".idw")
Dim oDocDescriptor As DocumentDescriptor
oDocDescriptor = oDestinationDoc.ReferencedDocumentDescriptors.Item(1)

Dim oFileDescriptor As FileDescriptor
oFileDescriptor = oDocDescriptor.ReferencedFileDescriptor

oFileDescriptor.ReplaceReference(NewDocPathName & Filetype)
oDestinationDoc.Update()
oDestinationDoc.Save
End If
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
Dim oRefPDoc As PartDocument = oRefDoc
Dim oRefPDef As PartComponentDefinition = oRefPDoc.ComponentDefinition
If oRefPDef.IsContentMember = False Then
oRefDoc.SaveAs(Left(oADoc.FullFileName, Len(oADoc.FullFileName) -4) & oLast3Chars & ".ipt", False)
End If
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
Dim oOccPDef As PartComponentDefinition = oOcc.Definition
If oOccPDef.IsContentMember = False Then
oOccNewFileName = Left(oADoc.FullFileName, Len(oADoc.FullFileName) -4) & oLast3Chars
End If
End If
oOcc.Replace(oOccNewFileName, False)
Next

0 Likes
Message 29 of 31

meikel.feisel
Explorer
Explorer

Hello,

 

I am sorry for digging out this post after so long but I've stumbled across a similar Problem.

 

Your code worked pretty fine in my assembly but I have some parts in my assembly which are not active.

They are disabled with the ilogic rule "Is.Active=false"

 

Is there a possibility to leave deactivated parts out when re-saving the assembly? Currently when I save my file it also saves the deactivated files and makes them visible again which leads to a lot of parts that shouldn't be in the assembly.

 

Thanks in advance!

0 Likes
Message 30 of 31

m.rymut
Advocate
Advocate

Hi,

is there a simple way to use this rule, but to change the way this rule changes names?

I have files with same prefix, and i would like to change the prefix in the new assembly.

For example, i have an assembly:

ER-1_1.1(A)_Main_assembly

ER-1_1.1(A)_Part_1

ER-1_1.1(A)_Part_2

ER-1_1.1(A)_Part_3

 

And i would like to copy and replace with other prefix like this:

ER-2_1.1(A1)_Main_assembly

ER-2_1.1(A1)_Part_1

ER-2_1.1(A1)_Part_2

ER-2_1.1(A1)_Part_3

 

I have tried to change your code  and change names using those lines:

Old_prefix = InputBox("Please enter old prefix", "Warning", "")
New_prefix = InputBox("Please enter new prefix", "Warning", "")
Replace(oADoc.FullFileName, Old_prefix, New_prefix)

i tried inputing it here:

oRefDoc.SaveAs(Replace(oADoc.FullFileName, Old_prefix, New_prefix) & ".ipt", True)

But i cant get it to work.

Any tips?

0 Likes
Message 31 of 31

layochim
Enthusiast
Enthusiast

This worked for me as well!  Instead of using the last 3 characters of the parts i'd like to save them from "Left" using the first 10 characters, i've attempted modifying the code but no success.  Any solution for this?

0 Likes