Author Topic: having problem with save function  (Read 3993 times)

0 Members and 1 Guest are viewing this topic.

michiel

  • Guest
having problem with save function
« on: June 28, 2012, 04:48:33 AM »
Hi all,

I'm having troubles with this line of code:

AcApX.AcadApplication appX = AcAp.Application.AcadApplication as AcApX.AcadApplication;
AcApX.AcadDocument curDoc = appX.ActiveDocument;
String path = textBox2.Text + "/" + textBox1.Text;
System.IO.Directory.CreateDirectory(path);
for (int i = 0; i < fileNames.Count; i++)
{
 AcApX.AcadDocument destDoc = appX.Documents.Open(fileNames.ToString(), false, null);
 String p = path + "/" + fileNames.ToString();
 destDoc.SaveAs(p);
 destDoc.Close(true);
 MessageBox.Show("saved and closed = " + path + "/" + fileNames.ToString());
}

I'm using BricsCAD, and the weird thing about this is that he saves the first drawing, but then crashes on "destDoc.SaveAs(p);", what am I doing wrong?

thx

Delegate

  • Guest
Re: having problem with save function
« Reply #1 on: June 28, 2012, 11:33:25 AM »
Can you provide more information on the filesNames variable?

Just that you have fileNames.Count and then fileNames.ToString()

Assuming this is an array or collection since is has a count property do you need to use filesNames(i).ToString(); in the loop?


n.yuan

  • Bull Frog
  • Posts: 348
Re: having problem with save function
« Reply #2 on: June 28, 2012, 03:57:44 PM »
Just be curious.

This is C# code, right?

In Lisp, one you use "/" for ""\" (or use "\\"). But in C#, it is not the case. Either use "\\" for "\" or use "@" precedes a "literal text string containing "\".

Are you sure BricsCAD exposes API that allows to use "\" in C# in a file path (since you said the first save is succeeded and only errors out on next drawing)?

Can you use Debug.Print/MessageBox to show variable p before SaveAs() is called to verify you do have a valid path?

57gmc

  • Bull Frog
  • Posts: 358
Re: having problem with save function
« Reply #3 on: June 28, 2012, 04:56:40 PM »
Also, perhaps you use it later, but in the code shown, curDoc is declared but not used.

michiel

  • Guest
Re: having problem with save function
« Reply #4 on: June 29, 2012, 09:55:59 AM »
Can you provide more information on the filesNames variable?

Just that you have fileNames.Count and then fileNames.ToString()

Assuming this is an array or collection since is has a count property do you need to use filesNames(i).ToString(); in the loop?

Yes fileNames is an ArrayList where I stored all my (String) fileNames.
I used the index of my arrayList mentioned [ i ], it's just not good copied...
String fileName = fileNames[ i ].ToString(); (this forum takes [ i ] as italic?)

Just be curious.

This is C# code, right?

In Lisp, one you use "/" for ""\" (or use "\\"). But in C#, it is not the case. Either use "\\" for "\" or use "@" precedes a "literal text string containing "\".

Are you sure BricsCAD exposes API that allows to use "\" in C# in a file path (since you said the first save is succeeded and only errors out on next drawing)?

Can you use Debug.Print/MessageBox to show variable p before SaveAs() is called to verify you do have a valid path?

The first two drawings can be saved, and are saved in the specific folder I chose. But from the third drawing I get this error on the db.saveAs(p) function:

Error HRESULT E_FAIL has been returned from a call to COM component:
BricscadApp.IAcadDocument.SaveAs(String FullFileName, Object SaveAsType, Object vSecurityParams)

Also, perhaps you use it later, but in the code shown, curDoc is declared but not used.

curDoc is not used, I removed it but still same problem

n.yuan

  • Bull Frog
  • Posts: 348
Re: having problem with save function
« Reply #5 on: June 29, 2012, 10:06:49 AM »
Sorry for a typo in my post:

"Are you sure BricsCAD exposes API that allows to use "\" in C# in a file path..."

Should be read as

"Are you sure BricsCAD exposes API that allows to use "/" in C# in a file path..."

So, my question still stands:

Are you saying that you can use a file path like this in C# (as if it is in Lisp)

string fileName = "C:/fodler1/folder2/folder3/theDwgFile.dwg";

Unless that is specificly allowed in BricsCAD (I have no idea what it is), in C#, it should be

string fileName = "C:\\fodler1\\folder2\\folder3\\theDwgFile.dwg";

or

string fielName=@"C:\fodler1\folder2\folder3\theDwgFile.dwg"

That is why I am curiuos how the first 1 or first 2 document could be saved as you claimed.


michiel

  • Guest
Re: having problem with save function
« Reply #6 on: June 29, 2012, 10:35:34 AM »

Well I don't think that there is a problem because the third path is the exact same format as first two paths that could be saved... But I'm gonna try to put all paths from '\' in '/' or '//', and look what that gives...

michiel

  • Guest
Re: having problem with save function
« Reply #7 on: June 29, 2012, 10:43:04 AM »
Sorry for a typo in my post:

"Are you sure BricsCAD exposes API that allows to use "\" in C# in a file path..."

Should be read as

"Are you sure BricsCAD exposes API that allows to use "/" in C# in a file path..."

So, my question still stands:

Are you saying that you can use a file path like this in C# (as if it is in Lisp)

string fileName = "C:/fodler1/folder2/folder3/theDwgFile.dwg";

Unless that is specificly allowed in BricsCAD (I have no idea what it is), in C#, it should be

string fileName = "C:\\fodler1\\folder2\\folder3\\theDwgFile.dwg";

or

string fielName=@"C:\fodler1\folder2\folder3\theDwgFile.dwg"

That is why I am curiuos how the first 1 or first 2 document could be saved as you claimed.

I putted al the paths in the '\\' format and still problem with the third drawing...

michiel

  • Guest
Re: having problem with save function
« Reply #8 on: July 01, 2012, 05:40:23 AM »
I removed the "destDoc.Close(true);" and now it works fine, strange...

Delegate

  • Guest
Re: having problem with save function
« Reply #9 on: July 01, 2012, 05:10:18 PM »
I removed the "destDoc.Close(true);" and now it works fine, strange...

Very strange! I got the code to work but maybe its a BricsCAD thing:

Do you now have to manually close all your drawings?

Out of interest is there more to the code - are you altering the drawings or just moving them? If just moving them why not use System.IO file move?

Code - C#: [Select]
  1.  public void MyCommand()
  2.         {
  3.             string olddir = "??";
  4.             string newdir = "??";
  5.             string[] files = Directory.GetFiles(olddir);
  6.             AcadApplication AcadApp = (AcadApplication)Application.AcadApplication;
  7.             AcadDocuments Docs = AcadApp.Documents;
  8.             AcadDocument Doc;
  9.             foreach (string item in files)
  10.             {
  11.                 Doc = Docs.Open(item,false,null);
  12.                 Doc.SaveAs(newdir + Doc.Name);
  13.                 Doc.Close(true);
  14.                
  15.             }
  16.             // Put your command code here
  17.         }
  18.