Skip to main content

Solidwork API Programming: The centerline macro updated!! Run with macro button~

The centerline macro updated!!

I didn't expect I can play with solidworks API programming again but it is actually interesting stuff. So the centerline macro code I wrote a long time ago was reviewed and I decided to modify just a single line. It makes a difference. The code was updated and accordingly the post updated as well. You can copy and paste and try to run referring to previous post.


The problem of previous code was the centerline can be made only on the rectangular face. Just simple change can make the target face to any polygon with an even number of edges. 

Here are changes:

...

For i = 0 To nEdgeCount / 2 - 1
    Set swSketchSeg = pModel.CreateLine2( _
        vMidPts(i * 3 + 0), vMidPts(i * 3 + 1), 0, _
        vMidPts(i * 3 + nEdgeCount * 3 / 2), vMidPts(i * 3 + nEdgeCount * 3 / 2 + 1), 0)
    swSketchSeg.ConstructionGeometry = True
    Debug.Print "vMidPts-from (" & i & ") = " & _
         Format$(vMidPts(i * 3 + 0) * 1000#, "0.0000") & "," & _
         Format$(vMidPts(i * 3 + 1) * 1000#, "0.0000") & " mm"
    Debug.Print "vMidPts-to   (" & i & ") = " & _
         Format$(vMidPts(i * 3 + nEdgeCount * 3 / 2) * 1000#, "0.0000") & "," & _
         Format$(vMidPts(i * 3 + nEdgeCount * 3 / 2 + 1) * 1000#, "0.0000") & " mm"
Next i

...

The red line above was updated from:
vMidPts(i * 3 + 6), vMidPts(i * 3 + 7), 0)



vMidPts is an array to store midpoint location data of edges of the face you selected. I was able to generalize this macro function by replacing hard number with half edge count. The hard number 6 works only on rectangle because 3*4/2=6, where the number of xyz coordinate is 3 and the edge count of rectangle is 4. 



Comments

Popular posts from this blog

How to use pySW - a Python Solidworks VBA API wrapper

 Recently I found a python package called "pySW" to be useful for those who are interested in both Solidworks API programming and python language. If you are familiar with Visual Studio Code and shell command in the terminal, you can easily install and play with it. You may want to visit its author's github repository and try his sample test program. I modified a little to read the lens template and generate the variants. It is almost identical to his box generation sample program. It looks like pySW project has never been updated for last 3 years though. Once the package is installed, you need modify a few lines to make it work. Let me explain one by one.  The web page link PySW 1.4 project in PyPI:  https://pypi.org/project/pySW/ Github repository:  https://github.com/kalyanpi4/pySW Package installation and test program workaround Install pySW Run the following command. You should install other dependent python packages.  pip install pySW ...

Solidworks API Programming: Macro Recording and Editing

Solidworks API Programming, Part 1:  Automatically generated VBA source code analysis Let's start Solidworks API Programming with macro source code analysis. I just explain what I have done in person. Although I have not read the book, "Automating SOLIDWORKS 2006 using MACROS ( http://www.amazon.com/Automating-SOLIDWORKS-2006-using-MACROS/dp/1585032638 )", I think it could be a good reference for serious users. If you know better or best book for Solidworks API Programming, please leave the comment. 1. Macro Recording: Tools - Macro - Record This is the common function of MS-based programs which support VBA just like Excel. This function record the user action itself converting it into VBA source code. 1) Just click Tools - Macro - Record 2) Do what you want to do within solidworks like sketching and extruding 3) After finishing your work, just click Stop 4) Save window pops up. Write the filename and save it. 2. Macro Editing: Tools - Macro - Edit... L...

Solidworks API Programming: Centerline Macro

Solidworks API Programming Part 2: Sketching the cross center line on random rectangular surface Precisely speaking, this program is for the random surface which has even number of edges. Nevertheless, this program is used to draw centerline within sketch on the surface. No matter how it forms angle with front, top or right plane, program should sketch the centerline correctly. So it's not simple. To do so, I need to consider transformation between 3D space and 2D sketch coordinates. Of course This kind of transformation function is supported by Solidworks API. Way to go! 1. Make new macro Just click Tools - Macro - New 2. Source code analysis In VBA, Comment symbol is ' (single quotation) and Continuation symbol is _ (underscore). '-------------------------------------- ' ' Preconditions: ' (1) Part or assembly is open. ' (2) Face is selected. ' ' Postconditions: Plane or face on which ' selected sket...