home *** CD-ROM | disk | FTP | other *** search
/ Chip 1997 July / Chip_1997-07_cd.bin / tema / mattes / ip / ip30 / data.1 / BLDCONS.WBS < prev    next >
Text File  |  1996-09-21  |  12KB  |  382 lines

  1. ' ******************************************************************************
  2. ' MKCONS5.WBS - CREATES CONSTRAINTS FROM USER SELECTED POINTS
  3. '    
  4. '    This Working Model script will create a constraint from points selected by
  5. '    the user.  If one point is selected, the user may create a Force or a Torque.
  6. '    If two points are selected, the user may create an actuator, damper
  7. '    pin joint, rod, rope, separator, spring, or springdamper.  If 3 or more points
  8. '    are selected, the user may create a pin joint.
  9. '
  10. ' Version 1.0, Created by Knowledge Revolution
  11. ' (C) Copyright Knowledge Revolution 1995   All Rights Reserved
  12. '
  13. ' ******************************************************************************
  14.  
  15. Dim MyDoc as WMDocument
  16. Dim ConstraintArray() as String
  17. Dim Numdigits as integer
  18. Dim CRLF as string
  19. global bodywidth as double
  20.  
  21.  
  22. Function StrDec(x as double, sigdigits as integer) as string
  23. '-------------------------------------------------------------------
  24. '    This function formats a string from a number passed to it.  The 
  25. '    string is left justified, and will have sigdigits decimal places.
  26. '-------------------------------------------------------------------
  27.  
  28.     StrDec = Format(x, "0." + string(sigdigits,"0"))
  29.  
  30. end function
  31.  
  32. Function BodyCheck as boolean
  33. '-------------------------------------------------------------------
  34. '    This function returns "true" if any selected points are
  35. '    attached to the same body (or background).
  36. '-------------------------------------------------------------------
  37.  
  38.     dim idarray(mydoc.selection.count) as integer
  39.  
  40.     BodyCheck = False
  41.     idarray(1) = mydoc.selection.point(1).body.id
  42.  
  43.     for i1 = 2 to mydoc.selection.count
  44.         for i2 = 1 to (i1-1)
  45.             if mydoc.selection.point(i1).body.id = idarray(i2) then
  46.                 BodyCheck = True
  47.             end if
  48.         next i2        
  49.         idarray(i1) = mydoc.selection.point(i1).body.id
  50.     next i1
  51. end function
  52.  
  53. Function PointsSelected as Integer
  54. '-------------------------------------------------------------------
  55. '    This routine counts the number of points selected.
  56. '-------------------------------------------------------------------
  57.  
  58.     dim count as integer
  59.     dim i as integer
  60.  
  61.     count = 0
  62.  
  63.     For i = 1 to mydoc.selection.count
  64.         If not(Mydoc.Selection.Point(i) is nothing) then
  65.             count = count +1
  66.         End If
  67.     Next i
  68.     PointsSelected = count
  69.  
  70. End Function
  71.  
  72. Function InterpretSelection as String
  73. '-------------------------------------------------------------------
  74. '    This function checks to see what the user has selected in 
  75. '    Working Model.  An incorrect selection returns "Error".  One
  76. '   point returns "OnePoint".  Two points returns "TwoPoint".  Three
  77. '    points returns "MultiPoint".
  78. '-------------------------------------------------------------------
  79.  
  80.     if Mydoc.selection.count = PointsSelected _
  81.         and Mydoc.selection.count > 0 then
  82.  
  83.         If mydoc.selection.count = 1 then
  84.             InterpretSelection = "OnePoint"
  85.         Else 
  86.             if mydoc.selection.count = 2 then
  87.                 InterpretSelection = "TwoPoint"    
  88.             Else 
  89.                 if bodycheck then
  90.                     InterpretSelection = "PointError"
  91.                 else
  92.                     InterpretSelection = "MultiPoint"
  93.                 end if
  94.             end if
  95.         end if
  96.     Else
  97.         InterpretSelection = "Error"
  98.     End if    
  99.  
  100. End Function
  101.  
  102. Sub FillListAdd (List() as String, ctype as string)
  103. '-------------------------------------------------------------------
  104. '    This subroutine is used to fill the scrolling dialog box.
  105. '-------------------------------------------------------------------
  106.  
  107.     if ctype = "OnePoint" then
  108.         redim list(1) as string
  109.         List(0) = "force"
  110.         List(1) = "torque"
  111.     else 
  112.         if ctype = "TwoPoint" then
  113.             redim list(7) as string
  114.              List(0) = "actuator"
  115.             List(1) = "damper"
  116.             List(2) = "pin"
  117.               List(3) = "rod"
  118.                List(4) = "rope"    
  119.                List(5) = "separator"
  120.                List(6) = "spring"
  121.                List(7) = "springdamper"
  122.            else 
  123.             if ctype = "MultiPoint" then
  124.                 redim list(0) as string
  125.                  List(0) = "pin"
  126.             end if
  127.         end if
  128.     end if
  129. End Sub
  130.  
  131. Function Angle (vec_x as double, vec_y as double) as Double
  132. '-------------------------------------------------------------------
  133. '    This function is used to calculate the angle an object should
  134. '    have in order to span two endpoints.
  135. '    Vec_x should equal:  point1_x - point2_x
  136. '    Vec_y should equal:  point1_y - point2_y
  137. '-------------------------------------------------------------------
  138.  
  139.     dim CalcAngle as double
  140.  
  141.     if vec_x = 0 then
  142.         angle = -PI/2
  143.     else
  144.         if vec_y = 0 then
  145.             angle = 0
  146.          else
  147.             CalcAngle = atn(vec_y/vec_x)
  148.             if vec_x > 0 then
  149.                 angle = CalcAngle
  150.             else
  151.                 angle = CalcAngle + PI
  152.             end if
  153.         end if
  154.     end if
  155.  
  156. End Function
  157.  
  158. Function xposglobal (pt as WMPoint) as double
  159. '-------------------------------------------------------------------
  160. '    This function calculates the x global position of a point.  If
  161. '    the point attached to the background, the x global position is
  162. '    simply its px.value.  If it is attached to a body, the global
  163. '    position must be calculated based upon the x and y local positions
  164. '    of the point, and the body's rotation.
  165. '-------------------------------------------------------------------
  166.  
  167.     dim tbody as WMbody
  168.  
  169.     If pt.body.id <> 0 then                  
  170.         set tbody = pt.body
  171.         xposglobal = (cos(tbody.pr.value)*pt.px.value) - _
  172.                         (sin(tbody.pr.value)*pt.py.value) + tbody.px.value
  173.     else
  174.         xposglobal = pt.px.value
  175.     end if
  176.  
  177. end function
  178.  
  179. Function yposglobal (pt as WMPoint) as double
  180. '-------------------------------------------------------------------
  181. '    This function calculates the y global position of a point.  If
  182. '    the point attached to the background, the y global position is
  183. '    simply its py.value.  If it is attached to a body, the global
  184. '    position must be calculated based upon the x and y local positions
  185. '    of the point, and the body's rotation.
  186. '-------------------------------------------------------------------
  187.  
  188.     dim tbody as WMbody
  189.  
  190.     If pt.body.id <> 0 then                  
  191.         set tbody = pt.body
  192.         yposglobal = (sin(tbody.pr.value)*pt.px.value) + _
  193.                         (cos(tbody.pr.value)*pt.py.value) + tbody.py.value
  194.     else
  195.         yposglobal = pt.py.value
  196.     end if
  197.  
  198. end function
  199.  
  200. Sub AddOnePoint(C as string)
  201. '-------------------------------------------------------------------
  202. '    This routine will add either a Force or a Torque to the 1 point
  203. '    selected.
  204. '-------------------------------------------------------------------
  205.  
  206.     Dim P1 as WMPoint
  207.     Dim MyConstraint as WmConstraint
  208.  
  209.     Set P1 = MyDoc.Selection.Point(1)
  210.  
  211.     Set MyConstraint = MyDoc.NewConstraint(c)
  212.      Set MyConstraint.point(1).px.value = p1.px.value
  213.     Set MyConstraint.point(1).py.value = p1.py.value
  214.       Set MyConstraint.point(1).body = p1.body
  215.     Set MyConstraint.point(1).px.formula = p1.px.formula
  216.     Set MyConstraint.point(1).py.formula = p1.py.formula
  217.  
  218.     If p1.constraint is nothing then    ' if point is not part
  219.         mydoc.delete p1                      ' of another constraint, 
  220.     end if                                  ' delete original
  221.  
  222. End Sub
  223.  
  224. Sub AddTwoPoint(C as String)
  225. '-------------------------------------------------------------------
  226. '   This subrouting will create the chosen constraint between  
  227. '    two selected points.
  228. '-------------------------------------------------------------------
  229.  
  230.     dim P1 as WMPoint
  231.     dim P2 as WMPoint
  232.     dim MyConstraint as WMConstraint
  233.  
  234.     Set P1 = MyDoc.Selection.Point(1)
  235.     Set P2 = MyDoc.Selection.Point(2)
  236.      
  237.     if C = "pin" then
  238.         if BodyCheck then
  239.             r= msgbox ("2 points cannot be attached to the" + _
  240.                          " same body (or the background) when making a pin joint.", _
  241.                         ebExclamation, "Selection Error")
  242.         else
  243.             Mydoc.Join
  244.         end if
  245.     else
  246.         Set MyConstraint = MyDoc.NewConstraint(c)
  247.         if c = "springdamper" or c = "rod" or c = "separator" or c = "rope" _ 
  248.                 or c = "spring" then
  249.             Set MyConstraint.length.value = sqr((xposglobal(p1) - xposglobal(p2))^2 + _
  250.                                                 (yposglobal(p1) - yposglobal(p2))^2)
  251.         end if
  252.          Set MyConstraint.point(1).px.value = p1.px.value
  253.         Set MyConstraint.point(1).py.value = p1.py.value
  254.           Set MyConstraint.point(2).px.value = p2.px.value
  255.           Set MyConstraint.point(2).py.value = p2.py.value
  256.           Set MyConstraint.point(1).body = p1.body
  257.            Set MyConstraint.point(2).body = p2.body
  258.         Set MyConstraint.point(1).px.formula = p1.px.formula
  259.         Set MyConstraint.point(1).py.formula = p1.py.formula
  260.         Set MyConstraint.point(2).px.formula = p2.px.formula
  261.         Set MyConstraint.point(2).py.formula = p2.py.formula
  262.  
  263.         If p1.constraint is nothing then    ' if point is not part
  264.             mydoc.delete p1                      ' of another constraint, 
  265.         end if                                  ' delete original
  266.     
  267.         if p2.constraint is nothing then
  268.             mydoc.delete p2
  269.         end if
  270.     end if
  271.  
  272. End Sub    
  273.  
  274. Sub AddMultiPoint(C as String)
  275. '-------------------------------------------------------------------
  276. '   This subroutine will create a pin joint from multiple points by 
  277. '    doing multiple joins.  
  278. '-------------------------------------------------------------------
  279.  
  280.     dim PointArray(mydoc.points.count) as WMPoint
  281.     dim numpoints as integer
  282.  
  283.     numpoints = mydoc.selection.count
  284.  
  285.     for i = 1 to numpoints
  286.         Set PointArray(i) = mydoc.selection.point(i)
  287.     next
  288.  
  289.     Mydoc.SelectAll False
  290.  
  291.     mydoc.select PointArray(1), true
  292.  
  293.     for i = 2 to numpoints
  294.         mydoc.select PointArray(i), true
  295.         mydoc.join
  296.     next
  297.  
  298. end sub
  299.     
  300. Sub AddControl(mode as string)
  301. '-------------------------------------------------------------------
  302. '    This routine displays the dialog box and then figures out what
  303. '    other routines to call based upon what the user selects.
  304. '-------------------------------------------------------------------
  305.  
  306. Begin Dialog AddConstraintDialog 115,52,165,48,"Build Constraint"
  307.     ListBox 4,4,76,45,ConstraintArray$,.ConstraintList
  308.     PushButton 96,8,64,14,"Add",.Add
  309.     CancelButton 96,28,64,14,.cancel
  310. End Dialog
  311.  
  312.      FillListAdd ConstraintArray,mode
  313.  
  314.     Dim CD as AddConstraintDialog
  315.  
  316.     rc=Dialog(CD,1)
  317.  
  318.     If rc = 0 then
  319.         exit sub 
  320.     else        ' The user selected a scroll list item
  321.            if Mode = "OnePoint" then
  322.             AddOnePoint constraintArray(cd.constraintlist)
  323.         Else
  324.             if Mode = "TwoPoint" then
  325.                 AddTwoPoint constraintArray(cd.constraintlist)
  326.             Else
  327.                 if Mode = "MultiPoint" then
  328.                     AddMultiPoint constraintArray(cd.constraintlist)
  329.                 End if
  330.             End if
  331.         end if
  332.      End If
  333.  
  334. End Sub
  335.  
  336. Sub Main()
  337. '-------------------------------------------------------------------
  338. '    This routine controls the flow of the script.  It first calls
  339. '    a function to verify the user has selected only points.  It then
  340. '    passes control to a subroutine to display the dialog box.
  341. '-------------------------------------------------------------------
  342.  
  343.     Set Mydoc = WM.Activedocument
  344.     dim mode as string
  345.  
  346.     If Basic.OS = ebwin16 or Basic.OS = ebwin32 then    ' Running windows
  347.         Crlf = chr$(13) + chr$(10)
  348.     else
  349.         If Basic.OS = ebmacintosh then            ' Running Macintosh
  350.             Crlf = chr$(13)
  351.         else
  352.             r = msgbox ("ERROR:  Unknown operating system: " + Basic.OS, ebExclamation, "Operating System Error")
  353.             exit sub
  354.         end if
  355.     end if
  356.     
  357.     mode = InterpretSelection
  358.  
  359.     if Mode = "Error" then
  360.         r = msgbox ("Only points may be selected when using this tool." + crlf + crlf +_
  361.             "This tool allows you to create constraints between points." + crlf + _
  362.             "The type of constraint that you can create depends on" + crlf +_
  363.             "the number of points selected:" + crlf + crlf +_
  364.             "A. 1 point  (Force, Torque)" + crlf +_ 
  365.             "B. 2 points (Actuator, Damper, Pin, Rod, Rope," + crlf +_
  366.             "                     Separator, Spring, Spring/Damper)" + crlf +_
  367.             "C. 3 or more points (Pin)", ebExclamation, "Selection Error") 
  368.     else
  369.         if Mode = "PointError" then
  370.             r= msgbox ("2 or more points cannot be attached to the same body" +_
  371.                     " (or the background) when > 2 points are selected.", _
  372.                         ebExclamation, "Selection Error")
  373.         else
  374.             numdigits = mydoc.decimaldigits
  375.             AddControl mode
  376.         end if
  377.     end if    
  378.        
  379. End Sub
  380.  
  381.  
  382.