Author Topic: Help writing a very simple 2D Script. (Python ?)  (Read 1421 times)

Offline M.Pingu

  • Ewok
  • *
  • Posts: 3
    • View Profile
Help writing a very simple 2D Script. (Python ?)
« on: May 20, 2023, 11:34:00 am »
My python script is now completed. It is a 2D (from dxf) script to generate a laser chamfer by varying Spindle , Feed and Number of contours

Example :
Profile 1 : F2000S30 offset 0.1
Profile 2 : F1900S35 offset 0.2
Profile 3 : F1800S40 offset 0.3 ...

A final operation makes a pocket with the last Feed and Spindle values. From a 2D sideview , the profile around a shape is a chamfer on both sides.

And here is the (amateurish) Code :

Code: [Select]
#Imports and definitions
from CamBam.CAM import *
from CamBam.UI import CamBamUI
from CamBam.Values import *
newdoc = CamBamUI.MainUI.ActiveView.CADFile
newdoc.MachiningOptions.PostProcessor = "Laserbot_2D" #My Postprocessor is deleting all the Z values and replacing M3 vith M4
Clear = 0.0
i = 0

#Define the Range and number of profiles

layer=0

lines=20
largeur=10 #thats width in french

Spin = 70
SpinMax=100

Feed = 4000
FeedMin=3000


#Calculation of increments

Spinplus = float(SpinMax-Spin)/float(lines)
Feedinc =(Feed-FeedMin)/(lines-1)
width=largeur-(lines/10)
name= "ContourMagique"

#Create the 2 parts
part= doc.CreatePart("Contours")
part2 = doc.CreatePart("Finish")


#Iterate the contours while varying SpindleSpeed Feed and Offset
while i <lines:

    profile=MOPProfile(newdoc,newdoc.Layers[layer].Entities.ToArray())
    profile.ToolDiameter = CBValue[float](0.1)
    profile.StepOver=CBValue[float](1)
    profile.Style="cutout"
    profile.Name= name+str(i)
    profile.SpindleSpeed = CBValue[int](Spinint)
    profile.CutFeedrate = CBValue[float](Feed)
    profile.RoughingClearance = CBValue[float](Clear)
    profile.GetCAMStyle()
    profile.InsideOutside = CBValue[InsideOutsideOptions](InsideOutsideOptions.Outside)
    profile.LeadInMove = CBValue[LeadMoveInfo](None)

   
    part.MachineOps.Add(profile)
   
    Spin += Spinplus
    Spinint=int(Spin)
    Clear+=0.1
    Feed-=Feedinc
    i+=1
   
#Create the Finishing pocket     
   
profile=MOPProfile(newdoc,newdoc.Layers[0].Entities.ToArray())
profile.Name="Finish"
profile.SpindleSpeed = CBValue[int](Spinint)
profile.CutFeedrate = CBValue[float](Feed)
profile.RoughingClearance = CBValue[float](Clear)
profile.ToolDiameter = CBValue[float](0.1)
profile.CutWidth = CBValue[float](width)
profile.StepOver=CBValue[float](1)

part2.MachineOps.Add(profile)


The main issue of the obtained Mops is there is absolutely no cut ordering . I guess making a new operation plugin would be necessary to do all the operations profile by profile... but that's another topic :)
Hope someone will find this usefull!


« Last Edit: May 22, 2023, 12:24:29 pm by M.Pingu »

Offline EddyCurrent

  • CNC Jedi
  • *****
  • Posts: 5190
  • Made in England
    • View Profile
Re: Help writing a very simple 2D Script. (Python ?)
« Reply #1 on: May 21, 2023, 10:06:24 am »
Try this;

Code: [Select]
from CamBam.CAM import *
from CamBam.UI import CamBamUI
from CamBam.Values import *
newdoc = CamBamUI.MainUI.ActiveView.CADFile

mypart = CamBamUI.MainUI.ActiveView.CADFile.CreatePart("mypart")
profile = MOPProfile(newdoc,newdoc.Layers[0].Entities.ToArray())
profile.Style="cutout"
profile.Part=mypart
profile.Name="script3"

CamBamUI.MainUI.InsertMOP(profile)

Layers[1] failed for me because I only had one layer in my CB file which was Layers[0]

This is a good resource; https://cambamcnc.com/ref/script.mop-automate
and the other script examples there too.
« Last Edit: May 21, 2023, 10:10:01 am by EddyCurrent »
Filmed in Supermarionation

Offline M.Pingu

  • Ewok
  • *
  • Posts: 3
    • View Profile
Re: Help writing a very simple 2D Script. (Python ?)
« Reply #2 on: May 21, 2023, 14:22:39 pm »
Hello, thanks for your help and validating the post!

I have advanced well with the API reference that i didn't find earlier. I will post my code  as it might help people starting with python scripting.

The main thing that remains is to delete the lead in moves in the generated operations... I guess it should look like
 
Code: [Select]
profile.LeadMoveInfo=CBValue[None] but it's not exactly that ^^

I also failed to create the mop in a specific part with
Code: [Select]
profile.Part=mypart .. seem like this is not how it should be done.

Any more help would be very appreciated on these two points, but i must say i'm already very happy to have made something work ^^

the rest of the code for the ones it might help , which although ugly does some more mop manipulations:

Code: [Select]
from CamBam.CAM import *
from CamBam.UI import CamBamUI
from CamBam.Values import *
newdoc = CamBamUI.MainUI.ActiveView.CADFile
newdoc.MachiningOptions.PostProcessor = "Laserbot_2D"
Clear = 0.0
i = 0


layer=0

lines=20
largeur=10

Spin = 70
SpinMax=100

Feed = 4000
FeedMin=3000

Spinint=int(Spin)
Spinplus = float(SpinMax-Spin)/float(lines)
Feedinc =(Feed-FeedMin)/(lines-1)
width=largeur-(lines/10)
name= "ContourMagique"



mypart = CamBamUI.MainUI.ActiveView.CADFile.CreatePart("ContoursPart")

while i <lines:

    profile=MOPProfile(newdoc,newdoc.Layers[layer].Entities.ToArray())
    profile.ToolDiameter = CBValue[float](0.1)
    profile.StepOver=CBValue[float](1)
    profile.LeadMoveInfo=CBValue[None]
    profile.Style="cutout"
    profile.Part=mypart
    profile.Name= name+str(i)
    profile.SpindleSpeed = CBValue[int](Spinint)
    profile.CutFeedrate = CBValue[float](Feed)
    profile.RoughingClearance = CBValue[float](Clear)
    CamBamUI.MainUI.InsertMOP(profile)
    Spin += Spinplus
    Spinint=int(Spin)
    Clear+=0.1
    Feed-=Feedinc
    i+=1
   
profile=MOPProfile(newdoc,newdoc.Layers[0].Entities.ToArray())
profile.Name="Finish"
profile.SpindleSpeed = CBValue[int](Spinint)
profile.CutFeedrate = CBValue[float](Feed)
profile.RoughingClearance = CBValue[float](Clear-0.1)
profile.ToolDiameter = CBValue[float](0.1)
profile.CutWidth = CBValue[float](width)
profile.StepOver=CBValue[float](1)
CamBamUI.MainUI.InsertMOP(profile)

Offline M.Pingu

  • Ewok
  • *
  • Posts: 3
    • View Profile
Re: Help writing a very simple 2D Script. (Python ?)
« Reply #3 on: May 22, 2023, 11:38:19 am »
Ok , i have found how to modify the Lead Move ( Lead In , Lead Out) Info . I will keep posting my snippets for python users :

Code: [Select]
profile.LeadInMove = CBValue[LeadMoveInfo](None)
Next i found how to assign MOPs to specific part :

Code: [Select]
part = doc.CreatePart("Contours")
part.MachineOps.Add(profile)

I guess my script is now done. Maybe this topic should be deleted or moved to scripts reference ?
« Last Edit: May 22, 2023, 11:59:11 am by M.Pingu »

Offline Dragonfly

  • CNC Jedi
  • *****
  • Posts: 2610
    • View Profile
Re: Help writing a very simple 2D Script. (Python ?)
« Reply #4 on: May 23, 2023, 13:20:56 pm »

I guess my script is now done. Maybe this topic should be deleted or moved to scripts reference ?

No, no!
I also struggle writing scripts in Python, so any success by another user is a very valuable contribution.