Why SolidWorks Surfacing Users Should Learn the API

SolidWorks provides a lot of power in making selections. But some things are just not possible out of the box. My colleague Alin was browsing the SolidWorks forums and came across this thread. Once the problematic face is deleted the model looked like this:

1

Patching this up is not terribly complicated as shown in Alin’s videos posted in the first two articles of this series (Episode 1, Episode 2).  It is however quite time consuming to make all the selections and clean it up.  If this only affects one model then you will save yourself a couple of minutes, but what if it is one of a hundred, then you will save yourself hours, it could even be one of thousands! In that case further automation is worthwhile, so your computer can do your job while you sleep.

The first step is to analyze what methods are possible and how to accomplish them.  In the API course you learn how to traverse part geometry and the object model, so that is a good starting point.  First we connect to SolidWorks, then we connect to the modeldoc (general object for everything that parts, assemblies, and drawings have in common), then the partdoc, then the bodies, then the faces, then the loops, then the edges.

The code to do this looks like this:

Set swApp = Application.SldWorks ‘Connect to SolidWorks
Set swModel = swApp.ActiveDoc ‘Connect to ModelDoc
Set swPartDoc = swModel ‘Connect to PartDoc
vBodies = swPartDoc.GetBodies2(swAllBodies, True) ‘Get Bodies
For i = LBound(vBodies) To UBound(vBodies)
Set swBody = vBodies(i)
vFaces = swBody.GetFaces ‘Get Faces
For j = LBound(vFaces) To UBound(vFaces)
Set swFace = vFaces(j)
vLoops = swFace.GetLoops ‘Get loops
For k = LBound(vLoops) To UBound(vLoops)
Set swLoop = vLoops(k)
If swLoop.IsOuter Then
vEdges = swLoop.GetEdges ‘Get edges
For l = LBound(vEdges) To UBound(vEdges)
Set swEdge = vEdges(l)
Next
End If
Next
Next
Next

 

If we add a couple of lines of code after Set swEdge = vEdges(l) then we will select all outer edges

Set swEntity = swEdge
swEntity.Select True

Almost everything that can be selected in the graphics area of SolidWorks is a subset of Entity, that includes bodies, surface bodies, edges, loops, etc.  Since it is a subset we can always set entity equal to any of these objects.  Entities also include a method for selection, since I don’t need to filter anything in my selection I used Select instead of Select4. The true is for append selection (behaved just like holding down Ctrl).

This is what the selection will look like

2

This is what the selection looks like if we change  If swLoop.IsOuter Then to  If swLoop.IsOuter = False Then

3

By the way selecting one entity at a time like this takes a lot longer than necessary because there is an animation.  There is a way to code around this, if you are interested please comment below.

Neither of these methods give the results that we are looking for.  So let’s take a close look at the problem, what do all of the edges that we want to select have in common?

4

All of the edges that are missing a face only have one adjoining face. It just so happens that the edge object also has a method called GetTwoAdjacentFaces2, guess what it can do for us.  We can take out the code to traverse loops and then add in the code that takes advantage of this method:
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swPartDoc As SldWorks.PartDoc
Dim swBody As SldWorks.Body2
Dim swFace As SldWorks.Face2
‘Dim swLoop As SldWorks.Loop2
Dim swEdge As SldWorks.Edge
Dim swEntity As SldWorks.Entity

Dim swAdjFace1 As SldWorks.Face2
Dim swAdjFace2 As SldWorks.Face2

Dim vBodies As Variant
Dim vFaces As Variant
‘Dim vLoops As Variant
Dim vEdges As Variant
Dim vAdjFaces As Variant

Dim i As Integer, j As Integer, k As Integer, l As Integer

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swPartDoc = swModel
vBodies = swPartDoc.GetBodies2(swAllBodies, True)
For i = LBound(vBodies) To UBound(vBodies)
Set swBody = vBodies(i)
vFaces = swBody.GetFaces
For j = LBound(vFaces) To UBound(vFaces)
Set swFace = vFaces(j)
vEdges = swFace.GetEdges
For k = LBound(vEdges) To UBound(vEdges)
Set swEdge = vEdges(k)

vAdjFaces = swEdge.GetTwoAdjacentFaces2
Set swAdjFace1 = vAdjFaces(0)
Set swAdjFace2 = vAdjFaces(1)

If swAdjFace1 Is Nothing Or swAdjFace2 Is Nothing Then
Set swEntity = swEdge
swEntity.Select True
End If
Next
Next
Next

End Sub

The Dim statements at the top are not required but they make writing/editing/maintaining the code a lot easier.  The new lines are:

vAdjFaces = swEdge.GetTwoAdjacentFaces2
Set swAdjFace1 = vAdjFaces(0)
Set swAdjFace2 = vAdjFaces(1)

If swAdjFace1 Is Nothing Or swAdjFace2 Is Nothing Then
Set swEntity = swEdge
swEntity.Select True
End If

Which simply says, for this edge (and we do this for every edge) get the two adjacent faces, if there is only one adjacent face then select the edge.

The result of running this code looks like this:

5

Since a planar surface has no options aside from the selection we can just add swModel.InsertPlanarRefSurface just before End Sub and it will add the planar surface.

 

 

 

Become a SolidWorks Expert

If you want to become more efficient with SolidWorks then learn SolidWorks live over the web with Javelin. Classes include Drawings, Surfacing, Sheet Metal, Simulation, plus GD&T.

Leave a Reply

Your email address will not be published. Required fields are marked *