Author Topic: Measure angle between two lines  (Read 6631 times)

Offline lloydsp

  • CNC Jedi
  • *****
  • Posts: 8331
    • View Profile
Measure angle between two lines
« on: March 14, 2013, 02:29:00 am »
PLEASE, someone clean this up and turn it into a plugin! (then please show your code...)

I just finally got SharpDevelop, and have begun to explore CB's objects.... but it's a long uphill battle, because I find I no longer know "C"   :-\

ANYway... this script works, but really needs a professional's touch.

Thanks.
Lloyd

-----
'AngleFinder.vbs
' Relative Angle between any two line segments
' they do NOT have to intersect

sub main

   dim angle1 as double
   dim angle2 as double

   dim line1X1 as single
   dim line1X2 as single
   dim line1y1 as single
   dim line1y2 as single
   dim line2X1 as single
   dim line2X2 as single
   dim line2y1 as single
   dim line2y2 as single

   dim startend as PointF
   dim minE as PointF
   dim maxE as PointF
   dim ent1 as Entity
   dim ent2 as Entity

   dim counter as single

   counter=0
   

    if view.SelectedEntities.Length=2 then

      for each ent as Entity in view.SelectedEntities
'         inval=msgbox(ent.PrimitiveType(),0,"Type=")

         counter=counter+1

         if counter=1 then
            ent1=ent
         end if
         if counter=2 then
            ent2=ent
         end if
   
      next
         ent1.GetExtents(minE,maxE)
         line1X1=minE.x
         line1X2=maxE.x
         line1Y1=minE.y
         line1Y2=maxE.y

'         msgbox(minE.x,0,"line 1 startx")
'         msgbox(minE.y,0,"line 1 starty")
'         msgbox(maxE.x,0,"line 1 endx")
'         msgbox(maxE.y,0,"line 1 endy")
         
         ent2.GetExtents(minE,maxE)
         line2X1=minE.x
         line2X2=maxE.x
         line2Y1=minE.y
         line2Y2=maxE.y

'         msgbox(minE.x,0,"line 2 startx")
'         msgbox(minE.y,0,"line 2 starty")
'         msgbox(maxE.x,0,"line 2 endx")
'         msgbox(maxE.y,0,"line 2 endy")

            angle1 = (180/Math.pi)*Math.atan2(line1X2 - line1X1,line1Y2 - line1Y1)
'         msgbox(angle1,0,"Angle1")

            angle2 = (180/Math.pi)*Math.atan2(line2X2 - line2X1,line2Y2 - line2Y1)
'         msgbox(angle2,0,"Angle2")

         msgbox(Math.abs(angle1-angle2),0,"Included Angle =")


   else
         msgbox("Must Be TWO lines selected",48,0)
   end if
end sub
 
"Pyro for Fun and Profit for More Than Fifty Years"

Offline pstemari

  • Storm Trooper
  • ***
  • Posts: 133
    • View Profile
Re: Measure angle between two lines
« Reply #1 on: April 22, 2013, 17:14:27 pm »
Leaving aside the select and msgbox stuff, the core is:

Code: [Select]
double AngleBetween(Line3F a, Line3F b) {
  Vector3F av = new Vector3F(a);
  Vector3F bv = new Vector3F(b);
  double sin = Vector3F.CrossProduct(av.Unit(), bv.Unit()).Length;
  double cos = Vector3F.DotProduct(av.Unit(), bv.Unit());
  return Math.Atan2(sin, cos);
}


Offline lloydsp

  • CNC Jedi
  • *****
  • Posts: 8331
    • View Profile
Re: Measure angle between two lines
« Reply #2 on: April 22, 2013, 18:02:35 pm »
Thanks!  Five or six more parts to machine today, then I'll try that as my script's 'core'.

But, then, how would one go about making that into a plugin, so it became a toolbar or menu item?

*See?  I said I hardly remember how to program! <G>*

Lloyd
« Last Edit: April 22, 2013, 18:04:08 pm by lloydsp »
"Pyro for Fun and Profit for More Than Fifty Years"

Offline pstemari

  • Storm Trooper
  • ***
  • Posts: 133
    • View Profile
Re: Measure angle between two lines
« Reply #3 on: April 23, 2013, 04:50:12 am »
There's a demo plugin that works as a starting point.  I need to bug the open source folks at the office so I can post my code online, but once you connect your subroutine to the menus, there's a data member on the CADView that holds references to the currently selected entities.

Look at the documentation topic for "hooking to the UI".