CamBam
News:
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
August 21, 2017, 07:29:38 am


Login with username, password and session length


Pages: [1] 2
  Print  
Author Topic: Please Help. Python script to open files, update gcode based on updated styles  (Read 635 times)
Darizona2017
CNC Ewok
*
Offline Offline

Posts: 9


View Profile
« on: June 17, 2017, 05:17:49 am »

I am trying to make a python script to batch process a list of files and output new Gcode when I update a "style".

I would like it to:
open the file (I figured out how to to this)
update the tool paths (I figured out how to do half of this)
output the new gcode
move on to the next file and repeat the process

The problem that I run into is that a window pops up and says that there is a cache conflict and the window asks if I want to use the old value, cancel, or use the new value.  I want to use the new value but I don't know how to script this.  I've tried multiple things but I can't seem to get any of them to work.


I include a version of what I was trying obviously it is not complete and things are commented out all over the place because I was just trying to get something to work.  Any direction would help.  I don't think this should be too complex.

--------------------------------------------------------------------------
# New CamBam Python Script
'''
print "hello matt"
n = dir(CAMUtils)

for m in n:
   print m
'''

from System.IO import Directory
from CamBam.CAM import *
from CamBam.UI import CamBamUI
from CamBam.Values import *
from CamBam.Util import *

cbFilePath = "C:\Users\Dustin\Google Drive\JAD Files\DVD_CNC patterns"
cbFileList = ["7010.cb"]

sourceFileName = cbFilePath + "\\" + cbFileList[0]

#cb = CADFile()
print "sourceFileName: ", sourceFileName
#cb.Open(currentFileName)


# Open a source file
# note: the second parameter '1' opens the file synchronously rather than in a worker thread
CamBamUI.MainUI.OpenFile(sourceFileName,1)

# The 'doc' global variable is always the CADFile before any file opens,
# so we create a new varialbe pointing to the newly opened active file...
newdoc = CamBamUI.MainUI.ActiveView.CADFile

# set any machining options required...
newdoc.MachiningOptions.PostProcessor = "DVDCNC"
newdoc.MachiningOptions.StyleLibrary = "JAD Style"

print MachineOpStatus

#dir(newdoc.ClearCBValueCache)
#EvaluateCBValues()
'''
for x in dir(MachineOp.ClearCBValueCache()):
   print x
'''
MachineOp.Update()
'''


for x in dir(newdoc):
   print x
'''
#generate the toolpaths
#CAMUtils.GenerateToolpaths(view)
#CAMUtils.GenerateGCodeOutput(view)
#CamBamUI.MainUI.Save


Logged
dave benson
CNC Jedi
*****
Offline Offline

Posts: 768


View Profile
« Reply #1 on: June 18, 2017, 14:21:51 pm »

Use the methods Here
Dave


* Cache Conflict Action Capture.PNG (20.73 KB, 313x626 - viewed 34 times.)

* Cache conflict action 2 Capture.PNG (25.92 KB, 674x590 - viewed 32 times.)
Logged
Darizona2017
CNC Ewok
*
Offline Offline

Posts: 9


View Profile
« Reply #2 on: June 18, 2017, 20:46:45 pm »

Thanks Dave.  I'll give that a try.
Logged
Darizona2017
CNC Ewok
*
Offline Offline

Posts: 9


View Profile
« Reply #3 on: June 18, 2017, 21:21:22 pm »

I have tried changing the cache conflict action to "use new value" manually and then saving the document, but it reverts to "unspecified" when I reopen the document. 

I would be fine setting the cache conflict action if I only have to do it once for each of the files that I have created and then just build it into a template for new files.

Or... do you know what line of code I would use to set the cacheconflictaction in python

from CamBam.Values import *
(CacheConflictAction.UseNewValue???)  not really sure how to implement this. 

Logged
dave benson
CNC Jedi
*****
Offline Offline

Posts: 768


View Profile
« Reply #4 on: June 19, 2017, 02:40:54 am »

HI Darizona2017

I'm afraid python is not my strong suit, so you'll have to bear with me.
In the pic 2 there are two methods.

CacheConflictAction and PropertyCacheConflictPrompt.

My guess is that you set the “CacheConflictAction” and then validate the “PropertyCacheConflictPrompt”

Something along the lines of :
CacheConflictAction.UseNewValue
and then 
PropertyCacheConflictPrompt.Ok

If you don't already have ILSpy grab it and have a look through the methods (it's a great help).
Sorry I can't be of more help.
 
One other thing .CB files are xml so maybe you could iterate through them and change  the values
there (I don't think you get the prompt then) and then use your python script (in CB) to generate the Gcode that way.

Dave
Logged
Darizona2017
CNC Ewok
*
Offline Offline

Posts: 9


View Profile
« Reply #5 on: June 20, 2017, 05:37:09 am »

I did get it to work.  See Code below. 

Now the script opens a file, and changes properties and saves the new gcode and save the file.  Unfortunately it will not work for multiple files in a loop.  Any ideas?

-----------------------------------------

Here is the code:

# New CamBam Python Script

from System.IO import Directory
from CamBam.CAM import *
from CamBam.UI import CamBamUI
from CamBam.Values import *


cbFilePath = "C:\Users\dustin\Google Drive\JAD\DVD_CNC patterns\DVD_CNC_Subroutines\Test Files"
cbFileList = ["7060.cb"]

fileName = cbFileList[0]

sourceFileName = cbFilePath + "\\" + fileName

print "-------------------------------------------------------"

#cb = CADFile()
print "sourceFileName: ", sourceFileName

# Open a source file
# note: the second parameter '1' opens the file synchronously rather than in a worker thread
CamBamUI.MainUI.OpenFile(sourceFileName,1)


# The 'doc' global variable is always the CADFile before any file opens,
# so we create a new varialbe pointing to the newly opened active file...
newdoc = CamBamUI.MainUI.ActiveView.CADFile


# set any machining options required...
newdoc.MachiningOptions.PostProcessor = "DVDCNC"
newdoc.MachiningOptions.StyleLibrary = "JAD style test"
newdoc.MachiningOptions.OutFile = "{$cbfile.name}.ngc"
newdoc.MachiningOptions.Tag = "test1"

print 'current cache conflict action:', newdoc.CacheConflictAction
print 'drawing units:', newdoc.DrawingUnits

#Change how the system deals with changes to the values applied by styles
newdoc.CacheConflictAction = CacheConflictAction.UseNewValue

# output the new gcode
CAMUtils.GenerateGCodeOutput(view)

CamBamUI.MainUI.FileSave
Logged
dave benson
CNC Jedi
*****
Offline Offline

Posts: 768


View Profile
« Reply #6 on: June 20, 2017, 08:30:03 am »

I think you are very close
the sudo code in C# would look something like
 File_Name_Path_String = Directory.GetCurrentDirectory();
                foreach (file Myfile in File_Name_Path_String
                {
                   check the extension for ".cb"
      if (so add it)
                               {
                                   cbfilelist.add(Myfile);
                               }
                }

so really all you have to work out is to populate the cbFileList[]
and then just iterate though the list of files using your code.
 
you may be able to do something like this which i wrote in c# for another program.

            // Allow the user to select multiple images.
            this.openFileDialog1.Multiselect = true;

so you could open the cb file dialog, and multiselect the files (in the dialog) and use the dialog methods
to populate the cbfilelist.
Dave
Logged
Darizona2017
CNC Ewok
*
Offline Offline

Posts: 9


View Profile
« Reply #7 on: June 21, 2017, 04:05:54 am »

I think I can figure that part out but for some reason when I have a list of more than one file and have it loop through those files with the code that I posted above it opens both files and it save the gcode for the first file but the second file just gets opened and nothing happens.  I don't get any errors though.

I do feel close but I feel like I am missing something.  Do you think it has to do with how I am opening the file synchronously instead of in a worker thread (I have no idea what that means but it was commented in one of the scripts that I copied.

Thanks for the help.
Logged
EddyCurrent
CNC Jedi
*****
Offline Offline

Posts: 3037



View Profile
« Reply #8 on: June 21, 2017, 08:39:45 am »

Did you look here for code examples ? http://www.cambam.info/ref/script
or here; http://www.cambam.co.uk/forum/index.php?topic=5134.msg40325#msg40325
Logged
Darizona2017
CNC Ewok
*
Offline Offline

Posts: 9


View Profile
« Reply #9 on: June 22, 2017, 04:09:34 am »

I have not checked out the second link.  I'll see where that takes me.

Thanks.

Logged
Darizona2017
CNC Ewok
*
Offline Offline

Posts: 9


View Profile
« Reply #10 on: June 22, 2017, 06:15:27 am »

I got it to work!!! Smiley

I'm not sure why, but I had to put some pauses in the script and now it creates the new gcode for all of the files in the list by looping through them sequentially.  I am not sure if the cb files are saving or not but that does not really matter to me as long as the new gcode is output.  I just put pauses all over the place so I don't know which ones are actually necessary.

I'm not sure if I will code in the functionality to search in certain directories for the cb files that I would want to update or just leave the list hard coded.  I have done something similar to searching for files based on name so I think I can handle that. 

I hope this code is useful for someone else.  Thanks for the help.

--------------------------------------------------------------------------------------
# New CamBam Python Script

from System.IO import Directory
from CamBam.CAM import *
from CamBam.UI import CamBamUI
from CamBam.Values import *

cbFilePath = "C:\Users\dustin\Google Drive\JAD\DVD_CNC patterns\DVD_CNC_Subroutines\Test Files"
cbFileList = ["7060.cb","7040.cb"]

#--------------------------------------------------------------
for fileName in cbFileList:
   sourceFileName = cbFilePath + "\\" + fileName

   # Open a source file
   # note: the second parameter '1' opens the file synchronously rather than in a worker thread
   CamBamUI.MainUI.OpenFile(sourceFileName,1)

   print "sourceFileName: ", sourceFileName

   # The 'doc' global variable is always the CADFile before any file opens,
   # so we create a new varialbe pointing to the newly opened active file...
   newdoc = CamBamUI.MainUI.ActiveView.CADFile

   print 'step1.1...'
   app.Sleep(100)
   
   # set any machining options required...
   newdoc.MachiningOptions.PostProcessor = "DVDCNC"
   newdoc.MachiningOptions.StyleLibrary = "JAD style"
   newdoc.MachiningOptions.OutFile = "{$cbfile.name}-3.ngc"
   newdoc.MachiningOptions.Tag = "test1"


   print 'step1.2...'
   app.Sleep(100)

   print 'current cache conflict action:', newdoc.CacheConflictAction
   print 'drawing units:', newdoc.DrawingUnits

   #Change how the system deals with changes to the values applied by styles
   newdoc.CacheConflictAction = CacheConflictAction.UseNewValue

   print 'step1.3...'
   app.Sleep(100)

   # output the new gcode
   CAMUtils.GenerateGCodeOutput(view)


   print 'step1.4...'
   app.Sleep(100)

   #Save the cambam file
   CamBamUI.MainUI.FileSave
print 'All files complete'
Logged
jk
Storm Trooper
***
Offline Offline

Posts: 184


View Profile
« Reply #11 on: June 22, 2017, 06:45:38 am »

Hi.

I think a single sleep after the CAMUtils.GenerateGCodeOutput is enough.
The gcode is generated in a separate thread, so wait is required for synchronization.

Code:
CAMUtils.GenerateGCodeOutput(view)

while CamBamUI.MainUI.ActiveView.IsThinking:
    app.Sleep(1)
Logged
dave benson
CNC Jedi
*****
Offline Offline

Posts: 768


View Profile
« Reply #12 on: June 22, 2017, 10:29:53 am »

Good on you for sticking at it.
I usually use a worker thread when i want to keep the UI responsive while the worker does something else that takes a long time to do. This way you can cancel an action that's taking too long.
Dave
Logged
Darizona2017
CNC Ewok
*
Offline Offline

Posts: 9


View Profile
« Reply #13 on: June 23, 2017, 03:33:33 am »

adding the lines from JK worked great.  Thanks.

Here is the final code. 

------------------------------------------------------------------------------

# Update Gcode Script
# Dustin Dienno + help from CamBam forum

# This script opens a list of cambam files and outputs the updated gcode
# This may be useful if you have multiple files and you update a cam style and you want
# to update the gcode on all of the files
# Version 1.0

from System.IO import Directory
from CamBam.CAM import *
from CamBam.UI import CamBamUI
from CamBam.Values import *

# file path of folder where files are located
cbFilePath = "C:\Users\dustin\Google Drive\JAD\DVD_CNC patterns\DVD_CNC_Subroutines\Test Files"
cbFileList = ["7060.cb","7040.cb"]      #list of cambam file names

#--------------------------------------------------------------
for fileName in cbFileList:
   sourceFileName = cbFilePath + "\\" + fileName

   # Open a source file
   # note: the second parameter '1' opens the file synchronously rather than in a worker thread
   CamBamUI.MainUI.OpenFile(sourceFileName,1)

   print "sourceFileName: ", sourceFileName

   # The 'doc' global variable is always the CADFile before any file opens,
   # so we create a new varialbe pointing to the newly opened active file...
   newdoc = CamBamUI.MainUI.ActiveView.CADFile
   
   # set any machining options required...
   newdoc.MachiningOptions.PostProcessor = "DVDCNC"
   newdoc.MachiningOptions.StyleLibrary = "JAD style"
   newdoc.MachiningOptions.OutFile = "{$cbfile.name}.ngc"
   newdoc.MachiningOptions.Tag = "n/a"

   print 'current cache conflict action:', newdoc.CacheConflictAction
   print 'drawing units:', newdoc.DrawingUnits

   #Change how the system deals with changes to the values applied by styles
   newdoc.CacheConflictAction = CacheConflictAction.UseNewValue

   # output the new gcode
   CAMUtils.GenerateGCodeOutput(view)
   
   # gcode output is done is a seperate thread so must wait for it to finish
   while CamBamUI.MainUI.ActiveView.IsThinking:
      app.Sleep(1)
   
   #Save the cambam file
   CamBamUI.MainUI.FileSave
print 'All files complete'
Logged
lloydsp
CNC Jedi
*****
Offline Offline

Posts: 7019



View Profile
« Reply #14 on: June 23, 2017, 16:15:47 pm »

You don't need to hard-code the file locations.

The Post-build-processor function of the PP will allow you to pass a shell command (to launch the script) AND a 'command tail' string that can convey the local PP-chosen dynamic paths to your script.

Here's an example I used back in my R2E4 controller days:

  <PostBuildCommand>wscript</PostBuildCommand>
  <PostBuildCommandArgs>"C:\Documents and Settings\All Users\Application Data\CamBam plus 0.9.8\post\r2e4 correct.vbs" "{$cbfile.name}.nc"</PostBuildCommandArgs>


Lloyd
Logged

"Pyro for Fun and Profit for More Than Fifty Years"
Pages: [1] 2
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2013, Simple Machines

Valid XHTML 1.0! Valid CSS! Dilber MC Theme by HarzeM
Page created in 0.145 seconds with 19 queries.

Copyright © 2008 HexRay Ltd. | Sitemap