Author Topic: Won't save thumbnail upon save  (Read 5600 times)

0 Members and 1 Guest are viewing this topic.

T.Willey

  • Needs a day job
  • Posts: 5251
Won't save thumbnail upon save
« on: January 14, 2011, 07:14:44 PM »
I have done it before, and I have even checked my older code, as I seem to be doing it the same way, but for this code it won't work.  I haven't coded in C# in awhile, so it was kind of fun to do this project.  It will allow you to select a number of drawings, and update attributes for any blocks within all files selected.  Here is the part that I'm having issues with.  If you need to see more, let me know.

The code works as expected except for the saving aspect.

Code: [Select]
void UpdBtnClick(object sender, EventArgs e)
{
string Dir = ( DwgListview.Tag as string ) + "\\";
Dictionary<string, Dictionary<string, Dictionary<string, string>>> UpdDict = new Dictionary<string, Dictionary<string, Dictionary<string, string>>>();
Dictionary<string, Dictionary<string, string>> BlkDict;
Dictionary<string, string> AttDict;
foreach ( ListViewItem lvi in UpdListview.Items ) {
string DwgPath = Dir + lvi.SubItems[ 0 ].Text;
string BlkName = lvi.SubItems[ 1 ].Text;
string Tag = lvi.SubItems[ 2 ].Text;
string Value = lvi.SubItems[ 3 ].Text;
if ( UpdDict.TryGetValue( DwgPath, out BlkDict ) ) UpdDict.Remove( DwgPath );
if ( BlkDict == null )
BlkDict = new Dictionary<string, Dictionary<string, string>>();
if ( BlkDict.TryGetValue( BlkName, out AttDict ) ) BlkDict.Remove( BlkName );
if ( AttDict == null ) AttDict = new Dictionary<string, string>();
if ( AttDict.ContainsKey( Tag ) ) AttDict.Remove( Tag );
AttDict.Add( Tag, Value );
BlkDict.Add( BlkName, AttDict );
UpdDict.Add( DwgPath, BlkDict );
}
Document Doc;
DocumentLock DocLock = null;
string nValue;
Database cDb = HostApplicationServices.WorkingDatabase;
foreach ( KeyValuePair<string, Dictionary<string, Dictionary<string, string>>> kvp in UpdDict ) {
Database Db = MyUtility.GetDatabaseAtPath( kvp.Key, cDb, FileOpenMode.OpenForReadAndWriteNoShare, FindFileHint.Default, out Doc );
if ( Db == null ) {
MessageBox.Show( "Could not open drawing: " + System.Environment.NewLine + kvp.Key );
continue;
}
if ( Doc == null ) HostApplicationServices.WorkingDatabase = Db;
else DocLock = Doc.LockDocument();
using ( Transaction Trans = Db.TransactionManager.StartTransaction() ) {
ObjectId BlkTblId = Db.BlockTableId;
BlkDict = kvp.Value;
foreach ( KeyValuePair<string, Dictionary<string, string>> kvp2 in BlkDict ) {
AttDict = kvp2.Value;
ObjectId BlkRecId = MyUtility.GetNonErasedTableRecordId( BlkTblId, kvp2.Key );
BlockTableRecord BlkRec = Trans.GetObject( BlkRecId, OpenMode.ForRead ) as BlockTableRecord;
foreach ( ObjectId id in BlkRec.GetBlockReferenceIds( true, true ) ) {
BlockReference BlkRef = Trans.GetObject( id, OpenMode.ForRead ) as BlockReference;
if ( BlkRef.IsErased ) continue;
foreach ( ObjectId attId in BlkRef.AttributeCollection ) {
AttributeReference AttRef = Trans.GetObject( attId, OpenMode.ForRead ) as AttributeReference;
if ( AttDict.TryGetValue( AttRef.Tag, out nValue ) ) {
AttRef.UpgradeOpen();
AttRef.TextString = nValue;
AttRef.DowngradeOpen();
}
}
}
}
Trans.Commit();
}
HostApplicationServices.WorkingDatabase = cDb;
if ( Doc == null ) {
Db.RetainOriginalThumbnailBitmap = true;
Db.SaveAs( kvp.Key, DwgVersion.Current );
Db.Dispose();
}
else DocLock.Dispose();
}
this.Close();
}
Tim

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

Please think about donating if this post helped you.

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Won't save thumbnail upon save
« Reply #1 on: January 14, 2011, 07:24:47 PM »
From the docs

Quote
If the database executing the SaveAs() function is not the current database in the AutoCAD editor, then the thumbnail preview image is not saved to fileName.


Does not say that in the overloaded method SaveAs() you are are using but this one.
Quote
public void SaveAs(
    string fileName,
    [MarshalAs(UnmanagedType.U1)] bool bBakAndRename,
    DwgVersion version,
    Autodesk.AutoCAD.DatabaseServices.SecurityParameters security
);

I would guess it applies also.


T.Willey

  • Needs a day job
  • Posts: 5251
Re: Won't save thumbnail upon save
« Reply #2 on: January 14, 2011, 07:35:58 PM »
The code works in other programs though, that is what is getting me.  I change the switching back to the current database to after the save, but that crashes acad.  There must be something I'm over looking.  Here are a couple examples of my other codes that work.

Code: [Select]
foreach (string Str in DwgList) {
try {
Document tempDoc;
DocumentLock tempDocLock = null;
//Database db = MyUtility.GetDatabaseAtPath( Str, cDb, System.IO.FileShare.Read, FindFileHint.Default, out tempDoc );
Database db = MyUtility.GetDatabaseAtPath( Str, cDb, FileOpenMode.OpenForReadAndReadShare, FindFileHint.Default, out tempDoc );
if ( tempDoc != null ) tempDocLock = tempDoc.LockDocument();
else if ( db == null ) {
CannotOpenAr[CannotOpenCnt] = Str;
++CannotOpenCnt;
continue;
}
else HostApplicationServices.WorkingDatabase = db;
BlockReference TtlBlk = GetTitleBlock (db);
if ( TtlBlk != null ) {
ReorderRevision (db, TtlBlk, DiaInfo);
HostApplicationServices.WorkingDatabase = cDb;
if ( tempDoc == null ) {
db.RetainOriginalThumbnailBitmap = true;
db.SaveAs (Str, DwgVersion.Current);
}
}
else {
NoTtlBlkAr[NoTtlBlkCnt] = Str;
++NoTtlBlkCnt;
}
if ( tempDoc == null ) db.Dispose();
else tempDocLock.Dispose();
}
catch {
CannotOpenAr[CannotOpenCnt] = Str + System.Environment.NewLine + "    " + e.ToString();
++CannotOpenCnt;

}
}

Code: [Select]
foreach (string Path in OpenDia.GetFilenames()) {
//string[] FileNames = OpenDia.FileNames;
//OpenDia.Dispose();
this.Visible = false;
//foreach (string Path in FileNames) {
try {
using (Database db = new Database(false, true)) {
db.ReadDwgFile (Path, System.IO.FileShare.Read, true, null);
if (db != HostApplicationServices.WorkingDatabase)
HostApplicationServices.WorkingDatabase = db;
GetTitleBlock(db, Path);
//MessageBox.Show(Doc.Name);
HostApplicationServices.WorkingDatabase = Doc.Database;
        db.RetainOriginalThumbnailBitmap = true;
db.SaveAs (Path, DwgVersion.Current);
}
}
catch (System.Exception ex) {
MessageBox.Show("Error in drawing: " + Path + "\n\n" + ex.Message);
}
}
Tim

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

Please think about donating if this post helped you.

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Won't save thumbnail upon save
« Reply #3 on: January 14, 2011, 07:39:14 PM »
not working
Code: [Select]
OpenForReadAndWriteNoShare


 working
Code: [Select]
OpenForReadAndReadShare

go to share it

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Won't save thumbnail upon save
« Reply #4 on: January 14, 2011, 07:45:02 PM »
That did it Jeff.  I don't know why, or how, but it did.  Here is the help, but I don't see anything that make it seem like one would do that.

Thanks.  Have a great weekend.

Quote
OpenForReadAndReadShare = 1  Open for read and allow read sharing (same as _SH_DENYWR). Using this value with Database.ReadDwgFile() gives the same behavior as in previous releases of ObjectARX, and prevents other applications from writing to the file. Also, if the file was already opened by another application for writing, then Database.ReadDwgFile() will fail. 


OpenForReadAndWriteNoShare = 2  Open for read/write and allow no sharing (same as _SH_DENYRW). Using this value with Database.ReadDwgFile prevents other applications from reading and writing to the file when the file is opened by Database.ReadDwgFile. An Database.ReadDwgFile call using this mode will fail if any other application has the drawing file open, or if the drawing file is read-only. 

Tim

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

Please think about donating if this post helped you.

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Won't save thumbnail upon save
« Reply #5 on: January 14, 2011, 07:52:49 PM »
I don't know why, or how, but it did. 
Me either.
Someone else had the same problem and I compared my code to their's and that was the only difference.

Just dumb luck.