Blog 8.24.2012 No Comments

CADSharp.com’s first-ever SolidWorks API programming contest has come to a close. Some of the finest API programmers out there stepped up to the challenge described in our last post: Write a macro in as few lines as possible that discovers and displays a single message box listing the children of all part components in the assembly. Without further adieu, here are the results:

1. Ivana K. (15 lines)
2. Josh B. (19 lines)
3. Jeff Z. (20 lines)
4. Satheesh P. (26 lines)
5. Matteo O. (28 lines)
6. Anonymous (57 lines)

Congratulations, Ivana! You will receive a $100 gift card to Amazon.com or one of its international affiliates. Josh and Jeff will each receive a $50 gift card as well. Many thanks to all who participated!

Download Ivana K’s solution

The Solution

Writing a top-tier macro that meets the contest requirements involves three challenges, in my opinion.

The first challenge is simply determining how to obtain the children of a part component. Its not as simple as you might think. IComponent2::GetChildren only returns child components, not child features, which means that on a part component this method returns an empty array. To get the children, we actually need to get the IFeature pointer than corresponds to the part component and then use IFeature::GetChildren. This method returns whatever is listed in the Parent/Child dialog box of a part component. The lesson is, a component is considered a feature by the SolidWorks API. With that in mind, we know to traverse the assembly as a set of features rather than components.

The second challenge is understanding how to write recursive functions. Recursion refers to a function referencing itself. A great example of recursion can be seen in the API Help example Traverse Assembly at Component and Feature Levels Using Recursion. Indeed, many contestants used a heavily-trimmed version of this macro as a means of traversing the FeatureManager tree. Recursion lets a programmer re-use code, which can result in having to write fewer lines of code overall. Such was the case with this macro.

The third challenge is knowing tricks and shortcuts within the VBA or VB.NET programming language to remove as many lines as possible. Ivana’s winning solution illustrates this well:

  1. The text displayed in ISldWorks::SendMsgToUser2 is actually the return value of the getNames functions.
  2. Most of the variable names are declared as function arguments or as For Each loop elements, instead of on their own line. (Note that declaring variables in For Each loops is only possible in VB.NET.)
  3. Automatic type-casting is used to reduce the number of variable declarations. For example, using “swMod.FeatureByName(c.Name2).getchildren” to get the child features of a component.
  4. Use AndAlso and OrElse operators (available in VB.NET) to effectively squish two conditional statements into one line. Normal And and Or operators require all statements to be verified, whereas AndAlso and OrElse will begin executing the statements contained within the conditional once the outcome is guaranteed. So in Ivana’s case, the following logic was applied to each component in the assembly: If the component is a sub-assembly then traverse its features, or, if the component has child features then it must be a part component so add the names of those child features to the final results string.

We can see, then, that VB.NET does offer some significant advantages in terms of reducing line length.

(Note: Should you desire to count the lines in the winning solution on your own, keep in mind that comments, blank lines, Imports statements, and Class declarations are not counted. Also, lines containing 2 or more variables declarations on the same line are counted as separate lines.)

Do know of any alternate solutions or have any questions about the solution we discussed? Please share below.

Until next time,
Keith

Want to be informed of future CADSharp.com contests? Sign up for our newsletter.


Questions and Comments

You must be logged in to post a comment.