{"id":5321,"date":"2014-04-06T17:04:38","date_gmt":"2014-04-07T00:04:38","guid":{"rendered":"https:\/\/blogs.plm.automation.siemens.com\/t5\/Solid-Edge-Blog\/VB-in-the-Variable-Table-Sky-s-the-Limit\/ba-p\/26946"},"modified":"2026-03-26T07:30:49","modified_gmt":"2026-03-26T11:30:49","slug":"vb-in-the-variable-table-skys-the-limit","status":"publish","type":"post","link":"https:\/\/blogs.sw.siemens.com\/solidedge\/vb-in-the-variable-table-skys-the-limit\/","title":{"rendered":"VB in the Variable Table &#8211; Sky&#8217;s the Limit"},"content":{"rendered":"<p><P>The Variable Table in Solid Edge supports VBScript functions. This presents endless possibilities for users who are not programmers in the strictest sense of the word but have a rudimentary understanding of writing logical expressions. The Variable Table can be used to define and edit functional relationships between the variables and dimensions of a design.<BR \/>The relationships can be defined using functions and formulas which can be<BR \/>\u2022 Mathematical,<BR \/>\u2022 Trigonometric,<BR \/>\u2022 Logical or<BR \/>\u2022 User-defined fnuctions in a BASIC or .bas file.<BR \/><BR \/>BASIC is Beginner&#8217;s All-purpose Symbolic Instruction Code.<BR \/>A rudimentary understanding of writing logical expressions in an all easy-to-use syntax using plain notepad is sufficient to start off.<BR \/><BR \/>This implies the user need not have any kind of indepth programming knowledge nor be a master of technologies like .Net or C++ to unleash the power of user-defined functions in Solid Edge.<BR \/><BR \/>Using an IDE such as Visual Studio can be useful for finding syntax errors and probably debugging little code snippets, though this is not absolutely necessary.<BR \/><BR \/><STRONG>\u2022 Mathematical and Trigonometric Functions<\/STRONG><\/P><br \/>\n<P><BR \/>It appears that the mathematical and geometric functions that Solid Edge support are directly mapped from the VBScript&#8217;s Math class.<BR \/><BR \/><span class=\"lia-inline-image-display-wrapper lia-image-align-inline\" style=\"width: 490px;\"><img decoding=\"async\" src=\"http:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/8\/2019\/09\/01-13.png\" alt=\"01.png\" title=\"01.png\" \/><\/span><BR \/><BR \/>The only important point to note are the SIN, COS and TAN functions which take the angle in radians and not degrees.<BR \/><BR \/><STRONG>\u2022 Logical functions<\/STRONG><\/P><\/p>\n<p><P>These remain largely un-documented. Use of logical operators like Less than (&lt;),&nbsp; greater than (&gt;), &lt;= and &gt;= are permitted in the formula for a Variable.<\/P><\/p>\n<p><P>For example, a simple implementation of the =Min(V1,V2) function can be written as:<BR \/><EM>= -1*( V1 &lt; V2 )* V1 +(-1*( V2 &lt;= V1 )* V2)<\/EM><BR \/><BR \/>In the above formula, the logical operator returns a -1 or 0 based on whether the condition evaluates to true or false. So given, V1 = 15 and V2 = 45, the above formula evaluates as follows:<BR \/>= -1*(-1)* 15 + (-1 * (0) * 45)<BR \/>= -15 + 0<BR \/>= 15<\/P><\/p>\n<p><P>This way the smaller number is always returned. Such type of formula are difficult to write and interpret hence rarely used.<BR \/><BR \/><STRONG>\u2022 VBScript Functions and Sub-routines<\/STRONG><\/P><br \/>\n<P><BR \/>Take an example of a shaft as shown below.<BR \/>The various dimension variables are seen in the Variable Table.<BR \/>The design of various parameters is as below:<BR \/>1. Keyway length is always 75 less than the shaft length.<BR \/>2. Hole diameter is 4.5 if ShaftDia is 20 or above and 2.75 if ShaftDia is less than 20.<BR \/>3. Shaft diameter has predefined values and a corresponding diameter for the hole.<BR \/><BR \/><\/P><br \/>\n<P><span class=\"lia-inline-image-display-wrapper lia-image-align-inline\" style=\"width: 520px;\"><img decoding=\"async\" src=\"http:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/8\/2019\/09\/02-13.png\" alt=\"02.png\" title=\"02.png\" \/><\/span><\/P><\/p>\n<p><P>Lets see the three cases in greater details.<\/P><br \/>\n<P><BR \/><STRONG>Case 1.<\/STRONG> The keyway length can be set right into the variable table by specifying it as:<BR \/>= (ShaftLength &#8211; 75)<BR \/><BR \/><STRONG>Note:<\/STRONG> It is always advisable to enclose even the simplest of expressions in parentheses.<\/P><br \/>\n<P><BR \/>This also presents an opportunity to setup a simple VB function to calculate the value for the KeywayLength variable. Start notepad and save the file as Shaft.bas making sure it does not have the .txt in addition. Type in the following:<BR \/><BR \/><FONT face=\"courier new,courier\">Function KeywayLength(ByVal ShaftLength As Double) As Double<\/FONT><BR \/><FONT face=\"courier new,courier\">&nbsp; KeywayLength = (ShaftLength &#8211; 75)<\/FONT><BR \/><FONT face=\"courier new,courier\">End Function<\/FONT><BR \/><BR \/><STRONG>Notes:<\/STRONG><BR \/>\u2022 The keyword Function indicates beginning of function definition.<BR \/>\u2022 The name of the function &#8216;KeywayLength&#8217; can be same as the variable name that is&nbsp; calculated.<BR \/>\u2022 The variable ShaftLength from the variable table is provided to the function in the round brackets or parentheses.<BR \/>\u2022 Double simply means a decimal number. For integer values, the word Integer is used.<BR \/>\u2022 A variable KeywayLength which is same as the function name is then calculated using the ShaftLength provided.<BR \/>\u2022 End Function denotes, the end of function definition.<BR \/>\u2022 Also note the &#8216;As Double&#8217; at the end of function declaration on line 1. The &#8216;As Double&#8217; indicates that the KeywayLength is also calculated as a decimal number.<BR \/><BR \/><span class=\"lia-inline-image-display-wrapper lia-image-align-inline\" style=\"width: 359px;\"><img decoding=\"async\" src=\"http:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/8\/2019\/09\/04-11.png\" alt=\"04.png\" title=\"04.png\" \/><\/span><BR \/><BR \/><STRONG>How to use in Solid Edge:<\/STRONG><BR \/>\u2022 Click in the formula cell for the KeywayLength variable in the variable table.<BR \/>\u2022 Click the Formula (Fx) button in the variable table.<BR \/>\u2022 The Function Wizard dialog appears. Select Visual Basic in the list.<BR \/>\u2022 Select the Shaft.bas file. The function KeywayLength appears in the list on the right side of the Wizard dialog. Select it.<BR \/>\u2022 Click Next.<BR \/><BR \/><span class=\"lia-inline-image-display-wrapper lia-image-align-inline\" style=\"width: 339px;\"><img decoding=\"async\" src=\"http:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/8\/2019\/09\/05-9.png\" alt=\"05.png\" title=\"05.png\" \/><\/span><BR \/><BR \/>\u2022 Step 2 of the wizard appears asking for the arguments for the function.<BR \/>\u2022 Type ShaftLength as the argument. This ShaftLength from the Variable Table is provided to the ShaftLength variable of the function.<BR \/>\u2022 Click Finish. The formula cell is filled with the path and name of the bas file and the function name with the argument &#8216;ShaftLength&#8217; in brackets.<BR \/>\u2022 Change the ShaftLength variable in the variable table and verify the KeywayLength updates accordingly.<BR \/><BR \/><STRONG>Tip:<\/STRONG> Make sure the &#8216;Automatic Update&#8217; button is checked ON on the Tools tab, Links group.<BR \/><BR \/><STRONG>Case 2.<\/STRONG> Hole diameter is 4.5 if ShaftDia is above 20 and 2.75 if ShaftDia is less than 20.<BR \/>For this add a function as below to the .bas file:<BR \/><BR \/><FONT face=\"courier new,courier\">Function HoleDia(ByVal ShaftDia As Double) As Double<\/FONT><BR \/><FONT face=\"courier new,courier\">&nbsp; If ShaftDia &gt;= 20 Then<\/FONT><BR \/><FONT face=\"courier new,courier\">&nbsp;&nbsp;&nbsp; HoleDia = 4.5<\/FONT><BR \/><FONT face=\"courier new,courier\">&nbsp; ElseIf ShaftDia &lt; 20 Then<\/FONT><BR \/><FONT face=\"courier new,courier\">&nbsp;&nbsp;&nbsp; HoleDia = 2.75<\/FONT><BR \/><FONT face=\"courier new,courier\">&nbsp; End If<\/FONT><BR \/><FONT face=\"courier new,courier\">End Function<\/FONT><BR \/><BR \/>and the formula for the HoleDia variable updates as below:<BR \/>D:TempShaft.HoleDia( ShaftDia )<BR \/>Experiment by changing the ShaftDia value to 20, less than 20 and more than 20.<\/P><br \/>\n<P><BR \/><STRONG>Case 3.<\/STRONG> Shaft diameter has predefined values and has a corresponding diameter for the hole as below:<BR \/><BR \/><span class=\"lia-inline-image-display-wrapper lia-image-align-inline\" style=\"width: 123px;\"><img decoding=\"async\" src=\"http:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/8\/2019\/09\/06-7.png\" alt=\"06.png\" title=\"06.png\" \/><\/span><BR \/><BR \/>The first step is to limit the values of the ShaftDia in the Variable Table to a range of given values. For this<\/P><br \/>\n<P>\u2022 Right click the ShaftDia variable in the variable table and select Variable Rule Editor from the menu.<BR \/>\u2022 Check ON &#8220;Limit Value To:&#8221;.<BR \/>\u2022 Select the radio button for &#8216;Descrete List:&#8217; and specify the range as:<BR \/><EM>{8.00 mm;10.00 mm;12.00 mm;17.00 mm;22.00 mm}<\/EM><BR \/>\u2022 The values are separated by a semi-colon and enclosed in curly braces. This automatically creates a drop-down list for the ShaftDia variable. Double click the current value to reveal the list.<BR \/><BR \/><span class=\"lia-inline-image-display-wrapper lia-image-align-inline\" style=\"width: 267px;\"><img decoding=\"async\" src=\"http:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/8\/2019\/09\/07-7.png\" alt=\"07.png\" title=\"07.png\" \/><\/span><BR \/><BR \/>To pick the value of HoleDia corresponding to the ShaftDia, write the function as below:<BR \/><BR \/><FONT face=\"courier new,courier\">Function HoleDia(ByVal ShaftDia As Double) As Double<\/FONT><BR \/><FONT face=\"courier new,courier\">&nbsp; Select Case ShaftDia<\/FONT><BR \/><FONT face=\"courier new,courier\">&nbsp;&nbsp;&nbsp; Case 8<\/FONT><BR \/><FONT face=\"courier new,courier\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HoleDia = 2.5<\/FONT><BR \/><FONT face=\"courier new,courier\">&nbsp;&nbsp;&nbsp; Case 10<\/FONT><BR \/><FONT face=\"courier new,courier\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HoleDia = 3.25<\/FONT><BR \/><FONT face=\"courier new,courier\">&nbsp;&nbsp;&nbsp; Case 12<\/FONT><BR \/><FONT face=\"courier new,courier\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HoleDia = 3.5<\/FONT><BR \/><FONT face=\"courier new,courier\">&nbsp;&nbsp;&nbsp; Case 17<\/FONT><BR \/><FONT face=\"courier new,courier\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HoleDia = 4<\/FONT><BR \/><FONT face=\"courier new,courier\">&nbsp;&nbsp;&nbsp; Case 22<\/FONT><BR \/><FONT face=\"courier new,courier\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HoleDia = 4<\/FONT><BR \/><FONT face=\"courier new,courier\">&nbsp; End Select<\/FONT><BR \/><FONT face=\"courier new,courier\">End Function<\/FONT><BR \/><BR \/>Here, the Select Case structure helps to set a value for a variable corresponding to another variable passed to the function.<BR \/><BR \/>As mentioned earlier, there many more logical constructs in the BASIC language, thus presenting huge possibilites and complexity to user-defined functions. The techniques discussed here should be helpful to make a small beginning.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Variable Table in Solid Edge supports VBScript functions. This presents endless possibilities for users who are not programmers in the strictest sense of the word but have a rudimentary understan&#8230;<\/p>\n","protected":false},"author":42979,"featured_media":5340,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spanish_translation":"","french_translation":"","german_translation":"","italian_translation":"","polish_translation":"","japanese_translation":"","chinese_translation":"","footnotes":""},"categories":[1,96],"tags":[33],"industry":[],"product":[],"coauthors":[],"class_list":["post-5321","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-news","category-tips-tricks","tag-developer"],"featured_image_url":"https:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/8\/2019\/09\/07-7.png","_links":{"self":[{"href":"https:\/\/blogs.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/posts\/5321","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/users\/42979"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/comments?post=5321"}],"version-history":[{"count":7,"href":"https:\/\/blogs.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/posts\/5321\/revisions"}],"predecessor-version":[{"id":5341,"href":"https:\/\/blogs.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/posts\/5321\/revisions\/5341"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/media\/5340"}],"wp:attachment":[{"href":"https:\/\/blogs.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/media?parent=5321"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/categories?post=5321"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/tags?post=5321"},{"taxonomy":"industry","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/industry?post=5321"},{"taxonomy":"product","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/product?post=5321"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/coauthors?post=5321"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}