Two small suggestions without diving into the code:
Try to open the part in SolidWorks (since we have the full path) and then try the replacement.
Otherwise, a bit barbaric method, insert the part into the assembly, replace the existing one, then delete it (check that we delete the first instance!)
I tried the first method, it seems that the replacement part must be opened to be taken into account (and accessible in writable maybe. If that's the case, it's going to annoy me a bit as the parts are on EPDM...)
But when trying to open it separately, the replacement does not take into account the occurrence of the part to be changed =o
Example:
In my assembly I have:
SE-01
-piece01<1>
-item02<1>
SE-02
-....
-....
-Item03<1>
-Room01<5>
-Room02<9>
And when, I want to change the "part01<5>" for example, I select it (by putting part01-5), so far everything works fine, I enter the name of the replacement part open next to it as well, and it changes not the "part01<5>" as selected but the "part01<1>" =o
So the function would not take into account the occurrence as I thought?
Because on the other hand when I want to change the "Piece03<1>" (or whatever the occurrence as long as it's the highest in the tree), then I have no worries =/
Yes, the part must be loaded in memory, and here is the translated help
You cannot replace a selected component with a component of the same name even if the components are in different folders.
The component must be a top-level element. It cannot be a component of a subassembly. If the application needs to replace a component in the subassembly, then it must open the subassembly and get the component from that assembly
This method closes all component files when called in an assembly. If the components have been changed, then those changes are not automatically saved. You must save the changes before the files are closed.
To avoid the need to change documents before opening the document you can use the API
swapp. DocumentVisible(False, swDocumentTypes_e.swDocPART) be sure to set to True after opening
The document will be open but not visible
For the replacement of occurrences this should work, because the command replaces the selected part even if it has other occurrences, it is the selection that will be replaced
I was indeed wondering if I had understood this part of the API help correctly in view of what it was like for me. Now I'm sure there's a binz somewhere x)
Indeed, in addition to replacing an unselected (and hidden!) part, this one is... in a subset! =o
This is a function that I have already used and that works... I don't really see what can happen with you
especially since from what I see the reversal is done between pieces that don't have the same name?
But it's complicated to ask the user to enter the name of the component + the occurrence, why not ask him to select the component in the tree, so no more risk of error in the names
Otherwise, the function, the example I put in Jean-Bapt's previous question that I paste here, allows you to get the names and offer them in a Combobox style drop-down menu
Dim swDoc As SldWorks.ModelDoc2
Dim swAss As SldWorks.AssemblyDoc
Dim stOldFileName As String, stNewFileName As String
Dim bStatus As Boolean
Sub main()
Set swApp = Application.SldWorks
Set swDoc = swApp.ActiveDoc
If swDoc Is Nothing Then Exit Sub
If swDoc.GetType <> swDocumentTypes_e.swDocASSEMBLY Then Exit Sub
Set swAss = swDoc
Dim swComponents As Variant: swComponents = swAss.GetComponents(True)
Yoann, if the replacement of an 'old' part by a 'new' part in an assembly is the subject, I allow myself to be skeptical. What for?
Already the existing constraints between the old 'and the other parts, in my opinion, are almost all becoming wobbly! Unless the old and the new have a lot of similarities (families of pieces). Unless there is a super mega SW that manages this? Don't dream!!
As a limit question, an assembly being open, the replacement of a part of a sub-assembly is impossible. It will then be necessary to open the sub-assembly for this to become possible.
In terms of occurrence, it is SW that allocates the occurrence when manually adding a 'clone' part in an assembly, it manages it. The macro will also have to take into account the occurrence of the old coin.
Finally, the [ReplaceComponents] method is not everything!
In my opinion, it only handles the replacement of an 'old' part by a 'new' part if and only if the latter is open, or, even more restrictive, open and present in the assembly.
So the idea, as Lucas says in the context of a new replacement part that is not part of the assembly, is to open it, insert it into the assembly, identify its occurrence, and finally replace the old one with the new one.
First of all, I want to apologize for the response time, I had to work on something completely different and didn't have time to dive back into it so
@CaronMaxime: Putting the part replacement function in full macro is indeed more than audacious, because of the constraints for example. But my goal is very specific, don't worry! My goal is related to format changes that are usually rather long in terms of handling for operators. I also test my macro (which is not only made up of replacecomponent but also of change of dimension of constraints in the assembly for example.
As for the wobbly constraints, they are indeed a little, but by going through the basic function of solidworks, the same thing happens to them^^
@jfaradon: Thank you for your macro :o It was indeed very useful to me to create the selection combobox you mention :-)
I'm finally going to close this question with this :-)