Get face that projected curve is on

I've been scratching my head at this for about 3 hours. I've read many examples and help files but I can't understand how to solve this. I have a sketch projected onto a surface to create a curve. After selecting the curve I want to be able to get the face it is on.

I found a macro in the help files that returns the gettype value. The value for the curve is 51, swSelREFEDGES. I tried setting the curve as a feature and then look at the parents of it but this did not work. I seem to be running around in circles with this. Could anyone point me in the right direction?

SolidworksApi/macros
Answers 5
SC
SC 2021-04-08 Best Answer

More debugging. I found a snippet of code in the help files that prints out a parts features statistics. The projected curve shows up as type 23, swSelREFCURVE. How does this curve show up as two different types? In my code I have

Set pathSel = swSelMgr.GetSelectedObject6(1, -1)
selVal = swSelMgr.GetSelectedObjectType3(1, -1)

This gives me a value of 51

However, in the help file code snippet

feattypes = swFeatStat.FeatureTypes

returns 23

SC
SC 2021-04-08

The second approach shouldn't be too bad. In the case where the vertices end on the edge of the surface, and there is another surface capping the edge, I can get the extrusion direction vector (I've done this before) of each surface. I can take the dot product of this vector with the normal vector of my sketch profile normal plane (which will always be parallel to the sketch plane of the surface extrusion that the curve lies on). If the dot product is 1 then the vectors are parallel and the surface in question is the one I want.

Update:

I've coded a solution using your 2nd method in combination with my surface retrieval. With the vertex, I select the edge that it is on. I then can get the two faces that share this edge and loop through them to find the dot product that results in 1. In the future I may have to adapt that code in the case where a vertex shares more than one edge. In that case would have to get an array of all the edges and then loop through them to get the faces and then loop through those to get the surfaces. sknormVec is my sketch plane normal vector that I found earlier in my code.

Set swVert = swobj.GetStartVertex
bool = swModel.Extension.SelectByID2("", "EDGE", swVert.GetPoint(0), swVert.GetPoint(1), swVert.GetPoint(2), False, 1, Nothing, 0)
Set swEdge = swSelMgr.GetSelectedObject6(1, -1)
vFaces = swEdge.GetTwoAdjacentFaces2()

For i = 0 To UBound(swFaces)
   Set swFaces(i) = vFaces(i)
   Set swSurf = swFaces(i).GetSurface
   surfParams = swSurf.GetExtrusionsurfParams() 'retrieves the direction vector of the surface extrusion
   Set extrVec = swMath.CreateVector((surfParams))
   dot = Abs(extrVec.dot(sknormVec)) 'vectors are parallel if dot = 1, perpendicular if dot = 0
   If dot = 1 Then 'the face is parallel to the feature axis
      Set swEnt = swFaces(i)
      bool = swEnt.Select4(True, swSelData)
      Set pathFace = swSelMgr.GetSelectedObject6(1, -1)
      swModel.ClearSelection2 True
   End If
Next i

JB
JB 2021-04-08

So, the object you will get if you select the curve in the graphics area is an Edge object.

I don't see a good accessor path back from the Edge object to the feature that created it.  Someone else may know...

Otherwise, I have two suggestions.

One would be:

1. Get a pointer to the Edge object using GetSelectedObject6

2. Traverse the feature tree looking for Curve features.  Then, for each curve featue:

     -Get segments

     -Check each segment to see if it IS the edge that was selected.

     -If you find a match, you've found the parent feature.

Not so great, but should be robust.

Next approach is one that I rarely recommend, but it should reliably get you the face you're after...I think: SelectByID2.

Get the point from the edge's start vertex and use those coordinates for a SelectByID2 using "FACE" for the selection type.

Dim swApp As Object
Sub main()

Set swApp = Application.SldWorks
Set myObj = swApp.ActiveDoc.SelectionManager.GetSelectedObject6(1, -1)
Dim swVer As SldWorks.Vertex
Set swVer = myObj.GetStartVertex
Dim swExt As SldWorks.ModelDocExtension
Set swExt = swApp.ActiveDoc.Extension
swExt.SelectByID2 "", "FACE", swVer.GetPoint(0), swVer.GetPoint(1), swVer.GetPoint(2), False, 1, Nothing, 0
End Sub

SC
SC 2021-04-08

I see. I learned something new. I have a Select Case for different types of selections i.e sketch segment, model edges. I'm now able to get the parents of curve if I select it from the feature tree, but how would I be able to do this from the graphics area?

JB
JB 2021-04-08

Pick it in the graphics area, get 51.

Pick it in the feature tree, get 23.

Similar:

Pick a face of an extrude, get swselFaces.

Pick the extrude in the tree, get swselFeatures.