Reposition component by API macro

Hello

 

Since this morning I have been looking for a solution to my problem but impossible to find. I would like to insert components and reposition them in an assembly according to the origin of the assembly or certain point, point 1, point 2 ...

 

I manage to create an assembly automatically, open all my files and even insert them into my assembly. The first component didn't even insert at the origin of the assembly and I don't know how you can reposition the whole thing on the origins or on points.

If anyone knows these two little lines of code, here are the lines of code that are interesting: 

'Ouverture de l'assemblage
Set Part = swApp.OpenDoc6(ActiveSheet.Range("H100"), 2, 0, "", longstatus, longwarnings)
swApp.ActivateDoc2 ActiveSheet.Range("H100"), False, longstatus

'--------------------------------------------
'assemblage des plans
boolstatus = Part.AddComponent(ActiveSheet.Range("H101"), ActiveSheet.Range("E101"), ActiveSheet.Range("F101"), ActiveSheet.Range("G101"))
boolstatus = Part.AddComponent(ActiveSheet.Range("H102"), ActiveSheet.Range("E102"), ActiveSheet.Range("F102"), ActiveSheet.Range("G102"))

 

 

I just need more lines between each insertion to define the positioning point of each part/set.

 

Hello

Check here to see  if it can help you.

Thank you for your quick answer, but I wanted to directly line the code and not rack my brains :'(

 

More seriously, I manage to insert my parts I want to move them after insertion. I can't find the job. 

To bounce back on your workstation, is there a way to load the component directly into the assembly without pre-loading it?

Hello

There's probably what you want in this discussion.

Kind regards

Hello to you d.roger.

First of all , I want to thank you for your answer, you posted right after I found the "mate" function , I realized that you could still do macros by "hand" to compare and unlock.

So I come up with something like this:

'assemblage des plans
boolstatus = Part.AddComponent(ActiveSheet.Range("H101"), ActiveSheet.Range("E101"), ActiveSheet.Range("F101"), ActiveSheet.Range("G101"))
    'positionnement de la pièce
            'suppression du "fixe" sur la première pièce
            boolstatus = Part.Extension.SelectByID2("K101", "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)
            Part.unFixComponent
    Part.ClearSelection2 True
    boolstatus = Part.Extension.SelectByID2("J100", "EXTSKETCHPOINT", 0, 0, 0, False, 1, Nothing, 0)
    boolstatus = Part.Extension.SelectByID2("J101" & "K101", "EXTSKETCHPOINT", 0, 0, 0, True, 1, Nothing, 0)
   ' Dim swMate As Mate2
    Set swMate = Part.AddMate5(20, -1, False, 0.824524202641418, 0.001, 0.001, 0.001, 0.001, 0.5235987755983, 0.5235987755983, 0.5235987755983, False, False, 0, longstatus)
    Part.ClearSelection2 True
    boolstatus = Part.Extension.SelectByID2("K101", "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)
    Part.FixComponent
    Part.ClearSelection2 True

 

My problem is that it doesn't select my component which is listed as "K101", so no constraint or anything set. What a macro, full of lines for nothing :'(

 

Thank you for your help, I'm still waiting for the right link or a solution.

What do your "J100", "J101" and "K101" correspond to?

Given the snippet of code, I assume that these are values taken from cells in an Excel file but do these values respect the necessary naming?

Like what:

- for "K101", if it is a component named "Part1" that is part of an assembly named "Assembly2" it should be "Part1-1@Assemblage2".

- for "J100", if it's the origin of Assemblage2, it should be "Point1@Origine@Assemblage2".

- for "J101", if it is the origin of Part1, it should be "Point1@Origine@Pièce1-1@Assemblage2".

Put the line "MsgBox (longstatus)" after the line "Set swMate = Part.AddMate5(20, -1, False, 0.824524202641418, 0.001, 0.001, 0.001, 0.001, 0.523598775983, 0.523598775983, 0.5235987755983, False, False, 0, longstatus)", this will allow you to retrieve the error code, to compare with these.

And of course, as mentioned in the other discussion, check that your parts are in solved mode in Solidworks, otherwise it doesn't work...

Kind regards

2 Likes

Hello

 

Already sorry for this wait before answering, I just looked back at my subject and here is what came out of it:

My macro opens all my parts/assembly well, before opening the main assembly and inserting the various components, I will probably have to find a way to open and close them in turn otherwise it will be heavy.

 

When inserting my various components in my assembly, they are well resolved but are all positioned at their point of gravity despite my swMate. So I'm blocked for the moment.

 

Thank you for your answer d.roger. The boxes that are referenced are, normally, well filled. I inserted the MsgBox line (longstatus), it returns the message 4 = Incorrect selections for mate. So I conclude that my boxes are not correctly filled.

In J, I place my reference points 'Point1@Origine@'. For the origin of the assembly I just have 'Point1@Origine'

In K I call my components 'Part1-1@assemblage1'

This part is to be reworked there I just want to make my editing work, but with my two lines of code:

boolstatus = Part.Extension.SelectByID2("J100", "EXTSKETCHPOINT", 0, 0, 0, False, 1, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("J101" & "K101", "EXTSKETCHPOINT", 0, 0, 0, True, 1, Nothing, 0)

 

Normally I don't have any mistakes, do I?

 

Last thing, my components don't fix in my assembly after my macro yet I have these three lines:

    boolstatus = Part.Extension.SelectByID2("K101", "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)
    Part.FixComponent
    Part.ClearSelection2 True

 

 

Thank you I keep you informed of my progress.

Hello

Your selections are therefore not good since solidworks is looking for elements named "J100", "J101" and "K101" in your parts and assemblies, you have to retrieve the values of the cells  in Excel and then pass them into string variables that you then assign to your SelectByID2 functions.

Dim FirstSelection As String
FirstSelection = 'valeur de la cellule Excel (attention ce n'est pas le numéro de la cellule)'
Dim SecondSelection As String
SecondSelection = 'valeur de la cellule Excel (attention ce n'est pas le numéro de la cellule)'
        
Part.ClearSelection2 True
boolstatus = swAssemblyDoc.Extension.SelectByID2(FirstSelection, "EXTSKETCHPOINT", 0, 0, 0, False, 1, Nothing, 0)
boolstatus = swAssemblyDoc.Extension.SelectByID2(SecondSelection, "EXTSKETCHPOINT", 0, 0, 0, True, 1, Nothing, 0)

Kind regards

Hello

 

Indeed returning the J100 or J101 value was not a good idea, beginner's mistake or someone not used to VBA. So I took what was in the front station to move forward, here's what it looks like:

 

        For i = 101 To 103
boolstatus = Part.AddComponent(ActiveSheet.Range("H" & i), 0, 0, 0)

Dim CompInsert As String
CompInsert = ActiveSheet.Range("E" & i) & "-1@" & ActiveSheet.Range("E100")
Dim FirstSelection As String
FirstSelection = "Point1@Origine@" & CompInsert
Dim SecondSelection As String
SecondSelection = "Point1@Origine"

    'positionnement de la pièce
            'suppression du "fixe" sur la première pièce
            boolstatus = Part.Extension.SelectByID2(CompInsert, "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)
            Part.unFixComponent

Part.ClearSelection2 True
boolstatus = Part.Extension.SelectByID2(FirstSelection, "EXTSKETCHPOINT", 0, 0, 0, False, 1, Nothing, 0)
boolstatus = Part.Extension.SelectByID2(SecondSelection, "EXTSKETCHPOINT", 0, 0, 0, True, 1, Nothing, 0)
            
Set swMate = Part.AddMate5(20, -1, False, 0, 0.001, 0.001, 0.001, 0.001, 0.5235987755983, 0.5235987755983, 0.5235987755983, False, False, 0, longstatus)

 

For the moment my macro manages to do these operations:

Open all the components one by one, to avoid the unresolved mode, I will think later of a less cumbersome solution.

It opens my main assembly or inserts the components.

It makes the component "free". (Variable "CompInsert" ok)

It places my origin/origin components and fixes them.

 

Thank you for your help, now I have to find a way to open my components directly in my assembly and make them solved.

No need to open all the components to put them on resolved, just list the threads of the assembly loaded in Solidworks using the "GetRootComponent3" function and then put them in resolved mode using the "SetComponentDelete" function.

Kind regards