Boolean return from ICurve.IsCircle seems strange

I was using ICurve.IsCircle as part of a VBA macro to select all circular edges in a sketch. I wanted to add all the circular edges to a collection so I could then parse through for all the full circles. I found (through Reddit questions and testing) that the return value is not truly boolean. It seems to return a 1/0 value instead of True/False. Strangely, it has no problem assigning the return value to a Boolean variable. However, an IF test of the value fails if attempting to use (Not True) in the IF test, even if testing against the Boolean variable assigned. To illustrate:

Dim isCurveCircular As Boolean

Dim i As Long

For i = LBound(vEdges) To UBound(vEdges)

 Dim swEdge As Edge
 Set swEdge = vEdges(i)
 Dim swCurve As Curve
 Set swCurve = swEdge.GetCurve()

 If swCurve Is Nothing Then GoTo NextEdge

 isCurveCircular = swCurve.IsCircle()

 'If Not isCurveCircular Then GoTo NextEdge
     'DOES NOT WORK for Circular Edges (ALWAYS executes GoTo)

 ‘If isCurveCircular <> 1 Then GoTo NextEdge
     'Works correctly - does not execute GoTo if curve IS circular

  If isCurveCircular = 0 Then GoTo NextEdge
     'Works correctly - executes GoTo if curve is NOT circular

Have a look at the following example from API help for your reference: https://help.solidworks.com/2024/English/api/sldworksapi/Select_Edges_of_All_Holes_on_Face_Example_VB.htm

1 Answer

1

This could have something to do with different definitions of booleans. Most booleans are integers underwater. Some systems define -1 as true, some define 1 as true. You can try casting a value to an integer to see which one it is.

In practice, I’ve seen “if boolean then” fail and “if boolean = true then” work well. It’s messy in VBA.

Yes, the issue only showed up when I tried to use the NOT operator. The way it was explained to me is that the problem is implicit conversions. The logical NOT operation is really a bitwise flip of ALL the bits, including the sign bit. Therefore, although True = 1 and False = 0 , (NOT True) = (False) but (NOT 1) = (-2) and in a boolean test, -2 is interpreted as True because it is non-zero. So if the boolean return value is really an integer behind the scenes, you can have problems.

Hmmm. Can you report this via your reseller?

Good idea. I assumed (being pretty new to vba) that this was old news, something known far and wide to those experienced in the craft, and I was just learning the hard way. I will start a ticket and see where it goes.

I asked on 3Dexperience and got an excellent answer. This is expected behavior, and is explained in help. https://help.solidworks.com/2023/english/api/sldworksapiprogguide/Overview/BOOL_and_VARIANT_BOOL_Are_Different_Types.htm?id=1.4.7