Author Topic: Can file be opened, one method  (Read 19254 times)

0 Members and 1 Guest are viewing this topic.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Can file be opened, one method
« Reply #15 on: September 06, 2006, 11:50:53 AM »
Glenn,

  So in your code you are saying to make a new Database and to not use the current drawing (first argument, set to false) and that you don't want any drawing associated with it right now (second argument, set to true)?

Then you open a drawing's database with 'ReadDwgFile', give it the full path (first argument) and then how you want the OS to handle what others are able to do when they try to open it when the code has it opened already (second argument).  I don't understand what the third argument is for, in SharpDevelop it states that the third argument is for 'allowCPConversion'.  And then the last argument is the password, which would be false to all of my drawings.

I'm guess (as I haven't tried it yet) that if it can't be open, then it will throw an error, which is why you put it into a 'try catch' phrase.

Thanks again.  I'm off to see if I can find something about 'allowCPConversion' on the net.  This is getting fun.  :-)
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Alexander Rivilis

  • Bull Frog
  • Posts: 214
  • Programmer from Kyiv (Ukraine)
Re: Can file be opened, one method
« Reply #16 on: September 06, 2006, 12:02:48 PM »
... I'm off to see if I can find something about 'allowCPConversion' on the net...
If you are writing application only for USA customers you can ignore allowCPConversion. It is mean Allow CodePage Conversion - e.g. conversion from code page in which this dwg-file was creating/editing last time to curren System CodePage (Windows CodePage). I  prefer allowCPConversion = false
« Last Edit: September 06, 2006, 12:11:20 PM by Alexander Rivilis »

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Can file be opened, one method
« Reply #17 on: September 06, 2006, 12:09:18 PM »
... I'm off to see if I can find something about 'allowCPConversion' on the net...
If you are writing application only fo USA customers you can ignore allowCPConversion. It is mean Allow CodePage Conversation - e.g. conversation from code page in which this dwg-file was creating/editing last time to curren System CodePage (Windows CodePage). I  prefer allowCPConversion = false
Thanks Alexander.  I'm only writing for myself right now, so US only, so I will do as you advise.  :-)
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Alexander Rivilis

  • Bull Frog
  • Posts: 214
  • Programmer from Kyiv (Ukraine)
Re: Can file be opened, one method
« Reply #18 on: September 06, 2006, 12:10:47 PM »
 :-D

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Can file be opened, one method
« Reply #19 on: September 06, 2006, 01:04:36 PM »
The problem with doing it this way is that the attributes don't get saved in the correct location.  When going the DBX route the attributes stay in the correct location.

Thanks for the lession.  If I have to update attributes (not left justified) then it seems I have to use DBX, but if not, then I can go with just opening the database and making changes.

Attachments show what I'm talking about.  The weird thing is, when you open the drawing, and move (or do any command to) the block (db-route) the attribute will move to the correct location.
« Last Edit: September 06, 2006, 01:05:49 PM by T.Willey »
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Alexander Rivilis

  • Bull Frog
  • Posts: 214
  • Programmer from Kyiv (Ukraine)
Re: Can file be opened, one method
« Reply #20 on: September 06, 2006, 01:07:22 PM »
The problem with doing it this way is that the attributes don't get saved in the correct location.  When going the DBX route the attributes stay in the correct location.
Are you remeber about Tony WorkingDatabase class?
« Last Edit: September 06, 2006, 01:08:38 PM by Alexander Rivilis »

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Can file be opened, one method
« Reply #21 on: September 06, 2006, 01:15:47 PM »
The problem with doing it this way is that the attributes don't get saved in the correct location.  When going the DBX route the attributes stay in the correct location.
Are you remeber about Tony WorkingDatabase class?
I didn't code that in.  I thought what Glenn posted was all I needed.  I can try that also then.  Be back in a little.. have to test it.  Thanks for pointing that out Alexander.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Can file be opened, one method
« Reply #22 on: September 06, 2006, 02:50:05 PM »
So with what Mick and Glenn said; Use Database to open drawings and make changes.

And what Alexander reminded me of what Tony said; Make sure that the Database you want to change is the current working database.

I came up with this (just a section of the code) that seems to be working correctly.  Is this a safe way to do it?

Any comments/suggestions welcomed.  Thanks in advance, and thanks for the help so far.

Code: [Select]
foreach (string Str in DwgList) {
      bool ShouldSave = false;
      //Database db = new Database (false, true);
      try {
      using (Database db = new Database (false, true)) {
      db.ReadDwgFile (Str, System.IO.FileShare.Read, true, null);
      if (db != HostApplicationServices.WorkingDatabase) {
      HostApplicationServices.WorkingDatabase = db;
      }
      if (HasLayer (db, "Cloud-UNKNOWN")) {
string Rev = (GetHighestRev(db));
if (Rev != "") {
UpdateRevBlock (db, Rev);
UpdateCloudLayer (db, "Cloud-UNKNOWN", "Cloud-" + Rev);
ShouldSave = true;
//MessageBox.Show ("Right before save.");
}
}
        if (ShouldSave == true) {
    db.RetainOriginalThumbnailBitmap = true;
db.SaveAs (Str, DwgVersion.Current);
      SaveAr[SaveCnt] = Str;
++SaveCnt;
        }
else {
DiscardAr[DiscardCnt] = Str;
++DiscardCnt;
}
      //db.Dispose();
      }
      }
      catch {
      CannotOpenAr[CannotOpenCnt] = Str;
      ++CannotOpenCnt;
      }
}
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Can file be opened, one method
« Reply #23 on: September 06, 2006, 03:23:01 PM »
I had to put in one for check.  I had to check to make sure the file wasn't read only before I tried to open it's database.  I tried it without checking, and Acad threw up and error message, and then crashed to the desktop.

Thanks again for all the help, and knowledge shared.  :-) :love:
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Glenn R

  • Guest
Re: Can file be opened, one method
« Reply #24 on: September 06, 2006, 06:30:03 PM »
Glenn,

  So in your code you are saying to make a new Database and to not use the current drawing (first argument, set to false) and that you don't want any drawing associated with it right now (second argument, set to true)?

Then you open a drawing's database with 'ReadDwgFile', give it the full path (first argument) and then how you want the OS to handle what others are able to do when they try to open it when the code has it opened already (second argument).  I don't understand what the third argument is for, in SharpDevelop it states that the third argument is for 'allowCPConversion'.  And then the last argument is the password, which would be false to all of my drawings.

I'm guess (as I haven't tried it yet) that if it can't be open, then it will throw an error, which is why you put it into a 'try catch' phrase.

Thanks again.  I'm off to see if I can find something about 'allowCPConversion' on the net.  This is getting fun.  :-)

Pretty much correct on all counts there Tim.
Changing the working dbase to the one you open in memory I suspect allows AutoCAD to do the justification/position mojo you are talking about, although I've never needed to or tried it myself.

Fun isn't it? :)

Cheers,
Glenn.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Can file be opened, one method
« Reply #25 on: September 06, 2006, 06:33:55 PM »
Glenn,

  So in your code you are saying to make a new Database and to not use the current drawing (first argument, set to false) and that you don't want any drawing associated with it right now (second argument, set to true)?

Then you open a drawing's database with 'ReadDwgFile', give it the full path (first argument) and then how you want the OS to handle what others are able to do when they try to open it when the code has it opened already (second argument).  I don't understand what the third argument is for, in SharpDevelop it states that the third argument is for 'allowCPConversion'.  And then the last argument is the password, which would be false to all of my drawings.

I'm guess (as I haven't tried it yet) that if it can't be open, then it will throw an error, which is why you put it into a 'try catch' phrase.

Thanks again.  I'm off to see if I can find something about 'allowCPConversion' on the net.  This is getting fun.  :-)

Pretty much correct on all counts there Tim.
Changing the working dbase to the one you open in memory I suspect allows AutoCAD to do the justification/position mojo you are talking about, although I've never needed to or tried it myself.
Cool thanks.  I think I'm starting to understand this (with all the help from people here).

Fun isn't it? :)

Cheers,
Glenn.
Very much so.  I love to learn, and when you can learn something that is useful, that is even better.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Glenn R

  • Guest
Re: Can file be opened, one method
« Reply #26 on: September 06, 2006, 06:36:50 PM »
here is my catch clause that I left out:

Code: [Select]
catch (Autodesk.AutoCAD.Runtime.Exception aex) {

if (stringBuilder == null)
stringBuilder = new StringBuilder();

if (aex.ErrorStatus == ErrorStatus.FileSharingViolation)
stringBuilder.AppendFormat("\n{0} is already open!", drawingName);
else if (aex.ErrorStatus == ErrorStatus.DwgNeedsRecovery)
stringBuilder.AppendFormat("\n{0} needs recovery!", drawingName);
else if (aex.ErrorStatus == ErrorStatus.BadDwgHeader)
stringBuilder.AppendFormat("\n{0} has a bad drawing header!", drawingName);
else
stringBuilder.AppendFormat("\n{0} has error {1}", drawingName, aex.Message);

continue;

} catch (System.Exception ex) {

ed.WriteMessage("\nError: {0}", ex.Message);
ed.WriteMessage("\nStack trace: {0}", ex.StackTrace);
return;

}

Also, I don't see you resetting the working dbase after you change it.......?????


T.Willey

  • Needs a day job
  • Posts: 5251
Re: Can file be opened, one method
« Reply #27 on: September 06, 2006, 06:54:17 PM »
here is my catch clause that I left out:

<snip>
Thanks.  This will be useful.

Also, I don't see you resetting the working dbase after you change it.......?????

Guess I didn't know I had to.  Would I do it like?
Code: [Select]
Document Doc = Autodesk.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database DocDb = Doc.Database;
if (DocDb != HostApplicationServices.WorkingDatabase) {
    HostApplicationServices.WorkingDatabase = DocDb;
}
Or could I just set it without checking?
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

LE

  • Guest
Re: Can file be opened, one method
« Reply #28 on: September 06, 2006, 07:10:19 PM »
reset?

I have done something similar using objectarx... and I simple call the delete operator - only when working with an external file....


Or this reset part is something particular only when using C# ?....


Thanks
« Last Edit: September 06, 2006, 07:11:36 PM by LE »

Glenn R

  • Guest
Re: Can file be opened, one method
« Reply #29 on: September 06, 2006, 07:20:55 PM »
When you fire your command, the working database in essence is the drawing/document your command was executed in.
You then reset it, but I never saw where you put it back to the original when it first started.

Tony's class did this automatically, as his class was implementing the IDisposable interface. Essentially, this means it can be used with a 'using' statement. When a 'using' statement's scope ends, it's destructor/dispose method is called and THAT's where Tony was resetting the working database back to the one he cached, which was passed in in the class's constructor...make sense?

Tony's class is essentially operating like a 'smart pointer' class/object in C++.

Cheers,
Glenn.
« Last Edit: September 06, 2006, 07:23:10 PM by Glenn R »