5 Replies Latest reply on Dec 20, 2012 1:25 PM by Dan Millar

    Macro to measure and record distance

    Dan Millar

      Hi all,


      I'm quite new to macros and I'm wondering if any one knows how to set up a macro that would be able to open a design table, increment a value by 5, close the design table, then measure the distance between two pre-selected components (essentially the Clearance Verification function) and finally record that value to an excel sheet (a different one from then the design table).

      Essentially what I'm trying to do is generate a table that will show the distance between two parts as one of the objects oscillates. I'm then looking to do this in multiple different orientations meaning there are a very high number of repetitions for this procedure (over 30,000).


      Being able to automate any part of the procedure would be a great help.





        • Re: Macro to measure and record distance
          Keith Rice



          This is certainly possible. Using API calls like IDesignTable::EditTable2 you can get a pointer to the Microsoft Excel worksheet in order to edit it. Likewise, you can get the clearance distance between two components using IDragOperator::Clearance.


          If you're looking to write this yourself and you're completely new to macros, I would recommend you check out the resources in the link below. You might also benefit from this article to make your learning more efficient: 7 Mistakes New SolidWorks API Programmers Make



          Video Tutorials for the SolidWorks API

            • Re: Macro to measure and record distance
              Dan Millar



              Thank you, that was very helpful. However, I'm going to hold off on clicking correct answer until I've had a chance to write the macro.




              • Re: Macro to measure and record distance
                Dan Millar



                I've finally found time to start writting this macro and unfortunately have gotten a bit stuck. I can't seem to get the IDragOperator::Clearance call to work correctly. Most importantly I'm not sure what to put in as the parameter to get the clearance between the correct components. The API help tells you to put in a single integer as the "index of the components". 


                At the moment the value that the clearance call is giving me prints out as: -1.#INF


                Below is the portion of the code that selects the appropriate components, attempts to get the clearance between them then clears the selection.



                  If intCurCol = 1 Then

                        strActColNam = "COLLAR_FDT_ClearTest-1@Float Displacement Param Model"


                        strActColNam = "COLLAR_FDT_ClearTest-2@Float Displacement Param Model"

                    End If


                    boolStatus = swFDmodel.Extension.SelectByID2(strActColNam, "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)

                    Debug.Print "1. " & boolStatus


                    boolStatus = swFDmodel.Extension.SelectByID2("CRC - MTRC_model-1@Float Displacement Param Model", _

                    "COMPONENT", 0, 0, 0, True, 0, Nothing, 0)

                    Debug.Print "2. " & boolStatus


                    swDragOp.DynamicClearanceEnabled = True


                    dblTreadDist = swDragOp.clearance(1)

                    Debug.Print dblTreadDist


                    swFDmodel.ClearSelection2 (True)




                Thank you in advance,


              • Re: Macro to measure and record distance
                David Wright

                Have you tried the ClearanceVerification tool? swCommands_ClearanceVerification