AnsweredAssumed Answered

Selecting a solid body with unknown ID

Question asked by J. R. on Feb 20, 2018
Latest reply on Feb 20, 2018 by J. R.



I am writing a macro which has to select a solid body (in a single-part model), and perform move-copy operation. The ID of the solid body may change, so SelectByID2 won't work. Using FeatureByPositionReverse won't work either, because there might be some features before and after the feature of the solid body.


I thought of using SelectByRay, because the position of the body is pretty much well known, but for some reason, InsertMoveCopyBody2 won't work with this kind of selection (nothing happens). This is what I tried:


Dim swApp As Object

Dim Part As Object

Dim boolstatus As Boolean

Dim longstatus As Long, longwarnings As Long


Sub main()

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc

Dim myFeature As Object


boolstatus = Part.Extension.SelectByRay(0.1, -1, 0.1, 0, -1, 0, 0, 2, False, 0, 0)

Set myFeature = Part.FeatureManager.InsertMoveCopyBody2(0, 0, 0, 0, 0, 0, 3#, 0, 0, 0, True, 1)

End Sub


Selection works, but InsertMoveCopyBody2 doesn't. If I replace selection line with boolstatus = Part.Extension.SelectByID2("body1", "SOLIDBODY", 0, 0, 0, False, 1, Nothing, 0), then it works, but as I said, the name of the solid body might be different every time, so it's no solution.


Is there a way to make SelectByRay work for this application, or is there a better way to select the only solid body in the model? After all, SelectByRay is quite crude a method, but I couldn't find anything better.


Thank you in advance!