Blog

Blog2023-10-15T01:29:24-04:00

Jedi skills : How to create bodies without using features

Yes, it’s true. You can create bodies “on-demand” without using features—using the SolidWorks API, of course. How does this magic happen? Enter in a rarely used but very powerful interface called IModeler. This interface contains no less than 30 members (not including obsolete members) for creating every kind of solid body, surface body, and curve you can think of—all without clogging up your feature tree. Best of all, since these temporary bodies are still true bodies, any IBody2 member can be used on them.

Why are they temporary? Because as soon as the IBody2 object for the temporary body becomes Nothing, *poof*. They’re gone. You ask, “What’s the point, then?” Consider these scenarios:

1. Previews. The most obvious use here would be a macro feature preview, but a preview could be used in other contexts as well. Temporary bodies can be easily updated with new user input, and the programmer can choose not to make the temporary body selectable—just like you’re familiar with already when previewing a feature.

2. Performance. IModeler generates temporary bodies far faster than standard features. Combine this with the default invisibility of temporary bodies and the ability to use IBody2 functions on them, and you have a great tool for calculating mass and volume properties very quickly and without the user knowing. For example, perhaps your macro or add-in needs to calculate the volume inside of a pressure vessel. Using IModeler you could create a body that consumes the pressure vessel, run a subtract operation using IBody2::Operations2 using the pressure vessel as the tool body, delete out the unnecessary bodies, and then find the remaining volume.

Finally, in the event that you do want the temporary body to become a “normal” feature, no problem. That is what IPartDoc::CreateFeatureFromBody3 is for. That brings us to our final use of IModeler:

3. Macro features. A macro feature is a custom feature and consequently any body produced by this feature cannot come from a standard feature, hence the need for IModeler.

Want to see a simple example IModeler in action? Try out this code with a blank part document open:

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swModeler As SldWorks.Modeler
Dim swBody As SldWorks.Body2
Dim swFeat As SldWorks.Feature
Dim swPart As SldWorks.PartDoc
    
Sub main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swModeler = swApp.GetModeler
    
    Dim dblData(8) As Double
    dblData(0) = 0
    dblData(1) = 0
    dblData(2) = 0
    dblData(3) = 1
    dblData(4) = 0
    dblData(5) = 0
    dblData(6) = 0.1
    dblData(7) = 0.1
    dblData(8) = 0.1
    
    Set swBody = swModeler.CreateBodyFromBox(dblData)
    Set swPart = swModel
    Set swFeat = swPart.CreateFeatureFromBody3(swBody, False, 0)
End Sub

The result should be a cube created instantly—without creating any sketches or using any features.

Want to learn more? Our video tutorial on temporary bodies, found in our complete VBA course, will show you how to do the following:

  • How to display temporary bodies (without converting them to permanent bodies)
  • How to check temporary bodies for faults
  • How to perform boolean operations (e.g., add, subtract, combine) on temporary bodies
  • How to change the color of temporary bodies
  • How to make temporary bodies unselectable

In the very near future we’ll be posting a video showing how to use temporary bodies in macro feature previews, so check back soon.

Happy coding,
Keith

PS: Don’t forget that our 20% off discount lasts only until March 10th! Just use coupon code “sww2012”.

Want to keep up with future CADSharp.com macros, videos, and live training classes? Sign up for our newsletter.

By |February 29th, 2012|0 Comments

Video: Learn API Basics in Just One Hour

At SolidWorks World 2012, I taught 30 API novices the following in a single hands-on session:

  • How to use the macro recorder
  • How to edit and clean up the recorded code
  • How to declare, assign values to, and use variables
  • How to research API calls in the API Help
  • How to implement auto-completion in VBA macros
  • How to create user forms with text boxes and buttons

A couple days ago I recorded myself delivering that same presentation, for you to watch for FREE if you sign up for our basic membership today.  As one student noted, this presentation will “take away your fear of the API”.  Lesson code and a free VBA cheat-sheet included.

If you’re already a member, just head over to the Videos section and watch it right now.  Hope you enjoy watching it as much as I enjoyed making it.

Keith

Want to keep up with future CADSharp.com content, webinars, and special offers? Sign up for our newsletter.

By |February 25th, 2012|0 Comments

SWW 2012 : Summary, developer poll results, coupon


Another SolidWorks World, come and gone.My first was in Anaheim two years ago.  I think the first one is always a bit overwhelming.  Last year was overwhelming because it was the first time I had presented (two presentations, actually) and I was barely ready.  This year, my first with CADSharp LLC, my focus was a little different.  Instead of trying to hit as many sessions as possible, I made sure my presentations were polished and spent much time talking with SolidWorks users and exhibitors to learn about their automation needs. (see poll results below)

My first presentation, Taking Macros to the People: Hands-On For Beginners, was a modified version of the same presentation I gave last year under the same name.  Last year I tried to accomplish too much and ran out of time.  This year we focused on the macro recorder more, and in an hour and a half we had a simple macro that let the user create an extruded base of a specified depth using a form.  Feedback was excellent.

My second presentation, Goldmember: 99 Must-Know Methods and Properties of the SolidWorks API, also went very well.  We covered a 500 line macro that automated the creation of a part, assembly, and drawing.  This was also well-received, with many people noting how much they learned.

As I mentioned, I didn’t attend many presentations, but one I did attend was “API Jump Start” by Arnav Mukherjee—principal software engineer at SolidWorks Corporation.  Arnav started off by showing some API basics before moving to a fascinating example of using Excel graphs to dynamically monitor calculations from the Motion API.  He topped it off by connecting SolidWorks with Microsoft’s Kinect technology, allowing audience members to move the model using Xbox controllers.

I also I also spent a good bit of time in the exhibitor’s hall, talking to as many add-in developers as I could.  My two main questions were, “How did you learn the SolidWorks API?” and “What language did you use to program your add-in?”  I haven’t done an official tally, but here’s the breakdown:


Concerning the first question, it is remarkable to me how many people used the macro recorder and and API Help to wade through their API education.  That is basically my story as well.  Only person told me that their development team had any serious in-house training system for teaching the API.  I was also intrigued to learn that one gentleman flew from Lithuania to England to take the official API training course.  Another user I talked was planning to fly from Bahrain to Texas (!).  As for the second question, I think this pretty well verifies that I ought to create a sister course .NET course for C#.  At least the add-in portion.

The San Diego Convention Center was gorgeous.  Here is a view looking down at the Gaslamp Quarter from the second floor, where the sessions were held.


As always, the food and entertaingment was amazing.  The CSWP event took place on a nearby retired aircraft carrier.  As for Tuesday night, the Gaslamp Block Party was back, and I think it lived up to its expectations.  I had a lot of great conversations with those who recognized me from the Goldmember presentation.

As for the general sessions, I missed the first day, but from what I can tell, no real big news.  Deelip Menezes thinks, based on certain comments made, that SolidWorks V6 will appear in 2013.  I’ll just have to see it to believe it.  Bernard Charles did state that when the new product comes out, no one will be forced to switch.  My main interest, of course, is what the API will look like.

If you want to watch the recap of SWW, it is at the end of General Session Day 3

What were some of your memorable experiences from SolidWorks World 2012?  Favorite presentations?

BONUS!

Until March 10th we are running a 20% discount on our Power User membership.  Just use the code “sww2012” (without quotes) when you purchase.

Want to keep up with future CADSharp.com macros, videos, and live training events? Sign up for our newsletter.

By |February 21st, 2012|2 Comments
Go to Top