Hi Everyone,
I am a beginner with VBA and the solidworks API.
I want to write a macro which when run, will find/select a face in the part using SelectByRay. It will then find a feature in the tree called 'TheFeature' and change its first selected entity to this face (no matter what the feature). Is there a generic method for setting a feature's selected face entities equivalent to attempting to click on a face/point/body whilst editing that feature. I hope I don't have to write a bunch of if statements which include every possibility of feature interface e.g. ISurfaceCutFeatureData, ISurfaceOffsetFeataureData...
I am testing my Macro on a surface offset feature - when the macro is run it should change the part so whichever face the ray first meets becomes the face which the feature offsets.
Cheers,
Harry
Macro for changing selected entity of feature
-
- Posts: 26
- Joined: Wed Jul 21, 2021 3:20 pm
- x 6
- x 2
Re: Macro for changing selected entity of feature
Maybe not the best way but this is what comes to mind. You could get the name of the face that you selected by your ray and then traverse through the feature tree to find which features all modify that face (by matching the name to the value returned by Ifeature GetFaces). Since multiple features could have modified the face then you would want to keep the last feature that returns the given face.
But maybe someone has a simpler way of doing things?
But maybe someone has a simpler way of doing things?
Re: Macro for changing selected entity of feature
Why are you wanting to use SelectByRay? It's pretty dependent on lots of stuff about your model view, geometry, etc. Typically this kind of thing is done by the user pre-selecting the desired face and then running the macro.
Re: Macro for changing selected entity of feature
Hi Jordan, Josh. Thanks for responding to this.
Josh to answer your question, I intend for this macro to run after every rebuild (it will be embedded into the design binder) so effectively instead of the surfaceoffsetfeature referencing a face ID, it will reference a ray in the model which will pick the face. Eventually this ray will be linked to a point and line in a 3D sketch. I'm fairly sure selectbyray is independent of view and selects based off a vector in model space?
Jordan, cheers for the advice. I think what I am looking for is a method of changing a surface offset feature's selected entities (I have given up trying for a general feature for now). Below is what I have come up with so far. This can successfully change the offset distance but cannot change the selected entities - im not sure where I'm going wrong.
If the current offset face is away from the origin (ray), then, when the macro is run, the offset face should change to the face which is over the origin (ray), but it doesn't.
Please help :'(
"Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swModelEx As SldWorks.ModelDocExtension
Dim swPart As SldWorks.PartDoc
Dim swSelMgr As SldWorks.SelectionMgr
Dim swFeature As SldWorks.FEATURE
Dim swSOFD As SldWorks.SurfaceOffsetFeatureData
Dim varFace As Variant
Dim BoolStat As Boolean
Dim LongStat As Long
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swModelEx = swModel.Extension
Set swPart = swApp.ActiveDoc
Set swSelMgr = swPart.SelectionManager
swModel.ClearSelection2 (True)
Set swFeature = swPart.FeatureByName("TheFeature")
Set swSOFD = swFeature.GetDefinition
BoolStat = swSOFD.AccessSelections(swModel, Nothing)
BoolStat = swModel.Extension.SelectByRay(0, 1, 0, 0, -1, 0, 0.0001, swSelFACES, False, 0, 0)
Set varFace = swSelMgr.GetSelectedObject6(1, 0)
swSOFD.Distance = swSOFD.Distance + 0.001
swSOFD.Entities = varFace
swModel.ClearSelection2 (True)
BoolStat = swFeature.ModifyDefinition(swSOFD, swModel, Nothing)
swSOFD.ReleaseSelectionAccess
End Sub"
Josh to answer your question, I intend for this macro to run after every rebuild (it will be embedded into the design binder) so effectively instead of the surfaceoffsetfeature referencing a face ID, it will reference a ray in the model which will pick the face. Eventually this ray will be linked to a point and line in a 3D sketch. I'm fairly sure selectbyray is independent of view and selects based off a vector in model space?
Jordan, cheers for the advice. I think what I am looking for is a method of changing a surface offset feature's selected entities (I have given up trying for a general feature for now). Below is what I have come up with so far. This can successfully change the offset distance but cannot change the selected entities - im not sure where I'm going wrong.
If the current offset face is away from the origin (ray), then, when the macro is run, the offset face should change to the face which is over the origin (ray), but it doesn't.
Please help :'(
"Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swModelEx As SldWorks.ModelDocExtension
Dim swPart As SldWorks.PartDoc
Dim swSelMgr As SldWorks.SelectionMgr
Dim swFeature As SldWorks.FEATURE
Dim swSOFD As SldWorks.SurfaceOffsetFeatureData
Dim varFace As Variant
Dim BoolStat As Boolean
Dim LongStat As Long
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swModelEx = swModel.Extension
Set swPart = swApp.ActiveDoc
Set swSelMgr = swPart.SelectionManager
swModel.ClearSelection2 (True)
Set swFeature = swPart.FeatureByName("TheFeature")
Set swSOFD = swFeature.GetDefinition
BoolStat = swSOFD.AccessSelections(swModel, Nothing)
BoolStat = swModel.Extension.SelectByRay(0, 1, 0, 0, -1, 0, 0.0001, swSelFACES, False, 0, 0)
Set varFace = swSelMgr.GetSelectedObject6(1, 0)
swSOFD.Distance = swSOFD.Distance + 0.001
swSOFD.Entities = varFace
swModel.ClearSelection2 (True)
BoolStat = swFeature.ModifyDefinition(swSOFD, swModel, Nothing)
swSOFD.ReleaseSelectionAccess
End Sub"