4 Replies Latest reply on Jan 22, 2016 3:09 PM by Joe Pickens

    I am try to get started on a macro to create a mated assembly

    Joe Pickens

      What I want the macro to do



      1.       Open Prompt to select Assembly Templet and prompt to save Assembly.  If ASM file is already open skip first step

      2.       Create empty text file with same name as the assembly

      3.       Prompt for an Excel file location with the Bill of Material

      4.       Search for CAD files that match the part #’s listed in the excel file A6:AX

      I.        I file found go to step 5

      II.      If file not found write part number in text file in format of part#-File Does Not Exist

      5.       Add part to Assembly File

      6.       Mate part in assembly file accordingly

      I.        Components will have Planes, Axes, and Points  in the feature three listed in importance order.

                                                                     i.      Ie. the macro for this part would ignore all default named planes (ie Front Plane, Top Plane, Right Plane, Plane1:PlaneX) and look at unique named planes in order so the first one would be the CENTER PL and see if this plane exists in the open top level assembly and if it does not exist in the top level assembly then it would look the planes in the first component in the ASM and so on and if this plane is not found in and components the macro would move on to the next one which would be the FRT AXLE PL. If the plane is found it is the mated and the macro will move on to the next plane

      1.       When inserting a component a plane, axes, point mate will only be done once but it can be mated to other inserting components.

      a.       Ie. When part 6 with planeA is being inserted into the ASM  and part 3 and part 4 have planeA as soon as the macro mates part 6 planeA to part 3 planA the macro should move on to trying to mate planeB in part 6 and not continue on trying to mate part 6 planeA to part 4 planeA

      II.      Like Geometry will only be mated ie Planes to Planes, Axes to Axes, and Points to Points

                                                                     i.      Plane to Plane mates are Coincident

                                                                   ii.      Axis to Axis mates are Concentric

                                                                  iii.      Point to Point mates are Coincident with axes aligned

      III.    Minimal mates are to be used when component is inserted

                                                                     i.      Max 3 Planer mates

                                                                   ii.      Max 1 Planer mate 1 Axis mate

                                                                  iii.      Max 1 Point mate

      IV.    If No mates are able to be made the component will be inserted in the position where its origin and the assembly origin are the same





      Pseudo Code


      1.       If SolidWorks assembly is not active then prompt to choose assembly templet

      2.       Create text Document

      3.       Set Assembly to Visable

      4.       Prompt for excel document to use for Bill of Material

      5.       Use Part #’s found in A6:AX to find Component files in found in Solidworks EPDM CAD folder and SubFolders

      6.       If component not found write to text file in format AX not in SolidWorks

      7.       If component found then Insert the component into Open Assembly where the Component Origen and Assembly origin are in same spot

      8.       Search part Feature tree for Points first then Planes then Axes that don’t have a default name scheme

      9.       Take first found feature in part and see if it matches its equivalent in the open assembly then in the preceding parts in the order they are in the assembly tree

      10.   If match found then mate

      11.   If mate causes mate error then remove mate

      12.   if Comonent is fully confined then go back to step four and compile for AX+1

      13.   If component is not fully defined look at Components Mates folder and if there is a Coincident and a Concentric mate then go back to step four and compile for AX+1


      14.   If component is not fully defined look at Components Mates folder and if there is NOT a Coincident and a Concentric mate then Move next Plane or Axis


        • Re: I am try to get started on a macro to create a mated assembly
          Josh Brady

          So... What is your question? 


          Do you want to write yourself, or do you want someone to write for you?  Free or paid?  I'd wager this one is a bit too great a time investment for someone to do for free. 


          If you want to write yourself, do you have any skills? You know,  numchuck skills, bow hunting skills, computer hacking skills, API skills?  I would roughly put this macro at a 7/10 difficulty rating for someone who (like me) has no training and whose real job is something else but plays at API really hard anyway.  Not to be discouraging, but unless you're already at least capable of 3 difficulty already you have too much to learn before you will be able to be helped on an internet forum toward this particular macro. 


          Of course, the preceding is the opinion of an unqualified hack based only on the content of your post.  No disrespect intended, just the ramblings of some guy on the internet who looks at the SolidWorks forums at 10PM.

            • Re: I am try to get started on a macro to create a mated assembly
              Joe Pickens

              A short background on me is I Have written ILogics in Inventor DiveWorks in Solidworks. I have Fixed small errors in macros and have slightly modified macros to do different things but i have never done programming or made a macro from scratch. I have taken this side task and have been identifying what has to be done to make it reality and this is my first round draft of what has to be accomplished in a macro to do this and i am looking to try to wright the macro and honestly for one of this magnitude I dont know were to start or even if it is possible to accomplish something of this magnitude with a macro. The hardest thing for me writhing a macro is finding what commands to accomplish what i want to do.


              In sort i want to know if its possible to do this in a macro and any hints on how i can accomplish this. Me paying for it or having someone give it to me for free wont really help me cause I will have to be able to Debug it and this is only Step one of what i am trying to accomplish.

                • Re: I am try to get started on a macro to create a mated assembly
                  Josh Brady

                  Sounds good... I appreciate your strong ownership desire to make it for yourself.  It sounds like you have a decent background with programming logic/structure and probably VBA, as well as an idea of how the code can decide what to do next. 


                  There are examples in the API help to do most of what you want to do... Traversing the feature tree, creating new documents, inserting components into an assembly etc.  Without a little more specific question it's hard to say the best place to start.  The trickiest part will probably be the mating.  I wrote a few macros some years back to add different kinds of mates between selected components.  Take a look at:





                  I would definitely recommend attacking this one in chunks.  Write one macro that does nothing but create a new assembly document and insert some components.  Another macro to add one mate between two components.  Another one to create a text file and read/write from it.  Don't try to put all the pieces together at once or it'll be too hard to track down bugs.