Let's start with a
Youtube movie. It captures the process of generating 3D CAD model of a UHV (ultra high vacuum) chamber at a mouse click using Solidworks API. VBA source code was executed on Visual Studio Community 2017, driving 2017 version of Solidworks. The entire process from 3D sketch skeleton to chamber main body to final assembly containing CF flanges properly mated in the correct positions and orientations is fully automated. Intermediate files are all cleared, leaving the final assembly only on the screen at the end. A highly efficient method particularly during chamber designing stage, since one only needs to change a few numbers defining port geometry and simply re-run the code if ports need to be moved / added / deleted / etc. In other perspective, mouse click and keyboard input by user in conventional Solidworks GUI are all converted to corresponding code, so that the whole script can be sequentially batch-executed all at once.
Solidworks API official documentation is not of high quality, probably because it serves small community of API programmers. Even worse, I had little experience not only with the API but also the Visual Basic itself. So there were so countless times in which the code snippet generated by Solidworks Macro didn't work when imported to VBA, and in which I could not find help from extensive Google search. In fact I chose VBA out of different languages since most of the helpful comments available online was for VBA. But I still had to waste hours occasionally trying to figure out what to do to make it work the way I wanted. This article is to summarize and share my knowledge and know-how obtained therefrom with wider public. The code was written in summer 2015 and has been forgotten since; it took me another day to retrieve, re-run and update the code to make sure that it works in Solidworks 2017. Now I am finally ready to write an article, hoping it helps developers of all languages through technical issues I already underwent.
First of all, you need to import references to the following two modules into your project:
Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Actual files are located in C:\Program Files\Solidworks Corp\SolidWorks\api\redist. (your location might be different). Also, refer to this
MDC article on the chamber geometry convention. Data structures and constructor compatible with the convention are given in lines up to 73. Lines 79~98 specify port geometry for my chamber which has two focal points. The variable 'FlangeOffsetDistance' represents the depth of the circular cutout to receive port tubing, measured from the face with knife-edge. Unfortunately I used meter units throughout so a lot of tiny sub-decimal-point numbers.
Main 'belly' of the chamber starts from line 146, in the order of: [1] 3d sketch [2] set a plane per port (this will be used for mating flanges in the later assembly stage) [3] main cylinder and port cylinders (solid cylinders at this point) [4] remove inner volume using shell operation for a specified shell thickness (you have to select EVERY circular faces for the shell operation to proceed as intended) [5] save parts file.
Line 226 and onward are for assembly. The code opens an empty assembly file, then load the above parts file, unfix it, place on the origin and properly align using planar coincident mates (lines 236~260). Note that the first part loaded into an assembly is set fixed, so you have to un-fix it first in order to move or re-orient it. The three mates used here are deleted but don't need to be. Through lines 262~291 each flange file is opened, inserted into the assembly, and mated (x3). The grand assembly is saved once flange insertion is all over.
Note that the flange parts files were drawn from scratch by me, duplicating specs and CAD models available on MDC site. (There were reasons I decided to do these chores instead of using theirs but I forgot.)
Overall, the code should be straightforward and easy to understand; because it doesn't invoke any 'algorithm' per se, and because I inserted a few comments. One exception is the line 201, with comments in lines 202~ 204:
swPart.ShowNamedView2("", 8)
If any of the reference planes turns out to be oriented such that the plane normal is parallel to your monitor screen, or equivalently if any plane appears to be a line, the code fails to select the circle on that plane, and shell command will work only partially. Such coincidence can be avoided if one rotate the view orientation. Possible new orientation depends on a number of parameters including port geometry and your monitor specs. Switching to Trimetric in line 201 worked in my case but should not work in general.
swPart.Extension.SelectByID2("Top", "PLANE", 0, 0, 0, True, 1, Nothing, 0)
swPart.Extension.SelectByID2("Top Plane@" & FlangeTypeString(portschematic(i).FlangeType) & "flange-" & FlangeUsageCount(portschematic(i).FlangeType) & "@" & AssemblyTitle, "PLANE", 0, 0, 0, True, 1, Nothing, 0)
One other thing to note is the internal name of fundamental reference planes, potentially different Solidworks version by version. Solidworks 2014 uses "Top" but 2017 uses "Top Plane" for instance. I designed individual flanges with Solidworks 2014 but ran the above procedure in Solidworks 2017. That is the reason for the mixed notation in the code.
Words can't capture everything. You should have issues, or find unclear statements above. Please feel free to leave a comment.