Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

What is the correct syntax for Component.Replace?

14 REPLIES 14
SOLVED
Reply
Message 1 of 15
Anonymous
1775 Views, 14 Replies

What is the correct syntax for Component.Replace?

Here is what I was told is the correct way to access Inventor's "Replace Component" command: ]

 

Component.Replace(OldNamePath, NewNamePath, False)

 

But I'm getting a syntax error.

 

What is the correct syntax? I'm not seeing it in the Inventor VBA library...

 

OldNamPath and NewNamePath are from different modules that create the full path name.

 

The error I'm getting is the "Expected =" error...

14 REPLIES 14
Message 2 of 15
mslosar
in reply to: Anonymous

That's the syntax for iLogic I believe.

 

There's a ComponentOccurence.Replace available per the programming help file.

 

It also contains examples for adding a new occurence and manipulating existing occurences.

Message 3 of 15
rjay75
in reply to: Anonymous

Are you doing this from iLogic or a VBA macro. If you're doing it from iLogic the command is Component.Replace but the parameters are:

 

Component.Replace(componentName, newPath, False) with componentName being the name of the component in the assembly.

 

If you're doing it from a VBA macro you need to set the ComponentOccurrance object of the component you want to change.

 

Dim occ as ComponentOccurrance

Set occ = (the component occurrance object you want to replace)

 

//To replace

occ.Replace(newPath, False)

Message 4 of 15
Anonymous
in reply to: mslosar

And that does the same thing as this?

 

Replace Component.jpg

Message 5 of 15
Anonymous
in reply to: rjay75

It's from the VBA! Thanks!

 

It has the same syntax error....the expected =....do you know why?

Message 6 of 15
mslosar
in reply to: Anonymous

If it works the same way as the ilogic version, it's trying to find the occurencename, not the filename.

 

If you insert Part01.ipt into an assembly, it'll show an occurnencename of Part01:1.

 

If you use the menus (or ilogic) and replace that component with Part02.ipt, it will change the occurence name to Part02:1.

 

To keep the component name the same, you need to rename the occurence after inserting the part. Just click twice to allow the reaname and call it something unique, like myPart:1. When you do that, regardless of what part you replace it with, the occurence name will rename myPart:1 and you can easily replace it with code.

Message 7 of 15
mslosar
in reply to: mslosar

Here you go...

 

Public Sub CReplace()

Dim oOccurrence As ComponentOccurrence
Set oOccurrence = ThisApplication.ActiveDocument.ComponentDefinition.Occurrences.Item(1)
oOccurrence.Replace "<filename>", True


End Sub

 

This works. You'll just need to find the item number of the component you're looking to replace.

Message 8 of 15
rjay75
in reply to: mslosar

Here's another way to do it from VBA as well if you know the filename of the component.

 

'Using just file paths
Sub ReplaceComponentByPath(oldPath As String, newPath As String)
  For Each occ In ThisApplication.ActiveDocument.ComponentDefinition.Occurrences
    If occ.ReferencedDocumentDescriptor.FullDocumentName = oldPath Then
      occ.Replace newPath, True 'Replaces all occurences
      Exit For
    End If
  Next occ
End Sub

'Using occurnce name and path
Sub ReplaceComponentByName(occName As String, newPath As String)
  For Each occ In ThisApplication.ActiveDocument.ComponentDefinition.Occurrences
    If occ.Name = occName Then
      occ.Replace newPath, True 'Replaces all occurences
      Exit For
    End If
  Next occ
End Sub

 These will loop through the component to replace all.

Message 9 of 15
Anonymous
in reply to: rjay75

None of these fully work, but they headed me towards the right path

Message 10 of 15
rjay75
in reply to: Anonymous

What exactly was your workflow? It looks like you're trying to replace a series of components that have a specific name to another set. How are the documents named? Are the components in the assembly browser renamed?

 

Also I think the you're confusing the DisplayName listed in the browser with the filename of the document. The two don't have to match. And for iLogic assemblies most likely won't match. If you're not using iLogic but straight vba then you're better off working with the filename since you're going to have to know that anyway to do replacements.

 

Lastly, do you have Vault installed or Design Assistant (This is usually installed alongside of Inventor by default). If so you can do a Copy Design on the assembly outside of Inventor that will do all of the copying, renaming, and replacing in one step.

Message 11 of 15
Anonymous
in reply to: rjay75

Let me answer your questions one by one so it doesn't get confusing. My answers are in bold.

 

What exactly was your workflow? It looks like you're trying to replace a series of components that have a specific name to another set. Yes, so what I did was I created a code that lets the user select a .ipj file folder, rename it and all the documents in it based on there prefixes, and then lets the user select an associated assembly file. Meanwhile in the background the contents of the original folder have been copied in to a temporary file in my C:/ drive. The user clicks open, which silently opens the assembly of which you can't see any parts because the associated parts have been renamed. The next step is to use the "Replace Component" function to replace the components. I will attach my .zip folder with all of this in it, though it is not completed because of that last part.

 

How are the documents named? Parts are named based on the Project folder. Say a project is named PrettyPonies. Parts and assemblies within it will be named PrettyPonies-001, PrettyPonies-002, PrettyPonies-003, etc.

 

Are the components in the assembly browser renamed? Yes, they were renamed and therefore need to be replaced. The originals are temporarily saved in a folder in my C:/ drive.

 

Also I think the you're confusing the DisplayName listed in the browser with the filename of the document. The two don't have to match. And for iLogic assemblies most likely won't match. If you're not using iLogic but straight vba then you're better off working with the filename since you're going to have to know that anyway to do replacements. Yes, I've been trying to work with the file names. This is how I completed the renaming section of the task.

 

Lastly, do you have Vault installed or Design Assistant (This is usually installed alongside of Inventor by default). If so you can do a Copy Design on the assembly outside of Inventor that will do all of the copying, renaming, and replacing in one step. We do not have Vault. Design Assistant, is of course, built in with the bundle. However, I've tried using Copy Design before and it didn't do...anything...that I could tell. I was told when I first started this project that I'd be able to take that code, modify it, and use it to do what I wanted it to do. By the time my computer was updated to Windows 7 so that I could open that source code, I'd already written most of this program. I'm on the very last step and just want to finish it at this point.

Message 12 of 15
rjay75
in reply to: Anonymous

Ok, then what you really need to to do is not worry about components at all. All you're doing is repairing the file references to be pointing to the new files. In that case I would do like Adam Nagy suggested in the other thread. 

 

        Dim f As File
        Set f = ThisApplication.ActiveDocument.File
        
Dim fd As FileDescriptor For Each fd In f.ReferencedFileDescriptors
If fd.FullFileName = OldNamePath Then fd.ReplaceReference (NewNamePath) End If Next

 Instead of doing anything with occurrences just fix file references. Note the Replace here has nothing to do with occurrences. It's just swapping file pointers from one to another.

Message 13 of 15
Anonymous
in reply to: rjay75

Thanks, that's what I'm looking at now. I got a really fun error that I'm trying to deal with. Thanks for all your help!

Message 14 of 15
rjay75
in reply to: Anonymous

Message 15 of 15
Anonymous
in reply to: rjay75

Okay thanks. I went ahead and accepted that solution. Hopefully I can get through this error.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report