EASY MACROS 2 - Query Face Geometry

By Rhyc Sandberg

Easy Macro Introduction

This is the second article in a series that teaches API programming through macro examples that are simple, fast to write, instructive and useful.

Rather than spending a lot of time trying to understand everything about programming and Visual Basic before writing anything, the intent of these articles is to jump head first into the shallow pool by showing the steps to write simple macros using Visual Basic for Applications (VBA). We have some excellent earlier blog articles that go into more detail about the concepts being used.

Query Face Geometry

 

What Geometry Type is that Face? - or, Query Face Geometry

Have you ever looked at a part in SOLIDWORKS and wondered what kind of geometry was used for a specific face? If it's someone else’s part or you imported it, it can be difficult to know what geometry makes up the body. Knowing this information can be helpful for many reasons – like troubleshooting errors and warnings from creating new features or generating g-code for CAM applications. In particular, it can be good to know whether the geometry is an ‘analytic/algebraic’ type (planes, cylinders, spheres, cones and tori) or a spline type. The algebraic types are defined to a higher precision. This macro emphasizes two main points:

  1. a frequently used data type for lists of values called ‘enumerations’, and
  2. that the APIs can have functionality that is not always available to the Graphical User Interface (GUI) that we normally use (i.e. there is no equivalent command when using SOLIDWORKS)

Macro Overview

We would like to query a face of some solid or surface body in our open document to find out what kind of geometry it is – a plane, cylinder, sphere, spline, etc. The basic steps will be something like this:

Prior to running the macro, the user needs to:

  1. Open a SOLIDWORKS document with a solid or surface body (part or assembly document)
  2. Select the face of the body that you want to query

The Macro will then:

  1. Get access to the selected face
  2. Get access to the face’s ‘Surface’ object (the part that holds the geometry definition)
  3. Call the API that grabs the geometry type of the surface
  4. Display the result of API

Finding the Right API in Help

One of the first steps in writing a macro is to find the right API method in the help file. This can be tricky if you don’t know where to look for it. The search is great, but you need to know the right terminology for it to work. You may need to try several different words to see what works. Or maybe start with a more general Google search to see what comes up – like ‘SOLIDWORKS API face geometry type’ or ‘SOLIDWORKS API surface geometry type’.

If these searches don’t help, try the forumsMySolidWorks, or log into our Support portal and search the Knowledge Base.

With a search for “surface type” in the API help file (using the double quotes to search for those two words used together), we get a list of useful topics. In fact, there is one in there called ‘Get Surface Type Example (VBA)’ which is what I used to create my version of this macro.

Looking further in the search result, we find a method of the ISurface interface (object) called ‘Identity’. The description of this method in the help file says ‘Gets the type of surface’. A better description would be ‘Gets the geometry type of the surface from a face’.

Accessing the ISurface Interface

First, we need to know how to access this ISurface method (See the first Easy Macro blog and SolidWorks API Building Blocks – Part 3 for more information on why this is necessary). If you click on the link on this help page for ‘ISurface’ it will bring you to the page for ISurface and includes a section titled ‘Accessors’ (ways to ‘access’ this interface). From this list we find the ‘GetSurface’ method from the IFace2 interface. We could keep going in that fashion to see how to access a face with the IFace2 Accessors. But this is where a new Interface object steps in – the Selection Manager, or ISelectionMgr. This interface gives us a way to capture selected entities in an open document.

Setting Up the DIM and SET statement to Access the Identify Method

We need to have DIM and SET statements for each of the object types needed to access our Identify Method. They are as follows:

Dim swap           As SldWorks.SldWorks

Dim swModel        As SldWorks.ModelDoc2

Dim swSelMgr       As SldWorks.SelectionMgr

Dim swFace         As SldWorks.Face2

Dim swSurf         As SldWorks.Surface

Set swApp = Application.SldWorks

Set swModel = swApp.ActiveDoc

Set swSelMgr = swModel.SelectionManager

Set swFace = swSelMgr.GetSelectedObject6(1)

Set swSurf = swFace.GetSurface

The Set statement for swFace is using a method from the SelectionMgr interface to grab the selected face. The ‘6’ at the end of ‘GetSelectedObject’ is the latest ‘version’ of this method and the ‘(1)’ is telling it to grab the first item in a list of entities. With the final ‘Set’ statement, we are getting the ‘Surface’ object for the selected face – and we can now use this to access our Identify method! Whew! All that, just to get access to the Identity Method on the selected face.

The Surface ‘Identity’ method

This method will return a text string that tells us what type of geometry is used for the selected face. There are several types of geometry that can be used to define a surface, so it must be picking from a list of the different types. The ‘Return Value’ line in the help page states that it is returning ‘Type of surface as defined in swSurfaceTypes_e’. This is a text string value from a list of values using a basic data type called an ‘enumeration’.

What’s an Enumeration?

In programming, an enumeration is a data type that is made up of a set of specific values called elements or members. There are many enumerations defined in the SolidWorks API. If you open up the API Help file, under the ‘SOLIDWORKS API Help’ topic, ‘SOLIDWORKS Enumerations’ is one of three sub topics listed. If you open up that topic, you will find *a lot* of enumerations for all kinds of things. You typically wouldn’t be looking into this section of help as a starting point of your macro. You would see an enumeration type as part of an API that you want to use, so you will look into the enumeration topic to see what values it has.

Clicking on the link for our swSurfaceTypes_e enumeration shows that we have 10 types of geometry that can be returned – 5 spline types and 5 algebraic types.

We can use the VB command ‘MsgBox’ to display the text from Identify like this:

MsgBox “Surface Type =  “ & swSurf.Identity

In this example, we ‘pretty’ it up a bit by using a text array (‘swSurfaceTypes(10) As String’) to give it a more meaningful text string then we get directly from the Method. And finally, the enumeration list is added as a comment at the top of the macro for reference.

Here is the code:

'----------------------------------------

' Written by Rhyc Sandberg, 2014 - derived from SOLIDWORKS API example

' Preconditions:

'          (1) Part or assembly document is open.

'          (2) Face you want identified is selected. '

' Postconditioins: None '

'-----------------------------------------

Option Explicit

' Here is the definition of this enumeration from the API help - for reference

' Public Enum swSurfaceTypes_e

'    PLANE_TYPE = 4001

'    CYLINDER_TYPE = 4002

'    CONE_TYPE = 4003

'    SPHERE_TYPE = 4004

'    TORUS_TYPE = 4005

'    BSURF_TYPE = 4006  - Spline

'    BLEND_TYPE = 4007  - Spline created from a fillet command

'    OFFSET_TYPE = 4008  - Spline created by offsetting another spline

'    EXTRU_TYPE = 4009   - Spline created by extruding a spline sketch entity

'    SREV_TYPE = 4010   - Spline created by revolving a spline sketch entity

' End Enum  

Sub main()

Dim swSurfaceTypes(10)      As String

swSurfaceTypes(1) = "PLANE"

swSurfaceTypes(2) = "CYLINDER"

swSurfaceTypes(3) = "CONE"

swSurfaceTypes(4) = "SPHERE"

swSurfaceTypes(5) = "TORUS"

swSurfaceTypes(6) = "SPLINE"

swSurfaceTypes(7) = "FILLET BLEND"

swSurfaceTypes(8) = "OFFSET SPLINE"

swSurfaceTypes(9) = "EXTRUDED SPLINE"

swSurfaceTypes(10) = "REVOLVED SPLINE"  

Dim swApp                   As SldWorks.SldWorks

Dim swModel                 As SldWorks.ModelDoc2

Dim swSelMgr                As SldWorks.SelectionMgr

Dim swFace                  As SldWorks.Face2

Dim swSurf                  As SldWorks.Surface  

Set swApp = Application.SldWorks

Set swModel = swApp.ActiveDoc

Set swSelMgr = swModel.SelectionManager

Set swFace = swSelMgr.GetSelectedObject5(1)

Set swSurf = swFace.GetSurface   MsgBox "Surface Type = " & swSurfaceTypes(swSurf.Identity - 4000)

End Sub  

Finally, you can create a keyboard shortcut to access this macro for a quick way to find out the geometry of a face. See the earlier blog article on keyboard shortcuts for the set up steps.  

Was this article helpful?
0 out of 0 found this helpful

Comments

0 comments

Please sign in to leave a comment.