onilink_

Untitled

Jun 18th, 2020
477
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.08 KB | None | 0 0
  1. # DRAG_KNIFE.PY
  2. # Rev: 23-Sep-2013
  3. # Bill Trondsen
  4. ###########################################################################
  5. # This CAMBAM script creates a polyline for Drag-Knife cutting
  6. # Select all desired polylines and run the script
  7. # Note: Material Surface set to Z=0
  8. # Use an engraving path with depth of cut set to zero
  9. # Drag-Knife Path Concept comes from:  www.DonekTools.com
  10. # Cut path is simulated by the path of a rear bicycle wheel
  11. # Knowing the rear wheel path, calculate the front wheel (tool path)
  12. # Formulas come from: http://math.ucsd.edu/~ebender/87/bicycle.pdf
  13. #
  14. ###########################################################################
  15. #  Set these variables for your tool and material
  16. #    Offset: knife tip distance from tool centerline
  17. #    CutDepth: material thickness
  18. #    RetractDepth: a smaller depth used for retract/swivel move
  19. #    SwivelAngle: if larger than this angle, tool retracts between segments
  20. ###########################################################################
  21.  
  22. Offset = .09
  23. CutDepth = .25
  24. RetractDepth = .24
  25. SwivelAngle = 20
  26. app.log("Offset       =  "+Offset.ToString())
  27. app.log("CutDepth     =  "+CutDepth.ToString())
  28. app.log("RetractDepth =  "+RetractDepth.ToString())
  29. app.log("SwivelAngle  =  "+SwivelAngle.ToString())
  30.  
  31. ####### MAIN LOOP #########################################
  32.  
  33. CamBamUI.MainUI.UndoBuffer.AddUndoPoint("Undo Here")
  34. CamBamUI.MainUI.UndoBuffer.Add(doc.ActiveLayer.Entities)
  35.  
  36. for ent in view.Selection:
  37.   if ent.GetType().Name == "Polyline":
  38.     xpoly = XPolyline(ent)
  39.     app.log("**********************************************************")
  40.     app.log("************************Polyline**************************")
  41.     app.log("**********************************************************")
  42.     app.log("Polyline:    "+ent.ID.ToString())
  43.     seg = xpoly.FirstSegment
  44.     poly = Polyline()
  45.     PrevSweep = 0
  46.     SegNum = 1
  47.  
  48. ######## Loop for each segment within the Polyline #########
  49.     while seg is not None:
  50.  
  51.       P1X = seg.P1.X
  52.       P1Y = seg.P1.Y
  53.       P2X = seg.P2.X
  54.       P2Y = seg.P2.Y
  55.       V1 = seg.VNormal1.Normal().Normal().Normal()
  56.       V2 = seg.VNormal2.Normal().Normal().Normal()
  57.       VFirst = Vector2F(1,0)
  58.       VLast = Vector2F(1,0)
  59.  
  60.       #### First Point is oriented to X-axis and calculate sweep/bulge to first segment
  61.       if seg == xpoly.FirstSegment:
  62.         CFirst = Point2F(P1X+Offset, P1Y)
  63.         CFirstXP = Vector3F.CrossProduct(Vector3F(1,0,0),Vector3F(V1.X,V1.Y,0))
  64.         CFirstDirection = Math.Sign(CFirstXP.Z)
  65.         if CFirstDirection == 0:
  66.           CFirstDirection = 1
  67.         CFirstSweep = Math.Acos(Vector2F.DotProduct(VFirst,V1)/(VFirst.Length*V1.Length))
  68.         CFirstB = Math.Tan(CFirstSweep/4)*CFirstDirection
  69.  
  70.         app.log("CFirst         "+CFirst.ToString())
  71.         app.log("CFirstSweep    "+CFirstSweep.ToString())
  72.         app.log("CFirstXP       "+CFirstXP.ToString())
  73.         app.log("CFirstDirection"+CFirstDirection.ToString())
  74.         app.log("CFirstB        "+CFirstB.ToString())
  75.  
  76.       C1 = Point2F(P1X+V1.X*Offset,P1Y+V1.Y*Offset)
  77.       if seg.GetType().Name == "XPolySegLine":      
  78.         C1B = 0
  79.         C3 = Point2F(P2X+V2.X*Offset, P2Y+V2.Y*Offset)
  80.       elif seg.GetType().Name == "XPolySegArc":
  81.         C1B = Math.Tan(seg.Sweep/4)
  82.         C3X = P2X+Offset/((1+(V2.Y/V2.X)**2)**.5)*Math.Sign(V2.X)
  83.         C3Y = P2Y+(Offset*V2.Y/V2.X)/((1+(V2.Y/V2.X)**2)**.5)*Math.Sign(V2.X)
  84.         C3 = Point2F(C3X,C3Y)
  85.  
  86.       # After last segment, orient tool to x-axis before fully retracting
  87.       V3 = Vector2F(seg.P2,C3)
  88.       if seg == xpoly.LastSegment:
  89.         C3XP = Vector3F.CrossProduct(Vector3F(V3.X,V3.Y,0),Vector3F(1,0,0))
  90.         C3Direction = Math.Sign(C3XP.Z)
  91.         if C3Direction == 0:
  92.           C3Direction = 1
  93.         C3Sweep = Math.Acos(Vector2F.DotProduct(V3,VLast)/(VLast.Length*V3.Length))
  94.         C3B = Math.Tan((C3Sweep)/4)*C3Direction
  95.       else:
  96.         VN1 = seg.NextSegment.VNormal1.Normal()
  97.         VN1.Invert()
  98.         C3XP = Vector3F.CrossProduct(Vector3F(V3.X,V3.Y,0),Vector3F(VN1.X,VN1.Y,0))
  99.         C3Direction = Math.Sign(C3XP.Z)
  100.         if C3Direction == 0:
  101.           C3Direction = 1
  102.         C3Sweep = Math.Acos(Vector2F.DotProduct(V3,VN1)/(V3.Length*VN1.Length))
  103.         C3B = Math.Tan((C3Sweep)/4)*C3Direction
  104.  
  105.       # After last segment, orient tool to x-axis before fully retracting
  106.       V3 = Vector2F(seg.P2,C3)
  107.       if seg == xpoly.LastSegment:
  108.         C3XP = Vector3F.CrossProduct(Vector3F(V3.X,V3.Y,0),Vector3F(1,0,0))
  109.         C3Direction = Math.Sign(C3XP.Z)
  110.         if C3Direction == 0:
  111.           C3Direction = 1
  112.         C3Sweep = Math.Acos(Vector2F.DotProduct(V3,VLast)/(VLast.Length*V3.Length))
  113.         C3B = Math.Tan((C3Sweep)/4)*C3Direction
  114.       else:
  115.         VN1 = seg.NextSegment.VNormal1.Normal()
  116.         VN1.Invert()
  117.         C3XP = Vector3F.CrossProduct(Vector3F(V3.X,V3.Y,0),Vector3F(VN1.X,VN1.Y,0))
  118.         C3Direction = Math.Sign(C3XP.Z)
  119.         if C3Direction == 0:
  120.           C3Direction = 1
  121.         C3Sweep = Math.Acos(Vector2F.DotProduct(V3,VN1)/(V3.Length*VN1.Length))
  122.         C3B = Math.Tan((C3Sweep)/4)*C3Direction
  123.  
  124.  
  125.       app.log("******************Segment*******************")
  126.       app.log("Segment      "+SegNum.ToString())
  127.       app.log("P1           "+seg.P1.ToString())
  128.       app.log("P2           "+seg.P2.ToString())
  129.       app.log("V1           "+V1.ToString())
  130.       app.log("V2           "+V2.ToString())
  131.       app.log("V2X SIGN     "+Math.Sign(V2.X).ToString())
  132.       app.log("V2Y SIGN     "+Math.Sign(V2.Y).ToString())
  133.       app.log("C1           "+C1.ToString())
  134.       app.log("C3           "+C3.ToString())
  135.       app.log("C1Bulge        "+C1B.ToString())
  136.       app.log("C3Bulge        "+C3B.ToString())
  137.  
  138. ########################################################
  139. # Add points to path with depth logic###################
  140. ########################################################
  141.  
  142.       if seg == xpoly.FirstSegment:
  143.         poly.Add(CFirst.X, CFirst.Y, RetractDepth, CFirstB)
  144.         poly.Add(C1.X, C1.Y, RetractDepth, 0)
  145.         poly.Add(C1.X, C1.Y, CutDepth, C1B)
  146.  
  147.       elif SwivelAngle >= PrevSweep:
  148.         poly.Add(C1.X, C1.Y, CutDepth, C1B)
  149.  
  150.       else:
  151.         poly.Add(C1.X, C1.Y, RetractDepth,0)
  152.         poly.Add(C1.X, C1.Y, CutDepth,C1B)
  153.  
  154.       C3Angle = C3Sweep*180/Math.PI
  155.  
  156.       if seg == xpoly.LastSegment:
  157.         CLast = Point2F(seg.P2.X+VLast.X*Offset, seg.P2.Y+VLast.Y*Offset)
  158.         poly.Add(C3.X, C3.Y, CutDepth, 0)
  159.         poly.Add(C3.X, C3.Y, RetractDepth, C3B)
  160.         poly.Add(CLast.X, CLast.Y, RetractDepth, 0)
  161.  
  162.       elif SwivelAngle >= C3Angle:
  163.         poly.Add(C3.X, C3.Y, CutDepth, C3B)
  164.  
  165.       else:
  166.         poly.Add(C3.X, C3.Y, CutDepth, 0)
  167.         poly.Add(C3.X, C3.Y, RetractDepth, C3B)
  168.  
  169.       app.log("SwivelAngle    "+SwivelAngle.ToString())
  170.       app.log("C3Sweep        "+C3Angle.ToString())
  171.       app.log("PrevSweep      "+PrevSweep.ToString())
  172.  
  173. # End of Main Loop #####################################
  174.       PrevSweep = C3Angle
  175.       SegNum += 1
  176.       seg = seg.NextSegment
  177.     doc.Add(poly)
Add Comment
Please, Sign In to add comment