« MidWest User Group Conference Update | Main | Geometry Lock Enhancement Request »

November 11, 2007

Framed Box Revisited

In March 2007, I wrote a tutorial on creating a SolidWorks Macro that would create a Framed Box.  This macro worked by creating a cube, then selecting 3 faces and cutting extrudes thru each of them.  I decided to revisit this series of macros and add a few enhancements.  What I wanted to do was to allow the user to control the feature sizes via a custom SolidWorks PropertyManager Page.  The PropertyManager Page gives a macro a look and feel that is it part of SolidWorks.  The PropertyManager Page requires a program module to set up a few variables to setup and launch the PropertyManager class and PropertyManagerHandler class.  There is more learning involved in implementing this feature.

I tried to figure this one out, but soon realized I need some help via a good example. 
I am a strong supporter of “Learning By Example” for anyone who writes programs or macros for SolidWorks. 

In my searching for a good example, I found the macro “VBA_PropertyManagerPage.swp” on SolidWorks’ website.  This macro was put together as an example of how to create the PropertyManager page and how to add all of the control types that are available.  I started with this example, customized the form to suit my needs and I integrated my FramedBox macro.

I wanted the new macro to dynamically update the model while the PropertyManager page stayed open.  While working on the new macro, the PropertyManager page kept closing unexpectedly.  This was due to a conflict between the dynamic updated (I wanted) and the controls at the top of the page.  This was corrected by removing the “swPropertyManager_OkayButton” and adding the “swPropertyManagerOptions_LockedPage

Because of the capabilities of the PropertyManager page, I realized that I could improve the flow of the macro.  Now the macro first creates the framed box model, and then presents the current dimensions to the user.  As the user changes the dimensions, the model is dynamically updated.  Run the “FrameBoxPMP.swp” macro to see the new version.  This is way cool!

My enhancements did not stop there.

One of the inherent problems with this macro was that it cannot be used to create geometry in an existing model that once contained any type of geometry, even a simple sketch.  This is due to the macro accessing and modifying the dimensions, after the model has been created.  Looking thru the macro, you will see parameter references like "D1@Sketch1", "D1@Extrude1", and more.  These are the parameters (dimensions) that were added as the model as it was created.  To properly access and change these parameters, the parameter references in the macro must match the parameter references in the model exactly.

The macro presumes that the first sketch it will create will be "Sketch1" and the first feature will be “Extrude1”.  If a feature or sketch once existed, and was later deleted, this will throw the parameter references off of what is expected, and the macro will fail.  I wanted to get beyond this problem.  What I needed to do was get the sketch and feature names that are created by the macro.

The prefix (D1, D2, etc) of these parameter references point to the dimension number in the sketch or feature.  Since I am only adding dimensions, these prefixes should not change.  The suffix (Sketch1, Extrude1, etc…) is what causes the problem because they point to the sketches and features in the model.

While you are in a sketch it is active, this is the best time to get its name.  Searching thru the SolidWorks Add-ins and API Help file, I found I could use “Part.GetActiveSketch2” and “Sketch.Name” to get the name of the active sketch.  Here is how I got the sketch name as soon as it was created:
  Part.InsertSketch2 True
  Set swSketch = Part.GetActiveSketch2
  fb_ExtrSketch = swSketch.Name
  Set swSketch = Nothing

You can’t get the feature name until after it’s created, and once it’s created, it is not active.  Looking in the SolidWorks Add-ins and API Help file I also found I could use “Part.FeatureByPositionReverse(0)” to get the name of the last feature created.  Here is how I retrieved the name of the feature.
  Set theFeature = Part.FeatureByPositionReverse(0)
  fb_ExtrFeature = theFeature.Name
  Set theFeature = Nothing

I incorporated capturing these names into the appropriate places in the BuildModel routine of my macro.  I saved the names to variables for later use.  Now I needed to change the parameter references to access the features and sketches that were created by the macro.  To do this, I needed to find all existing parameter references and incorporate the variables containing the feature and sketch names.  References to "D1@Sketch1" were changed to "D1@" & fb_ExtrSketch, and so on.

Now, it does not matter how many times you run the macro in the same model, and delete all of the geometry, this macro will work as intended.

Whether or not this macro is done is still unknown.  It all depends on what I want or need it to do in the future.  Who knows?  I may visit it again in the future.

You can download accompanying macros here.

Once again, good luck.

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/t/trackback/861717/23259406

Listed below are links to weblogs that reference Framed Box Revisited:

Comments

Great teaching example Lenny, and way to go staying with the program until you got it to do what you want.

Thanks, Diego

This macro is primarily a teaching aid to illustrate some of the thoughts and working behind bringing a macro from a recorded macro to a fully functioning macro. The macro could be considered complete at any stage the programmer (or user) wants or needs. By including and discussing the various stages, and identifying the processes between each stage, readers can see how to improve some of their own macros. I do hope that other users can learn from my experiences, and improve their knowledge of SolidWorks macros.

Nice work. The Property Manager is notoriously difficult to work with from a Macro instead of an Addin. Good job wrestling it into submission : )

Jeff,
Thanks for the comment. Once I found a good example, it was easy to follow it to see how it worked. The "VBA_PropertyManagerPage.swp" example was a big help and I also found it helpful to "lock" the PropertyManagerPage so it stayed on until the user closed it.
Thanks again. I'll see you in San Diego.

Post a comment

If you have a TypeKey or TypePad account, please Sign In