Hi,
I am writing a macro that needs to collect references to solid bodies (Body2 objects) that user may select in an assembly environment through different ways, for example:
1) Select a component itself from a FeatureManager tree;
2) Select a body face from the graphics area;
3) Select a body from the component (part) Solid Bodies folder.
It is extremely important to get a reference to a Component body, not just ModelDoc2 body. This is because there may be multiple instances of that component in the assembly, and I need a body reference from that specific component instance which the user intended to select, because I will need to retrieve it's positional and rotational data in the assembly through MathTransform. To achieve this, I covered the first two selection cases like this:
1) If component is selected, macro gets references to all bodies within selected component by running GetBodies3 on Component2. Boom, done.
2) If face is selected, macro gets the name of the selected body by running GetBody on the selected Face2. Then, it converts that Face2 to Entity, and runs GetComponent on that Entity to get the component reference. Then again it runs GetBodies3 on it to retrieve a full list of bodies, and runs a simple foreach loop to find the body which matches the name retrieved initially.
However, for the third case - when the user selects a body directly from the Solid Bodies folder - I mean like this:
...Then I'm at a loss. I can't figure out any way to determine from which specific component instance did the user select that body. Is there any way to get that reference?
Getting a reference to a component body selected from the FeatureManager tree
Getting a reference to a component body selected from the FeatureManager tree
GetSelectedObjectsComponent4
Go to full post-
- Posts: 9
- Joined: Thu Apr 13, 2023 4:19 am
- Location: Croatia
- x 8
- x 2
Re: Getting a reference to a component body selected from the FeatureManager tree
When you say component, do you mean Cube<2> for that particular selection? If so, this should be fine.
Code: Select all
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swDoc As SldWorks.ModelDoc2
Dim swSelMgr As SldWorks.SelectionMgr
Dim swSelData As SldWorks.SelectData
Dim swBody As SldWorks.Body2
Dim bool As Boolean
Dim val As Long
Set swApp = Application.SldWorks
Set swDoc = swApp.ActiveDoc
Set swSelMgr = swDoc.SelectionManager
Set swSelData = swSelMgr.CreateSelectData
Set swBody = swSelMgr.GetSelectedObject6(1, -1)
bool = swBody.Select2(False, swSelData)
End Sub
Looking for opportunities related to SolidWorks API usage.
Re: Getting a reference to a component body selected from the FeatureManager tree
I tried it - this retrieves a reference to the body of ModelDoc2 (Cube document), not the body of the specific Component2 instance (Cube<2> component), so the macro can't get the positional data for that body in the assembly reference. I need to figure out which component instance does that body belong to.mario malic wrote: ↑Sun Oct 01, 2023 3:56 pm When you say component, do you mean Cube<2> for that particular selection? If so, this should be fine.
Code: Select all
Sub main() Dim swApp As SldWorks.SldWorks Dim swDoc As SldWorks.ModelDoc2 Dim swSelMgr As SldWorks.SelectionMgr Dim swSelData As SldWorks.SelectData Dim swBody As SldWorks.Body2 Dim bool As Boolean Dim val As Long Set swApp = Application.SldWorks Set swDoc = swApp.ActiveDoc Set swSelMgr = swDoc.SelectionManager Set swSelData = swSelMgr.CreateSelectData Set swBody = swSelMgr.GetSelectedObject6(1, -1) bool = swBody.Select2(False, swSelData) End Sub
Re: Getting a reference to a component body selected from the FeatureManager tree
GetSelectedObjectsComponent4
-
- Posts: 9
- Joined: Thu Apr 13, 2023 4:19 am
- Location: Croatia
- x 8
- x 2
Re: Getting a reference to a component body selected from the FeatureManager tree
As Josh said, GetSelectedObjectsComponent4 should get it. I tried getting it this way, but it didn't work for some reason on SW2021 (I might have made a mistake trying to get it), but this works on SW2022.
Code: Select all
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swDoc As SldWorks.ModelDoc2
Dim swSelMgr As SldWorks.SelectionMgr
Dim swSelData As SldWorks.SelectData
Dim swComp As SldWorks.Component2
Set swApp = Application.SldWorks
Set swDoc = swApp.ActiveDoc
Set swSelMgr = swDoc.SelectionManager
Set swSelData = swSelMgr.CreateSelectData
Set swComp = swSelMgr.GetSelectedObjectsComponent4(1, -1)
Debug.Print swComp.Name2
End Sub
Looking for opportunities related to SolidWorks API usage.
Re: Getting a reference to a component body selected from the FeatureManager tree
Thanks, Josh! I didn't realize this call could be used like that. I tested and it works.
Not sure why it wasn't working, it worked for me on SW 2021. Perhaps try to remove that "Set swSelData = swSelMgr.CreateSelectData" line, it doesn't do anything in this context I think.mario malic wrote: ↑Mon Oct 02, 2023 3:34 am As Josh said, GetSelectedObjectsComponent4 should get it. I tried getting it this way, but it didn't work for some reason on SW2021 (I might have made a mistake trying to get it), but this works on SW2022.Code: Select all
Sub main() Dim swApp As SldWorks.SldWorks Dim swDoc As SldWorks.ModelDoc2 Dim swSelMgr As SldWorks.SelectionMgr Dim swSelData As SldWorks.SelectData Dim swComp As SldWorks.Component2 Set swApp = Application.SldWorks Set swDoc = swApp.ActiveDoc Set swSelMgr = swDoc.SelectionManager Set swSelData = swSelMgr.CreateSelectData Set swComp = swSelMgr.GetSelectedObjectsComponent4(1, -1) Debug.Print swComp.Name2 End Sub
-
- Posts: 9
- Joined: Thu Apr 13, 2023 4:19 am
- Location: Croatia
- x 8
- x 2
Re: Getting a reference to a component body selected from the FeatureManager tree
I forgot to remove swSelData from previous macro, it does work.
Looking for opportunities related to SolidWorks API usage.