Assemble parts with mate references automatically?

Hi there, is it possible to do the following, and if so, how.

I've got an API program that creates an assembly from predefined parts, and the predefined parts have mate references already built in.

For example.

Part 1 has a mate reference called "front", and a mate reference called "rear"

Part 2 has a mate reference called "front"

Part 3 has a mate reference called "rear"

If I manually create an assembly, and then insert parts 1, 2 and 3 in that order they all snap together as planned. Recording and then playing back a macro of this just dumps the second and third parts anywhere without mating them.

Is it possible using SW Student 2011-2012 to do this programatically? I've read lots of webpages and tutorials which don't seem to work, including this one:

http://files.solidworks.com/swexpress/api_tips/SWX012012_APItip.html which causes my Solidworks to crash.

Any ideas on this? It seems like such a simple thing to do, and I can do it manually in a few clicks, but am stumped how to do it via the API.

I'm using VBA by the way.

Cheers

Rory

SolidworksApi macros
Answers 7
KR
KR 2012-04-03 Best Answer

Hi Rory,

First, yes, the student version has the same API as the commercial version so what can be done in one can be done in the other.

As far as I know, using mate references as a basis for mating in the API isn't that simple.  You have to traverse the feature manager tree of the components, get the mate references, get the mate reference entities, and then use those entities to create a mate with IAssemblyDoc::AddMate3.  The advantage of using mate references with the API isn't the simplicity of writing the code, it is the fact that you always know for certain what entities need to be mated programmatically.

However, if you want to go the route of mating progrmmatically with the API then a simpler method is to have the appropriate entities named during part creation.  For example, in a part called A right click on an edge and go to Properties.  You can give the edge a name like "PartACoincident1".  Then in your other part, B, you can have a face with the name "PartBCoincident1".  Then in your assembly you can traverse all of the edges and faces (IBody2::GetEdges and IBody2::GetFaces) until the edges and faces with those names are found (IModelDoc2::GetEntityName), select them (IEntity::Select4), and then add the mate (IAssemblyDoc::AddMate3).

Often times, accomplishing a task in the API is very different from how it is accomplished in the user interface.  In many cases this does mean that the steps taken by the API are far fewer than the steps one would take manually.  Other times it means the opposite.  I think this is a case of the latter.

Keith

Video Tutorials for the SolidWorks API

SF
SF 2012-04-03

Hi Keith, thanks for the reply. It looks like great minds think alike as after much investigation over the last 24 hours I'd already come to the conclusion that the mate reference method was overly complicated and I had already spent most of the evening doing pretty much what you've suggested.

I've created a few pieces of reference geometry (planes, axis etc.) which I've renamed. My API program now looks for those named items and uses them as the basis of the assembly mates.

It seems to do exactly what I want so I think this is sorted.

Regards

Rory

KR
KR 2012-04-04

Good to hear, Rory.  Please mark this thread as "answered" so others know you found a solution.

Keith

Learn the SolidWorks API

AY
AY 2012-05-03

Evening, sir.

I'm engineering student working on my final year project.

This is exactly what i'm struggling with.

I have to create parts and directly assemble them together.

I have read through many discussion and tried some methods but

doesn't seem to work with my current project.

I know that I have to rename the faces, edges and planes to call them for assembly

using mate reference but I can't find any example on that.

Through this discussion, this might be the one i'm looking for.

Can I ask for your kindness for macro file example for this project.

I really need example to work with vba programming.

U can send to my email - aslam.yusof@gmail.com

Thanks on behalf of your kindness

KR
KR 2012-05-03

Aslam,

Here is a blog post on SolidWorks API assembly automation I just wrote on the topic that might help.  I highly recommend you do not use mate references with assembly automation, if you can avoid it.  Ironically it makes everything harder not easier.

That blog post also links to our many tutorials and macros on mating automation.

Keith

Video Tutorials for the SolidWorks API

SF
SF 2012-09-04

Hi Keith,

Could you please help me to solve this problem? My post link: How to get the name of Mate Entity by a set of Entity Parameters?

Cheers,

Bomiao

SF
SF 2013-03-28

Hi ,


I am trying to mate parts using the very same approach. I am very new to SW, let alone the APIs. I am looking for an example which shows how to iterate through features in a part. If you know of any such examples, can you please provide a reference.

Thanks,

Hersh