Solidworks-Using Macros to Import Points
SOLIDWORKS-Using Macros to Import Points
Another day, another flower. Sunflowers are beautiful and have delicious seeds, who wouldn’t want to model them? The only bad part is they have hundreds of seeds in an atypical pattern, and I do not want to spend the rest of my life placing points in a sketch.
Good thing SOLIDWORKS has my back, and I can use a macro to insert a text file with coordinates for my seed’s placements, and then I will use a sketch driven pattern to do the rest.
First things first, I have to put the Cartesian coordinates (x,y,z) into a text file.
NOTE: These points will be imported into SOLIDWORKS under the assumption they are in meters, so plan accordingly with non-metric points. You can also scale the points later.
I used some fancy equation to match the swirl pattern found on sunflowers, and used built-in excel functions to list out my positions.
The first column are x coordinates, the second y, and if I had z coordinates, I would have put them in the third column. Put whatever points you want; follow your heart. Save your Excel file as a text file, mine is entitled Pattern Positioning.txt.
Now comes the good stuff. Go to Solidworks, start a new part, and click on Tools, Macro, and New.
This should bring up the Microsoft Visual Basic Format, and now you have to write the code that will store your points.
The code I used is below. What it does is opens the text file and loops through it, storing the data into “skPoints.”
Once the code is written, save it and go back to Solidworks. Go to Tools, Macro, and then Run. Select the macro you just wrote, mine is conveniently titled “Macro1”.
If there are no bugs in your code, the points will be inserted in a 3D sketch, centered around the origin.
And voila, I have my pattern. I used the pattern to place seed-shaped cuts on my sunflower, and added in the petals and coloring to make it look nicer.
Thanks for reading. Leave a comment below if you think the sunflower pattern looks more like a showerhead than a flower.