SelectByID2 : retrieve ID from selected component

Programming and macros
colt
Posts: 54
Joined: Tue Mar 30, 2021 5:43 pm
Answers: 0
x 14
x 22

SelectByID2 : retrieve ID from selected component

Unread post by colt »

I am using selectByID2 to add mates in the top level assembly. I select two entites in the graphics area or model tree and get their component using

Code: Select all

swEntity := SelMgr.GetSelectedObject6(1, 0)
swComponentA := swEntity.GetComponent
'or  
swComponentA := SelMgr.GetSelectedObject6(1, 0)

swEntity := SelMgr.GetSelectedObject6(2, 0)
swComponentB := swEntity.GetComponent
'or  
swComponentB := SelMgr.GetSelectedObject6(2, 0)
Then I use selectByID2 to grab each primary plane from the two components and add a mate between them.

Currently I am hard coding the format of the ID because this macro is used on assemblies where the file structure is known. Now I want to generalize my program for any assembly structure.
How can I build or get the ID of swComponentA with respect to the top assembly for use in SelectByID2? The macro recorder gives something like this

Code: Select all

boolstatus = Part.Extension.SelectByID2("Right Plane@HMC-ADPT1200-NB-1-1@HMC-ADPT1200-1/HMC-PLCD-539-3@HMC-ADPT1200-NB-1", "PLANE", -9.84228209404137E-05, 1.09972117172674E-04, -0.154934245329059, True, 0, Nothing, 0)
Eventually I want to be able to specify a feature in swComponentA to use during mating in a loop.
Example: myFeature would be all three primary planes.

Code: Select all

id = myFeature & "@HMC-ADPT1200-NB-1-1@HMC-ADPT1200-1/HMC-PLCD-539-3@HMC-ADPT1200-NB-1"
by Rob » Fri May 07, 2021 5:46 am
Hi @colt

I would look at selecting directly with the appropriate method.

Here's some code I wrote the other day that mates a component plane to an assembly plane and then mates the component origin to a sketch point - not a selectbyid2 in sight ;)
image.png
I'm still a bit new to this but I believe you need to make sure that you have the correct object.
Artem has a great post on this https://www.codestack.net/solidworks-ap ... selection/
You should also check out this one https://www.codestack.net/solidworks-ap ... y/context/
Go to full post
User avatar
Rob
Posts: 128
Joined: Mon Mar 08, 2021 3:46 pm
Answers: 2
Location: Mighty Glossop, UK
x 787
x 207
Contact:

Re: SelectByID2 : retrieve ID from selected component

Unread post by Rob »

Hi @colt

I would look at selecting directly with the appropriate method.

Here's some code I wrote the other day that mates a component plane to an assembly plane and then mates the component origin to a sketch point - not a selectbyid2 in sight ;)
image.png
I'm still a bit new to this but I believe you need to make sure that you have the correct object.
Artem has a great post on this https://www.codestack.net/solidworks-ap ... selection/
You should also check out this one https://www.codestack.net/solidworks-ap ... y/context/
User avatar
AlexB
Posts: 448
Joined: Thu Mar 18, 2021 1:38 pm
Answers: 24
x 243
x 398

Re: SelectByID2 : retrieve ID from selected component

Unread post by AlexB »

I have to agree with @Rob that SelectByID2 is the least robust way to make a selection.

This has always been a helpful site for me since Artem has put together so many useful examples:
https://www.codestack.net/solidworks-ap ... selection/
colt
Posts: 54
Joined: Tue Mar 30, 2021 5:43 pm
Answers: 0
x 14
x 22

Re: SelectByID2 : retrieve ID from selected component

Unread post by colt »

Rob wrote: Fri May 07, 2021 5:46 am Here's some code I wrote the other day that mates a component plane to an assembly plane and then mates
If only the macro recorder spit out code that was that useful
Post Reply