Author Topic: Get Directory of opened file  (Read 3226 times)

Offline billo

  • Ewok
  • *
  • Posts: 33
    • View Profile
Get Directory of opened file
« on: January 28, 2023, 17:20:07 pm »
In my process I want to open a .dfx file and then automatically produce gcode into the Directory than contains that .dfx. Everything works nicely, except that I can't seem to find the name of the Directory from which the .dfx was loaded.

I see that it is actually stored in "CamBam 1.0.config" in "<DefaultDrawingDirectory>C:\MyLocalFiles\CamBamTemp</DefaultDrawingDirectory>" (I suppose I could open the .config and scan directly, but ugh).

I've tried the enticing CamBamConfig.DefaultDrawingDirectory, but that returns a "getset_descriptor". Is there a way to evaluate that to the actual property value?

Or, is there another way to get what I need?

tia, Bill

Offline dave benson

  • CNC Jedi
  • *****
  • Posts: 1807
    • View Profile
Re: Get Directory of opened file
« Reply #1 on: January 28, 2023, 22:55:50 pm »
Bill, you don’t want the default drawing directory as your file could be anywhere.
Have a rummage through CamBamUI.
This is what I do in C#:
Drawingpath = Path.GetDirectoryName(CamBamUI.MainUI.ActiveView.CADFile.Filename);

The API docs are scant, so you’ll have to work hard for your supper.
As I recall there is a script called automagic or something close to that.
It may be worth a look.

Offline billo

  • Ewok
  • *
  • Posts: 33
    • View Profile
Re: Get Directory of opened file
« Reply #2 on: January 29, 2023, 21:06:37 pm »
Thanks Dave. I thought that was perfect, but it turns out that if you open a .dfx, as opposed to a .cb, CADFile.Filename is "none".

If I then look in the .config file, the DefaultDrawingDirector IS updated to the directory of the .dfx I opened.

Also, if I then do a doc.Save, CB prompts me to save the new .cb file (which contains the .dfx data) to the directory from which the .dfx was opened. At that point the doc.Filename does contain the full path I need. However, CB prompts me to confirm the doc.Save and I'm trying to avoid showing ANY prompt dialogs in my script. If I could do a doc.Save and somehow force "no prompt" that would work also, but I don't see that option.

It seems like the info I need is available and being used internally by CB, but I can't seem to get at it.
« Last Edit: January 29, 2023, 21:27:30 pm by billo »

Offline dave benson

  • CNC Jedi
  • *****
  • Posts: 1807
    • View Profile
Re: Get Directory of opened file
« Reply #3 on: January 30, 2023, 23:34:41 pm »
Have a look here in the object browser or ilspy.

Offline billo

  • Ewok
  • *
  • Posts: 33
    • View Profile
Re: Get Directory of opened file
« Reply #4 on: February 05, 2023, 16:03:40 pm »
After hours of object browsing and trying dozens of alternatives, here is the best I could find.

It requires one more user interaction than I would like, but it's acceptable.

Code: [Select]
from CamBam.CAM import *
from CamBam.UI import CamBamUI

CamBamUI.MainUI.OpenFile() # async
doc.Save() # Does two things: 1) gets the folder of the .dxf 2) awaits async OpenFile

print doc.Filename, doc.Name # after Save this has the correct full Path but the old Name
view = CamBamUI.MainUI.ActiveView
newdoc = view.CADFile
print newdoc.Filename, newdoc.Name # the newdoc has Path of None, but the correct Name