Page 1 of 1

SelectByID2 : retrieve ID from selected component

Posted: Thu May 06, 2021 1:39 pm
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"

Re: SelectByID2 : retrieve ID from selected component

Posted: Fri May 07, 2021 5:46 am
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/

Re: SelectByID2 : retrieve ID from selected component

Posted: Fri May 07, 2021 9:21 am
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/

Re: SelectByID2 : retrieve ID from selected component

Posted: Fri May 07, 2021 3:06 pm
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